-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.js
175 lines (146 loc) · 5.03 KB
/
parser.js
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// # Copyright (c) 2014-2021 Feudal Code Limitada - MIT license #
"use strict"
import { tokenize } from "./tokenizer.js"
import { init as initExpression, eatExpression } from "./expression.js"
import { init as initFunction, eatGlobalFunction } from "./function.js"
import { err, unexpected } from "./helper.js"
import { init as initLitExpression, eatLiteralExpression } from "./expression-literal.js"
import { init as initScan, see, eat, eatValue, eatKind, eatName, eatString, eatTrueEndOfLine } from "./scan.js"
import { init as initRegister, registerImport, registerPrivateVariable, registerPublicVariable, registerPrivateFunction, registerPublicFunction, markUsed } from "./register.js"
var rat
var mayHaveGlobal
export function parse(r) {
rat = r
rat.tokens = tokenize(rat)
initScan(rat)
initFunction(rat)
initRegister(rat)
initExpression(rat)
initLitExpression(rat)
mayHaveGlobal = true
eatUseStrict()
eatImports()
mainLoop()
}
// use strict /////////////////////////////////////////////////////////////////
function eatUseStrict() {
const token = eat()
if (token.value != '"use strict"') { err(token, 'missing "use strict"') }
eatTrueEndOfLine()
}
// imports ////////////////////////////////////////////////////////////////////
function eatImports() {
while (see().value == "import") { eatImport() }
}
function eatImport() {
eat() // import
eatValue("{")
const nameTokens = eatImportNames() // also eats '}'
eatValue("from")
const fileToken = eatString()
registerImport(fileToken, nameTokens)
eatTrueEndOfLine()
}
function eatImportNames() {
//
// ASSUMES ALL NAMES HAVE NICKNAMES (name 'as' nickname)
// INSERTS 'null' AS PLACEHOLDER FOR NICKNAME IF NEEDED
//
if (see().value == "}") { eat(); return [ ] }
//
const nameTokens = [ ]
while (true) {
nameTokens.push(eatName())
if (see().value == "as") {
eat()
nameTokens.push(eatName())
}
else {
nameTokens.push(null)
}
//
const token = eat()
if (token.value == ",") { continue }
if (token.value == "}") { return nameTokens }
//
unexpected(token)
}
}
// mainLoop ///////////////////////////////////////////////////////////////////
function mainLoop() {
while (true) {
const token = eat()
if (token.value == "init") { eatInitOrMain(token); break } // function demands end of file
if (token.value == "main") { eatInitOrMain(token); break } // function demands end of file
if (token.value == "var") { eatGlobal(token, false, false); continue }
if (token.value == "const") { eatGlobal(token, false, true); continue }
if (token.value == "function") { eatFunction(false); continue }
if (token.value == "export") { eatExport(); continue }
if (token.kind == "end-of-file") { break }
unexpected(token)
}
}
// export /////////////////////////////////////////////////////////////////////
function eatExport() {
const token = eat()
if (token.value == "var") { eatGlobal(token, true, false); return }
if (token.value == "const") { eatGlobal(token, true, true); return }
if (token.value == "function") { eatFunction(true); return }
unexpected(token)
}
// global /////////////////////////////////////////////////////////////////////
function eatGlobal(token, isExporting, isConstant) {
if (! mayHaveGlobal) { err(token, "can't declare global variable after function") }
token = eatName()
if (isExporting) {
registerPublicVariable(token, isConstant)
}
else {
registerPrivateVariable(token, isConstant)
}
token = eat()
if (token.kind == "end-of-line") { return }
if (token.value != "=") { unexpected(token) }
eatLiteralExpression()
eatTrueEndOfLine()
}
// function ///////////////////////////////////////////////////////////////////
function eatFunction(isExporting) {
mayHaveGlobal = false
if (isExporting) {
registerPublicFunction(eatName())
}
else {
registerPrivateFunction(eatName())
}
eatGlobalFunction()
eatTrueEndOfLine()
}
// init or main ///////////////////////////////////////////////////////////////
function eatInitOrMain(token) {
markUsed(token)
//
eatValue("(")
eatArguments()
eatValue(")")
eatTrueEndOfLine()
eatKind("end-of-file")
function eatArguments() {
if (see().value == ")") { return }
eatArgument()
}
function eatArgument() {
eatExpression()
const kind = see().kind
if (kind == ")") { return }
if (kind == "end-of-line") {
eat()
if (see().kind == ")") { return }
unexpected(eat())
}
const token = eat()
if (token.value != ",") { unexpected(token) }
eatArgument()
}
}
//