-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.js
160 lines (132 loc) · 4.07 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
'use strict'
const { MVal, Err } = require('./common')
class Parser {
constructor (text, x = 0, y = 0) {
this.text = text
this.x = x
this.y = y
}
parse () {
this.whitespace()
const lexemes = []
while (this.x < this.text.length) {
lexemes.push(this.lexeme())
this.whitespace()
}
return lexemes
}
// somestring -> new MVal(symbol, somestring)
// 123 -> new MVal(number, 1234)
// () -> new MVal(list, [])
// (+ 1 2) -> new MVal(list, [new MVal(symbol, +), new MVal(number, 1),
// new MVal(number, 2)])
lexeme () {
if (this.x >= this.text.length) {
throw new Error('no lexeme')
} else if (this.text[this.x] >= '0' && this.text[this.x] <= '9') {
return this.number()
} else if (this.text[this.x] === '"') {
return this.string()
} else if (this.text[this.x] === '\'') {
return this.quote()
} else if (this.text[this.x] === '#') {
return this.hash()
} else if (this.text[this.x] === '(') {
return this.list()
} else if (this.text[this.x] === ')') {
throw Err.aboutText('unexpectedClosingBracket', [this.x, this.y], [this.x + 1, this.y])
} else {
return this.symbol()
}
}
number () {
const start = this.x
this.untilSeparator()
const num = this.text.substring(start, this.x)
if (isFinite(num)) {
return new MVal('number', parseInt(num, 10), [start, this.y], [this.x, this.y])
} else {
throw Err.aboutText('badNumber', [start, this.y], [this.x, this.y])
}
}
string () {
this.x++
const start = this.x
while (this.x < this.text.length && this.text[this.x] !== '"') {
this.x++
}
if (this.text[this.x] === '"') {
const str = this.text.substring(start, this.x)
this.x++
return new MVal('string', str, [start - 1, this.y], [this.x, this.y])
} else {
throw Err.aboutText('unclosedString', [start - 1, this.y], [this.x, this.y])
}
}
list () {
const start = [this.x, this.y]
const list = []
this.x++
while (this.x < this.text.length && this.text[this.x] !== ')') {
list.push(this.lexeme())
this.whitespace()
}
if (this.text[this.x] === ')') {
this.x++
return new MVal('list', list, start, [this.x, this.y])
} else {
throw Err.aboutText('unclosedList', start, [this.x, this.y])
}
}
quote () {
const start = [this.x, this.y]
const list = []
list.push(new MVal('symbol', 'quote', [this.x, this.y]))
this.x++
list.push(this.lexeme())
return new MVal('list', list, start, [this.x, this.y])
}
hash () {
this.x++
const start = this.x
this.untilSeparator()
const thing = this.text.substring(start, this.x)
if (thing === 't' || thing === 'T') {
return new MVal('boolean', true, [start, this.y], [this.x, this.y])
} else if (thing === 'f' || thing === 'F') {
return new MVal('boolean', false, [start, this.y], [this.x, this.y])
} else if (thing[0] === '!') {
return new MVal('shebang', thing.substring(1), [start, this.y], [this.x, this.y])
} else {
throw Err.aboutText('unsupported', [start - 1, this.y], [this.x, this.y])
}
}
symbol () {
const start = this.x
this.untilSeparator()
return new MVal('symbol', this.text.substring(start, this.x), [start, this.y], [this.x, this.y])
}
untilSeparator () {
while (this.x < this.text.length && !this.isWhitespace() && this.text[this.x] !== ')') {
this.x++
}
}
whitespace () {
while (this.x < this.text.length && this.isWhitespace()) {
this.x++
}
while (this.x < this.text.length && this.text[this.x] === ';') {
this.x++
while (this.x < this.text.length && this.text[this.x] !== '\r' && this.text[this.x] !== '\n') {
this.x++
}
while (this.x < this.text.length && this.isWhitespace()) {
this.x++
}
}
}
isWhitespace () {
return this.text[this.x] === ' ' || this.text[this.x] === '\r' || this.text[this.x] === '\n' || this.text[this.x] === '\f'
}
}
module.exports = { Parser }