-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathparser.c
1625 lines (1475 loc) · 48.2 KB
/
parser.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
/* Driver template for the LEMON parser generator.
** The author disclaims copyright to this source code.
*/
/* First off, code is include which follows the "include" declaration
** in the input file. */
#include <stdio.h>
// 28 "parser.lemon"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "ext/standard/php_smart_str.h"
#include "php_phalcon.h"
#include "phalcon.h"
#include "parser.h"
#include "scanner.h"
#include "annot.h"
#include "kernel/main.h"
#include "kernel/memory.h"
#include "kernel/fcall.h"
#include "kernel/exception.h"
static zval *phannot_ret_literal_zval(int type, phannot_parser_token *T)
{
zval *ret;
MAKE_STD_ZVAL(ret);
array_init(ret);
add_assoc_long(ret, "type", type);
if (T) {
add_assoc_stringl(ret, "value", T->token, T->token_len, 0);
efree(T);
}
return ret;
}
static zval *phannot_ret_array(zval *items)
{
zval *ret;
MAKE_STD_ZVAL(ret);
array_init(ret);
add_assoc_long(ret, "type", PHANNOT_T_ARRAY);
if (items) {
add_assoc_zval(ret, "items", items);
}
return ret;
}
static zval *phannot_ret_zval_list(zval *list_left, zval *right_list)
{
zval *ret;
HashPosition pos;
HashTable *list;
MAKE_STD_ZVAL(ret);
array_init(ret);
if (list_left) {
list = Z_ARRVAL_P(list_left);
if (zend_hash_index_exists(list, 0)) {
zend_hash_internal_pointer_reset_ex(list, &pos);
for (;; zend_hash_move_forward_ex(list, &pos)) {
zval ** item;
if (zend_hash_get_current_data_ex(list, (void**) &item, &pos) == FAILURE) {
break;
}
Z_ADDREF_PP(item);
add_next_index_zval(ret, *item);
}
zval_ptr_dtor(&list_left);
} else {
add_next_index_zval(ret, list_left);
}
}
add_next_index_zval(ret, right_list);
return ret;
}
static zval *phannot_ret_named_item(phannot_parser_token *name, zval *expr)
{
zval *ret;
MAKE_STD_ZVAL(ret);
array_init(ret);
add_assoc_zval(ret, "expr", expr);
if (name != NULL) {
add_assoc_stringl(ret, "name", name->token, name->token_len, 0);
efree(name);
}
return ret;
}
static zval *phannot_ret_annotation(phannot_parser_token *name, zval *arguments, phannot_scanner_state *state)
{
zval *ret;
MAKE_STD_ZVAL(ret);
array_init(ret);
add_assoc_long(ret, "type", PHANNOT_T_ANNOTATION);
if (name) {
add_assoc_stringl(ret, "name", name->token, name->token_len, 0);
efree(name);
}
if (arguments) {
add_assoc_zval(ret, "arguments", arguments);
}
Z_ADDREF_P(state->active_file);
add_assoc_zval(ret, "file", state->active_file);
add_assoc_long(ret, "line", state->active_line);
return ret;
}
// 139 "parser.c"
/* Next is all token values, in a form suitable for use by makeheaders.
** This section will be null unless lemon is run with the -m switch.
*/
/*
** These constants (all generated automatically by the parser generator)
** specify the various kinds of tokens (terminals) that the parser
** understands.
**
** Each symbol here is a terminal symbol in the grammar.
*/
/* Make sure the INTERFACE macro is defined.
*/
#ifndef INTERFACE
# define INTERFACE 1
#endif
/* The next thing included is series of defines which control
** various aspects of the generated parser.
** JJCODETYPE is the data type used for storing terminal
** and nonterminal numbers. "unsigned char" is
** used if there are fewer than 250 terminals
** and nonterminals. "int" is used otherwise.
** JJNOCODE is a number of type JJCODETYPE which corresponds
** to no legal terminal or nonterminal number. This
** number is used to fill in empty slots of the hash
** table.
** JJFALLBACK If defined, this indicates that one or more tokens
** have fall-back values which should be used if the
** original value of the token will not parse.
** JJACTIONTYPE is the data type used for storing terminal
** and nonterminal numbers. "unsigned char" is
** used if there are fewer than 250 rules and
** states combined. "int" is used otherwise.
** phannot_JTOKENTYPE is the data type used for minor tokens given
** directly to the parser from the tokenizer.
** JJMINORTYPE is the data type used for all minor tokens.
** This is typically a union of many types, one of
** which is phannot_JTOKENTYPE. The entry in the union
** for base tokens is called "jj0".
** JJSTACKDEPTH is the maximum depth of the parser's stack.
** phannot_ARG_SDECL A static variable declaration for the %extra_argument
** phannot_ARG_PDECL A parameter declaration for the %extra_argument
** phannot_ARG_STORE Code to store %extra_argument into jjpParser
** phannot_ARG_FETCH Code to extract %extra_argument from jjpParser
** JJNSTATE the combined number of states.
** JJNRULE the number of rules in the grammar
** JJERRORSYMBOL is the code number of the error symbol. If not
** defined, then do no error processing.
*/
#define JJCODETYPE unsigned char
#define JJNOCODE 28
#define JJACTIONTYPE unsigned char
#define phannot_JTOKENTYPE phannot_parser_token*
typedef union {
phannot_JTOKENTYPE jj0;
zval* jj36;
int jj55;
} JJMINORTYPE;
#define JJSTACKDEPTH 100
#define phannot_ARG_SDECL phannot_parser_status *status;
#define phannot_ARG_PDECL ,phannot_parser_status *status
#define phannot_ARG_FETCH phannot_parser_status *status = jjpParser->status
#define phannot_ARG_STORE jjpParser->status = status
#define JJNSTATE 40
#define JJNRULE 25
#define JJERRORSYMBOL 18
#define JJERRSYMDT jj55
#define JJ_NO_ACTION (JJNSTATE+JJNRULE+2)
#define JJ_ACCEPT_ACTION (JJNSTATE+JJNRULE+1)
#define JJ_ERROR_ACTION (JJNSTATE+JJNRULE)
/* Next are that tables used to determine what action to take based on the
** current state and lookahead token. These tables are used to implement
** functions that take a state number and lookahead value and return an
** action integer.
**
** Suppose the action integer is N. Then the action is determined as
** follows
**
** 0 <= N < JJNSTATE Shift N. That is, push the lookahead
** token onto the stack and goto state N.
**
** JJNSTATE <= N < JJNSTATE+JJNRULE Reduce by rule N-JJNSTATE.
**
** N == JJNSTATE+JJNRULE A syntax error has occurred.
**
** N == JJNSTATE+JJNRULE+1 The parser accepts its input.
**
** N == JJNSTATE+JJNRULE+2 No such action. Denotes unused
** slots in the jj_action[] table.
**
** The action table is constructed as a single large table named jj_action[].
** Given state S and lookahead X, the action is computed as
**
** jj_action[ jj_shift_ofst[S] + X ]
**
** If the index value jj_shift_ofst[S]+X is out of range or if the value
** jj_lookahead[jj_shift_ofst[S]+X] is not equal to X or if jj_shift_ofst[S]
** is equal to JJ_SHIFT_USE_DFLT, it means that the action is not in the table
** and that jj_default[S] should be used instead.
**
** The formula above is for computing the action when the lookahead is
** a terminal symbol. If the lookahead is a non-terminal (as occurs after
** a reduce action) then the jj_reduce_ofst[] array is used in place of
** the jj_shift_ofst[] array and JJ_REDUCE_USE_DFLT is used in place of
** JJ_SHIFT_USE_DFLT.
**
** The following are the tables generated in this section:
**
** jj_action[] A single table containing all actions.
** jj_lookahead[] A table containing the lookahead for each entry in
** jj_action. Used to detect hash collisions.
** jj_shift_ofst[] For each state, the offset into jj_action for
** shifting terminals.
** jj_reduce_ofst[] For each state, the offset into jj_action for
** shifting non-terminals after a reduce.
** jj_default[] Default action for each state.
*/
static JJACTIONTYPE jj_action[] = {
/* 0 */ 4, 28, 15, 38, 12, 14, 16, 18, 20, 21,
/* 10 */ 22, 23, 24, 4, 31, 4, 28, 15, 40, 12,
/* 20 */ 30, 16, 18, 20, 21, 22, 23, 24, 3, 31,
/* 30 */ 4, 17, 15, 6, 19, 35, 16, 18, 20, 21,
/* 40 */ 22, 23, 24, 5, 31, 15, 7, 27, 11, 16,
/* 50 */ 54, 54, 15, 25, 27, 11, 16, 15, 32, 27,
/* 60 */ 11, 16, 66, 1, 2, 39, 41, 15, 4, 10,
/* 70 */ 11, 16, 15, 9, 9, 37, 16, 8, 13, 36,
/* 80 */ 9, 29, 34, 54, 54, 54, 54, 54, 26, 54,
/* 90 */ 54, 54, 54, 54, 54, 54, 33,
};
static JJCODETYPE jj_lookahead[] = {
/* 0 */ 2, 3, 22, 5, 6, 25, 26, 9, 10, 11,
/* 10 */ 12, 13, 14, 2, 16, 2, 3, 22, 0, 6,
/* 20 */ 25, 26, 9, 10, 11, 12, 13, 14, 22, 16,
/* 30 */ 2, 3, 22, 4, 6, 25, 26, 9, 10, 11,
/* 40 */ 12, 13, 14, 3, 16, 22, 23, 24, 25, 26,
/* 50 */ 27, 27, 22, 23, 24, 25, 26, 22, 23, 24,
/* 60 */ 25, 26, 19, 20, 21, 22, 0, 22, 2, 24,
/* 70 */ 25, 26, 22, 1, 1, 25, 26, 5, 7, 8,
/* 80 */ 1, 7, 8, 27, 27, 27, 27, 27, 15, 27,
/* 90 */ 27, 27, 27, 27, 27, 27, 17,
};
#define JJ_SHIFT_USE_DFLT (-3)
static signed char jj_shift_ofst[] = {
/* 0 */ 11, 18, 66, -3, 40, 29, -2, 72, -3, 13,
/* 10 */ -3, -3, 71, 28, -3, -3, -3, -3, -3, -3,
/* 20 */ -3, -3, -3, -3, 13, 73, -3, -3, 74, 28,
/* 30 */ -3, 13, 79, -3, 28, -3, 28, -3, -3, -3,
};
#define JJ_REDUCE_USE_DFLT (-21)
static signed char jj_reduce_ofst[] = {
/* 0 */ 43, -21, 6, -21, -21, -21, 23, -21, -21, 45,
/* 10 */ -21, -21, -21, -20, -21, -21, -21, -21, -21, -21,
/* 20 */ -21, -21, -21, -21, 30, -21, -21, -21, -21, -5,
/* 30 */ -21, 35, -21, -21, 10, -21, 50, -21, -21, -21,
};
static JJACTIONTYPE jj_default[] = {
/* 0 */ 65, 65, 65, 42, 65, 46, 65, 65, 44, 65,
/* 10 */ 47, 49, 58, 65, 50, 54, 55, 56, 57, 58,
/* 20 */ 59, 60, 61, 62, 65, 65, 63, 48, 56, 65,
/* 30 */ 52, 65, 65, 64, 65, 53, 65, 51, 45, 43,
};
#define JJ_SZ_ACTTAB (sizeof(jj_action)/sizeof(jj_action[0]))
/* The next table maps tokens into fallback tokens. If a construct
** like the following:
**
** %fallback ID X Y Z.
**
** appears in the grammer, then ID becomes a fallback token for X, Y,
** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
** but it does not parse, the type of the token is changed to ID and
** the parse is retried before an error is thrown.
*/
#ifdef JJFALLBACK
static const JJCODETYPE jjFallback[] = {
};
#endif /* JJFALLBACK */
/* The following structure represents a single element of the
** parser's stack. Information stored includes:
**
** + The state number for the parser at this level of the stack.
**
** + The value of the token stored at this level of the stack.
** (In other words, the "major" token.)
**
** + The semantic value stored at this level of the stack. This is
** the information used by the action routines in the grammar.
** It is sometimes called the "minor" token.
*/
struct jjStackEntry {
int stateno; /* The state-number */
int major; /* The major token value. This is the code
** number for the token at this stack level */
JJMINORTYPE minor; /* The user-supplied minor token value. This
** is the value of the token */
};
typedef struct jjStackEntry jjStackEntry;
/* The state of the parser is completely contained in an instance of
** the following structure */
struct jjParser {
int jjidx; /* Index of top element in stack */
int jjerrcnt; /* Shifts left before out of the error */
phannot_ARG_SDECL /* A place to hold %extra_argument */
jjStackEntry jjstack[JJSTACKDEPTH]; /* The parser's stack */
};
typedef struct jjParser jjParser;
#ifndef NDEBUG
#include <stdio.h>
static FILE *jjTraceFILE = 0;
static char *jjTracePrompt = 0;
#endif /* NDEBUG */
#ifndef NDEBUG
/*
** Turn parser tracing on by giving a stream to which to write the trace
** and a prompt to preface each trace message. Tracing is turned off
** by making either argument NULL
**
** Inputs:
** <ul>
** <li> A FILE* to which trace output should be written.
** If NULL, then tracing is turned off.
** <li> A prefix string written at the beginning of every
** line of trace output. If NULL, then tracing is
** turned off.
** </ul>
**
** Outputs:
** None.
*/
void phannot_Trace(FILE *TraceFILE, char *zTracePrompt){
jjTraceFILE = TraceFILE;
jjTracePrompt = zTracePrompt;
if( jjTraceFILE==0 ) jjTracePrompt = 0;
else if( jjTracePrompt==0 ) jjTraceFILE = 0;
}
#endif /* NDEBUG */
#ifndef NDEBUG
/* For tracing shifts, the names of all terminals and nonterminals
** are required. The following table supplies these names */
static const char *jjTokenName[] = {
"$", "COMMA", "AT", "IDENTIFIER",
"PARENTHESES_OPEN", "PARENTHESES_CLOSE", "STRING", "EQUALS",
"COLON", "INTEGER", "DOUBLE", "NULL",
"FALSE", "TRUE", "BRACKET_OPEN", "BRACKET_CLOSE",
"SBRACKET_OPEN", "SBRACKET_CLOSE", "error", "program",
"annotation_language", "annotation_list", "annotation", "argument_list",
"argument_item", "expr", "array",
};
#endif /* NDEBUG */
#ifndef NDEBUG
/* For tracing reduce actions, the names of all rules are required.
*/
static const char *jjRuleName[] = {
/* 0 */ "program ::= annotation_language",
/* 1 */ "annotation_language ::= annotation_list",
/* 2 */ "annotation_list ::= annotation_list annotation",
/* 3 */ "annotation_list ::= annotation",
/* 4 */ "annotation ::= AT IDENTIFIER PARENTHESES_OPEN argument_list PARENTHESES_CLOSE",
/* 5 */ "annotation ::= AT IDENTIFIER PARENTHESES_OPEN PARENTHESES_CLOSE",
/* 6 */ "annotation ::= AT IDENTIFIER",
/* 7 */ "argument_list ::= argument_list COMMA argument_item",
/* 8 */ "argument_list ::= argument_item",
/* 9 */ "argument_item ::= expr",
/* 10 */ "argument_item ::= STRING EQUALS expr",
/* 11 */ "argument_item ::= STRING COLON expr",
/* 12 */ "argument_item ::= IDENTIFIER EQUALS expr",
/* 13 */ "argument_item ::= IDENTIFIER COLON expr",
/* 14 */ "expr ::= annotation",
/* 15 */ "expr ::= array",
/* 16 */ "expr ::= IDENTIFIER",
/* 17 */ "expr ::= INTEGER",
/* 18 */ "expr ::= STRING",
/* 19 */ "expr ::= DOUBLE",
/* 20 */ "expr ::= NULL",
/* 21 */ "expr ::= FALSE",
/* 22 */ "expr ::= TRUE",
/* 23 */ "array ::= BRACKET_OPEN argument_list BRACKET_CLOSE",
/* 24 */ "array ::= SBRACKET_OPEN argument_list SBRACKET_CLOSE",
};
#endif /* NDEBUG */
/*
** This function returns the symbolic name associated with a token
** value.
*/
const char *phannot_TokenName(int tokenType){
#ifndef NDEBUG
if( tokenType>0 && tokenType<(sizeof(jjTokenName)/sizeof(jjTokenName[0])) ){
return jjTokenName[tokenType];
}else{
return "Unknown";
}
#else
return "";
#endif
}
/*
** This function allocates a new parser.
** The only argument is a pointer to a function which works like
** malloc.
**
** Inputs:
** A pointer to the function used to allocate memory.
**
** Outputs:
** A pointer to a parser. This pointer is used in subsequent calls
** to phannot_ and phannot_Free.
*/
void *phannot_Alloc(void *(*mallocProc)(size_t)){
jjParser *pParser;
pParser = (jjParser*)(*mallocProc)( (size_t)sizeof(jjParser) );
if( pParser ){
pParser->jjidx = -1;
}
return pParser;
}
/* The following function deletes the value associated with a
** symbol. The symbol can be either a terminal or nonterminal.
** "jjmajor" is the symbol code, and "jjpminor" is a pointer to
** the value.
*/
static void jj_destructor(JJCODETYPE jjmajor, JJMINORTYPE *jjpminor){
switch( jjmajor ){
/* Here is inserted the actions which take place when a
** terminal or non-terminal is destroyed. This can happen
** when the symbol is popped from the stack during a
** reduce or during error processing or when a parser is
** being destroyed before it is finished parsing.
**
** Note: during a reduce, the only symbols destroyed are those
** which appear on the RHS of the rule, but which are not used
** inside the C code.
*/
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
// 222 "parser.lemon"
{
if ((jjpminor->jj0)) {
if ((jjpminor->jj0)->free_flag) {
efree((jjpminor->jj0)->token);
}
efree((jjpminor->jj0));
}
}
// 507 "parser.c"
break;
case 20:
case 21:
case 22:
case 23:
case 24:
case 25:
// 235 "parser.lemon"
{ zval_ptr_dtor(&(jjpminor->jj36)); }
// 517 "parser.c"
break;
default: break; /* If no destructor action specified: do nothing */
}
}
/*
** Pop the parser's stack once.
**
** If there is a destructor routine associated with the token which
** is popped from the stack, then call it.
**
** Return the major token number for the symbol popped.
*/
static int jj_pop_parser_stack(jjParser *pParser){
JJCODETYPE jjmajor;
jjStackEntry *jjtos = &pParser->jjstack[pParser->jjidx];
if( pParser->jjidx<0 ) return 0;
#ifndef NDEBUG
if( jjTraceFILE && pParser->jjidx>=0 ){
fprintf(jjTraceFILE,"%sPopping %s\n",
jjTracePrompt,
jjTokenName[jjtos->major]);
}
#endif
jjmajor = jjtos->major;
jj_destructor( jjmajor, &jjtos->minor);
pParser->jjidx--;
return jjmajor;
}
/*
** Deallocate and destroy a parser. Destructors are all called for
** all stack elements before shutting the parser down.
**
** Inputs:
** <ul>
** <li> A pointer to the parser. This should be a pointer
** obtained from phannot_Alloc.
** <li> A pointer to a function used to reclaim memory obtained
** from malloc.
** </ul>
*/
void phannot_Free(
void *p, /* The parser to be deleted */
void (*freeProc)(void*) /* Function used to reclaim memory */
){
jjParser *pParser = (jjParser*)p;
if( pParser==0 ) return;
while( pParser->jjidx>=0 ) jj_pop_parser_stack(pParser);
(*freeProc)((void*)pParser);
}
/*
** Find the appropriate action for a parser given the terminal
** look-ahead token iLookAhead.
**
** If the look-ahead token is JJNOCODE, then check to see if the action is
** independent of the look-ahead. If it is, return the action, otherwise
** return JJ_NO_ACTION.
*/
static int jj_find_shift_action(
jjParser *pParser, /* The parser */
int iLookAhead /* The look-ahead token */
){
int i;
int stateno = pParser->jjstack[pParser->jjidx].stateno;
/* if( pParser->jjidx<0 ) return JJ_NO_ACTION; */
i = jj_shift_ofst[stateno];
if( i==JJ_SHIFT_USE_DFLT ){
return jj_default[stateno];
}
if( iLookAhead==JJNOCODE ){
return JJ_NO_ACTION;
}
i += iLookAhead;
if( i<0 || i>=JJ_SZ_ACTTAB || jj_lookahead[i]!=iLookAhead ){
#ifdef JJFALLBACK
int iFallback; /* Fallback token */
if( iLookAhead<sizeof(jjFallback)/sizeof(jjFallback[0])
&& (iFallback = jjFallback[iLookAhead])!=0 ){
#ifndef NDEBUG
if( jjTraceFILE ){
fprintf(jjTraceFILE, "%sFALLBACK %s => %s\n",
jjTracePrompt, jjTokenName[iLookAhead], jjTokenName[iFallback]);
}
#endif
return jj_find_shift_action(pParser, iFallback);
}
#endif
return jj_default[stateno];
}else{
return jj_action[i];
}
}
/*
** Find the appropriate action for a parser given the non-terminal
** look-ahead token iLookAhead.
**
** If the look-ahead token is JJNOCODE, then check to see if the action is
** independent of the look-ahead. If it is, return the action, otherwise
** return JJ_NO_ACTION.
*/
static int jj_find_reduce_action(
jjParser *pParser, /* The parser */
int iLookAhead /* The look-ahead token */
){
int i;
int stateno = pParser->jjstack[pParser->jjidx].stateno;
i = jj_reduce_ofst[stateno];
if( i==JJ_REDUCE_USE_DFLT ){
return jj_default[stateno];
}
if( iLookAhead==JJNOCODE ){
return JJ_NO_ACTION;
}
i += iLookAhead;
if( i<0 || i>=JJ_SZ_ACTTAB || jj_lookahead[i]!=iLookAhead ){
return jj_default[stateno];
}else{
return jj_action[i];
}
}
/*
** Perform a shift action.
*/
static void jj_shift(
jjParser *jjpParser, /* The parser to be shifted */
int jjNewState, /* The new state to shift in */
int jjMajor, /* The major token to shift in */
JJMINORTYPE *jjpMinor /* Pointer ot the minor token to shift in */
){
jjStackEntry *jjtos;
jjpParser->jjidx++;
if( jjpParser->jjidx>=JJSTACKDEPTH ){
phannot_ARG_FETCH;
jjpParser->jjidx--;
#ifndef NDEBUG
if( jjTraceFILE ){
fprintf(jjTraceFILE,"%sStack Overflow!\n",jjTracePrompt);
}
#endif
while( jjpParser->jjidx>=0 ) jj_pop_parser_stack(jjpParser);
/* Here code is inserted which will execute if the parser
** stack every overflows */
phannot_ARG_STORE; /* Suppress warning about unused %extra_argument var */
return;
}
jjtos = &jjpParser->jjstack[jjpParser->jjidx];
jjtos->stateno = jjNewState;
jjtos->major = jjMajor;
jjtos->minor = *jjpMinor;
#ifndef NDEBUG
if( jjTraceFILE && jjpParser->jjidx>0 ){
int i;
fprintf(jjTraceFILE,"%sShift %d\n",jjTracePrompt,jjNewState);
fprintf(jjTraceFILE,"%sStack:",jjTracePrompt);
for(i=1; i<=jjpParser->jjidx; i++)
fprintf(jjTraceFILE," %s",jjTokenName[jjpParser->jjstack[i].major]);
fprintf(jjTraceFILE,"\n");
}
#endif
}
/* The following table contains information about every rule that
** is used during the reduce.
*/
static struct {
JJCODETYPE lhs; /* Symbol on the left-hand side of the rule */
unsigned char nrhs; /* Number of right-hand side symbols in the rule */
} jjRuleInfo[] = {
{ 19, 1 },
{ 20, 1 },
{ 21, 2 },
{ 21, 1 },
{ 22, 5 },
{ 22, 4 },
{ 22, 2 },
{ 23, 3 },
{ 23, 1 },
{ 24, 1 },
{ 24, 3 },
{ 24, 3 },
{ 24, 3 },
{ 24, 3 },
{ 25, 1 },
{ 25, 1 },
{ 25, 1 },
{ 25, 1 },
{ 25, 1 },
{ 25, 1 },
{ 25, 1 },
{ 25, 1 },
{ 25, 1 },
{ 26, 3 },
{ 26, 3 },
};
static void jj_accept(jjParser*); /* Forward Declaration */
/*
** Perform a reduce action and the shift that must immediately
** follow the reduce.
*/
static void jj_reduce(
jjParser *jjpParser, /* The parser */
int jjruleno /* Number of the rule by which to reduce */
){
int jjgoto; /* The next state */
int jjact; /* The next action */
JJMINORTYPE jjgotominor; /* The LHS of the rule reduced */
jjStackEntry *jjmsp; /* The top of the parser's stack */
int jjsize; /* Amount to pop the stack */
phannot_ARG_FETCH;
jjmsp = &jjpParser->jjstack[jjpParser->jjidx];
#ifndef NDEBUG
if( jjTraceFILE && jjruleno>=0
&& jjruleno<sizeof(jjRuleName)/sizeof(jjRuleName[0]) ){
fprintf(jjTraceFILE, "%sReduce [%s].\n", jjTracePrompt,
jjRuleName[jjruleno]);
}
#endif /* NDEBUG */
switch( jjruleno ){
/* Beginning here are the reduction cases. A typical example
** follows:
** case 0:
** // <lineno> <grammarfile>
** { ... } // User supplied code
** // <lineno> <thisfile>
** break;
*/
case 0:
// 231 "parser.lemon"
{
status->ret = jjmsp[0].minor.jj36;
}
// 759 "parser.c"
break;
case 1:
case 14:
case 15:
// 237 "parser.lemon"
{
jjgotominor.jj36 = jjmsp[0].minor.jj36;
}
// 768 "parser.c"
break;
case 2:
// 243 "parser.lemon"
{
jjgotominor.jj36 = phannot_ret_zval_list(jjmsp[-1].minor.jj36, jjmsp[0].minor.jj36);
}
// 775 "parser.c"
break;
case 3:
case 8:
// 247 "parser.lemon"
{
jjgotominor.jj36 = phannot_ret_zval_list(NULL, jjmsp[0].minor.jj36);
}
// 783 "parser.c"
break;
case 4:
// 254 "parser.lemon"
{
jjgotominor.jj36 = phannot_ret_annotation(jjmsp[-3].minor.jj0, jjmsp[-1].minor.jj36, status->scanner_state);
jj_destructor(2,&jjmsp[-4].minor);
jj_destructor(4,&jjmsp[-2].minor);
jj_destructor(5,&jjmsp[0].minor);
}
// 793 "parser.c"
break;
case 5:
// 258 "parser.lemon"
{
jjgotominor.jj36 = phannot_ret_annotation(jjmsp[-2].minor.jj0, NULL, status->scanner_state);
jj_destructor(2,&jjmsp[-3].minor);
jj_destructor(4,&jjmsp[-1].minor);
jj_destructor(5,&jjmsp[0].minor);
}
// 803 "parser.c"
break;
case 6:
// 262 "parser.lemon"
{
jjgotominor.jj36 = phannot_ret_annotation(jjmsp[0].minor.jj0, NULL, status->scanner_state);
jj_destructor(2,&jjmsp[-1].minor);
}
// 811 "parser.c"
break;
case 7:
// 268 "parser.lemon"
{
jjgotominor.jj36 = phannot_ret_zval_list(jjmsp[-2].minor.jj36, jjmsp[0].minor.jj36);
jj_destructor(1,&jjmsp[-1].minor);
}
// 819 "parser.c"
break;
case 9:
// 278 "parser.lemon"
{
jjgotominor.jj36 = phannot_ret_named_item(NULL, jjmsp[0].minor.jj36);
}
// 826 "parser.c"
break;
case 10:
case 12:
// 282 "parser.lemon"
{
jjgotominor.jj36 = phannot_ret_named_item(jjmsp[-2].minor.jj0, jjmsp[0].minor.jj36);
jj_destructor(7,&jjmsp[-1].minor);
}
// 835 "parser.c"
break;
case 11:
case 13:
// 286 "parser.lemon"
{
jjgotominor.jj36 = phannot_ret_named_item(jjmsp[-2].minor.jj0, jjmsp[0].minor.jj36);
jj_destructor(8,&jjmsp[-1].minor);
}
// 844 "parser.c"
break;
case 16:
// 308 "parser.lemon"
{
jjgotominor.jj36 = phannot_ret_literal_zval(PHANNOT_T_IDENTIFIER, jjmsp[0].minor.jj0);
}
// 851 "parser.c"
break;
case 17:
// 312 "parser.lemon"
{
jjgotominor.jj36 = phannot_ret_literal_zval(PHANNOT_T_INTEGER, jjmsp[0].minor.jj0);
}
// 858 "parser.c"
break;
case 18:
// 316 "parser.lemon"
{
jjgotominor.jj36 = phannot_ret_literal_zval(PHANNOT_T_STRING, jjmsp[0].minor.jj0);
}
// 865 "parser.c"
break;
case 19:
// 320 "parser.lemon"
{
jjgotominor.jj36 = phannot_ret_literal_zval(PHANNOT_T_DOUBLE, jjmsp[0].minor.jj0);
}
// 872 "parser.c"
break;
case 20:
// 324 "parser.lemon"
{
jjgotominor.jj36 = phannot_ret_literal_zval(PHANNOT_T_NULL, NULL);
jj_destructor(11,&jjmsp[0].minor);
}
// 880 "parser.c"
break;
case 21:
// 328 "parser.lemon"
{
jjgotominor.jj36 = phannot_ret_literal_zval(PHANNOT_T_FALSE, NULL);
jj_destructor(12,&jjmsp[0].minor);
}
// 888 "parser.c"
break;
case 22:
// 332 "parser.lemon"
{
jjgotominor.jj36 = phannot_ret_literal_zval(PHANNOT_T_TRUE, NULL);
jj_destructor(13,&jjmsp[0].minor);
}
// 896 "parser.c"
break;
case 23:
// 336 "parser.lemon"
{
jjgotominor.jj36 = phannot_ret_array(jjmsp[-1].minor.jj36);
jj_destructor(14,&jjmsp[-2].minor);
jj_destructor(15,&jjmsp[0].minor);
}
// 905 "parser.c"
break;
case 24:
// 340 "parser.lemon"
{
jjgotominor.jj36 = phannot_ret_array(jjmsp[-1].minor.jj36);
jj_destructor(16,&jjmsp[-2].minor);
jj_destructor(17,&jjmsp[0].minor);
}
// 914 "parser.c"
break;
};
jjgoto = jjRuleInfo[jjruleno].lhs;
jjsize = jjRuleInfo[jjruleno].nrhs;
jjpParser->jjidx -= jjsize;
jjact = jj_find_reduce_action(jjpParser,jjgoto);
if( jjact < JJNSTATE ){
jj_shift(jjpParser,jjact,jjgoto,&jjgotominor);
}else if( jjact == JJNSTATE + JJNRULE + 1 ){
jj_accept(jjpParser);
}
}
/*
** The following code executes when the parse fails
*/
static void jj_parse_failed(
jjParser *jjpParser /* The parser */
){
phannot_ARG_FETCH;
#ifndef NDEBUG
if( jjTraceFILE ){
fprintf(jjTraceFILE,"%sFail!\n",jjTracePrompt);
}
#endif
while( jjpParser->jjidx>=0 ) jj_pop_parser_stack(jjpParser);
/* Here code is inserted which will be executed whenever the
** parser fails */
phannot_ARG_STORE; /* Suppress warning about unused %extra_argument variable */
}
/*
** The following code executes when a syntax error first occurs.
*/
static void jj_syntax_error(
jjParser *jjpParser, /* The parser */
int jjmajor, /* The major type of the error token */
JJMINORTYPE jjminor /* The minor type of the error token */
){
phannot_ARG_FETCH;
#define JTOKEN (jjminor.jj0)
// 159 "parser.lemon"
if (status->scanner_state->start_length) {
{
char *token_name = NULL;
const phannot_token_names *tokens = phannot_tokens;
int token_found = 0;
int active_token = status->scanner_state->active_token;
int near_length = status->scanner_state->start_length;
if (active_token) {
do {
if (tokens->code == active_token) {
token_found = 1;
token_name = tokens->name;
break;
}
++tokens;
} while (tokens[0].code != 0);
}
if (!token_name) {
token_found = 0;
token_name = estrndup("UNKNOWN", strlen("UNKNOWN"));
}
status->syntax_error_len = 128 + strlen(token_name) + Z_STRLEN_P(status->scanner_state->active_file);
status->syntax_error = emalloc(sizeof(char) * status->syntax_error_len);
if (near_length > 0) {
if (status->token->value) {
snprintf(status->syntax_error, status->syntax_error_len, "Syntax error, unexpected token %s(%s), near to '%s' in %s on line %d", token_name, status->token->value, status->scanner_state->start, Z_STRVAL_P(status->scanner_state->active_file), status->scanner_state->active_line);
} else {
snprintf(status->syntax_error, status->syntax_error_len, "Syntax error, unexpected token %s, near to '%s' in %s on line %d", token_name, status->scanner_state->start, Z_STRVAL_P(status->scanner_state->active_file), status->scanner_state->active_line);
}
} else {
if (active_token != PHANNOT_T_IGNORE) {
if (status->token->value) {
snprintf(status->syntax_error, status->syntax_error_len, "Syntax error, unexpected token %s(%s), at the end of docblock in %s on line %d", token_name, status->token->value, Z_STRVAL_P(status->scanner_state->active_file), status->scanner_state->active_line);
} else {
snprintf(status->syntax_error, status->syntax_error_len, "Syntax error, unexpected token %s, at the end of docblock in %s on line %d", token_name, Z_STRVAL_P(status->scanner_state->active_file), status->scanner_state->active_line);
}
} else {
snprintf(status->syntax_error, status->syntax_error_len, "Syntax error, unexpected EOF, at the end of docblock in %s on line %d", Z_STRVAL_P(status->scanner_state->active_file), status->scanner_state->active_line);
}