-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparser.js
More file actions
132 lines (103 loc) · 2.65 KB
/
parser.js
File metadata and controls
132 lines (103 loc) · 2.65 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const {
Lexer,
TOKEN_PRAGMA,
TOKEN_PRAGMA_VALUE,
TOKEN_LANGKEY,
TOKEN_LANGKEY_ASSIGN,
TOKEN_LANGKEY_STRING,
TOKEN_LANGKEY_END,
TOKEN_COMMENT,
TOKEN_INLINE_COMMENT,
TOKEN_WHITESPACE,
TOKEN_NEWLINE
} = require('./lexer')
const isAnyWhitespace = (input) => input.type === TOKEN_WHITESPACE || input.type === TOKEN_NEWLINE
class Parser {
constructor(str) {
this.lexer = new Lexer(str)
this.peeked = null
this.current = null
}
nextToken() {
let tok
if (this.peeked) {
tok = this.peeked
this.peeked = null
return tok
}
tok = this.lexer.nextToken()
while(tok && isAnyWhitespace(tok)) {
tok = this.lexer.nextToken()
}
this.current = tok
return tok
}
peekToken() {
this.peeked = this.peeked || this.nextToken()
return this.peeked
}
pushToken(tok) {
if (this.peeked) throw new Error('[pushToken] can only push one token on between reads')
this.peeked = tok
}
skip(type) {
const tok = this.nextToken()
if (!tok || tok.type !== type) {
this.pushToken(tok)
return false
}
return true
}
expect(type, onCurrent = false) {
const tok = onCurrent ? this.current : this.nextToken()
if (tok.type !== type) this.error(`expected ${type}, got ${tok.type}`, tok.lineno, tok.colno)
return tok
}
error(msg, lineno, colno) {
if ((lineno === undefined || colno === undefined) && this.peekToken()) {
const tok = this.peekToken()
lineno = tok.lineno
colno = tok.colno
}
throw new Error(`${msg}, ${lineno}:${colno}`)
}
parseStatement() {
const { type, value } = this.current
let nextToken
switch (type) {
case TOKEN_PRAGMA:
this.peekToken()
nextToken = this.expect(TOKEN_PRAGMA_VALUE)
return { type, [value]: nextToken.value }
case TOKEN_COMMENT:
case TOKEN_INLINE_COMMENT:
return { type, value }
case TOKEN_LANGKEY:
this.expect(TOKEN_LANGKEY_ASSIGN)
let strings = []
nextToken = this.expect(TOKEN_LANGKEY_STRING)
while (nextToken.type === TOKEN_LANGKEY_STRING) {
strings.push(nextToken.value)
nextToken = this.nextToken()
}
this.expect(TOKEN_LANGKEY_END, true)
return { type, [value]: strings }
default:
this.error(`Unknown token ${type}`)
}
}
parse() {
let tok
let buf = []
while (tok = this.nextToken()) {
const { value, lineno, colno } = tok
// console.log(tok)
const node = this.parseStatement()
buf.push(Object.assign(node, { lineno, colno }))
}
return buf
}
}
module.exports = {
Parser
}