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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
use syntax::ast::token::*;
use syntax::ast::expr::*;
use syntax::ast::constant::*;
use syntax::ast::op::*;
use syntax::ast::punc::*;
use syntax::ast::keyword::*;
use collections::treemap::TreeMap;
use std::fmt;
use std::vec::Vec;
macro_rules! mk (
($def:expr) => (
Expr::new($def, try!(self.get_token(self.pos - 1)).pos, try!(self.get_token(self.pos - 1)).pos)
);
($def:expr, $first:expr) => (
Expr::new($def, $first.pos, try!(self.get_token(self.pos - 1)).pos)
);
)
#[deriving(Clone, PartialEq)]
pub enum ParseError {
Expected(Vec<TokenData>, Token, &'static str),
ExpectedExpr(&'static str, Expr),
UnexpectedKeyword(Keyword),
AbruptEnd
}
impl fmt::Show for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Expected(ref wanted, ref got, ref routine) if wanted.len() == 0 => write!(f, "{}:{}: Expected expression for {}, got {}", got.pos.line_number, got.pos.column_number, routine, got.data),
Expected(ref wanted, ref got, ref routine) => {
try!(write!(f, "{}:{}: ", got.pos.line_number, got.pos.column_number));
try!(write!(f, "Expected "));
let last = wanted.last().unwrap();
for wanted_token in wanted.iter() {
try!(write!(f, "'{}'{}", wanted_token, if wanted_token == last {""} else {", "}));
}
try!(write!(f, " for {}", routine));
write!(f, " but got {}", got.data)
},
UnexpectedKeyword(ref key) => {
write!(f, "Unexpected {}", key)
}
ExpectedExpr(ref wanted, ref got) => {
write!(f, "Expected {}, but got {}", wanted, got)
},
AbruptEnd => {
write!(f, "Abrupt end")
}
}
}
}
pub type ParseResult = Result<Expr, ParseError>;
pub struct Parser {
tokens: Vec<Token>,
pos: uint
}
impl Parser {
#[inline(always)]
pub fn new(tokens: Vec<Token>) -> Parser {
Parser {tokens: tokens, pos: 0}
}
pub fn parse_all(&mut self) -> ParseResult {
let mut exprs = Vec::new();
while self.pos < self.tokens.len() {
let result = try!(self.parse());
exprs.push(result);
}
Ok(mk!(BlockExpr(exprs)))
}
fn parse_struct(&mut self, keyword:Keyword) -> ParseResult {
match keyword {
KThrow => {
let thrown = try!(self.parse());
Ok(mk!(ThrowExpr(box thrown)))
},
KVar => {
let mut vars = Vec::new();
loop {
let name = match self.get_token(self.pos) {
Ok(Token { data: TIdentifier(ref name), ..}) => name.clone(),
Ok(tok) => return Err(Expected(vec!(TIdentifier("identifier".into_string())), tok, "var statement")),
Err(AbruptEnd) => break,
Err(e) => return Err(e)
};
self.pos += 1;
match self.get_token(self.pos) {
Ok(Token {data: TPunctuator(PAssign), ..}) => {
self.pos += 1;
let val = try!(self.parse());
vars.push((name, Some(val)));
match self.get_token(self.pos) {
Ok(Token {data: TPunctuator(PComma), ..}) => self.pos += 1,
_ => break
}
},
Ok(Token {data: TPunctuator(PComma), ..}) => {
self.pos += 1;
vars.push((name, None));
},
_ => {
vars.push((name, None));
break;
}
}
}
Ok(mk!(VarDeclExpr(vars)))
},
KReturn => Ok(mk!(ReturnExpr(Some(box try!(self.parse()).clone())))),
KNew => {
let call = try!(self.parse());
match call.def {
CallExpr(ref func, ref args) => Ok(mk!(ConstructExpr(func.clone(), args.clone()))),
_ => Err(ExpectedExpr("constructor", call))
}
},
KTypeOf => Ok(mk!(TypeOfExpr(box try!(self.parse())))),
KIf => {
try!(self.expect_punc(POpenParen, "if block"));
let cond = try!(self.parse());
try!(self.expect_punc(PCloseParen, "if block"));
let expr = try!(self.parse());
let next = self.get_token(self.pos + 1);
Ok(mk!(IfExpr(box cond, box expr, if next.is_ok() && next.unwrap().data == TKeyword(KElse) {
self.pos += 2;
Some(box try!(self.parse()))
} else {
None
})))
},
KWhile => {
try!(self.expect_punc(POpenParen, "while condition"));
let cond = try!(self.parse());
try!(self.expect_punc(PCloseParen, "while condition"));
let expr = try!(self.parse());
Ok(mk!(WhileLoopExpr(box cond, box expr)))
},
KSwitch => {
try!(self.expect_punc(POpenParen, "switch value"));
let value = self.parse();
try!(self.expect_punc(PCloseParen, "switch value"));
try!(self.expect_punc(POpenBlock, "switch block"));
let mut cases = Vec::new();
let mut default = None;
while self.pos + 1 < self.tokens.len() {
let tok = try!(self.get_token(self.pos));
self.pos += 1;
match tok.data {
TKeyword(KCase) => {
let cond = self.parse();
let mut block = Vec::new();
try!(self.expect_punc(PColon, "switch case"));
loop {
match try!(self.get_token(self.pos)).data {
TKeyword(KCase) | TKeyword(KDefault) => break,
TPunctuator(PCloseBlock) => break,
_ => block.push(try!(self.parse()))
}
}
cases.push((cond.unwrap(), block));
},
TKeyword(KDefault) => {
let mut block = Vec::new();
try!(self.expect_punc(PColon, "default switch case"));
loop {
match try!(self.get_token(self.pos)).data {
TKeyword(KCase) | TKeyword(KDefault) => break,
TPunctuator(PCloseBlock) => break,
_ => block.push(try!(self.parse()))
}
}
default = Some(mk!(BlockExpr(block)));
},
TPunctuator(PCloseBlock) => break,
_ => return Err(Expected(vec!(TKeyword(KCase), TKeyword(KDefault), TPunctuator(PCloseBlock)), tok, "switch block"))
}
}
try!(self.expect_punc(PCloseBlock, "switch block"));
Ok(mk!(SwitchExpr(box value.unwrap(), cases, match default {
Some(v) => Some(box v),
None => None
})))
},
KFunction => {
let tk = try!(self.get_token(self.pos));
let name = match tk.data {
TIdentifier(ref name) => {
self.pos += 1;
Some(name.clone())
},
TPunctuator(POpenParen) => None,
_ => return Err(Expected(vec!(TIdentifier("identifier".into_string())), tk.clone(), "function name"))
};
try!(self.expect_punc(POpenParen, "function"));
let mut args:Vec<String> = Vec::new();
let mut tk = try!(self.get_token(self.pos));
while tk.data != TPunctuator(PCloseParen) {
match tk.data {
TIdentifier(ref id) => args.push(id.clone()),
_ => return Err(Expected(vec!(TIdentifier("identifier".into_string())), tk.clone(), "function arguments"))
}
self.pos += 1;
if try!(self.get_token(self.pos)).data == TPunctuator(PComma) {
self.pos += 1;
}
tk = try!(self.get_token(self.pos));
}
self.pos += 1;
let block = try!(self.parse());
Ok(mk!(FunctionDeclExpr(name, args, box block)))
},
_ => Err(UnexpectedKeyword(keyword))
}
}
pub fn parse(&mut self) -> ParseResult {
if self.pos > self.tokens.len() {
return Err(AbruptEnd);
}
let token = try!(self.get_token(self.pos));
self.pos += 1;
let expr : Expr = match token.data {
TPunctuator(PSemicolon) | TComment(_) if self.pos < self.tokens.len() => try!(self.parse()),
TPunctuator(PSemicolon) | TComment(_) => mk!(ConstExpr(CUndefined)),
TNumericLiteral(num) =>
mk!(ConstExpr(CNum(num))),
TNullLiteral =>
mk!(ConstExpr(CNull)),
TStringLiteral(text) =>
mk!(ConstExpr(CString(text))),
TBooleanLiteral(val) =>
mk!(ConstExpr(CBool(val))),
TIdentifier(ref s) if s.as_slice() == "undefined" =>
mk!(ConstExpr(CUndefined)),
TIdentifier(s) =>
mk!(LocalExpr(s)),
TKeyword(keyword) =>
try!(self.parse_struct(keyword)),
TPunctuator(POpenParen) => {
match try!(self.get_token(self.pos)).data {
TPunctuator(PCloseParen) if try!(self.get_token(self.pos + 1)).data == TPunctuator(PArrow) => {
self.pos += 2;
let expr = try!(self.parse());
mk!(ArrowFunctionDeclExpr(Vec::new(), box expr), token)
},
_ => {
let next = try!(self.parse());
let next_tok = try!(self.get_token(self.pos));
self.pos += 1;
match next_tok.data {
TPunctuator(PCloseParen) => next,
TPunctuator(PComma) => {let mut args = vec!(match next.def {
LocalExpr(name) => name,
_ => "".into_string()
}, match try!(self.get_token(self.pos)).data {
TIdentifier(ref id) => id.clone(),
_ => "".into_string()
});
let mut expect_ident = true;
loop {
self.pos += 1;
let curr_tk = try!(self.get_token(self.pos));
match curr_tk.data {
TIdentifier(ref id) if expect_ident => {
args.push(id.clone());
expect_ident = false;
},
TPunctuator(PComma) => {
expect_ident = true;
},
TPunctuator(PCloseParen) => {
self.pos += 1;
break;
},
_ if expect_ident => return Err(Expected(vec!(TIdentifier("identifier".into_string())), curr_tk, "arrow function")),
_ => return Err(Expected(vec!(TPunctuator(PComma), TPunctuator(PCloseParen)), curr_tk, "arrow function"))
}
}
try!(self.expect(TPunctuator(PArrow), "arrow function"));
let expr = try!(self.parse());
mk!(ArrowFunctionDeclExpr(args, box expr), token)
}
_ => return Err(Expected(vec!(TPunctuator(PCloseParen)), next_tok, "brackets"))
}
}
}
},
TPunctuator(POpenBracket) => {
let mut array : Vec<Expr> = Vec::new();
let mut expect_comma_or_end = try!(self.get_token(self.pos)).data == TPunctuator(PCloseBracket);
loop {
let token = try!(self.get_token(self.pos));
if token.data == TPunctuator(PCloseBracket) && expect_comma_or_end {
self.pos += 1;
break;
} else if token.data == TPunctuator(PComma) && expect_comma_or_end {
expect_comma_or_end = false;
} else if token.data == TPunctuator(PComma) && !expect_comma_or_end {
array.push(mk!(ConstExpr(CNull)));
expect_comma_or_end = false;
} else if expect_comma_or_end {
return Err(Expected(vec!(TPunctuator(PComma), TPunctuator(PCloseBracket)), token.clone(), "array declaration"));
} else {
let parsed = try!(self.parse());
self.pos -= 1;
array.push(parsed);
expect_comma_or_end = true;
}
self.pos += 1;
}
mk!(ArrayDeclExpr(array), token)
},
TPunctuator(POpenBlock) if try!(self.get_token(self.pos)).data == TPunctuator(PCloseBlock) => {
self.pos += 1;
mk!(ObjectDeclExpr(box TreeMap::new()), token)
},
TPunctuator(POpenBlock) if try!(self.get_token(self.pos + 1)).data == TPunctuator(PColon) => {
let mut map = box TreeMap::new();
while try!(self.get_token(self.pos - 1)).data == TPunctuator(PComma) || map.len() == 0 {
let tk = try!(self.get_token(self.pos));
let name = match tk.data {
TIdentifier(ref id) => id.clone(),
TStringLiteral(ref str) => str.clone(),
_ => return Err(Expected(vec!(TIdentifier("identifier".into_string()), TStringLiteral("string".into_string())), tk, "object declaration"))
};
self.pos += 1;
try!(self.expect(TPunctuator(PColon), "object declaration"));
let value = try!(self.parse());
map.insert(name, value);
self.pos += 1;
}
mk!(ObjectDeclExpr(map), token)
},
TPunctuator(POpenBlock) => {
let mut exprs = Vec::new();
loop {
if try!(self.get_token(self.pos)).data == TPunctuator(PCloseBlock) {
break;
} else {
exprs.push(try!(self.parse()));
}
}
self.pos += 1;
mk!(BlockExpr(exprs), token)
},
TPunctuator(PSub) =>
mk!(UnaryOpExpr(UnaryMinus, box try!(self.parse()))),
TPunctuator(PAdd) =>
mk!(UnaryOpExpr(UnaryPlus, box try!(self.parse()))),
TPunctuator(PNot) =>
mk!(UnaryOpExpr(UnaryNot, box try!(self.parse()))),
TPunctuator(PInc) =>
mk!(UnaryOpExpr(UnaryIncrementPre, box try!(self.parse()))),
TPunctuator(PDec) =>
mk!(UnaryOpExpr(UnaryDecrementPre, box try!(self.parse()))),
_ => return Err(Expected(Vec::new(), token.clone(), "script"))
};
if self.pos >= self.tokens.len() {
Ok(expr)
} else {
self.parse_next(expr)
}
}
fn get_token(&self, pos:uint) -> Result<Token, ParseError> {
if pos < self.tokens.len() {
Ok(self.tokens.get(pos).clone())
} else {
Err(AbruptEnd)
}
}
fn parse_next(&mut self, expr:Expr) -> ParseResult {
let next = try!(self.get_token(self.pos));
let mut carry_on = true;
let mut result = expr.clone();
match next.data {
TPunctuator(PDot) => {
self.pos += 1;
let tk = try!(self.get_token(self.pos));
match tk.data {
TIdentifier(ref s) => result = mk!(GetConstFieldExpr(box expr, s.to_string())),
_ => return Err(Expected(vec!(TIdentifier("identifier".into_string())), tk, "field access"))
}
self.pos += 1;
},
TPunctuator(POpenParen) => {
let mut args = Vec::new();
let mut expect_comma_or_end = try!(self.get_token(self.pos + 1)).data == TPunctuator(PCloseParen);
loop {
self.pos += 1;
let token = try!(self.get_token(self.pos));
if token.data == TPunctuator(PCloseParen) && expect_comma_or_end {
self.pos += 1;
break;
} else if token.data == TPunctuator(PComma) && expect_comma_or_end {
expect_comma_or_end = false;
} else if expect_comma_or_end {
return Err(Expected(vec!(TPunctuator(PComma), TPunctuator(PCloseParen)), token, "function call arguments"));
} else {
let parsed = try!(self.parse());
self.pos -= 1;
args.push(parsed);
expect_comma_or_end = true;
}
}
result = mk!(CallExpr(box expr, args));
},
TPunctuator(PQuestion) => {
self.pos += 1;
let if_e = try!(self.parse());
try!(self.expect(TPunctuator(PColon), "if expression"));
let else_e = try!(self.parse());
result = mk!(IfExpr(box expr, box if_e, Some(box else_e)));
},
TPunctuator(POpenBracket) => {
self.pos += 1;
let index = try!(self.parse());
try!(self.expect(TPunctuator(PCloseBracket), "array index"));
result = mk!(GetFieldExpr(box expr, box index));
},
TPunctuator(PSemicolon) | TComment(_) => {
self.pos += 1;
},
TPunctuator(PAssign) => {
self.pos += 1;
let next = try!(self.parse());
result = mk!(AssignExpr(box expr, box next));
},
TPunctuator(PArrow) => {
self.pos += 1;
let mut args = Vec::with_capacity(1);
match result.def {
LocalExpr(name) => args.push(name),
_ => return Err(ExpectedExpr("identifier", result))
}
let next = try!(self.parse());
result = mk!(ArrowFunctionDeclExpr(args, box next));
},
TPunctuator(PAdd) =>
result = try!(self.binop(BinNum(OpAdd), expr)),
TPunctuator(PSub) =>
result = try!(self.binop(BinNum(OpSub), expr)),
TPunctuator(PMul) =>
result = try!(self.binop(BinNum(OpMul), expr)),
TPunctuator(PDiv) =>
result = try!(self.binop(BinNum(OpDiv), expr)),
TPunctuator(PMod) =>
result = try!(self.binop(BinNum(OpMod), expr)),
TPunctuator(PBoolAnd) =>
result = try!(self.binop(BinLog(LogAnd), expr)),
TPunctuator(PBoolOr) =>
result = try!(self.binop(BinLog(LogOr), expr)),
TPunctuator(PAnd) =>
result = try!(self.binop(BinBit(BitAnd), expr)),
TPunctuator(POr) =>
result = try!(self.binop(BinBit(BitOr), expr)),
TPunctuator(PXor) =>
result = try!(self.binop(BinBit(BitXor), expr)),
TPunctuator(PLeftSh) =>
result = try!(self.binop(BinBit(BitShl), expr)),
TPunctuator(PRightSh) =>
result = try!(self.binop(BinBit(BitShr), expr)),
TPunctuator(PEq) =>
result = try!(self.binop(BinComp(CompEqual), expr)),
TPunctuator(PNotEq) =>
result = try!(self.binop(BinComp(CompNotEqual), expr)),
TPunctuator(PStrictEq) =>
result = try!(self.binop(BinComp(CompStrictEqual), expr)),
TPunctuator(PStrictNotEq) =>
result = try!(self.binop(BinComp(CompStrictNotEqual), expr)),
TPunctuator(PLessThan) =>
result = try!(self.binop(BinComp(CompLessThan), expr)),
TPunctuator(PLessThanOrEq) =>
result = try!(self.binop(BinComp(CompLessThanOrEqual), expr)),
TPunctuator(PGreaterThan) =>
result = try!(self.binop(BinComp(CompGreaterThan), expr)),
TPunctuator(PGreaterThanOrEq) =>
result = try!(self.binop(BinComp(CompGreaterThanOrEqual), expr)),
TPunctuator(PInc) =>
result = mk!(UnaryOpExpr(UnaryIncrementPost, box try!(self.parse()))),
TPunctuator(PDec) =>
result = mk!(UnaryOpExpr(UnaryDecrementPost, box try!(self.parse()))),
_ => carry_on = false
};
if carry_on && self.pos < self.tokens.len() {
self.parse_next(result)
} else {
Ok(result)
}
}
fn binop(&mut self, op:BinOp, orig:Expr) -> Result<Expr, ParseError> {
let (precedence, assoc) = op.get_precedence_and_assoc();
self.pos += 1;
let next = try!(self.parse());
Ok(match next.def {
BinOpExpr(ref op2, ref a, ref b) => {
let other_precedence = op2.get_precedence();
if precedence < other_precedence || (precedence == other_precedence && !assoc) {
mk!(BinOpExpr(*op2, b.clone(), box mk!(BinOpExpr(op.clone(), box orig, a.clone()))))
} else {
mk!(BinOpExpr(op, box orig, box next.clone()))
}
},
_ => mk!(BinOpExpr(op, box orig, box next))
})
}
fn expect(&mut self, tk:TokenData, routine:&'static str) -> Result<(), ParseError> {
self.pos += 1;
let curr_tk = try!(self.get_token(self.pos - 1));
if curr_tk.data != tk {
Err(Expected(vec!(tk), curr_tk, routine))
} else {
Ok(())
}
}
#[inline(always)]
fn expect_punc(&mut self, p:Punctuator, routine:&'static str) -> Result<(), ParseError> {
self.expect(TPunctuator(p), routine)
}
}