-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser-old.js
More file actions
875 lines (772 loc) · 24.9 KB
/
parser-old.js
File metadata and controls
875 lines (772 loc) · 24.9 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
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
"use strict";
let ArrayDef = require("./entities/array");
let ArrayAt = require("./entities/arrayAt");
let Assignment = require("./entities/assignment");
let Attr = require("./entities/attr");
let BinaryExp = require("./entities/binaryExp");
let Block = require("./entities/block");
let Call = require("./entities/call");
let Def = require("./entities/def");
let ElemFunc = require("./entities/elemFunc");
let For = require("./entities/for");
let HashMap = require("./entities/hashMap");
let If = require("./entities/if");
let Include = require("./entities/include");
let Iterable = require("./entities/iterable");
let Label = require("./entities/label");
let Literal = require("./entities/literal");
let Lookup = require("./entities/lookup");
let Program = require("./entities/program");
let Return = require("./entities/return");
let Selector = require("./entities/selector");
let SpecialBlock = require("./entities/specialBlock");
let Template = require("./entities/template");
let This = require("./entities/this");
let Token = require("./entities/token");
let UnaryExp = require("./entities/unaryExp");
let While = require("./entities/while");
var lits = ["stringlit", "intlit", "floatlit", "boollit"];
var builtins = ['script', 'style'];
var controlTypes = ['if', 'for', 'while'];
var binAssignOps = ["plus", "minus", "multop", "boolop"];
module.exports = (scannerTokens, error, verbose) => {
var tokens = scannerTokens;
var lastToken;
var log = (message) => {
if(verbose) {
console.log(message);
}
};
var matchLog = (message) => {
log(message + ` at line ${tokens[0].line} col ${tokens[0].column}`);
};
var program = () => {
matchLog("Matching the program");
let tree = new Program( block() );
match("EOF");
return tree;
};
var block = () => {
matchLog("Matching block");
let statements = [];
// If we get a dedent or an EOF, we have no more block
while(!at("dedent") && !at("EOF")){
while( at("newline") ) {
match("newline");
}
let stmt = statement();
// They can put as many blank lines as they'd like, but that
// doesn't mean we have to pay attention to them
if(stmt !== "blank"){
statements.push( stmt );
}
// child blocks are special cases. Since dedents come after the
// newlines, the ChildBlock pattern needs to consume the newline.
if(lastToken.type !== "dedent" && !at("EOF")){
do{
match("newline");
} while( at("newline") );
}
}
return new Block(statements);
};
var statement = () => {
matchLog("Matching a statement");
if( at("comment") ) {
return new Token( match("comment") );
}
else if ( at('template') ) {
return template();
}
else if( at(controlTypes) ) {
return control();
}
else if( at("def") ) {
return def();
}
else if( at("return") ) {
match("return");
return new Return( exp() );
}
else if( atExp() ){
let ourExp = exp();
if( at("equals") || at(binAssignOps) ) {
if( at(binAssignOps) ){
if( at("boolop") ) {
let op = new Token( match("boolop") );
match("equals");
ourExp = new Assignment(
ourExp,
new BinaryExp(ourExp, exp(), op)
);
}
else if( at("multop") ) {
let op = new Token( match("multop") );
match("equals");
ourExp = new Assignment(
ourExp,
new BinaryExp(ourExp, exp(), op)
);
}
else {
error.hint = "You have 'id *something*= *stuff*' and we're trying to figure out"
+ "what the *something* means. We're looking for stuff like +=, -= etc";
let op = new Token( at("minus") ? match("minus") : match("plus") );
match("equals");
ourExp = new Assignment(
ourExp,
new BinaryExp(ourExp, exp(), op)
);
}
}
else {
match("equals");
ourExp = new Assignment( ourExp, exp());
}
}
return ourExp;
}
else {
error.hint = "Did you put a blank line without indentation?";
error.expected("some kind of statement", tokens.shift());
error.hint = "";
}
};
var template = () => {
matchLog('Matching template');
let filename = match("template").text;
let labels = [];
if( at("newline") ) {
match("newline");
match("indent");
do{
labels.push({
"label": label(),
"body": childBlock()
});
} while ( !at("dedent") );
match("dedent");
}
return new Template(filename, labels, verbose);
};
var label = () =>{
matchLog("Matching label");
match("openCurly");
let ourLabel = new Label( match("bareword") );
match("closeCurly");
return ourLabel;
};
var childBlock = () => {
matchLog("Matching childBlock");
match("newline");
match("indent");
let ourBlock;
if( at("js") ){
ourBlock = JSBlock();
}
else if( at("css") ){
ourBlock = CSSBlock();
}
else {
ourBlock = block();
}
if( !at("EOF")){
match("dedent");
}
return ourBlock;
};
var JSBlock = () => {
matchLog("Matching JS Block");
let statements = [];
if( atExp() ){
statements.push( exp() );
}
do {
statements.push( match("js") );
if( atExp() ){
statements.push( exp() );
}
// If they put newlines in their JS, more power to them
while( at("newline") ){
match("newline");
}
} while( at("js") );
return new SpecialBlock( statements, "js" );
};
var CSSBlock = () => {
matchLog("Matching CSS Block");
let statements = [];
if( atExp() ){
statements.push( exp() );
}
do {
statements.push( match("css") );
if( atExp() ){
statements.push( exp() );
}
// If they put newlines in their CSS, more power to them
while( at("newline") ){
match("newline");
}
} while( at("css") );
return new SpecialBlock( statements, "css" );
};
var control = () => {
matchLog("Matching control block");
if( at("if")) {
return ifDef();
}
else if( at("for") ) {
return forDef();
}
else if( at("while") ) {
return whileDef();
}
else {
error.parse(`${tokens[0].text} is not a recognized control statement`, tokens.shift());
}
};
var ifDef = () => {
matchLog("Matching if");
let conditionals = [];
if( at("if") ) {
match("if");
let condition = exp();
conditionals.push({
condition: condition,
body: childBlock()
});
while( at("else-if") ) {
match("else-if");
conditionals.push({
condition: exp(),
body: childBlock()
});
}
if( at("else") ) {
match("else");
conditionals.push({
body: childBlock()
});
}
}
else {
error.expected('an if statement', tokens.shift());
}
return new If( conditionals );
};
var forDef = () => {
matchLog("Matching for");
match('for');
let ourId = new Token(match("id"));
match("in");
return new For(ourId, iterable(), childBlock());
};
var iterable = () => {
matchLog("Matching iterable");
return new Iterable( exp() );
};
var arrayDef = () => {
matchLog("Matching array");
match("openSquare");
let elems = [];
if( !at("closeSquare") ){
if(atSequential(["intlit", "range", "intlit"])) {
let a = match("intlit");
let aVal = parseInt( a.text() );
match("range");
let b = match("intlit");
let bVal = parseInt( b.text() );
if( isNaN(aVal) ){
error.parse(`'${a.text()}' has to be a number`, a);
}
else if( isNaN(bVal) ){
error.parse(`'${b.text()}' has to be a number`, b);
}
else{
if( aVal < bVal ) {
for( let i = aVal; i < bVal; ++i ){
elems.push( new Literal("Intlit", {
type: "intlit",
text: i,
line: a.line(),
column: a.column()
}));
}
}
}
}
else if( at("newline") ) {
elems = argBlock();
}
else {
elems = [];
do{
elems.push(arg());
} while( !at("closeSquare") );
}
}
match("closeSquare");
return new ArrayDef( elems );
};
var argBlock = () => {
matchLog("Matching argBlock");
match("newline");
match("indent");
let ourArgs = [];
do {
ourArgs.push(arg());
match("newline");
} while( !at("dedent") );
match("dedent");
return ourArgs;
};
var arg = () => {
matchLog("Matching arg");
return exp();
};
var whileDef = () => {
matchLog("Matching while");
if( at("while") ) {
return new While( exp(), childBlock() );
}
else {
error.expected('a while statement', tokens.shift());
}
};
var def = () => {
matchLog("Matching definition");
match("def");
let name = new Token(match("bareword"));
match("openParen");
let params = [];
while( at("id") ) {
params.push( new Token(match("id")) );
if( at("comma") ) {
match("comma");
}
}
match("closeParen");
return new Def( name, params, childBlock() );
};
var exp = () => {
matchLog("Matching exp");
return ternaryIfExp();
}
var ternaryIfExp = () => {
matchLog("Matching ternaryIfExp");
let ourExp = boolExp();
if( at("question") ) {
match("question");
ourExp = [{
condition: ourExp,
body: boolExp()
}];
match("colon");
ourExp.push({ body: boolExp()});
}
return ourExp;
};
var boolExp = () => {
matchLog("Matching boolExp");
let ourExp = relExp();
while( at("boolop") && !atSequential(["boolop", "equals"])) {
let ourOp = match("boolop");
ourExp = new BinaryExp( ourExp, relExp(), new Token(ourOp) );
}
return ourExp;
};
var relExp = () => {
matchLog("Matching relExp");
let ourExp = addExp();
while( at("relop") && !atSequential(["relop", "equals"])) {
let ourOp = match("relop");
ourExp = new BinaryExp( ourExp, addExp(), new Token(ourOp) );
}
return ourExp;
};
var addExp = () => {
matchLog("Matching addExp");
let ourExp = multExp();
while( at(["plus", "minus"]) && !atIndex("equals", 1) ) {
let ourOp = at("plus") ? match("plus") : match("minus");
ourExp = new BinaryExp( ourExp, multExp(), new Token(ourOp) );
}
return ourExp;
};
var multExp = () => {
matchLog("Matching multExp");
let ourExp = postfixExp();
while( at("multop") && !atSequential(["multop", "equals"])) {
let ourOp = match("multop");
ourExp = new BinaryExp( ourExp, postfixExp(), new Token(ourOp) );
}
return ourExp;
};
var postfixExp = () => {
matchLog("Matching postfixExp");
let ourExp = elemFuncExp();
if( at("postfixop") ) {
ourExp = new UnaryExp( ourExp, new Token(match("postfixop")) );
}
return ourExp;
};
var elemFuncExp = () => {
matchLog("Matching elemFuncExp");
let ourExp = arrayElemExp();
while ( at("tilde") ) {
match("tilde")
let func = new Token(match("bareword"));
let ourArgs;
error.hint = "The ~ operator is for member functions only. Thus, if you don't put parens after, we're going to gobble up as many potential arguments as we can";
if( atArgs() ) {
ourArgs = args();
}
error.hint = "";
ourExp = new ElemFunc( ourExp, func, ourArgs);
}
return ourExp;
};
var arrayElemExp = () => {
matchLog("Matching arrayElemExp");
let ourExp = miscExp();
while( at("openSquare") ) {
match("openSquare");
let index;
if( atSequential(["bareword", "closeSquare"]) ) {
index = new Token(match("bareword"));
}
else {
index = exp();
}
ourExp = new ArrayAt(ourExp, index);
match("closeSquare");
}
return ourExp;
};
var miscExp = () => {
matchLog("Matching miscExp");
if( at(lits) ) {
return literal();
}
else if( atSequential(["openCurly", "bareword", "closeCurly"]) ) {
return label();
}
else if( at("include") ) {
return include();
}
else if( at("openSquare") ){
return arrayDef();
}
else if( at("openCurly") ) {
return hashMap();
}
else if( at("id") ) {
return new Token(match("id"));
}
else if( at("this") ) {
return new This(match("this"));
}
else if( at(["dot", "hash"]) ) {
return htmlSelect();
}
else if( at("openParen") ) {
match("openParen");
let exp = exp();
match("closeParen");
return exp;
}
else if( at(["prefixop", "minus"]) ) {
let op = at("prefixop") ? match("prefixop") : match("minus");
return new UnaryExp(exp(), op);
}
else if( at(["bareword", ...builtins])){
return call();
}
else{
error.expected('some kind of expression', tokens.shift());
}
};
var literal = () => {
matchLog("Matching literal");
for( let lit of lits ) {
if( at(lit) ) {
if( at("stringlit") ) {
return stringDef();
}
else {
return new Literal(lit, match(lit));
}
}
}
error.expected('some kind of literal', tokens.shift());
};
var stringDef = () => {
matchLog("Matching string");
let actualStr = new Token(match("stringlit"));
let str = new Literal("stringlit", actualStr);
while( at("interpolate") ) {
let interp = match("interpolate");
let fakePlusToken = {
type: "plus",
line: interp.line,
column: interp.column
}
let stringAndInter = new BinaryExp(str, exp(), fakePlusToken);
interp = match("/interpolate");
fakePlusToken = {
type: "plus",
line: interp.line,
column: interp.column
}
str = new BinaryExp(
stringAndInter,
new Literal("stringlit", match("stringlit")),
fakePlusToken
);
}
return str;
}
var include = () => {
matchLog("Matching include");
match("include");
return new Include(match("stringlit"), verbose);
};
var call = () => {
matchLog("Matching call");
let name;
let ourAttrs = [];
let ourArgs;
if( at(builtins) ){
name = builtIn();
}
else{
name = new Token(match("bareword"));
}
let classes = [];
while( at("dot") ){
classes.push(htmlClass());
}
if(classes.length > 0){
ourAttrs.push( new Attr("class", classes) );
}
if( at("hash") ) {
ourAttrs.push( new Attr("id", htmlId()) );
}
if( at("openSquare") ) {
ourAttrs = ourAttrs.concat( attrs() );
}
if( atArgs() ){
ourArgs = args();
}
return new Call(name, ourAttrs, ourArgs);
};
var builtIn = () => {
matchLog("Matching builtIn");
for( let builtin of builtins ) {
if( at(builtin) ) {
return new Token(match(builtin));
}
}
error.expected('some kind of built in function', tokens.shift());
};
var htmlSelect = () => {
matchLog("Matching htmlSelect");
if( at("dot") ){
return htmlClass();
}
else {
return htmlId();
}
};
var htmlClass = () => {
matchLog("Matching htmlClass");
match("dot");
let ourExp = match("bareword");
// Because of the .
ourExp.column--;
return new Selector("HtmlClass", new Token(ourExp));
}
var htmlId = () => {
matchLog("Matching htmlId");
match("hash");
let ourExp = match("bareword");
// Because of the #
ourExp.column--;
return new Selector("HtmlId", new Token(ourExp));
}
var attrs = () => {
matchLog("Matching attrs");
match("openSquare")
if( atBlock() ) {
let ourBlock = attrBlock();
match("closeSquare");
return ourBlock;
}
let ourAttrs = [];
while( !at("closeSquare") ) {
ourAttrs.push(attr());
}
match("closeSquare");
return ourAttrs;
};
var attrBlock = () => {
matchLog("Matching attrBlock");
match("newline");
match("indent");
let ourAttrs = [];
while( !at("dedent") ) {
ourAttrs.push(attr());
match("newline");
}
match("dedent");
return ourAttrs;
};
var attr = () => {
matchLog("Matching attr");
let key;
let value;
error.hint = "Did you use a reserved word as an index, like 'if', or did you forget an @ symbol?";
if(at("stringlit")) {
let str = match("stringlit");
// Is it an empty string?
if(!str.text.length){
error.parse("HashMap key can't be empty", str);
}
key = str.text;
}
else if(at(builtins)){
key = builtIn().text;
}
else {
key = match("bareword").text;
}
error.hint = "";
if(at("equals")) {
match("equals");
}
else {
match("colon");
}
value = exp();
return new Attr(key, value);
};
var args = () => {
matchLog("Matching args");
if( at("openParen") ){
match("openParen");
let ourArgs = [];
if( !at("closeParen") ) {
ourArgs.push(arg());
}
while( !at("closeParen") ) {
if( at("comma") ) {
match("comma");
}
ourArgs.push(arg());
}
match("closeParen");
return new Block(ourArgs);
}
else if( atBlock() ){
return childBlock();
}
else{
error.hint = "If you don't use parens or commas, we're going to try to gobble up as many expressions as we can as arguments.";
let ourArgs = [arg()];
while( at("comma") || atExp() ) {
if( at("comma") ) {
match("comma");
}
ourArgs.push(arg());
}
error.hint = "";
return new Block(ourArgs);
}
};
var hashMap = () => {
matchLog("Matching hashMap");
match("openCurly");
let pairs;
if( atBlock() ) {
pairs = attrBlock();
}
else {
if( at("bareword") ) {
pairs = [];
while( !at("closeCurly") ) {
pairs.push(attr());
}
}
}
match("closeCurly");
return new HashMap(pairs);
};
// Returns true if the next token has type "type", or a type in "type" if
// type is an array, false otherwise
var at = ( type ) => {
return atIndex( type, 0 );
};
var atIndex = ( type, i ) => {
if(tokens.length <= i) {
return false;
}
if(Array.isArray( type )) {
for(let j = 0; j < type.length; ++j) {
if( type[j] === tokens[i].type ) {
return true;
}
}
return false;
}
return type === tokens[i].type;
}
// Like at except if type is an array, it checks one after the other.
var atSequential = ( type ) => {
for(let i = 0; i < type.length; ++i) {
// If we don't have enough tokens or the token we're at doesn't
// match, no sale.
if(tokens.length <= i || type[i] !== tokens[i].type) {
return false;
}
}
return true;
};
var atBlock = () =>{
return atSequential(["newline", "indent"]);
}
var atArgs = () =>{
return at("openParen") || atBlock() || atExp();
}
var atExp = () =>{
return at([...lits,
"include",
"openSquare",
"openCurly",
"id",
"this",
"dot",
"hash",
"openParen",
"prefixop",
"minus",
...builtins,
"bareword"]);
}
// Pops off the top token if its type matches 'type', returns an error otherwise
var match = ( type ) => {
if( !tokens.length ) {
error.parse(`Expected ${type}, got end of program`);
}
else if( type === undefined || type === tokens[0].type) {
log(`Matched "${type}"` + (tokens[0].text ? ` with text "${tokens[0].text}"` : ""));
lastToken = tokens.shift();
log(`Tokens remaining: ${tokens.length}`);
return lastToken;
}
else {
error.expected(type, tokens.shift());
}
};
return program();
};