forked from robvenn/nunjucks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.js
1284 lines (1094 loc) · 38.6 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
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
var lexer = require('./lexer');
var nodes = require('./nodes');
// jshint -W079
var Object = require('./object');
var lib = require('./lib');
var Parser = Object.extend({
init: function (tokens) {
this.tokens = tokens;
this.peeked = null;
this.breakOnBlocks = null;
this.dropLeadingWhitespace = false;
this.extensions = [];
},
nextToken: function (withWhitespace) {
var tok;
if(this.peeked) {
if(!withWhitespace && this.peeked.type === lexer.TOKEN_WHITESPACE) {
this.peeked = null;
}
else {
tok = this.peeked;
this.peeked = null;
return tok;
}
}
tok = this.tokens.nextToken();
if(!withWhitespace) {
while(tok && tok.type === lexer.TOKEN_WHITESPACE) {
tok = this.tokens.nextToken();
}
}
return tok;
},
peekToken: function () {
this.peeked = this.peeked || this.nextToken();
return this.peeked;
},
pushToken: function(tok) {
if(this.peeked) {
throw new Error('pushToken: can only push one token on between reads');
}
this.peeked = tok;
},
fail: function (msg, lineno, colno) {
if((lineno === undefined || colno === undefined) && this.peekToken()) {
var tok = this.peekToken();
lineno = tok.lineno;
colno = tok.colno;
}
if (lineno !== undefined) lineno += 1;
if (colno !== undefined) colno += 1;
throw new lib.TemplateError(msg, lineno, colno);
},
skip: function(type) {
var tok = this.nextToken();
if(!tok || tok.type !== type) {
this.pushToken(tok);
return false;
}
return true;
},
expect: function(type) {
var tok = this.nextToken();
if(tok.type !== type) {
this.fail('expected ' + type + ', got ' + tok.type,
tok.lineno,
tok.colno);
}
return tok;
},
skipValue: function(type, val) {
var tok = this.nextToken();
if(!tok || tok.type !== type || tok.value !== val) {
this.pushToken(tok);
return false;
}
return true;
},
skipSymbol: function(val) {
return this.skipValue(lexer.TOKEN_SYMBOL, val);
},
advanceAfterBlockEnd: function(name) {
var tok;
if(!name) {
tok = this.peekToken();
if(!tok) {
this.fail('unexpected end of file');
}
if(tok.type !== lexer.TOKEN_SYMBOL) {
this.fail('advanceAfterBlockEnd: expected symbol token or ' +
'explicit name to be passed');
}
name = this.nextToken().value;
}
tok = this.nextToken();
if(tok && tok.type === lexer.TOKEN_BLOCK_END) {
if(tok.value.charAt(0) === '-') {
this.dropLeadingWhitespace = true;
}
}
else {
this.fail('expected block end in ' + name + ' statement');
}
return tok;
},
advanceAfterVariableEnd: function() {
if(!this.skip(lexer.TOKEN_VARIABLE_END)) {
this.fail('expected variable end');
}
},
parseFor: function() {
var forTok = this.peekToken();
var node;
var endBlock;
if(this.skipSymbol('for')) {
node = new nodes.For(forTok.lineno, forTok.colno);
endBlock = 'endfor';
}
else if(this.skipSymbol('asyncEach')) {
node = new nodes.AsyncEach(forTok.lineno, forTok.colno);
endBlock = 'endeach';
}
else if(this.skipSymbol('asyncAll')) {
node = new nodes.AsyncAll(forTok.lineno, forTok.colno);
endBlock = 'endall';
}
else {
this.fail('parseFor: expected for{Async}', forTok.lineno, forTok.colno);
}
node.name = this.parsePrimary();
if(!(node.name instanceof nodes.Symbol)) {
this.fail('parseFor: variable name expected for loop');
}
var type = this.peekToken().type;
if(type === lexer.TOKEN_COMMA) {
// key/value iteration
var key = node.name;
node.name = new nodes.Array(key.lineno, key.colno);
node.name.addChild(key);
while(this.skip(lexer.TOKEN_COMMA)) {
var prim = this.parsePrimary();
node.name.addChild(prim);
}
}
if(!this.skipSymbol('in')) {
this.fail('parseFor: expected "in" keyword for loop',
forTok.lineno,
forTok.colno);
}
node.arr = this.parseExpression();
this.advanceAfterBlockEnd(forTok.value);
node.body = this.parseUntilBlocks(endBlock, 'else');
if(this.skipSymbol('else')) {
this.advanceAfterBlockEnd('else');
node.else_ = this.parseUntilBlocks(endBlock);
}
this.advanceAfterBlockEnd();
return node;
},
parseMacro: function() {
var macroTok = this.peekToken();
if(!this.skipSymbol('macro')) {
this.fail('expected macro');
}
var name = this.parsePrimary(true);
var args = this.parseSignature();
var node = new nodes.Macro(macroTok.lineno,
macroTok.colno,
name,
args);
this.advanceAfterBlockEnd(macroTok.value);
node.body = this.parseUntilBlocks('endmacro');
this.advanceAfterBlockEnd();
return node;
},
parseCall: function() {
// a call block is parsed as a normal FunCall, but with an added
// 'caller' kwarg which is a Caller node.
var callTok = this.peekToken();
if(!this.skipSymbol('call')) {
this.fail('expected call');
}
var callerArgs = this.parseSignature(true) || new nodes.NodeList();
var macroCall = this.parsePrimary();
this.advanceAfterBlockEnd(callTok.value);
var body = this.parseUntilBlocks('endcall');
this.advanceAfterBlockEnd();
var callerName = new nodes.Symbol(callTok.lineno,
callTok.colno,
'caller');
var callerNode = new nodes.Caller(callTok.lineno,
callTok.colno,
callerName,
callerArgs,
body);
// add the additional caller kwarg, adding kwargs if necessary
var args = macroCall.args.children;
if (!(args[args.length-1] instanceof nodes.KeywordArgs)) {
args.push(new nodes.KeywordArgs());
}
var kwargs = args[args.length - 1];
kwargs.addChild(new nodes.Pair(callTok.lineno,
callTok.colno,
callerName,
callerNode));
return new nodes.Output(callTok.lineno,
callTok.colno,
[macroCall]);
},
parseWithContext: function() {
var tok = this.peekToken();
var withContext = null;
if(this.skipSymbol('with')) {
withContext = true;
}
else if(this.skipSymbol('without')) {
withContext = false;
}
if(withContext !== null) {
if(!this.skipSymbol('context')) {
this.fail('parseFrom: expected context after with/without',
tok.lineno,
tok.colno);
}
}
return withContext;
},
parseImport: function() {
var importTok = this.peekToken();
if(!this.skipSymbol('import')) {
this.fail('parseImport: expected import',
importTok.lineno,
importTok.colno);
}
var template = this.parseExpression();
if(!this.skipSymbol('as')) {
this.fail('parseImport: expected "as" keyword',
importTok.lineno,
importTok.colno);
}
var target = this.parseExpression();
var withContext = this.parseWithContext();
var node = new nodes.Import(importTok.lineno,
importTok.colno,
template,
target,
withContext);
this.advanceAfterBlockEnd(importTok.value);
return node;
},
parseFrom: function() {
var fromTok = this.peekToken();
if(!this.skipSymbol('from')) {
this.fail('parseFrom: expected from');
}
var template = this.parseExpression();
if(!this.skipSymbol('import')) {
this.fail('parseFrom: expected import',
fromTok.lineno,
fromTok.colno);
}
var names = new nodes.NodeList(),
withContext;
while(1) {
var nextTok = this.peekToken();
if(nextTok.type === lexer.TOKEN_BLOCK_END) {
if(!names.children.length) {
this.fail('parseFrom: Expected at least one import name',
fromTok.lineno,
fromTok.colno);
}
// Since we are manually advancing past the block end,
// need to keep track of whitespace control (normally
// this is done in `advanceAfterBlockEnd`
if(nextTok.value.charAt(0) === '-') {
this.dropLeadingWhitespace = true;
}
this.nextToken();
break;
}
if(names.children.length > 0 && !this.skip(lexer.TOKEN_COMMA)) {
this.fail('parseFrom: expected comma',
fromTok.lineno,
fromTok.colno);
}
var name = this.parsePrimary();
if(name.value.charAt(0) === '_') {
this.fail('parseFrom: names starting with an underscore ' +
'cannot be imported',
name.lineno,
name.colno);
}
if(this.skipSymbol('as')) {
var alias = this.parsePrimary();
names.addChild(new nodes.Pair(name.lineno,
name.colno,
name,
alias));
}
else {
names.addChild(name);
}
withContext = this.parseWithContext();
}
return new nodes.FromImport(fromTok.lineno,
fromTok.colno,
template,
names,
withContext);
},
parseBlock: function() {
var tag = this.peekToken();
if(!this.skipSymbol('block')) {
this.fail('parseBlock: expected block', tag.lineno, tag.colno);
}
var node = new nodes.Block(tag.lineno, tag.colno);
node.name = this.parsePrimary();
if(!(node.name instanceof nodes.Symbol)) {
this.fail('parseBlock: variable name expected',
tag.lineno,
tag.colno);
}
this.advanceAfterBlockEnd(tag.value);
node.body = this.parseUntilBlocks('endblock');
this.skipSymbol('endblock');
this.skipSymbol(node.name.value);
var tok = this.peekToken();
if(!tok) {
this.fail('parseBlock: expected endblock, got end of file');
}
this.advanceAfterBlockEnd(tok.value);
return node;
},
parseExtends: function() {
var tagName = 'extends';
var tag = this.peekToken();
if(!this.skipSymbol(tagName)) {
this.fail('parseTemplateRef: expected '+ tagName);
}
var node = new nodes.Extends(tag.lineno, tag.colno);
node.template = this.parseExpression();
this.advanceAfterBlockEnd(tag.value);
return node;
},
parseInclude: function() {
var tagName = 'include';
var tag = this.peekToken();
if(!this.skipSymbol(tagName)) {
this.fail('parseInclude: expected '+ tagName);
}
var node = new nodes.Include(tag.lineno, tag.colno);
node.template = this.parseExpression();
if(this.skipSymbol('ignore') && this.skipSymbol('missing')) {
node.ignoreMissing = true;
}
this.advanceAfterBlockEnd(tag.value);
return node;
},
parseIf: function() {
var tag = this.peekToken();
var node;
if(this.skipSymbol('if') || this.skipSymbol('elif')) {
node = new nodes.If(tag.lineno, tag.colno);
}
else if(this.skipSymbol('ifAsync')) {
node = new nodes.IfAsync(tag.lineno, tag.colno);
}
else {
this.fail('parseIf: expected if or elif',
tag.lineno,
tag.colno);
}
node.cond = this.parseExpression();
this.advanceAfterBlockEnd(tag.value);
node.body = this.parseUntilBlocks('elif', 'else', 'endif');
var tok = this.peekToken();
switch(tok && tok.value) {
case 'elif':
node.else_ = this.parseIf();
break;
case 'else':
this.advanceAfterBlockEnd();
node.else_ = this.parseUntilBlocks('endif');
this.advanceAfterBlockEnd();
break;
case 'endif':
node.else_ = null;
this.advanceAfterBlockEnd();
break;
default:
this.fail('parseIf: expected elif, else, or endif, ' +
'got end of file');
}
return node;
},
parseSet: function() {
var tag = this.peekToken();
if(!this.skipSymbol('set')) {
this.fail('parseSet: expected set', tag.lineno, tag.colno);
}
var node = new nodes.Set(tag.lineno, tag.colno, []);
var target;
while((target = this.parsePrimary())) {
node.targets.push(target);
if(!this.skip(lexer.TOKEN_COMMA)) {
break;
}
}
if(!this.skipValue(lexer.TOKEN_OPERATOR, '=')) {
if (!this.skip(lexer.TOKEN_BLOCK_END)) {
this.fail('parseSet: expected = or block end in set tag',
tag.lineno,
tag.colno);
}
else {
node.body = new nodes.Capture(
tag.lineno,
tag.colno,
this.parseUntilBlocks('endset')
);
node.value = null;
this.advanceAfterBlockEnd();
}
}
else {
node.value = this.parseExpression();
this.advanceAfterBlockEnd(tag.value);
}
return node;
},
parseStatement: function () {
var tok = this.peekToken();
var node;
if(tok.type !== lexer.TOKEN_SYMBOL) {
this.fail('tag name expected', tok.lineno, tok.colno);
}
if(this.breakOnBlocks &&
lib.indexOf(this.breakOnBlocks, tok.value) !== -1) {
return null;
}
switch(tok.value) {
case 'raw': return this.parseRaw();
case 'if':
case 'ifAsync':
return this.parseIf();
case 'for':
case 'asyncEach':
case 'asyncAll':
return this.parseFor();
case 'block': return this.parseBlock();
case 'extends': return this.parseExtends();
case 'include': return this.parseInclude();
case 'set': return this.parseSet();
case 'macro': return this.parseMacro();
case 'call': return this.parseCall();
case 'import': return this.parseImport();
case 'from': return this.parseFrom();
case 'filter': return this.parseFilterStatement();
default:
if (this.extensions.length) {
for (var i = 0; i < this.extensions.length; i++) {
var ext = this.extensions[i];
if (lib.indexOf(ext.tags || [], tok.value) !== -1) {
return ext.parse(this, nodes, lexer);
}
}
}
this.fail('unknown block tag: ' + tok.value, tok.lineno, tok.colno);
}
return node;
},
parseRaw: function() {
// Look for upcoming raw blocks (ignore all other kinds of blocks)
var rawBlockRegex = /([\s\S]*?){%\s*(raw|endraw)\s*(?=%})%}/;
var rawLevel = 1;
var str = '';
var matches = null;
// Skip opening raw token
// Keep this token to track line and column numbers
var begun = this.advanceAfterBlockEnd();
// Exit when there's nothing to match
// or when we've found the matching "endraw" block
while((matches = this.tokens._extractRegex(rawBlockRegex)) && rawLevel > 0) {
var all = matches[0];
var pre = matches[1];
var blockName = matches[2];
// Adjust rawlevel
if(blockName === 'raw') {
rawLevel += 1;
} else if(blockName === 'endraw') {
rawLevel -= 1;
}
// Add to str
if(rawLevel === 0) {
// We want to exclude the last "endraw"
str += pre;
// Move tokenizer to beginning of endraw block
this.tokens.backN(all.length - pre.length);
} else {
str += all;
}
}
return new nodes.Output(
begun.lineno,
begun.colno,
[new nodes.TemplateData(begun.lineno, begun.colno, str)]
);
},
parsePostfix: function(node) {
var lookup, tok = this.peekToken();
while(tok) {
if(tok.type === lexer.TOKEN_LEFT_PAREN) {
// Function call
node = new nodes.FunCall(tok.lineno,
tok.colno,
node,
this.parseSignature());
}
else if(tok.type === lexer.TOKEN_LEFT_BRACKET) {
// Reference
lookup = this.parseAggregate();
if(lookup.children.length > 1) {
this.fail('invalid index');
}
node = new nodes.LookupVal(tok.lineno,
tok.colno,
node,
lookup.children[0]);
}
else if(tok.type === lexer.TOKEN_OPERATOR && tok.value === '.') {
// Reference
this.nextToken();
var val = this.nextToken();
if(val.type !== lexer.TOKEN_SYMBOL) {
this.fail('expected name as lookup value, got ' + val.value,
val.lineno,
val.colno);
}
// Make a literal string because it's not a variable
// reference
lookup = new nodes.Literal(val.lineno,
val.colno,
val.value);
node = new nodes.LookupVal(tok.lineno,
tok.colno,
node,
lookup);
}
else {
break;
}
tok = this.peekToken();
}
return node;
},
parseExpression: function() {
var node = this.parseInlineIf();
return node;
},
parseInlineIf: function() {
var node = this.parseOr();
if(this.skipSymbol('if')) {
var cond_node = this.parseOr();
var body_node = node;
node = new nodes.InlineIf(node.lineno, node.colno);
node.body = body_node;
node.cond = cond_node;
if(this.skipSymbol('else')) {
node.else_ = this.parseOr();
} else {
node.else_ = null;
}
}
return node;
},
parseOr: function() {
var node = this.parseAnd();
while(this.skipSymbol('or')) {
var node2 = this.parseAnd();
node = new nodes.Or(node.lineno,
node.colno,
node,
node2);
}
return node;
},
parseAnd: function() {
var node = this.parseNot();
while(this.skipSymbol('and')) {
var node2 = this.parseNot();
node = new nodes.And(node.lineno,
node.colno,
node,
node2);
}
return node;
},
parseNot: function() {
var tok = this.peekToken();
if(this.skipSymbol('not')) {
return new nodes.Not(tok.lineno,
tok.colno,
this.parseNot());
}
return this.parseIn();
},
parseIn: function() {
var node = this.parseCompare();
while(1) {
// check if the next token is 'not'
var tok = this.nextToken();
if (!tok) { break; }
var invert = tok.type === lexer.TOKEN_SYMBOL && tok.value === 'not';
// if it wasn't 'not', put it back
if (!invert) { this.pushToken(tok); }
if (this.skipSymbol('in')) {
var node2 = this.parseCompare();
node = new nodes.In(node.lineno,
node.colno,
node,
node2);
if (invert) {
node = new nodes.Not(node.lineno,
node.colno,
node);
}
}
else {
// if we'd found a 'not' but this wasn't an 'in', put back the 'not'
if (invert) { this.pushToken(tok); }
break;
}
}
return node;
},
parseCompare: function() {
var compareOps = ['==', '===', '!=', '!==', '<', '>', '<=', '>='];
var expr = this.parseConcat();
var ops = [];
while(1) {
var tok = this.nextToken();
if(!tok) {
break;
}
else if(lib.indexOf(compareOps, tok.value) !== -1) {
ops.push(new nodes.CompareOperand(tok.lineno,
tok.colno,
this.parseConcat(),
tok.value));
}
else {
this.pushToken(tok);
break;
}
}
if(ops.length) {
return new nodes.Compare(ops[0].lineno,
ops[0].colno,
expr,
ops);
}
else {
return expr;
}
},
// finds the '~' for string concatenation
parseConcat: function(){
var node = this.parseAdd();
while(this.skipValue(lexer.TOKEN_TILDE, '~')) {
var node2 = this.parseAdd();
node = new nodes.Concat(node.lineno,
node.colno,
node,
node2);
}
return node;
},
parseAdd: function() {
var node = this.parseSub();
while(this.skipValue(lexer.TOKEN_OPERATOR, '+')) {
var node2 = this.parseSub();
node = new nodes.Add(node.lineno,
node.colno,
node,
node2);
}
return node;
},
parseSub: function() {
var node = this.parseMul();
while(this.skipValue(lexer.TOKEN_OPERATOR, '-')) {
var node2 = this.parseMul();
node = new nodes.Sub(node.lineno,
node.colno,
node,
node2);
}
return node;
},
parseMul: function() {
var node = this.parseDiv();
while(this.skipValue(lexer.TOKEN_OPERATOR, '*')) {
var node2 = this.parseDiv();
node = new nodes.Mul(node.lineno,
node.colno,
node,
node2);
}
return node;
},
parseDiv: function() {
var node = this.parseFloorDiv();
while(this.skipValue(lexer.TOKEN_OPERATOR, '/')) {
var node2 = this.parseFloorDiv();
node = new nodes.Div(node.lineno,
node.colno,
node,
node2);
}
return node;
},
parseFloorDiv: function() {
var node = this.parseMod();
while(this.skipValue(lexer.TOKEN_OPERATOR, '//')) {
var node2 = this.parseMod();
node = new nodes.FloorDiv(node.lineno,
node.colno,
node,
node2);
}
return node;
},
parseMod: function() {
var node = this.parsePow();
while(this.skipValue(lexer.TOKEN_OPERATOR, '%')) {
var node2 = this.parsePow();
node = new nodes.Mod(node.lineno,
node.colno,
node,
node2);
}
return node;
},
parsePow: function() {
var node = this.parseUnary();
while(this.skipValue(lexer.TOKEN_OPERATOR, '**')) {
var node2 = this.parseUnary();
node = new nodes.Pow(node.lineno,
node.colno,
node,
node2);
}
return node;
},
parseUnary: function(noFilters) {
var tok = this.peekToken();
var node;
if(this.skipValue(lexer.TOKEN_OPERATOR, '-')) {
node = new nodes.Neg(tok.lineno,
tok.colno,
this.parseUnary(true));
}
else if(this.skipValue(lexer.TOKEN_OPERATOR, '+')) {
node = new nodes.Pos(tok.lineno,
tok.colno,
this.parseUnary(true));
}
else {
node = this.parsePrimary();
}
if(!noFilters) {
node = this.parseFilter(node);
}
return node;
},
parsePrimary: function (noPostfix) {
var tok = this.nextToken();
var val;
var node = null;
if(!tok) {
this.fail('expected expression, got end of file');
}
else if(tok.type === lexer.TOKEN_STRING) {
val = tok.value;
}
else if(tok.type === lexer.TOKEN_INT) {
val = parseInt(tok.value, 10);
}
else if(tok.type === lexer.TOKEN_FLOAT) {
val = parseFloat(tok.value);
}
else if(tok.type === lexer.TOKEN_BOOLEAN) {
if(tok.value === 'true') {
val = true;
}
else if(tok.value === 'false') {
val = false;
}
else {
this.fail('invalid boolean: ' + tok.value,
tok.lineno,
tok.colno);
}
}
else if(tok.type === lexer.TOKEN_NONE) {
val = null;
}
else if (tok.type === lexer.TOKEN_REGEX) {
val = new RegExp(tok.value.body, tok.value.flags);
}
if(val !== undefined) {
node = new nodes.Literal(tok.lineno, tok.colno, val);
}
else if(tok.type === lexer.TOKEN_SYMBOL) {
node = new nodes.Symbol(tok.lineno, tok.colno, tok.value);
if(!noPostfix) {
node = this.parsePostfix(node);
}
}
else {
// See if it's an aggregate type, we need to push the
// current delimiter token back on
this.pushToken(tok);
node = this.parseAggregate();
}
if(node) {
return node;
}
else {
this.fail('unexpected token: ' + tok.value,
tok.lineno,
tok.colno);
}
},
parseFilterName: function() {
var tok = this.expect(lexer.TOKEN_SYMBOL);
var name = tok.value;
while(this.skipValue(lexer.TOKEN_OPERATOR, '.')) {
name += '.' + this.expect(lexer.TOKEN_SYMBOL).value;
}
return new nodes.Symbol(tok.lineno, tok.colno, name);
},
parseFilterArgs: function(node) {
if(this.peekToken().type === lexer.TOKEN_LEFT_PAREN) {
// Get a FunCall node and add the parameters to the
// filter
var call = this.parsePostfix(node);
return call.args.children;
}
return [];