Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80559 views
1
// Acorn is a tiny, fast JavaScript parser written in JavaScript.
2
//
3
// Acorn was written by Marijn Haverbeke, Ingvar Stepanyan, and
4
// various contributors and released under an MIT license.
5
//
6
// Git repositories for Acorn are available at
7
//
8
// http://marijnhaverbeke.nl/git/acorn
9
// https://github.com/marijnh/acorn.git
10
//
11
// Please use the [github bug tracker][ghbt] to report issues.
12
//
13
// [ghbt]: https://github.com/marijnh/acorn/issues
14
//
15
// This file defines the main parser interface. The library also comes
16
// with a [error-tolerant parser][dammit] and an
17
// [abstract syntax tree walker][walk], defined in other files.
18
//
19
// [dammit]: acorn_loose.js
20
// [walk]: util/walk.js
21
22
import {Parser} from "./state"
23
import {getOptions} from "./options"
24
import "./parseutil"
25
import "./statement"
26
import "./lval"
27
import "./expression"
28
29
export {Parser, plugins} from "./state"
30
export {defaultOptions} from "./options"
31
export {SourceLocation} from "./location"
32
export {getLineInfo} from "./location"
33
export {Node} from "./node"
34
export {TokenType, types as tokTypes} from "./tokentype"
35
export {TokContext, types as tokContexts} from "./tokencontext"
36
export {isIdentifierChar, isIdentifierStart} from "./identifier"
37
export {Token} from "./tokenize"
38
export {isNewLine, lineBreak, lineBreakG} from "./whitespace"
39
40
export const version = "1.2.2"
41
42
// The main exported interface (under `self.acorn` when in the
43
// browser) is a `parse` function that takes a code string and
44
// returns an abstract syntax tree as specified by [Mozilla parser
45
// API][api].
46
//
47
// [api]: https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API
48
49
export function parse(input, options) {
50
let p = parser(options, input)
51
let startPos = p.pos, startLoc = p.options.locations && p.curPosition()
52
p.nextToken()
53
return p.parseTopLevel(p.options.program || p.startNodeAt(startPos, startLoc))
54
}
55
56
// This function tries to parse a single expression at a given
57
// offset in a string. Useful for parsing mixed-language formats
58
// that embed JavaScript expressions.
59
60
export function parseExpressionAt(input, pos, options) {
61
let p = parser(options, input, pos)
62
p.nextToken()
63
return p.parseExpression()
64
}
65
66
// Acorn is organized as a tokenizer and a recursive-descent parser.
67
// The `tokenize` export provides an interface to the tokenizer.
68
69
export function tokenizer(input, options) {
70
return parser(options, input)
71
}
72
73
function parser(options, input) {
74
return new Parser(getOptions(options), String(input))
75
}
76
77