Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80551 views
1
import {reservedWords, keywords} from "./identifier"
2
import {types as tt} from "./tokentype"
3
import {lineBreak} from "./whitespace"
4
5
export function Parser(options, input, startPos) {
6
this.options = options
7
this.sourceFile = this.options.sourceFile || null
8
this.isKeyword = keywords[this.options.ecmaVersion >= 6 ? 6 : 5]
9
this.isReservedWord = reservedWords[this.options.ecmaVersion]
10
this.input = input
11
12
// Load plugins
13
this.loadPlugins(this.options.plugins)
14
15
// Set up token state
16
17
// The current position of the tokenizer in the input.
18
if (startPos) {
19
this.pos = startPos
20
this.lineStart = Math.max(0, this.input.lastIndexOf("\n", startPos))
21
this.curLine = this.input.slice(0, this.lineStart).split(lineBreak).length
22
} else {
23
this.pos = this.lineStart = 0
24
this.curLine = 1
25
}
26
27
// Properties of the current token:
28
// Its type
29
this.type = tt.eof
30
// For tokens that include more information than their type, the value
31
this.value = null
32
// Its start and end offset
33
this.start = this.end = this.pos
34
// And, if locations are used, the {line, column} object
35
// corresponding to those offsets
36
this.startLoc = this.endLoc = null
37
38
// Position information for the previous token
39
this.lastTokEndLoc = this.lastTokStartLoc = null
40
this.lastTokStart = this.lastTokEnd = this.pos
41
42
// The context stack is used to superficially track syntactic
43
// context to predict whether a regular expression is allowed in a
44
// given position.
45
this.context = this.initialContext()
46
this.exprAllowed = true
47
48
// Figure out if it's a module code.
49
this.strict = this.inModule = this.options.sourceType === "module"
50
51
// Used to signify the start of a potential arrow function
52
this.potentialArrowAt = -1
53
54
// Flags to track whether we are in a function, a generator.
55
this.inFunction = this.inGenerator = false
56
// Labels in scope.
57
this.labels = []
58
59
// If enabled, skip leading hashbang line.
60
if (this.pos === 0 && this.options.allowHashBang && this.input.slice(0, 2) === '#!')
61
this.skipLineComment(2)
62
}
63
64
Parser.prototype.extend = function(name, f) {
65
this[name] = f(this[name])
66
}
67
68
// Registered plugins
69
70
export const plugins = {}
71
72
Parser.prototype.loadPlugins = function(plugins) {
73
for (let name in plugins) {
74
let plugin = exports.plugins[name]
75
if (!plugin) throw new Error("Plugin '" + name + "' not found")
76
plugin(this, plugins[name])
77
}
78
}
79
80