-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathparse.d
More file actions
1465 lines (1263 loc) · 36.4 KB
/
Copy pathparse.d
File metadata and controls
1465 lines (1263 loc) · 36.4 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
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
/* Digital Mars DMDScript source code.
* Copyright (c) 2000-2002 by Chromium Communications
* D version Copyright (c) 2004-2010 by Digital Mars
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
* written by Walter Bright
* http://www.digitalmars.com
*
* D2 port by Dmitry Olshansky
*
* DMDScript is implemented in the D Programming Language,
* http://www.digitalmars.com/d/
*
* For a C++ implementation of DMDScript, including COM support, see
* http://www.digitalmars.com/dscript/cppscript.html
*/
module dmdscript.parse;
import dmdscript.script;
import dmdscript.lexer;
import dmdscript.functiondefinition;
import dmdscript.expression;
import dmdscript.statement;
import dmdscript.identifier;
import dmdscript.ir;
import dmdscript.errmsgs;
class Parser : Lexer
{
uint flags;
enum
{
normal = 0,
initial = 1,
allowIn = 0,
noIn = 2,
// Flag if we're in the for statement header, as
// automatic semicolon insertion is suppressed inside it.
inForHeader = 4,
}
FunctionDefinition lastnamedfunc;
this(d_string sourcename, d_string base, int useStringtable)
{
//writefln("Parser.this(base = '%s')", base);
super(sourcename, base, useStringtable);
nextToken(); // start up the scanner
}
~this()
{
lastnamedfunc = null;
}
/**********************************************
* Return !=0 on error, and fill in *perrinfo.
*/
static int parseFunctionDefinition(out FunctionDefinition pfd,
immutable(char)[] params, immutable(char)[] bdy, out ErrInfo perrinfo)
{
Parser p;
Identifier*[] parameters;
TopStatement[] topstatements;
FunctionDefinition fd = null;
int result;
p = new Parser("anonymous", params, 0);
// Parse FormalParameterList
while(p.token.value != TOKeof)
{
if(p.token.value != TOKidentifier)
{
p.error(errmsgtbl[ERR_FPL_EXPECTED_IDENTIFIER], p.token.toString());
goto Lreturn;
}
parameters ~= p.token.ident;
p.nextToken();
if(p.token.value == TOKcomma)
p.nextToken();
else if(p.token.value == TOKeof)
break;
else
{
p.error(errmsgtbl[ERR_FPL_EXPECTED_COMMA], p.token.toString());
goto Lreturn;
}
}
if(p.errinfo.message)
goto Lreturn;
destroy(p);
// Parse StatementList
p = new Parser("anonymous", bdy, 0);
for(;; )
{
TopStatement ts;
if(p.token.value == TOKeof)
break;
ts = p.parseStatement();
topstatements ~= ts;
}
fd = new FunctionDefinition(0, 0, null, parameters, topstatements);
Lreturn:
pfd = fd;
perrinfo = p.errinfo;
result = (p.errinfo.message != null);
destroy(p);
p = null;
return result;
}
/**********************************************
* Return !=0 on error, and fill in *perrinfo.
*/
int parseProgram(out TopStatement[] topstatements, ErrInfo *perrinfo)
{
topstatements = parseTopStatements();
check(TOKeof);
//writef("parseProgram done\n");
*perrinfo = errinfo;
//clearstack();
return errinfo.message != null;
}
TopStatement[] parseTopStatements()
{
TopStatement[] topstatements;
TopStatement ts;
//writefln("parseTopStatements()");
for(;; )
{
switch(token.value)
{
case TOKfunction:
ts = parseFunction(0);
topstatements ~= ts;
break;
case TOKeof:
return topstatements;
case TOKrbrace:
return topstatements;
default:
ts = parseStatement();
topstatements ~= ts;
break;
}
}
assert(0);
}
/***************************
* flag:
* 0 Function statement
* 1 Function literal
*/
TopStatement parseFunction(int flag)
{
Identifier* name;
Identifier*[] parameters;
TopStatement[] topstatements;
FunctionDefinition f;
Expression e = null;
Loc loc;
//writef("parseFunction()\n");
loc = currentline;
nextToken();
name = null;
if(token.value == TOKidentifier)
{
name = token.ident;
nextToken();
if(!flag && token.value == TOKdot)
{
// Regard:
// function A.B() { }
// as:
// A.B = function() { }
// This is not ECMA, but a jscript feature
e = new IdentifierExpression(loc, name);
name = null;
while(token.value == TOKdot)
{
nextToken();
if(token.value == TOKidentifier)
{
e = new DotExp(loc, e, token.ident);
nextToken();
}
else
{
error(errmsgtbl[ERR_EXPECTED_IDENTIFIER_2PARAM], ".", token.toString());
break;
}
}
}
}
check(TOKlparen);
if(token.value == TOKrparen)
nextToken();
else
{
for(;; )
{
if(token.value == TOKidentifier)
{
parameters ~= token.ident;
nextToken();
if(token.value == TOKcomma)
{
nextToken();
continue;
}
if(!check(TOKrparen))
break;
}
else
error(ERR_EXPECTED_IDENTIFIER);
break;
}
}
check(TOKlbrace);
topstatements = parseTopStatements();
check(TOKrbrace);
f = new FunctionDefinition(loc, 0, name, parameters, topstatements);
f.isliteral = flag;
lastnamedfunc = f;
//writef("parseFunction() done\n");
if(!e)
return f;
// Construct:
// A.B = function() { }
Expression e2 = new FunctionLiteral(loc, f);
e = new AssignExp(loc, e, e2);
Statement s = new ExpStatement(loc, e);
return s;
}
/*****************************************
*/
Statement parseStatement()
{
Statement s;
Token *t;
Loc loc;
//writefln("parseStatement()");
loc = currentline;
switch(token.value)
{
case TOKidentifier:
case TOKthis:
// Need to look ahead to see if it is a declaration, label, or expression
t = peek(&token);
if(t.value == TOKcolon && token.value == TOKidentifier)
{ // It's a label
Identifier *ident;
ident = token.ident;
nextToken();
nextToken();
s = parseStatement();
s = new LabelStatement(loc, ident, s);
}
else if(t.value == TOKassign ||
t.value == TOKdot ||
t.value == TOKlbracket)
{
Expression exp;
exp = parseExpression();
parseOptionalSemi();
s = new ExpStatement(loc, exp);
}
else
{
Expression exp;
exp = parseExpression(initial);
parseOptionalSemi();
s = new ExpStatement(loc, exp);
}
break;
case TOKreal:
case TOKstring:
case TOKdelete:
case TOKlparen:
case TOKplusplus:
case TOKminusminus:
case TOKplus:
case TOKminus:
case TOKnot:
case TOKtilde:
case TOKtypeof:
case TOKnull:
case TOKnew:
case TOKtrue:
case TOKfalse:
case TOKvoid:
{ Expression exp;
exp = parseExpression(initial);
parseOptionalSemi();
s = new ExpStatement(loc, exp);
break; }
case TOKvar:
{
Identifier *ident;
Expression init;
VarDeclaration v;
VarStatement vs;
vs = new VarStatement(loc);
s = vs;
nextToken();
for(;; )
{
loc = currentline;
if(token.value != TOKidentifier)
{
error(errmsgtbl[ERR_EXPECTED_IDENTIFIER_PARAM], token.toString());
break;
}
ident = token.ident;
init = null;
nextToken();
if(token.value == TOKassign)
{
uint flags_save;
nextToken();
flags_save = flags;
flags &= ~initial;
init = parseAssignExp();
flags = flags_save;
}
v = new VarDeclaration(loc, ident, init);
vs.vardecls ~= v;
if(token.value != TOKcomma)
break;
nextToken();
}
if(!(flags & inForHeader))
parseOptionalSemi();
break;
}
case TOKlbrace:
{ BlockStatement bs;
nextToken();
bs = new BlockStatement(loc);
/*while(token.value != TOKrbrace)
{
if(token.value == TOKeof)
{
error(ERR_UNTERMINATED_BLOCK);
break;
}
bs.statements ~= parseStatement();
}*/
bs.statements ~= parseTopStatements();
s = bs;
nextToken();
// The following is to accommodate the jscript bug:
// if (i) {return(0);}; else ...
/*if(token.value == TOKsemicolon)
nextToken();*/
break; }
case TOKif:
{ Expression condition;
Statement ifbody;
Statement elsebody;
nextToken();
condition = parseParenExp();
ifbody = parseStatement();
if(token.value == TOKelse)
{
nextToken();
elsebody = parseStatement();
}
else
elsebody = null;
s = new IfStatement(loc, condition, ifbody, elsebody);
break; }
case TOKswitch:
{ Expression condition;
Statement bdy;
nextToken();
condition = parseParenExp();
bdy = parseStatement();
s = new SwitchStatement(loc, condition, bdy);
break; }
case TOKcase:
{ Expression exp;
nextToken();
exp = parseExpression();
check(TOKcolon);
s = new CaseStatement(loc, exp);
break; }
case TOKdefault:
nextToken();
check(TOKcolon);
s = new DefaultStatement(loc);
break;
case TOKwhile:
{ Expression condition;
Statement bdy;
nextToken();
condition = parseParenExp();
bdy = parseStatement();
s = new WhileStatement(loc, condition, bdy);
break; }
case TOKsemicolon:
nextToken();
s = new EmptyStatement(loc);
break;
case TOKdo:
{ Statement bdy;
Expression condition;
nextToken();
bdy = parseStatement();
check(TOKwhile);
condition = parseParenExp();
//We do what most browsers now do, ie allow missing ';'
//like " do{ statement; }while(e) statement; " and that even w/o linebreak
if(token.value == TOKsemicolon)
nextToken();
//parseOptionalSemi();
s = new DoStatement(loc, bdy, condition);
break; }
case TOKfor:
{
Statement init;
Statement bdy;
nextToken();
flags |= inForHeader;
check(TOKlparen);
if(token.value == TOKvar)
{
init = parseStatement();
}
else
{
Expression e;
e = parseOptionalExpression(noIn);
init = e ? new ExpStatement(loc, e) : null;
}
if(token.value == TOKsemicolon)
{
Expression condition;
Expression increment;
nextToken();
condition = parseOptionalExpression();
check(TOKsemicolon);
increment = parseOptionalExpression();
check(TOKrparen);
flags &= ~inForHeader;
bdy = parseStatement();
s = new ForStatement(loc, init, condition, increment, bdy);
}
else if(token.value == TOKin)
{
Expression inexp;
VarStatement vs;
// Check that there's only one VarDeclaration
// in init.
if(init.st == VARSTATEMENT)
{
vs = cast(VarStatement)init;
if(vs.vardecls.length != 1)
error(errmsgtbl[ERR_TOO_MANY_IN_VARS], vs.vardecls.length);
}
nextToken();
inexp = parseExpression();
check(TOKrparen);
flags &= ~inForHeader;
bdy = parseStatement();
s = new ForInStatement(loc, init, inexp, bdy);
}
else
{
error(errmsgtbl[ERR_IN_EXPECTED], token.toString());
s = null;
}
break;
}
case TOKwith:
{ Expression exp;
Statement bdy;
nextToken();
exp = parseParenExp();
bdy = parseStatement();
s = new WithStatement(loc, exp, bdy);
break; }
case TOKbreak:
{ Identifier *ident;
nextToken();
if(token.sawLineTerminator && token.value != TOKsemicolon)
{ // Assume we saw a semicolon
ident = null;
}
else
{
if(token.value == TOKidentifier)
{
ident = token.ident;
nextToken();
}
else
ident = null;
parseOptionalSemi();
}
s = new BreakStatement(loc, ident);
break; }
case TOKcontinue:
{ Identifier *ident;
nextToken();
if(token.sawLineTerminator && token.value != TOKsemicolon)
{ // Assume we saw a semicolon
ident = null;
}
else
{
if(token.value == TOKidentifier)
{
ident = token.ident;
nextToken();
}
else
ident = null;
parseOptionalSemi();
}
s = new ContinueStatement(loc, ident);
break; }
case TOKgoto:
{ Identifier *ident;
nextToken();
if(token.value != TOKidentifier)
{
error(errmsgtbl[ERR_GOTO_LABEL_EXPECTED], token.toString());
s = null;
break;
}
ident = token.ident;
nextToken();
parseOptionalSemi();
s = new GotoStatement(loc, ident);
break; }
case TOKreturn:
{ Expression exp;
nextToken();
if(token.sawLineTerminator && token.value != TOKsemicolon)
{ // Assume we saw a semicolon
s = new ReturnStatement(loc, null);
}
else
{
exp = parseOptionalExpression();
parseOptionalSemi();
s = new ReturnStatement(loc, exp);
}
break; }
case TOKthrow:
{ Expression exp;
nextToken();
exp = parseExpression();
parseOptionalSemi();
s = new ThrowStatement(loc, exp);
break; }
case TOKtry:
{ Statement bdy;
Identifier *catchident;
Statement catchbody;
Statement finalbody;
nextToken();
bdy = parseStatement();
if(token.value == TOKcatch)
{
nextToken();
check(TOKlparen);
catchident = null;
if(token.value == TOKidentifier)
catchident = token.ident;
check(TOKidentifier);
check(TOKrparen);
catchbody = parseStatement();
}
else
{
catchident = null;
catchbody = null;
}
if(token.value == TOKfinally)
{
nextToken();
finalbody = parseStatement();
}
else
finalbody = null;
if(!catchbody && !finalbody)
{
error(ERR_TRY_CATCH_EXPECTED);
s = null;
}
else
{
s = new TryStatement(loc, bdy, catchident, catchbody, finalbody);
}
break; }
default:
error(errmsgtbl[ERR_STATEMENT_EXPECTED], token.toString());
nextToken();
s = null;
break;
}
//writefln("parseStatement() done");
return s;
}
Expression parseOptionalExpression(uint flags = 0)
{
Expression e;
if(token.value == TOKsemicolon || token.value == TOKrparen)
e = null;
else
e = parseExpression(flags);
return e;
}
// Follow ECMA 7.8.1 rules for inserting semicolons
void parseOptionalSemi()
{
if(token.value != TOKeof &&
token.value != TOKrbrace &&
!(token.sawLineTerminator && (flags & inForHeader) == 0)
)
check(TOKsemicolon);
}
int check(TOK value)
{
if(token.value != value)
{
error(errmsgtbl[ERR_EXPECTED_GENERIC], token.toString(), Token.toString(value));
return 0;
}
nextToken();
return 1;
}
/********************************* Expression Parser ***************************/
Expression parseParenExp()
{
Expression e;
check(TOKlparen);
e = parseExpression();
check(TOKrparen);
return e;
}
Expression parsePrimaryExp(int innew)
{
Expression e;
Loc loc;
loc = currentline;
switch(token.value)
{
case TOKthis:
e = new ThisExpression(loc);
nextToken();
break;
case TOKnull:
e = new NullExpression(loc);
nextToken();
break;
case TOKtrue:
e = new BooleanExpression(loc, 1);
nextToken();
break;
case TOKfalse:
e = new BooleanExpression(loc, 0);
nextToken();
break;
case TOKreal:
e = new RealExpression(loc, token.realvalue);
nextToken();
break;
case TOKstring:
e = new StringExpression(loc, token.string);
token.string = null; // release to gc
nextToken();
break;
case TOKregexp:
e = new RegExpLiteral(loc, token.string);
token.string = null; // release to gc
nextToken();
break;
case TOKidentifier:
e = new IdentifierExpression(loc, token.ident);
token.ident = null; // release to gc
nextToken();
break;
case TOKlparen:
e = parseParenExp();
break;
case TOKlbracket:
e = parseArrayLiteral();
break;
case TOKlbrace:
/*if(flags & initial)
{
error(ERR_OBJ_LITERAL_IN_INITIALIZER);
nextToken();
return null;
}*/
e = parseObjectLiteral();
break;
case TOKfunction:
// if (flags & initial)
// goto Lerror;
e = parseFunctionLiteral();
break;
case TOKnew:
{ Expression newarg;
Expression[] arguments;
nextToken();
newarg = parsePrimaryExp(1);
arguments = parseArguments();
e = new NewExp(loc, newarg, arguments);
break; }
default:
// Lerror:
error(errmsgtbl[ERR_EXPECTED_EXPRESSION], token.toString());
nextToken();
return null;
}
return parsePostExp(e, innew);
}
Expression[] parseArguments()
{
Expression[] arguments = null;
if(token.value == TOKlparen)
{
nextToken();
if(token.value != TOKrparen)
{
for(;; )
{
Expression arg;
arg = parseAssignExp();
arguments ~= arg;
if(token.value == TOKrparen)
break;
if(!check(TOKcomma))
break;
}
}
nextToken();
}
return arguments;
}
Expression parseArrayLiteral()
{
Expression e;
Expression[] elements;
Loc loc;
//writef("parseArrayLiteral()\n");
loc = currentline;
check(TOKlbracket);
if(token.value != TOKrbracket)
{
for(;; )
{
if(token.value == TOKcomma)
// Allow things like [1,2,,,3,]
// Like Explorer 4, and unlike Netscape, the
// trailing , indicates another null element.
//Netscape was right - FIXED
elements ~= cast(Expression)null;
else if(token.value == TOKrbracket)
{
//elements ~= cast(Expression)null;
break;
}
else
{
e = parseAssignExp();
elements ~= e;
if(token.value != TOKcomma)
break;
}
nextToken();
}
}
check(TOKrbracket);
e = new ArrayLiteral(loc, elements);
return e;
}
Expression parseObjectLiteral()
{
Expression e;
Field[] fields;
Loc loc;
//writef("parseObjectLiteral()\n");
loc = currentline;
check(TOKlbrace);
if(token.value == TOKrbrace)
nextToken();
else
{
for(;; )
{
Field f;
Identifier* ident;
switch(token.value){
case TOKidentifier:
ident = token.ident;
break;
case TOKstring,TOKnumber,TOKreal:
ident = Identifier.build(token.toString());
break;
default:
error(ERR_EXPECTED_IDENTIFIER);
break;
}
nextToken();
check(TOKcolon);
f = new Field(ident, parseAssignExp());
fields ~= f;
if(token.value != TOKcomma)
break;
nextToken();
if(token.value == TOKrbrace)//allow trailing comma
break;
}
check(TOKrbrace);
}
e = new ObjectLiteral(loc, fields);
return e;
}
Expression parseFunctionLiteral()
{
FunctionDefinition f;
Loc loc;
loc = currentline;
f = cast(FunctionDefinition)parseFunction(1);
return new FunctionLiteral(loc, f);
}
Expression parsePostExp(Expression e, int innew)
{
Loc loc;
for(;; )
{
loc = currentline;
//loc = (Loc)token.ptr;
switch(token.value)
{
case TOKdot:
nextToken();
if(token.value == TOKidentifier)
{
e = new DotExp(loc, e, token.ident);
}
else
{
error(errmsgtbl[ERR_EXPECTED_IDENTIFIER_2PARAM], ".", token.toString());
return e;
}
break;
case TOKplusplus:
if(token.sawLineTerminator && !(flags & inForHeader))
goto Linsert;
e = new PostIncExp(loc, e);
break;
case TOKminusminus:
if(token.sawLineTerminator && !(flags & inForHeader))
{
Linsert:
// insert automatic semicolon
insertSemicolon(token.sawLineTerminator);
return e;
}
e = new PostDecExp(loc, e);
break;
case TOKlparen:
{ // function call
Expression[] arguments;