-
Notifications
You must be signed in to change notification settings - Fork 153
/
ejparse.c
1808 lines (1535 loc) · 35.7 KB
/
ejparse.c
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
/*
* ejparse.c -- Ejscript(TM) Parser
*
* Copyright (c) GoAhead Software Inc., 1995-2010. All Rights Reserved.
*
* See the file "license.txt" for usage and redistribution license requirements
*
*/
/******************************** Description *********************************/
/*
* Ejscript parser. This implementes a subset of the JavaScript language.
* Multiple Ejscript parsers can be opened at a time.
*/
/********************************** Includes **********************************/
#include "ejIntrn.h"
#ifdef CE
#include "CE/wincompat.h"
#endif
/********************************** Local Data ********************************/
ej_t **ejHandles; /* List of ej handles */
int ejMax = -1; /* Maximum size of */
/****************************** Forward Declarations **************************/
#ifndef B_STATS
#define setString(a,b,c) setstring(b,c)
#endif
static ej_t *ejPtr(int eid);
static void clearString(char_t **ptr);
static void setString(B_ARGS_DEC, char_t **ptr, char_t *s);
static void appendString(char_t **ptr, char_t *s);
static int parse(ej_t *ep, int state, int flags);
static int parseStmt(ej_t *ep, int state, int flags);
static int parseDeclaration(ej_t *ep, int state, int flags);
static int parseArgs(ej_t *ep, int state, int flags);
static int parseCond(ej_t *ep, int state, int flags);
static int parseExpr(ej_t *ep, int state, int flags);
static int evalExpr(ej_t *ep, char_t *lhs, int rel, char_t *rhs);
static int evalCond(ej_t *ep, char_t *lhs, int rel, char_t *rhs);
static int evalFunction(ej_t *ep);
static void freeFunc(ejfunc_t *func);
static void ejRemoveNewlines(ej_t *ep, int state);
/************************************* Code ***********************************/
/*
* Initialize a Ejscript engine
*/
int ejOpenEngine(sym_fd_t variables, sym_fd_t functions)
{
ej_t *ep;
int eid, vid;
if ((eid = hAllocEntry((void***) &ejHandles, &ejMax, sizeof(ej_t))) < 0) {
return -1;
}
ep = ejHandles[eid];
ep->eid = eid;
/*
* Create a top level symbol table if one is not provided for variables and
* functions. Variables may create other symbol tables for block level
* declarations so we use hAlloc to manage a list of variable tables.
*/
if ((vid = hAlloc((void***) &ep->variables)) < 0) {
ejMax = hFree((void***) &ejHandles, ep->eid);
return -1;
}
if (vid >= ep->variableMax) {
ep->variableMax = vid + 1;
}
if (variables == -1) {
ep->variables[vid] = symOpen(64) + EJ_OFFSET;
ep->flags |= FLAGS_VARIABLES;
} else {
ep->variables[vid] = variables + EJ_OFFSET;
}
if (functions == -1) {
ep->functions = symOpen(64);
ep->flags |= FLAGS_FUNCTIONS;
} else {
ep->functions = functions;
}
ejLexOpen(ep);
/*
* Define standard constants
*/
ejSetGlobalVar(ep->eid, T("null"), NULL);
return ep->eid;
}
/******************************************************************************/
/*
* Close
*/
void ejCloseEngine(int eid)
{
ej_t *ep;
int i;
if ((ep = ejPtr(eid)) == NULL) {
return;
}
bfreeSafe(B_L, ep->error);
ep->error = NULL;
bfreeSafe(B_L, ep->result);
ep->result = NULL;
ejLexClose(ep);
for (i = ep->variableMax - 1; i >= 0; i--) {
if (ep->flags & FLAGS_VARIABLES) {
symClose(ep->variables[i] - EJ_OFFSET);
}
ep->variableMax = hFree((void***) &ep->variables, i);
}
if (ep->flags & FLAGS_FUNCTIONS) {
symClose(ep->functions);
}
ejMax = hFree((void***) &ejHandles, ep->eid);
bfree(B_L, ep);
}
#ifndef __NO_EJ_FILE
/******************************************************************************/
/*
* Evaluate a Ejscript file
*/
char_t *ejEvalFile(int eid, char_t *path, char_t **emsg)
{
gstat_t sbuf;
ej_t *ep;
char_t *script, *rs;
char *fileBuf;
int fd;
a_assert(path && *path);
if (emsg) {
*emsg = NULL;
}
if ((ep = ejPtr(eid)) == NULL) {
return NULL;
}
#if !defined(WIN32)
fd = gopen(path, O_RDONLY | O_BINARY, 0666);
#else
_sopen_s(&fd, path, O_RDONLY | O_BINARY, _SH_DENYNO, 0666);
#endif
if (fd < 0) {
ejError(ep, T("Bad handle %d"), eid);
return NULL;
}
if (gstat(path, &sbuf) < 0) {
gclose(fd);
ejError(ep, T("Cant stat %s"), path);
return NULL;
}
if ((fileBuf = balloc(B_L, sbuf.st_size + 1)) == NULL) {
gclose(fd);
ejError(ep, T("Cant malloc %d"), sbuf.st_size);
return NULL;
}
if (gread(fd, fileBuf, sbuf.st_size) != (int)sbuf.st_size) {
gclose(fd);
bfree(B_L, fileBuf);
ejError(ep, T("Error reading %s"), path);
return NULL;
}
fileBuf[sbuf.st_size] = '\0';
gclose(fd);
if ((script = ballocAscToUni(fileBuf, sbuf.st_size)) == NULL) {
bfree(B_L, fileBuf);
ejError(ep, T("Cant malloc %d"), sbuf.st_size + 1);
return NULL;
}
bfree(B_L, fileBuf);
rs = ejEvalBlock(eid, script, emsg);
bfree(B_L, script);
return rs;
}
#endif /* __NO_EJ_FILE */
/******************************************************************************/
/*
* Create a new variable scope block so that consecutive ejEval calls may
* be made with the same varible scope. This space MUST be closed with
* ejCloseBlock when the evaluations are complete.
*/
int ejOpenBlock(int eid)
{
ej_t *ep;
int vid;
if((ep = ejPtr(eid)) == NULL) {
return -1;
}
if ((vid = hAlloc((void***) &ep->variables)) < 0) {
return -1;
}
if (vid >= ep->variableMax) {
ep->variableMax = vid + 1;
}
ep->variables[vid] = symOpen(64) + EJ_OFFSET;
return vid;
}
/******************************************************************************/
/*
* Close a variable scope block. The vid parameter is the return value from
* the call to ejOpenBlock
*/
int ejCloseBlock(int eid, int vid)
{
ej_t *ep;
if((ep = ejPtr(eid)) == NULL) {
return -1;
}
symClose(ep->variables[vid] - EJ_OFFSET);
ep->variableMax = hFree((void***) &ep->variables, vid);
return 0;
}
/******************************************************************************/
/*
* Create a new variable scope block and evaluate a script. All variables
* created during this context will be automatically deleted when complete.
*/
char_t *ejEvalBlock(int eid, char_t *script, char_t **emsg)
{
char_t* returnVal;
int vid;
a_assert(script);
vid = ejOpenBlock(eid);
returnVal = ejEval(eid, script, emsg);
ejCloseBlock(eid, vid);
return returnVal;
}
/******************************************************************************/
/*
* Parse and evaluate a Ejscript. The caller may provide a symbol table to
* use for variables and function definitions. Return char_t pointer on
* success otherwise NULL pointer is returned.
*/
char_t *ejEval(int eid, char_t *script, char_t **emsg)
{
ej_t *ep;
ejinput_t *oldBlock;
int state;
void *endlessLoopTest;
int loopCounter;
a_assert(script);
if (emsg) {
*emsg = NULL;
}
if ((ep = ejPtr(eid)) == NULL) {
return NULL;
}
setString(B_L, &ep->result, T(""));
/*
* Allocate a new evaluation block, and save the old one
*/
oldBlock = ep->input;
ejLexOpenScript(ep, script);
/*
* Do the actual parsing and evaluation
*/
loopCounter = 0;
endlessLoopTest = NULL;
do {
state = parse(ep, STATE_BEGIN, FLAGS_EXE);
if (state == STATE_RET) {
state = STATE_EOF;
}
/*
* prevent parser from going into infinite loop. If parsing the same
* line 10 times then fail and report Syntax error. Most normal error
* are caught in the parser itself.
*/
if (endlessLoopTest == ep->input->script.servp) {
if (loopCounter++ > 10) {
state = STATE_ERR;
ejError(ep, T("Syntax error"));
}
} else {
endlessLoopTest = ep->input->script.servp;
loopCounter = 0;
}
} while (state != STATE_EOF && state != STATE_ERR);
ejLexCloseScript(ep);
/*
* Return any error string to the user
*/
if (state == STATE_ERR && emsg) {
*emsg = bstrdup(B_L, ep->error);
}
/*
* Restore the old evaluation block
*/
ep->input = oldBlock;
if (state == STATE_EOF) {
return ep->result;
}
if (state == STATE_ERR) {
return NULL;
}
return ep->result;
}
/******************************************************************************/
/*
* Recursive descent parser for Ejscript
*/
static int parse(ej_t *ep, int state, int flags)
{
a_assert(ep);
switch (state) {
/*
* Any statement, function arguments or conditional expressions
*/
case STATE_STMT:
if ((state = parseStmt(ep, state, flags)) != STATE_STMT_DONE &&
state != STATE_EOF && state != STATE_STMT_BLOCK_DONE &&
state != STATE_RET) {
state = STATE_ERR;
}
break;
case STATE_DEC:
if ((state = parseStmt(ep, state, flags)) != STATE_DEC_DONE &&
state != STATE_EOF) {
state = STATE_ERR;
}
break;
case STATE_EXPR:
if ((state = parseStmt(ep, state, flags)) != STATE_EXPR_DONE &&
state != STATE_EOF) {
state = STATE_ERR;
}
break;
/*
* Variable declaration list
*/
case STATE_DEC_LIST:
state = parseDeclaration(ep, state, flags);
break;
/*
* Function argument string
*/
case STATE_ARG_LIST:
state = parseArgs(ep, state, flags);
break;
/*
* Logical condition list (relational operations separated by &&, ||)
*/
case STATE_COND:
state = parseCond(ep, state, flags);
break;
/*
* Expression list
*/
case STATE_RELEXP:
state = parseExpr(ep, state, flags);
break;
}
if (state == STATE_ERR && ep->error == NULL) {
ejError(ep, T("Syntax error"));
}
return state;
}
/******************************************************************************/
/*
* Parse any statement including functions and simple relational operations
*/
static int parseStmt(ej_t *ep, int state, int flags)
{
ejfunc_t func;
ejfunc_t *saveFunc;
ejinput_t condScript, endScript, bodyScript, incrScript;
char_t *value, *identifier;
int done, expectSemi, thenFlags, elseFlags, tid, cond, forFlags;
int ejVarType;
a_assert(ep);
/*
* Set these to NULL, else we try to free them if an error occurs.
*/
endScript.putBackToken = NULL;
bodyScript.putBackToken = NULL;
incrScript.putBackToken = NULL;
condScript.putBackToken = NULL;
expectSemi = 0;
saveFunc = NULL;
for (done = 0; !done; ) {
tid = ejLexGetToken(ep, state);
switch (tid) {
default:
ejLexPutbackToken(ep, TOK_EXPR, ep->token);
done++;
break;
case TOK_ERR:
state = STATE_ERR;
done++;
break;
case TOK_EOF:
state = STATE_EOF;
done++;
break;
case TOK_NEWLINE:
break;
case TOK_SEMI:
/*
* This case is when we discover no statement and just a lone ';'
*/
if (state != STATE_STMT) {
ejLexPutbackToken(ep, tid, ep->token);
}
done++;
break;
case TOK_ID:
/*
* This could either be a reference to a variable or an assignment
*/
identifier = NULL;
setString(B_L, &identifier, ep->token);
/*
* Peek ahead to see if this is an assignment
*/
tid = ejLexGetToken(ep, state);
if (tid == TOK_ASSIGNMENT) {
if (parse(ep, STATE_RELEXP, flags) != STATE_RELEXP_DONE) {
clearString(&identifier);
goto error;
}
if (flags & FLAGS_EXE) {
if ( state == STATE_DEC ) {
ejSetLocalVar(ep->eid, identifier, ep->result);
} else {
ejVarType = ejGetVar(ep->eid, identifier, &value);
if (ejVarType > 0) {
ejSetLocalVar(ep->eid, identifier, ep->result);
} else {
ejSetGlobalVar(ep->eid, identifier, ep->result);
}
}
}
} else if (tid == TOK_INC_DEC ) {
value = NULL;
if (flags & FLAGS_EXE) {
ejVarType = ejGetVar(ep->eid, identifier, &value);
if (ejVarType < 0) {
ejError(ep, T("Undefined variable %s\n"), identifier);
goto error;
}
setString(B_L, &ep->result, value);
if (evalExpr(ep, value, (int) *ep->token, T("1")) < 0) {
state = STATE_ERR;
break;
}
if (ejVarType > 0) {
ejSetLocalVar(ep->eid, identifier, ep->result);
} else {
ejSetGlobalVar(ep->eid, identifier, ep->result);
}
}
} else {
/*
* If we are processing a declaration, allow undefined vars
*/
value = NULL;
if (state == STATE_DEC) {
if (ejGetVar(ep->eid, identifier, &value) > 0) {
ejError(ep, T("Variable already declared"),
identifier);
clearString(&identifier);
goto error;
}
ejSetLocalVar(ep->eid, identifier, NULL);
} else {
if ( flags & FLAGS_EXE ) {
if (ejGetVar(ep->eid, identifier, &value) < 0) {
ejError(ep, T("Undefined variable %s\n"),
identifier);
clearString(&identifier);
goto error;
}
}
}
setString(B_L, &ep->result, value);
ejLexPutbackToken(ep, tid, ep->token);
}
clearString(&identifier);
if (state == STATE_STMT) {
expectSemi++;
}
done++;
break;
case TOK_LITERAL:
/*
* Set the result to the literal (number or string constant)
*/
setString(B_L, &ep->result, ep->token);
if (state == STATE_STMT) {
expectSemi++;
}
done++;
break;
case TOK_FUNCTION:
/*
* We must save any current ep->func value for the current stack frame
*/
if (ep->func) {
saveFunc = ep->func;
}
memset(&func, 0, sizeof(ejfunc_t));
setString(B_L, &func.fname, ep->token);
ep->func = &func;
setString(B_L, &ep->result, T(""));
if (ejLexGetToken(ep, state) != TOK_LPAREN) {
freeFunc(&func);
goto error;
}
if (parse(ep, STATE_ARG_LIST, flags) != STATE_ARG_LIST_DONE) {
freeFunc(&func);
ep->func = saveFunc;
goto error;
}
/*
* Evaluate the function if required
*/
if (flags & FLAGS_EXE && evalFunction(ep) < 0) {
freeFunc(&func);
ep->func = saveFunc;
goto error;
}
freeFunc(&func);
ep->func = saveFunc;
if (ejLexGetToken(ep, state) != TOK_RPAREN) {
goto error;
}
if (state == STATE_STMT) {
expectSemi++;
}
done++;
break;
case TOK_IF:
if (state != STATE_STMT) {
goto error;
}
if (ejLexGetToken(ep, state) != TOK_LPAREN) {
goto error;
}
/*
* Evaluate the entire condition list "(condition)"
*/
if (parse(ep, STATE_COND, flags) != STATE_COND_DONE) {
goto error;
}
if (ejLexGetToken(ep, state) != TOK_RPAREN) {
goto error;
}
/*
* This is the "then" case. We need to always parse both cases and
* execute only the relevant case.
*/
if (*ep->result == '1') {
thenFlags = flags;
elseFlags = flags & ~FLAGS_EXE;
} else {
thenFlags = flags & ~FLAGS_EXE;
elseFlags = flags;
}
/*
* Process the "then" case. Allow for RETURN statement
*/
switch (parse(ep, STATE_STMT, thenFlags)) {
case STATE_RET:
return STATE_RET;
case STATE_STMT_DONE:
break;
default:
goto error;
}
/*
* check to see if there is an "else" case
*/
ejRemoveNewlines(ep, state);
tid = ejLexGetToken(ep, state);
if (tid != TOK_ELSE) {
ejLexPutbackToken(ep, tid, ep->token);
done++;
break;
}
/*
* Process the "else" case. Allow for return.
*/
switch (parse(ep, STATE_STMT, elseFlags)) {
case STATE_RET:
return STATE_RET;
case STATE_STMT_DONE:
break;
default:
goto error;
}
done++;
break;
case TOK_FOR:
/*
* Format for the expression is:
*
* for (initial; condition; incr) {
* body;
* }
*/
if (state != STATE_STMT) {
goto error;
}
if (ejLexGetToken(ep, state) != TOK_LPAREN) {
goto error;
}
/*
* Evaluate the for loop initialization statement
*/
if (parse(ep, STATE_EXPR, flags) != STATE_EXPR_DONE) {
goto error;
}
if (ejLexGetToken(ep, state) != TOK_SEMI) {
goto error;
}
/*
* The first time through, we save the current input context just
* to each step: prior to the conditional, the loop increment and the
* loop body.
*/
ejLexSaveInputState(ep, &condScript);
if (parse(ep, STATE_COND, flags) != STATE_COND_DONE) {
goto error;
}
cond = (*ep->result != '0');
if (ejLexGetToken(ep, state) != TOK_SEMI) {
goto error;
}
/*
* Don't execute the loop increment statement or the body first time
*/
forFlags = flags & ~FLAGS_EXE;
ejLexSaveInputState(ep, &incrScript);
if (parse(ep, STATE_EXPR, forFlags) != STATE_EXPR_DONE) {
goto error;
}
if (ejLexGetToken(ep, state) != TOK_RPAREN) {
goto error;
}
/*
* Parse the body and remember the end of the body script
*/
ejLexSaveInputState(ep, &bodyScript);
if (parse(ep, STATE_STMT, forFlags) != STATE_STMT_DONE) {
goto error;
}
ejLexSaveInputState(ep, &endScript);
/*
* Now actually do the for loop. Note loop has been rotated
*/
while (cond && (flags & FLAGS_EXE) ) {
/*
* Evaluate the body
*/
ejLexRestoreInputState(ep, &bodyScript);
switch (parse(ep, STATE_STMT, flags)) {
case STATE_RET:
return STATE_RET;
case STATE_STMT_DONE:
break;
default:
goto error;
}
/*
* Evaluate the increment script
*/
ejLexRestoreInputState(ep, &incrScript);
if (parse(ep, STATE_EXPR, flags) != STATE_EXPR_DONE) {
goto error;
}
/*
* Evaluate the condition
*/
ejLexRestoreInputState(ep, &condScript);
if (parse(ep, STATE_COND, flags) != STATE_COND_DONE) {
goto error;
}
cond = (*ep->result != '0');
}
ejLexRestoreInputState(ep, &endScript);
done++;
break;
case TOK_VAR:
if (parse(ep, STATE_DEC_LIST, flags) != STATE_DEC_LIST_DONE) {
goto error;
}
done++;
break;
case TOK_COMMA:
ejLexPutbackToken(ep, TOK_EXPR, ep->token);
done++;
break;
case TOK_LPAREN:
if (state == STATE_EXPR) {
if (parse(ep, STATE_RELEXP, flags) != STATE_RELEXP_DONE) {
goto error;
}
if (ejLexGetToken(ep, state) != TOK_RPAREN) {
goto error;
}
return STATE_EXPR_DONE;
}
done++;
break;
case TOK_RPAREN:
ejLexPutbackToken(ep, tid, ep->token);
return STATE_EXPR_DONE;
case TOK_LBRACE:
/*
* This handles any code in braces except "if () {} else {}"
*/
if (state != STATE_STMT) {
goto error;
}
/*
* Parse will return STATE_STMT_BLOCK_DONE when the RBRACE is seen
*/
do {
state = parse(ep, STATE_STMT, flags);
} while (state == STATE_STMT_DONE);
/*
* Allow return statement.
*/
if (state == STATE_RET) {
return state;
}
if (ejLexGetToken(ep, state) != TOK_RBRACE) {
goto error;
}
return STATE_STMT_DONE;
case TOK_RBRACE:
if (state == STATE_STMT) {
ejLexPutbackToken(ep, tid, ep->token);
return STATE_STMT_BLOCK_DONE;
}
goto error;
case TOK_RETURN:
if (parse(ep, STATE_RELEXP, flags) != STATE_RELEXP_DONE) {
goto error;
}
if (flags & FLAGS_EXE) {
while ( ejLexGetToken(ep, state) != TOK_EOF );
done++;
return STATE_RET;
}
break;
}
}
if (expectSemi) {
tid = ejLexGetToken(ep, state);
if (tid != TOK_SEMI && tid != TOK_NEWLINE) {
goto error;
}
/*
* Skip newline after semi-colon
*/
ejRemoveNewlines(ep, state);
}
/*
* Free resources and return the correct status
*/
doneParse:
if (tid == TOK_FOR) {
ejLexFreeInputState(ep, &condScript);
ejLexFreeInputState(ep, &incrScript);
ejLexFreeInputState(ep, &endScript);
ejLexFreeInputState(ep, &bodyScript);
}
if (state == STATE_STMT) {
return STATE_STMT_DONE;
} else if (state == STATE_DEC) {
return STATE_DEC_DONE;
} else if (state == STATE_EXPR) {
return STATE_EXPR_DONE;
} else if (state == STATE_EOF) {
return state;
} else {
return STATE_ERR;
}
/*
* Common error exit
*/
error:
state = STATE_ERR;
goto doneParse;
}
/******************************************************************************/
/*
* Parse variable declaration list
*/
static int parseDeclaration(ej_t *ep, int state, int flags)
{
int tid;
a_assert(ep);
/*
* Declarations can be of the following forms:
* var x;
* var x, y, z;
* var x = 1 + 2 / 3, y = 2 + 4;
*
* We set the variable to NULL if there is no associated assignment.
*/
do {
if ((tid = ejLexGetToken(ep, state)) != TOK_ID) {
return STATE_ERR;
}
ejLexPutbackToken(ep, tid, ep->token);
/*
* Parse the entire assignment or simple identifier declaration
*/
if (parse(ep, STATE_DEC, flags) != STATE_DEC_DONE) {
return STATE_ERR;
}
/*
* Peek at the next token, continue if comma seen
*/
tid = ejLexGetToken(ep, state);
if (tid == TOK_SEMI) {
return STATE_DEC_LIST_DONE;
} else if (tid != TOK_COMMA) {
return STATE_ERR;
}
} while (tid == TOK_COMMA);
if (tid != TOK_SEMI) {
return STATE_ERR;
}
return STATE_DEC_LIST_DONE;
}
/******************************************************************************/
/*
* Parse function arguments
*/
static int parseArgs(ej_t *ep, int state, int flags)
{
int tid, aid;
a_assert(ep);
do {
state = parse(ep, STATE_RELEXP, flags);
if (state == STATE_EOF || state == STATE_ERR) {
return state;
}
if (state == STATE_RELEXP_DONE) {
aid = hAlloc((void***) &ep->func->args);
ep->func->args[aid] = bstrdup(B_L, ep->result);
ep->func->nArgs++;
}
/*
* Peek at the next token, continue if more args (ie. comma seen)
*/
tid = ejLexGetToken(ep, state);
if (tid != TOK_COMMA) {
ejLexPutbackToken(ep, tid, ep->token);
}
} while (tid == TOK_COMMA);
if (tid != TOK_RPAREN && state != STATE_RELEXP_DONE) {
return STATE_ERR;
}
return STATE_ARG_LIST_DONE;
}
/******************************************************************************/
/*
* Parse conditional expression (relational ops separated by ||, &&)
*/