-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsstate.js
More file actions
111 lines (90 loc) · 3.56 KB
/
jsstate.js
File metadata and controls
111 lines (90 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
var reservedWords = require('./jsidentifier').reservedWords
var keywords = require('./jsidentifier').keywords
var tt = require('./jstokentype').types
var lineBreak = require('./jswhitespace').lineBreak
var getOptions = require('./jsoptions').getOptions
// Registered plugins
var plugins = exports.plugins = {}
function keywordRegexp(words) {
return new RegExp("^(" + words.replace(/ /g, "|") + ")$")
}
exports.Parser = function Parser(options, input, startPos) {
this.options = options = getOptions(options)
this.sourceFile = options.sourceFile
this.keywords = keywordRegexp(keywords[options.ecmaVersion >= 6 ? 6 : 5])
var reserved = options.allowReserved ? "" :
reservedWords[options.ecmaVersion] + (options.sourceType == "module" ? " await" : "")
this.reservedWords = keywordRegexp(reserved)
var reservedStrict = (reserved ? reserved + " " : "") + reservedWords.strict
this.reservedWordsStrict = keywordRegexp(reservedStrict)
this.reservedWordsStrictBind = keywordRegexp(reservedStrict + " " + reservedWords.strictBind)
this.input = String(input)
// Used to signal to callers of `readWord1` whether the word
// contained any escape sequences. This is needed because words with
// escape sequences must not be interpreted as keywords.
this.containsEsc = false
// Load plugins
this.loadPlugins(options.plugins)
// Set up token state
// The current position of the tokenizer in the input.
if (startPos) {
this.pos = startPos
this.lineStart = Math.max(0, this.input.lastIndexOf("\n", startPos))
this.curLine = this.input.slice(0, this.lineStart).split(lineBreak).length
} else {
this.pos = this.lineStart = 0
this.curLine = 1
}
// Properties of the current token:
// Its type
this.type = tt.eof
// For tokens that include more information than their type, the value
this.value = null
// Its start and end offset
this.start = this.end = this.pos
// And, if locations are used, the {line, column} object
// Position information for the previous token
this.lastTokStart = this.lastTokEnd = this.pos
// The context stack is used to superficially track syntactic
// context to predict whether a regular expression is allowed in a
// given position.
this.context = this.initialContext()
this.exprAllowed = true
// Figure out if it's a module code.
this.strict = true//this.inModule = options.sourceType === "module"
// Used to signify the start of a potential arrow function
this.potentialArrowAt = -1
// Flags to track whether we are in a function, a generator.
this.inFunction = this.inGenerator = false
// Labels in scope.
this.labels = []
// stored comment array
this.storeComments = options.storeComments
this.insertCommas = options.insertCommas
// If enabled, skip leading hashbang line.
if (this.pos === 0 && options.allowHashBang && this.input.slice(0, 2) === '#!')
this.skipLineComment(2)
}
function ParserPrototype(){
// DEPRECATED Kept for backwards compatibility until 3.0 in case a plugin uses them
this.isKeyword = function(word) { return this.keywords.test(word) }
this.isReservedWord = function(word) { return this.reservedWords.test(word) }
this.extend = function(name, f) {
this[name] = f(this[name])
}
this.loadPlugins = function(pluginConfigs) {
for (var name in pluginConfigs) {
var plugin = plugins[name]
if (!plugin) throw new Error("Plugin '" + name + "' not found")
plugin(this, pluginConfigs[name])
}
}
this.parse = function() {
var node = this.options.program || this.startNode()
this.nextToken()
return this.parseTopLevel(node)
}
this.tokenize = function(){
}
}
ParserPrototype.call(exports.Parser.prototype)