forked from gfwilliams/tiny-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsParser.cpp
1514 lines (1239 loc) · 35.4 KB
/
jsParser.cpp
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
/*
* File: jsParser.cpp
* Author: ghernan
*
* Created on November 28, 2016, 10:36 PM
*/
#include "ascript_pch.hpp"
#include "jsParser.h"
#include "utils.h"
#include "jsVars.h"
#include "ScriptException.h"
using namespace std;
Ref<AstNode> emptyStatement(ScriptPosition pos);
ParseResult parseSimpleStatement (CScriptToken token);
ExprResult parseBodyStatement (CScriptToken token);
ExprResult parseBlock (CScriptToken token);
ExprResult parseVar (CScriptToken token);
ParseResult parseIf (CScriptToken token);
ParseResult parseWhile (CScriptToken token);
ParseResult parseFor (CScriptToken token);
ExprResult parseForEach (CScriptToken token);
ParseResult parseReturn (CScriptToken token);
ExprResult parseArgumentList(CScriptToken token, Ref<AstNode> function);
ExprResult parseExpression (CScriptToken token);
ExprResult parseAssignment (CScriptToken token);
ExprResult parseLeftExpr (CScriptToken token);
ExprResult parseConditional (CScriptToken token);
ExprResult parseLogicalOrExpr (CScriptToken token);
ExprResult parseLogicalAndExpr (CScriptToken token);
ExprResult parseBitwiseOrExpr (CScriptToken token);
ExprResult parseBitwiseXorExpr (CScriptToken token);
ExprResult parseBitwiseAndExpr (CScriptToken token);
ExprResult parseEqualityExpr (CScriptToken token);
ExprResult parseRelationalExpr (CScriptToken token);
ExprResult parseShiftExpr (CScriptToken token);
ExprResult parseAddExpr (CScriptToken token);
ExprResult parseMultiplyExpr (CScriptToken token);
ExprResult parsePowerExpr (CScriptToken token);
ExprResult parseUnaryExpr (CScriptToken token);
ExprResult parsePostFixExpr (CScriptToken token);
ExprResult parseIdentifier (CScriptToken token);
//ExprResult parseNewExpr (CScriptToken token);
ExprResult parseCallExpr (CScriptToken token);
ExprResult parseMemberExpr (CScriptToken token);
ExprResult parsePrimaryExpr (CScriptToken token);
ExprResult parseFunctionExpr (CScriptToken token);
ExprResult parseArrayLiteral (CScriptToken token);
ExprResult parseObjectLiteral (CScriptToken token);
ExprResult parseCallArguments (CScriptToken token, Ref<AstNode> fnExpr);
ExprResult parseArrayAccess (CScriptToken token, Ref<AstNode> arrayExpr);
ExprResult parseMemberAccess (CScriptToken token, Ref<AstNode> objExpr);
ExprResult parseObjectProperty (CScriptToken token, Ref<AstNode> objExpr);
ExprResult parseBinaryLROp (CScriptToken token, LEX_TYPES opType, ExprResult::ParseFunction childParser);
ExprResult parseBinaryLROp (CScriptToken token, const int *types, ExprResult::ParseFunction childParser);
ExprResult parseBinaryRLOp (CScriptToken token, LEX_TYPES opType, ExprResult::ParseFunction childParser);
ExprResult parseBinaryRLOp (CScriptToken token, const int *types, ExprResult::ParseFunction childParser);
ExprResult parseActorExpr (CScriptToken token);
ExprResult parseActorMember (CScriptToken token);
ExprResult parseInputMessage (CScriptToken token);
ExprResult parseOutputMessage (CScriptToken token);
ExprResult parseConnectExpr (CScriptToken token);
ExprResult parseClassExpr (CScriptToken token);
ExprResult parseExtends (CScriptToken token);
ExprResult parseClassMember (CScriptToken token);
ExprResult parseExport (CScriptToken token);
ExprResult parseImport (CScriptToken token);
bool isAssignment(CScriptToken token)
{
const LEX_TYPES t = token.type();
return t == '=' || (t > LEX_ASSIGN_BASE && t < LEX_ASSIGN_MAX);
}
bool oneOf (CScriptToken token, const char* chars)
{
const LEX_TYPES t = token.type();
if ( t >= LEX_ASSIGN_BASE )
return false;
for (; *chars != 0; ++chars)
if (*chars == (char)t)
return true;
return false;
}
bool oneOf (CScriptToken token, const int* ids)
{
const LEX_TYPES t = token.type();
for (; *ids != 0; ++ids)
if (*ids == t)
return true;
return false;
}
/**
* Crates an empty statement
* @return
*/
Ref<AstNode> emptyStatement(ScriptPosition pos)
{
return AstLiteral::createNull(pos);
}
/**
* Parses an script, which is a list of statements
* @param token
* @return
*/
ParseResult parseScript(CScriptToken token)
{
auto script = astCreateScript(token.getPosition());
while (!token.eof())
{
const ParseResult parseRes = parseStatement (token);
script->addChild(parseRes.ast);
token = parseRes.nextToken;
}
return ParseResult(token, script);
}
/**
* Parses one statement
* @param token
* @return 'ParseResult' structure, with the generated AST tree, and a pointer to
* the next token.
*/
ParseResult parseStatement (CScriptToken token)
{
switch ((int)token.type())
{
case '{':
{
ExprResult r = parseObjectLiteral(token);
if (r.ok())
return r.toParseResult();
else
return parseBlock(token).toParseResult();
}
case ';': return ParseResult (token.next(), emptyStatement(token.getPosition()));
case LEX_R_VAR:
case LEX_R_CONST:
return parseVar (token).toParseResult();
case LEX_R_IF: return parseIf (token);
case LEX_R_WHILE: return parseWhile (token);
case LEX_R_FOR: return parseFor (token);
case LEX_R_RETURN: return parseReturn (token);
case LEX_R_FUNCTION:return parseFunctionExpr (token).toParseResult();
case LEX_R_ACTOR: return parseActorExpr (token).toParseResult();
case LEX_R_CLASS: return parseClassExpr (token).toParseResult();
case LEX_R_EXPORT: return parseExport (token).toParseResult();
case LEX_R_IMPORT: return parseImport (token).toParseResult();
default:
return parseSimpleStatement(token);
}//switch
}
/**
* Parses a simple statement (no blocks, no reserved words)
* @param token
* @return
*/
ParseResult parseSimpleStatement (CScriptToken token)
{
switch ((int)token.type())
{
case LEX_ID:
case LEX_INT:
case LEX_FLOAT:
case LEX_STR:
case '-':
case '+':
case '!':
case '~':
case LEX_PLUSPLUS:
case LEX_MINUSMINUS:
{
ExprResult r = parseExpression (token);
return r.toParseResult();
}
case LEX_R_VAR:
case LEX_R_CONST:
return parseVar (token).toParseResult();
default:
errorAt(token.getPosition(), "Unexpected token: '%s'", token.text().c_str());
return ParseResult (token.next(), emptyStatement(token.getPosition()));
}//switch
}
/**
* Parses an statement which is part of the body of an 'if' or loop
* @param token
* @return
*/
ExprResult parseBodyStatement (CScriptToken token)
{
ExprResult r(token);
if (token.type() == ';')
{
r = r.skip();
r.result = emptyStatement(token.getPosition());
}
else
{
ParseResult result = parseStatement(token);
switch ((int)token.type())
{
case '{':
case LEX_R_IF:
case LEX_R_WHILE:
case LEX_R_FOR:
case LEX_R_FUNCTION:
break;
default:
//with all other statements, it may follow a semicolon.
if (result.nextToken.type() == ';')
result.nextToken = result.nextToken.next();
}
r.token = result.nextToken;
r.result = result.ast;
}//else.
return r.final();
}
/**
* Parses a code block
* @param token
* @return Code block object
*/
ExprResult parseBlock (CScriptToken token)
{
//TODO: Better handling of ';'
Ref<AstNode> block = astCreateBlock(token);
ExprResult r(token);
r = r.require('{');
while (r.ok() && r.token.type() != '}')
{
//TODO: Migrate all parsing functions to 'ExprResult'
ParseResult pr = parseStatement(r.token);
block->addChild (pr.ast);
r.token = pr.nextToken;
}
r = r.require('}');
if (r.ok())
r.result = block;
return r.final();
}
/**
* Parses a 'var' declaration
* @param token
* @return
*/
ExprResult parseVar (CScriptToken token)
{
ScriptPosition pos = token.getPosition();
ExprResult r(token);
bool isConst = false;
if (r.token.type() == LEX_R_CONST)
{
isConst = true;
r = r.skip();
}
else
r = r.require(LEX_R_VAR);
if (!r.ok())
return r.final();
const string name = r.token.text();
r = r.require(LEX_ID);
Ref<AstNode> initExp;
if (r.ok() && r.token.type() == '=')
{
//Initialization is optional.
r = r.skip().then(parseExpression);
initExp = r.result;
}
if (r.ok())
r.result = astCreateVar (pos, name, initExp, isConst);
return r;
}
/**
* Parses an 'if' statement
* @param token
* @return
*/
ParseResult parseIf (CScriptToken token)
{
ScriptPosition pos = token.getPosition();
token = token.match(LEX_R_IF);
token = token.match('(');
ExprResult rCondition = parseExpression(token);
rCondition.throwIfError();
token = rCondition.token;
ParseResult r = parseBodyStatement(token.match(')')).toParseResult();
Ref<AstNode> thenSt = r.ast;
Ref<AstNode> elseSt;
token = r.nextToken;
if (token.type() == LEX_R_ELSE)
{
r = parseBodyStatement(token.next()).toParseResult();
elseSt = r.ast;
token = r.nextToken;
}
auto result = astCreateIf (pos, rCondition.result, thenSt, elseSt);
return ParseResult (token, result);
}
/**
* Parses a 'while' statement
* @param token
* @return
*/
ParseResult parseWhile (CScriptToken token)
{
ScriptPosition pos = token.getPosition();
token = token.match(LEX_R_WHILE);
token = token.match('(');
ExprResult rCondition = parseExpression(token);
rCondition.throwIfError();
token = rCondition.token;
ParseResult r = parseBodyStatement(token.match(')')).toParseResult();
auto result = astCreateFor (pos,
Ref<AstNode>(),
rCondition.result,
Ref<AstNode>(),
r.ast);
return ParseResult (r.nextToken, result);
}
/**
* Parses a 'for' statement
* @param token
* @return
*/
ParseResult parseFor (CScriptToken token)
{
ExprResult res = parseForEach(token);
if (res.ok())
return res.toParseResult();
//TODO: Better handling of ';'
ScriptPosition pos = token.getPosition();
Ref<AstNode> init;
Ref<AstNode> increment;
Ref<AstNode> body;
Ref<AstNode> condition;
token = token.match(LEX_R_FOR);
token = token.match('(');
ParseResult r;
//Initialization
if (token.type() != ';')
{
r = parseSimpleStatement(token);
init = r.ast;
token = r.nextToken.match(';');
}
else
token = token.next();
ExprResult rCondition = parseExpression(token);
rCondition.throwIfError();
condition = rCondition.result;
token = rCondition.token.match(';');
if (token.type() != ')')
{
r = parseSimpleStatement(token);
increment = r.ast;
token = r.nextToken;
}
token = token.match(')');
r = parseBodyStatement(token).toParseResult();
body = r.ast;
token = r.nextToken;
auto result = astCreateFor (pos, init, condition, increment, body);
return ParseResult (token, result);
}
/**
* Parses a for loop which iterates over the elements of a sequence.
* 'for (x in z)...'
* @param token
* @return
*/
ExprResult parseForEach (CScriptToken token)
{
ExprResult r(token);
r = r.require(LEX_R_FOR).require('(');
if (r.error())
return r.final();
r = r.then(parseIdentifier).requireId("in");
if (r.error())
return r.final();
auto itemDecl = r.result;
r = r.then(parseExpression).require(')');
if (r.error())
return r.final();
auto seqExpression = r.result;
r = r.then (parseBodyStatement);
if (r.ok())
r.result = astCreateForEach (token.getPosition(), itemDecl, seqExpression, r.result);
return r.final();
}
/**
* Parses a return statement
* @param token
* @return
*/
ParseResult parseReturn (CScriptToken token)
{
ScriptPosition pos = token.getPosition();
Ref<AstNode> result;
token = token.match(LEX_R_RETURN);
if (token.type() == ';')
token = token.next();
else
{
ExprResult r = parseExpression(token);
r.throwIfError();
token = r.token;
result = r.result;
if (token.type() == ';')
token = token.next();
}
auto ret = astCreateReturn(pos, result);
return ParseResult(token, ret);
}
/**
* Parses a function argument list.
* @param token
* @param fnNode Function into which add the arguments
* @return ExprResult containing the same function AST node received, with
* the parameters added.
*/
ExprResult parseArgumentList(CScriptToken token, Ref<AstNode> fnNode)
{
ExprResult r(token);
r = r.require('(');
while (r.ok() && r.token.type() != ')')
{
const string name = r.token.text();
r = r.require (LEX_ID);
if (r.ok())
fnNode->addParam(name);
if (r.token.type() != ')')
r = r.require (',');
}
r = r.require(')');
if (r.ok())
r.result = fnNode;
return r.final();
}
/**
* Parses a Javascript expression
* @param token Startup token
* @param exception Controls if the function shall throw an exception on an error.
* @return
*/
ExprResult parseExpression (CScriptToken token)
{
//TODO: Support comma operator (¡qué pereza!)
return parseAssignment (token);
}
/**
* Parses an assignment expression.
* @param token
* @param exception
* @return
*/
ExprResult parseAssignment (CScriptToken token)
{
ExprResult r = parseLeftExpr(token);
const Ref<AstNode> lexpr = r.result;
const int op = r.token.type();
const ScriptPosition pos = r.token.getPosition();
r = r.require(isAssignment).then (parseAssignment);
if (r.ok())
{
auto result = astCreateAssignment(pos, op, lexpr, r.result);
r.result = result;
return r.final();
}
else
return parseConditional(token);
}
/**
* Parses an expression which can be at the left side of an assignment
* @param token
* @param exception
* @return
*/
ExprResult parseLeftExpr (CScriptToken token)
{
ExprResult r = parseCallExpr (token);
return r.orElse (parseMemberExpr).final();
}
/**
* Parses a conditional expression ('?' operator)
* @param token
* @return
*/
ExprResult parseConditional (CScriptToken token)
{
ExprResult r = parseLogicalOrExpr (token);
if (r.ok() && r.token.type() == '?')
{
const Ref<AstNode> condition =r.result;
r = r.skip();
r = r.then (parseAssignment).require(':');
const Ref<AstNode> thenExpr =r.result;
r = r.then (parseAssignment);
const Ref<AstNode> elseExpr =r.result;
if (r.ok())
r.result = astCreateConditional (token.getPosition(),
condition,
thenExpr,
elseExpr);
}
return r.final();
}
/**
* Parses a 'logical OR' expression ('||' operator)
* @param token
* @return
*/
ExprResult parseLogicalOrExpr (CScriptToken token)
{
return parseBinaryLROp(token, LEX_OROR, parseLogicalAndExpr);
}
/**
* Parses a 'logical AND' expression ('&&' operator)
* @param token
* @return
*/
ExprResult parseLogicalAndExpr (CScriptToken token)
{
return parseBinaryLROp(token, LEX_ANDAND, parseBitwiseOrExpr);
}
/**
* Parses a 'bitwise OR' expression ('|' operator)
* @param token
* @return
*/
ExprResult parseBitwiseOrExpr(CScriptToken token)
{
return parseBinaryLROp(token, LEX_TYPES('|'), parseBitwiseXorExpr);
}
/**
* Parses a 'bitwise XOR' expression ('^' operator)
* @param token
* @return
*/
ExprResult parseBitwiseXorExpr(CScriptToken token)
{
return parseBinaryLROp(token, LEX_TYPES('^'), parseBitwiseAndExpr);
}
/**
* Parses a 'bitwise XOR' expression ('&' operator)
* @param token
* @return
*/
ExprResult parseBitwiseAndExpr(CScriptToken token)
{
return parseBinaryLROp(token, LEX_TYPES('&'), parseEqualityExpr);
}
/**
* Parses a equality expression (see operators in code)
* @param token
* @return
*/
ExprResult parseEqualityExpr(CScriptToken token)
{
const int operators[] = {LEX_EQUAL, LEX_TYPEEQUAL, LEX_NEQUAL, LEX_NTYPEEQUAL, 0 };
return parseBinaryLROp(token, operators, parseRelationalExpr);
}
/**
* Parses a relational expression (<, >, <=, >=, instanceof, in) operators
* @param token
* @return
*/
ExprResult parseRelationalExpr(CScriptToken token)
{
//TODO: Missing 'instanceof' and 'in' operators
const int operators[] = {'<', '>', LEX_LEQUAL, LEX_GEQUAL, 0 };
return parseBinaryLROp(token, operators, parseShiftExpr);
}
/**
* Parses a bit shift expression.
* @param token
* @return
*/
ExprResult parseShiftExpr(CScriptToken token)
{
const int operators[] = {LEX_LSHIFT, LEX_RSHIFT, LEX_RSHIFTUNSIGNED, 0 };
return parseBinaryLROp(token, operators, parseAddExpr);
}
/**
* Parses an add or substract expression
* @param token
* @return
*/
ExprResult parseAddExpr (CScriptToken token)
{
const int operators[] = {'+', '-', 0 };
return parseBinaryLROp(token, operators, parseMultiplyExpr);
}
/**
* Parses a multiply, divide or modulus expression.
* @param token
* @return
*/
ExprResult parseMultiplyExpr (CScriptToken token)
{
const int operators[] = {'*', '/', '%', 0 };
return parseBinaryLROp(token, operators, parsePowerExpr);
}
/**
* Parses a exponenciation operation (operator '**')
* @param token
* @return
*/
ExprResult parsePowerExpr(CScriptToken token)
{
return parseBinaryRLOp(token, LEX_POWER, parseUnaryExpr);
}
/**
* Parses an unary expression
* @param token
* @return
*/
ExprResult parseUnaryExpr (CScriptToken token)
{
//TODO: Missing: delete, void & typeof
const int operators[] = {'+', '-', '~', '!', LEX_PLUSPLUS, LEX_MINUSMINUS , 0};
if (oneOf (token, operators))
{
ExprResult r = parseUnaryExpr(token.next());
if (r.ok())
{
r.result = astCreatePrefixOp(token, r.result);
return r.final();
}
else
return ExprResult(token, r.errorDesc);
}
else
return parsePostFixExpr(token);
}
/**
* Parses a postfix expression
* @param token
* @return
*/
ExprResult parsePostFixExpr (CScriptToken token)
{
const int operators[] = {LEX_PLUSPLUS, LEX_MINUSMINUS , 0};
ExprResult r = parseLeftExpr(token);
if (r.ok() && oneOf(r.token, operators))
{
r.result = astCreatePostfixOp(r.token, r.result);
r = r.skip();
}
return r.final();
}
/**
* Parses an identifier
* @param token
* @return
*/
ExprResult parseIdentifier (CScriptToken token)
{
ExprResult r = ExprResult(token).require(LEX_ID);
if (r.ok())
r.result = AstIdentifier::create(token);
return r.final();
}
/**
* Parses a function call expression
* @param token
* @return
*/
ExprResult parseCallExpr (CScriptToken token)
{
ExprResult r = ExprResult(token).then(parseMemberExpr).then(parseCallArguments);
while (r.ok() && oneOf (r.token, "([."))
{
const LEX_TYPES t = r.token.type();
if (t == '(')
r = r.then(parseCallArguments);
else if (t == '[')
r = r.then(parseArrayAccess);
else
{
ASSERT (t == '.');
r = r.then (parseMemberAccess);
}
}//while
return r.final();
}
/**
* Parses a member access expression
* @note The name is taken from the production of the standard Javascript grammar.
* But in my opinion, the production has too broad scope to be called just 'member access'
* @param token
* @return
*/
ExprResult parseMemberExpr (CScriptToken token)
{
ExprResult r = parsePrimaryExpr(token).orElse(parseFunctionExpr);
while (!r.error() && oneOf (r.token, "[."))
{
const LEX_TYPES t = r.token.type();
if (t == '[')
r = r.then(parseArrayAccess);
else
{
ASSERT (t == '.');
r = r.then (parseMemberAccess);
}
}//while
return r.final();
}
/**
* Parses a primary expression. A primary expression is an identifier name,
* a constant, an array literal, an object literal or an expression between parenthesis
* @param token
* @return
*/
ExprResult parsePrimaryExpr (CScriptToken token)
{
switch ((int)token.type())
{
case LEX_R_TRUE: //valueNode = AstLiteral::create(jsTrue()); break;
case LEX_R_FALSE: //valueNode = AstLiteral::create(jsFalse()); break;
case LEX_R_NULL:
case LEX_FLOAT:
case LEX_INT:
case LEX_STR: //valueNode = AstLiteral::create(createConstant(token)); break;
return ExprResult (token.next(), AstLiteral::create(token));
case LEX_ID: return parseIdentifier(token);
case '[': return parseArrayLiteral(token);
case '{': return parseObjectLiteral(token);
case '(':
return ExprResult(token)
.require('(')
.then(parseExpression)
.require(')').final();
default:
return ExprResult(token)
.getError("Unexpected token: '%s'", token.text().c_str());
}//switch
}
/**
* Parses a function expression
* @param token
* @return
*/
ExprResult parseFunctionExpr (CScriptToken token)
{
ScriptPosition pos = token.getPosition();
string name;
ExprResult r(token);
r = r.require(LEX_R_FUNCTION);
if (r.error())
return r.final();
//unnamed functions are legal.
if (r.token.type() == LEX_ID)
{
name = r.token.text();
r = r.skip();
}
Ref<AstFunction> function = AstFunction::create (pos, name);
r.result = function;
r = r.then(parseArgumentList).then(parseBlock);
if (r.ok())
{
function->setCode (r.result);
r.result = function;
}
return r.final();
}
/**
* Parses an array literal.
* @param token
* @return
*/
ExprResult parseArrayLiteral (CScriptToken token)
{
ExprResult r(token);
r = r.require('[');
auto array = astCreateArray(token.getPosition());
while (r.ok() && r.token.type() != ']')
{
//Skip empty entries
while (r.token.type() == ',')
{
array->addChild( AstLiteral::createNull(r.token.getPosition()));
r = r.require(',');
}
if (r.token.type() != ']')
{
r = r.then(parseAssignment);
if (r.ok())
{
array->addChild(r.result);
if (r.token.type() != ']')
r = r.require(',');
}
}
}//while
r = r.require(']');
if (r.ok())
r.result = array;
return r.final();
}
/**
* Parses an object literal (JSON)
* @param token
* @return
*/
ExprResult parseObjectLiteral (CScriptToken token)
{
ExprResult r(token);
r = r.require('{');
Ref<AstObject> object = AstObject::create(token.getPosition());
r.result = object;
while (r.ok() && r.token.type() != '}')
{
r = r.then(parseObjectProperty);
if (r.token.type() != '}')
r = r.require(',');
}//while
r = r.require('}');
return r.final();
}
/**
* Parses function call arguments.
* @note This function creates the function call AST node.
* @param token next token
* @param fnExpr Expression from which the function reference is obtained
* @return
*/
ExprResult parseCallArguments (CScriptToken token, Ref<AstNode> fnExpr)
{
ExprResult r(token);
r = r.require('(');
auto call = astCreateFnCall(token.getPosition(), fnExpr/*, false*/);
while (r.ok() && r.token.type() != ')')
{