-
Notifications
You must be signed in to change notification settings - Fork 3
/
lisp-parser.js
185 lines (161 loc) · 5.33 KB
/
lisp-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
176
177
178
179
180
181
182
183
184
185
// Parser
lisp.Parser = function(str) {
// We parse by cutting off chunks of 'input'
this.str = str;
this.input = str;
this.pos = 0;
};
lisp.Parser.prototype = {
// Return a nice error position description
parseError: function() {
console.log(this.pos);
// Try to find the whole line
for (var start = this.pos; start >= 0 && this.str.charAt(start) != '\n';
start--)
;
start++;
for (var end = this.pos; end < this.str.length && this.str.charAt(end) != '\n';
end++)
;
throw 'Parse error: ' + this.str.substring(start, this.pos) + '<HERE>' +
this.str.substring(this.pos, end);
},
// Token types, and regexps used to match them.
// withEnd=true means that we require a space (or non-atom) immediately
// after the token
tokenTypes: [
{ type: '(', re: /^\(/ },
{ type: ')', re: /^\)/ },
{ type: '.', re: /^\./, withEnd: true },
{ type: 'quote', re: /^\'/ },
{ type: 'quasiquote', re: /^`/ },
{ type: 'unquote', re: /^,/ },
{ type: 'number', re: /^-?(\.\d+|\d+\.\d*|\d+)/, withEnd: true },
{ type: 'symbol', re: /^[^\s\(\);]+/, withEnd: true }
],
tokenEnd: /^[\(\)\s;]/,
consumeSpaces: function() {
var inComment = false;
for (var pos = 0; pos < this.input.length; pos++) {
var c = this.input.charAt(pos);
if (inComment)
{
if (c == '\n')
inComment = false;
} else {
if (c == ';')
inComment = true;
else if (/\S/.test(c)) // not a space
break;
}
}
if (pos > 0) {
this.pos += pos;
this.input = this.input.substr(pos);
}
},
// Read a single token. Return null on end of input
readToken: function() {
this.consumeSpaces();
if (this.input.length == 0)
return null;
for (var i = 0; i < this.tokenTypes.length; ++i) {
var t = this.tokenTypes[i];
var m = t.re.exec(this.input);
if (m == null)
continue;
var n = m[0].length;
// do we require a non-atom after this token?
if (t.withEnd)
if (!(n == this.input.length ||
this.tokenEnd.test(this.input.charAt(n))))
continue;
this.pos += n;
this.input = this.input.substr(n);
return { type: t.type, s: m[0] };
}
this.parseError();
},
// Push a token back to input
unreadToken: function(token) {
this.pos -= token.s.length;
this.input = token.s + this.input;
},
empty: function() {
return this.input.length == 0;
},
// Try to parse one term. Returns a lisp term on success, null on end of input
// or unexpected input
readTerm: function() {
var tok = this.readToken();
if (tok == null) // end of input
return null;
switch (tok.type) {
case 'number':
return new lisp.Number(parseFloat(tok.s));
case 'symbol':
{
var s = tok.s.toLowerCase();
if (s == 'nil')
return lisp.nil;
else
return new lisp.Symbol(s);
}
case 'quote':
case 'quasiquote':
case 'unquote':
{
var term = this.readTerm();
if (term == null)
this.parseError();
return lisp.form1(tok.type, term);
}
case '(':
// cons/list - we respect the dot-notation (1 2 . 3)
{
var cdr = lisp.nil;
var list = [];
for (;;) {
var term = this.readTerm();
if (term != null) {
list.push(term);
} else {
// end of list
var tok = this.readToken();
if (tok == null)
this.parseError();
// first check for '. term'
if (tok.type == '.') {
cdr = this.readTerm();
if (cdr == null)
this.parseError();
tok = this.readToken();
if (tok == null)
this.parseError();
}
// then check for ')'
if (tok.type != ')')
this.parseError();
return lisp.listToTerm(list, cdr);
}
} // for
} // case
default:
this.unreadToken(tok);
return null;
} // switch
},
// Check if the rest of the string is empty
ensureEmpty: function() {
this.consumeSpaces();
if (this.input.length > 0)
this.parseError();
}
};
// Parse a string. Returns a term, or null if the string is empty
lisp.parse = function(str) {
var parser = new lisp.Parser(str);
var term = parser.readTerm();
parser.ensureEmpty();
return term;
};