-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
parse.y
16139 lines (14563 loc) · 505 KB
/
parse.y
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
/**********************************************************************
parse.y -
$Author$
created at: Fri May 28 18:02:42 JST 1993
Copyright (C) 1993-2007 Yukihiro Matsumoto
**********************************************************************/
%require "3.0"
%{
#if !YYPURE
# error needs pure parser
#endif
#define YYDEBUG 1
#define YYERROR_VERBOSE 1
#define YYSTACK_USE_ALLOCA 0
/* For Ripper */
#ifdef RUBY_EXTCONF_H
# include RUBY_EXTCONF_H
#endif
#include "ruby/internal/config.h"
#include <errno.h>
#ifdef UNIVERSAL_PARSER
#include "internal/ruby_parser.h"
#include "parser_node.h"
#include "universal_parser.c"
#ifdef RIPPER
#define STATIC_ID2SYM p->config->static_id2sym
#define rb_str_coderange_scan_restartable p->config->str_coderange_scan_restartable
#endif
#else
#include "internal.h"
#include "internal/compile.h"
#include "internal/compilers.h"
#include "internal/complex.h"
#include "internal/encoding.h"
#include "internal/error.h"
#include "internal/hash.h"
#include "internal/io.h"
#include "internal/numeric.h"
#include "internal/parse.h"
#include "internal/rational.h"
#include "internal/re.h"
#include "internal/ruby_parser.h"
#include "internal/symbol.h"
#include "internal/thread.h"
#include "internal/variable.h"
#include "node.h"
#include "parser_node.h"
#include "probes.h"
#include "regenc.h"
#include "ruby/encoding.h"
#include "ruby/regex.h"
#include "ruby/ruby.h"
#include "ruby/st.h"
#include "ruby/util.h"
#include "ruby/ractor.h"
#include "symbol.h"
#ifndef RIPPER
static VALUE
syntax_error_new(void)
{
return rb_class_new_instance(0, 0, rb_eSyntaxError);
}
#endif
static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, const YYLTYPE *loc);
#define compile_callback rb_suppress_tracing
#endif /* !UNIVERSAL_PARSER */
#define NODE_SPECIAL_EMPTY_ARGS ((NODE *)-1)
#define NODE_EMPTY_ARGS_P(node) ((node) == NODE_SPECIAL_EMPTY_ARGS)
static int rb_parser_string_hash_cmp(rb_parser_string_t *str1, rb_parser_string_t *str2);
#ifndef RIPPER
static rb_parser_string_t *rb_parser_string_deep_copy(struct parser_params *p, const rb_parser_string_t *original);
#endif
static int
node_integer_cmp(rb_node_integer_t *n1, rb_node_integer_t *n2)
{
return (n1->minus != n2->minus ||
n1->base != n2->base ||
strcmp(n1->val, n2->val));
}
static int
node_float_cmp(rb_node_float_t *n1, rb_node_float_t *n2)
{
return (n1->minus != n2->minus ||
strcmp(n1->val, n2->val));
}
static int
node_rational_cmp(rb_node_rational_t *n1, rb_node_rational_t *n2)
{
return (n1->minus != n2->minus ||
n1->base != n2->base ||
n1->seen_point != n2->seen_point ||
strcmp(n1->val, n2->val));
}
static int
node_imaginary_cmp(rb_node_imaginary_t *n1, rb_node_imaginary_t *n2)
{
return (n1->minus != n2->minus ||
n1->base != n2->base ||
n1->seen_point != n2->seen_point ||
n1->type != n2->type ||
strcmp(n1->val, n2->val));
}
static int
rb_parser_regx_hash_cmp(rb_node_regx_t *n1, rb_node_regx_t *n2)
{
return (n1->options != n2->options ||
rb_parser_string_hash_cmp(n1->string, n2->string));
}
static st_index_t rb_parser_str_hash(rb_parser_string_t *str);
static st_index_t rb_char_p_hash(const char *c);
static int
literal_cmp(st_data_t val, st_data_t lit)
{
if (val == lit) return 0;
NODE *node_val = RNODE(val);
NODE *node_lit = RNODE(lit);
enum node_type type_val = nd_type(node_val);
enum node_type type_lit = nd_type(node_lit);
if (type_val != type_lit) {
return -1;
}
switch (type_lit) {
case NODE_INTEGER:
return node_integer_cmp(RNODE_INTEGER(node_val), RNODE_INTEGER(node_lit));
case NODE_FLOAT:
return node_float_cmp(RNODE_FLOAT(node_val), RNODE_FLOAT(node_lit));
case NODE_RATIONAL:
return node_rational_cmp(RNODE_RATIONAL(node_val), RNODE_RATIONAL(node_lit));
case NODE_IMAGINARY:
return node_imaginary_cmp(RNODE_IMAGINARY(node_val), RNODE_IMAGINARY(node_lit));
case NODE_STR:
return rb_parser_string_hash_cmp(RNODE_STR(node_val)->string, RNODE_STR(node_lit)->string);
case NODE_SYM:
return rb_parser_string_hash_cmp(RNODE_SYM(node_val)->string, RNODE_SYM(node_lit)->string);
case NODE_REGX:
return rb_parser_regx_hash_cmp(RNODE_REGX(node_val), RNODE_REGX(node_lit));
case NODE_LINE:
return node_val->nd_loc.beg_pos.lineno != node_lit->nd_loc.beg_pos.lineno;
case NODE_FILE:
return rb_parser_string_hash_cmp(RNODE_FILE(node_val)->path, RNODE_FILE(node_lit)->path);
case NODE_ENCODING:
return RNODE_ENCODING(node_val)->enc != RNODE_ENCODING(node_lit)->enc;
default:
#ifdef UNIVERSAL_PARSER
abort();
#else
rb_bug("unexpected node: %s, %s", ruby_node_name(type_val), ruby_node_name(type_lit));
#endif
}
}
static st_index_t
literal_hash(st_data_t a)
{
NODE *node = (NODE *)a;
enum node_type type = nd_type(node);
switch (type) {
case NODE_INTEGER:
return rb_char_p_hash(RNODE_INTEGER(node)->val);
case NODE_FLOAT:
return rb_char_p_hash(RNODE_FLOAT(node)->val);
case NODE_RATIONAL:
return rb_char_p_hash(RNODE_RATIONAL(node)->val);
case NODE_IMAGINARY:
return rb_char_p_hash(RNODE_IMAGINARY(node)->val);
case NODE_STR:
return rb_parser_str_hash(RNODE_STR(node)->string);
case NODE_SYM:
return rb_parser_str_hash(RNODE_SYM(node)->string);
case NODE_REGX:
return rb_parser_str_hash(RNODE_REGX(node)->string);
case NODE_LINE:
return (st_index_t)node->nd_loc.beg_pos.lineno;
case NODE_FILE:
return rb_parser_str_hash(RNODE_FILE(node)->path);
case NODE_ENCODING:
return (st_index_t)RNODE_ENCODING(node)->enc;
default:
#ifdef UNIVERSAL_PARSER
abort();
#else
rb_bug("unexpected node: %s", ruby_node_name(type));
#endif
}
}
static inline int
parse_isascii(int c)
{
return '\0' <= c && c <= '\x7f';
}
#undef ISASCII
#define ISASCII parse_isascii
static inline int
parse_isspace(int c)
{
return c == ' ' || ('\t' <= c && c <= '\r');
}
#undef ISSPACE
#define ISSPACE parse_isspace
static inline int
parse_iscntrl(int c)
{
return ('\0' <= c && c < ' ') || c == '\x7f';
}
#undef ISCNTRL
#define ISCNTRL(c) parse_iscntrl(c)
static inline int
parse_isupper(int c)
{
return 'A' <= c && c <= 'Z';
}
static inline int
parse_islower(int c)
{
return 'a' <= c && c <= 'z';
}
static inline int
parse_isalpha(int c)
{
return parse_isupper(c) || parse_islower(c);
}
#undef ISALPHA
#define ISALPHA(c) parse_isalpha(c)
static inline int
parse_isdigit(int c)
{
return '0' <= c && c <= '9';
}
#undef ISDIGIT
#define ISDIGIT(c) parse_isdigit(c)
static inline int
parse_isalnum(int c)
{
return parse_isalpha(c) || parse_isdigit(c);
}
#undef ISALNUM
#define ISALNUM(c) parse_isalnum(c)
static inline int
parse_isxdigit(int c)
{
return parse_isdigit(c) || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
}
#undef ISXDIGIT
#define ISXDIGIT(c) parse_isxdigit(c)
#include "parser_st.h"
#undef STRCASECMP
#define STRCASECMP rb_parser_st_locale_insensitive_strcasecmp
#undef STRNCASECMP
#define STRNCASECMP rb_parser_st_locale_insensitive_strncasecmp
#ifdef RIPPER
#include "ripper_init.h"
#endif
enum rescue_context {
before_rescue,
after_rescue,
after_else,
after_ensure,
};
struct lex_context {
unsigned int in_defined: 1;
unsigned int in_kwarg: 1;
unsigned int in_argdef: 1;
unsigned int in_def: 1;
unsigned int in_class: 1;
BITFIELD(enum rb_parser_shareability, shareable_constant_value, 2);
BITFIELD(enum rescue_context, in_rescue, 2);
unsigned int cant_return: 1;
};
typedef struct RNode_DEF_TEMP rb_node_def_temp_t;
#if defined(__GNUC__) && !defined(__clang__)
// Suppress "parameter passing for argument of type 'struct
// lex_context' changed" notes. `struct lex_context` is file scope,
// and has no ABI compatibility issue.
RBIMPL_WARNING_PUSH()
RBIMPL_WARNING_IGNORED(-Wpsabi)
RBIMPL_WARNING_POP()
// Not sure why effective even after popped.
#endif
#include "parse.h"
#define NO_LEX_CTXT (struct lex_context){0}
#ifndef WARN_PAST_SCOPE
# define WARN_PAST_SCOPE 0
#endif
#define TAB_WIDTH 8
#define yydebug (p->debug) /* disable the global variable definition */
#define YYFPRINTF(out, ...) rb_parser_printf(p, __VA_ARGS__)
#define YY_LOCATION_PRINT(File, loc, p) \
rb_parser_printf(p, "%d.%d-%d.%d", \
(loc).beg_pos.lineno, (loc).beg_pos.column,\
(loc).end_pos.lineno, (loc).end_pos.column)
#define YYLLOC_DEFAULT(Current, Rhs, N) \
do \
if (N) \
{ \
(Current).beg_pos = YYRHSLOC(Rhs, 1).beg_pos; \
(Current).end_pos = YYRHSLOC(Rhs, N).end_pos; \
} \
else \
{ \
(Current).beg_pos = YYRHSLOC(Rhs, 0).end_pos; \
(Current).end_pos = YYRHSLOC(Rhs, 0).end_pos; \
} \
while (0)
#define YY_(Msgid) \
(((Msgid)[0] == 'm') && (strcmp((Msgid), "memory exhausted") == 0) ? \
"nesting too deep" : (Msgid))
#define RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(Current) \
rb_parser_set_location_from_strterm_heredoc(p, &p->lex.strterm->u.heredoc, &(Current))
#define RUBY_SET_YYLLOC_OF_DELAYED_TOKEN(Current) \
rb_parser_set_location_of_delayed_token(p, &(Current))
#define RUBY_SET_YYLLOC_OF_HEREDOC_END(Current) \
rb_parser_set_location_of_heredoc_end(p, &(Current))
#define RUBY_SET_YYLLOC_OF_DUMMY_END(Current) \
rb_parser_set_location_of_dummy_end(p, &(Current))
#define RUBY_SET_YYLLOC_OF_NONE(Current) \
rb_parser_set_location_of_none(p, &(Current))
#define RUBY_SET_YYLLOC(Current) \
rb_parser_set_location(p, &(Current))
#define RUBY_INIT_YYLLOC() \
{ \
{p->ruby_sourceline, (int)(p->lex.ptok - p->lex.pbeg)}, \
{p->ruby_sourceline, (int)(p->lex.pcur - p->lex.pbeg)}, \
}
#define IS_lex_state_for(x, ls) ((x) & (ls))
#define IS_lex_state_all_for(x, ls) (((x) & (ls)) == (ls))
#define IS_lex_state(ls) IS_lex_state_for(p->lex.state, (ls))
#define IS_lex_state_all(ls) IS_lex_state_all_for(p->lex.state, (ls))
# define SET_LEX_STATE(ls) \
parser_set_lex_state(p, ls, __LINE__)
static inline enum lex_state_e parser_set_lex_state(struct parser_params *p, enum lex_state_e ls, int line);
typedef VALUE stack_type;
static const rb_code_location_t NULL_LOC = { {0, -1}, {0, -1} };
# define SHOW_BITSTACK(stack, name) (p->debug ? rb_parser_show_bitstack(p, stack, name, __LINE__) : (void)0)
# define BITSTACK_PUSH(stack, n) (((p->stack) = ((p->stack)<<1)|((n)&1)), SHOW_BITSTACK(p->stack, #stack"(push)"))
# define BITSTACK_POP(stack) (((p->stack) = (p->stack) >> 1), SHOW_BITSTACK(p->stack, #stack"(pop)"))
# define BITSTACK_SET_P(stack) (SHOW_BITSTACK(p->stack, #stack), (p->stack)&1)
# define BITSTACK_SET(stack, n) ((p->stack)=(n), SHOW_BITSTACK(p->stack, #stack"(set)"))
/* A flag to identify keyword_do_cond, "do" keyword after condition expression.
Examples: `while ... do`, `until ... do`, and `for ... in ... do` */
#define COND_PUSH(n) BITSTACK_PUSH(cond_stack, (n))
#define COND_POP() BITSTACK_POP(cond_stack)
#define COND_P() BITSTACK_SET_P(cond_stack)
#define COND_SET(n) BITSTACK_SET(cond_stack, (n))
/* A flag to identify keyword_do_block; "do" keyword after command_call.
Example: `foo 1, 2 do`. */
#define CMDARG_PUSH(n) BITSTACK_PUSH(cmdarg_stack, (n))
#define CMDARG_POP() BITSTACK_POP(cmdarg_stack)
#define CMDARG_P() BITSTACK_SET_P(cmdarg_stack)
#define CMDARG_SET(n) BITSTACK_SET(cmdarg_stack, (n))
struct vtable {
ID *tbl;
int pos;
int capa;
struct vtable *prev;
};
struct local_vars {
struct vtable *args;
struct vtable *vars;
struct vtable *used;
# if WARN_PAST_SCOPE
struct vtable *past;
# endif
struct local_vars *prev;
struct {
NODE *outer, *inner, *current;
} numparam;
NODE *it;
};
enum {
ORDINAL_PARAM = -1,
NO_PARAM = 0,
NUMPARAM_MAX = 9,
};
#define DVARS_INHERIT ((void*)1)
#define DVARS_TOPSCOPE NULL
#define DVARS_TERMINAL_P(tbl) ((tbl) == DVARS_INHERIT || (tbl) == DVARS_TOPSCOPE)
typedef struct token_info {
const char *token;
rb_code_position_t beg;
int indent;
int nonspc;
struct token_info *next;
} token_info;
typedef struct end_expect_token_locations {
const rb_code_position_t *pos;
struct end_expect_token_locations *prev;
} end_expect_token_locations_t;
typedef struct parser_string_buffer_elem {
struct parser_string_buffer_elem *next;
long len; /* Total length of allocated buf */
long used; /* Current usage of buf */
rb_parser_string_t *buf[FLEX_ARY_LEN];
} parser_string_buffer_elem_t;
typedef struct parser_string_buffer {
parser_string_buffer_elem_t *head;
parser_string_buffer_elem_t *last;
} parser_string_buffer_t;
#define AFTER_HEREDOC_WITHOUT_TERMINTOR ((rb_parser_string_t *)1)
/*
Structure of Lexer Buffer:
lex.pbeg lex.ptok lex.pcur lex.pend
| | | |
|------------+------------+------------|
|<---------->|
token
*/
struct parser_params {
YYSTYPE *lval;
YYLTYPE *yylloc;
struct {
rb_strterm_t *strterm;
rb_parser_lex_gets_func *gets;
rb_parser_input_data input;
parser_string_buffer_t string_buffer;
rb_parser_string_t *lastline;
rb_parser_string_t *nextline;
const char *pbeg;
const char *pcur;
const char *pend;
const char *ptok;
enum lex_state_e state;
/* track the nest level of any parens "()[]{}" */
int paren_nest;
/* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */
int lpar_beg;
/* track the nest level of only braces "{}" */
int brace_nest;
} lex;
stack_type cond_stack;
stack_type cmdarg_stack;
int tokidx;
int toksiz;
int heredoc_end;
int heredoc_indent;
int heredoc_line_indent;
char *tokenbuf;
struct local_vars *lvtbl;
st_table *pvtbl;
st_table *pktbl;
int line_count;
int ruby_sourceline; /* current line no. */
const char *ruby_sourcefile; /* current source file */
VALUE ruby_sourcefile_string;
rb_encoding *enc;
token_info *token_info;
st_table *case_labels;
rb_node_exits_t *exits;
VALUE debug_buffer;
VALUE debug_output;
struct {
rb_parser_string_t *token;
int beg_line;
int beg_col;
int end_line;
int end_col;
} delayed;
rb_ast_t *ast;
int node_id;
st_table *warn_duplicate_keys_table;
int max_numparam;
ID it_id;
struct lex_context ctxt;
NODE *eval_tree_begin;
NODE *eval_tree;
const struct rb_iseq_struct *parent_iseq;
#ifdef UNIVERSAL_PARSER
const rb_parser_config_t *config;
#endif
/* compile_option */
signed int frozen_string_literal:2; /* -1: not specified, 0: false, 1: true */
unsigned int command_start:1;
unsigned int eofp: 1;
unsigned int ruby__end__seen: 1;
unsigned int debug: 1;
unsigned int has_shebang: 1;
unsigned int token_seen: 1;
unsigned int token_info_enabled: 1;
# if WARN_PAST_SCOPE
unsigned int past_scope_enabled: 1;
# endif
unsigned int error_p: 1;
unsigned int cr_seen: 1;
#ifndef RIPPER
/* Ruby core only */
unsigned int do_print: 1;
unsigned int do_loop: 1;
unsigned int do_chomp: 1;
unsigned int do_split: 1;
unsigned int error_tolerant: 1;
unsigned int keep_tokens: 1;
VALUE error_buffer;
rb_parser_ary_t *debug_lines;
/*
* Store specific keyword locations to generate dummy end token.
* Refer to the tail of list element.
*/
end_expect_token_locations_t *end_expect_token_locations;
/* id for terms */
int token_id;
/* Array for term tokens */
rb_parser_ary_t *tokens;
#else
/* Ripper only */
VALUE value;
VALUE result;
VALUE parsing_thread;
VALUE s_value; /* Token VALUE */
VALUE s_lvalue; /* VALUE generated by rule action (reduce) */
VALUE s_value_stack;
#endif
};
#define NUMPARAM_ID_P(id) numparam_id_p(p, id)
#define NUMPARAM_ID_TO_IDX(id) (unsigned int)(((id) >> ID_SCOPE_SHIFT) - (tNUMPARAM_1 - 1))
#define NUMPARAM_IDX_TO_ID(idx) TOKEN2LOCALID((tNUMPARAM_1 - 1 + (idx)))
static int
numparam_id_p(struct parser_params *p, ID id)
{
if (!is_local_id(id) || id < (tNUMPARAM_1 << ID_SCOPE_SHIFT)) return 0;
unsigned int idx = NUMPARAM_ID_TO_IDX(id);
return idx > 0 && idx <= NUMPARAM_MAX;
}
static void numparam_name(struct parser_params *p, ID id);
#ifdef RIPPER
static void
after_shift(struct parser_params *p)
{
if (p->debug) {
rb_parser_printf(p, "after-shift: %+"PRIsVALUE"\n", p->s_value);
}
rb_ary_push(p->s_value_stack, p->s_value);
p->s_value = Qnil;
}
static void
before_reduce(int len, struct parser_params *p)
{
// Initialize $$ with $1.
if (len) p->s_lvalue = rb_ary_entry(p->s_value_stack, -len);
}
static void
after_reduce(int len, struct parser_params *p)
{
for (int i = 0; i < len; i++) {
VALUE tos = rb_ary_pop(p->s_value_stack);
if (p->debug) {
rb_parser_printf(p, "after-reduce pop: %+"PRIsVALUE"\n", tos);
}
}
if (p->debug) {
rb_parser_printf(p, "after-reduce push: %+"PRIsVALUE"\n", p->s_lvalue);
}
rb_ary_push(p->s_value_stack, p->s_lvalue);
p->s_lvalue = Qnil;
}
static void
after_shift_error_token(struct parser_params *p)
{
if (p->debug) {
rb_parser_printf(p, "after-shift-error-token:\n");
}
rb_ary_push(p->s_value_stack, Qnil);
}
static void
after_pop_stack(int len, struct parser_params *p)
{
for (int i = 0; i < len; i++) {
VALUE tos = rb_ary_pop(p->s_value_stack);
if (p->debug) {
rb_parser_printf(p, "after-pop-stack pop: %+"PRIsVALUE"\n", tos);
}
}
}
#else
static void
after_shift(struct parser_params *p)
{
}
static void
before_reduce(int len, struct parser_params *p)
{
}
static void
after_reduce(int len, struct parser_params *p)
{
}
static void
after_shift_error_token(struct parser_params *p)
{
}
static void
after_pop_stack(int len, struct parser_params *p)
{
}
#endif
#define intern_cstr(n,l,en) rb_intern3(n,l,en)
#define STRING_NEW0() rb_parser_encoding_string_new(p,0,0,p->enc)
#define STR_NEW(ptr,len) rb_enc_str_new((ptr),(len),p->enc)
#define STR_NEW0() rb_enc_str_new(0,0,p->enc)
#define STR_NEW2(ptr) rb_enc_str_new((ptr),strlen(ptr),p->enc)
#define STR_NEW3(ptr,len,e,func) parser_str_new(p, (ptr),(len),(e),(func),p->enc)
#define TOK_INTERN() intern_cstr(tok(p), toklen(p), p->enc)
#define VALID_SYMNAME_P(s, l, enc, type) (rb_enc_symname_type(s, l, enc, (1U<<(type))) == (int)(type))
#ifndef RIPPER
static inline int
char_at_end(struct parser_params *p, VALUE str, int when_empty)
{
long len = RSTRING_LEN(str);
return len > 0 ? (unsigned char)RSTRING_PTR(str)[len-1] : when_empty;
}
#endif
static void
pop_pvtbl(struct parser_params *p, st_table *tbl)
{
st_free_table(p->pvtbl);
p->pvtbl = tbl;
}
static void
pop_pktbl(struct parser_params *p, st_table *tbl)
{
if (p->pktbl) st_free_table(p->pktbl);
p->pktbl = tbl;
}
#define STRING_BUF_DEFAULT_LEN 16
static void
string_buffer_init(struct parser_params *p)
{
parser_string_buffer_t *buf = &p->lex.string_buffer;
const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * STRING_BUF_DEFAULT_LEN;
buf->head = buf->last = xmalloc(size);
buf->head->len = STRING_BUF_DEFAULT_LEN;
buf->head->used = 0;
buf->head->next = NULL;
}
static void
string_buffer_append(struct parser_params *p, rb_parser_string_t *str)
{
parser_string_buffer_t *buf = &p->lex.string_buffer;
if (buf->head->used >= buf->head->len) {
parser_string_buffer_elem_t *elem;
long n = buf->head->len * 2;
const size_t size = offsetof(parser_string_buffer_elem_t, buf) + sizeof(rb_parser_string_t *) * n;
elem = xmalloc(size);
elem->len = n;
elem->used = 0;
elem->next = NULL;
buf->last->next = elem;
buf->last = elem;
}
buf->last->buf[buf->last->used++] = str;
}
static void
string_buffer_free(struct parser_params *p)
{
parser_string_buffer_elem_t *elem = p->lex.string_buffer.head;
while (elem) {
parser_string_buffer_elem_t *next_elem = elem->next;
for (long i = 0; i < elem->used; i++) {
rb_parser_string_free(p, elem->buf[i]);
}
xfree(elem);
elem = next_elem;
}
}
#ifndef RIPPER
static void flush_debug_buffer(struct parser_params *p, VALUE out, VALUE str);
static void
debug_end_expect_token_locations(struct parser_params *p, const char *name)
{
if(p->debug) {
VALUE mesg = rb_sprintf("%s: [", name);
int i = 0;
for (end_expect_token_locations_t *loc = p->end_expect_token_locations; loc; loc = loc->prev) {
if (i > 0)
rb_str_cat_cstr(mesg, ", ");
rb_str_catf(mesg, "[%d, %d]", loc->pos->lineno, loc->pos->column);
i++;
}
rb_str_cat_cstr(mesg, "]\n");
flush_debug_buffer(p, p->debug_output, mesg);
}
}
static void
push_end_expect_token_locations(struct parser_params *p, const rb_code_position_t *pos)
{
if(!p->error_tolerant) return;
end_expect_token_locations_t *locations;
locations = ALLOC(end_expect_token_locations_t);
locations->pos = pos;
locations->prev = p->end_expect_token_locations;
p->end_expect_token_locations = locations;
debug_end_expect_token_locations(p, "push_end_expect_token_locations");
}
static void
pop_end_expect_token_locations(struct parser_params *p)
{
if(!p->end_expect_token_locations) return;
end_expect_token_locations_t *locations = p->end_expect_token_locations->prev;
ruby_sized_xfree(p->end_expect_token_locations, sizeof(end_expect_token_locations_t));
p->end_expect_token_locations = locations;
debug_end_expect_token_locations(p, "pop_end_expect_token_locations");
}
static end_expect_token_locations_t *
peek_end_expect_token_locations(struct parser_params *p)
{
return p->end_expect_token_locations;
}
static const char *
parser_token2char(struct parser_params *p, enum yytokentype tok)
{
switch ((int) tok) {
#define TOKEN2CHAR(tok) case tok: return (#tok);
#define TOKEN2CHAR2(tok, name) case tok: return (name);
TOKEN2CHAR2(' ', "word_sep");
TOKEN2CHAR2('!', "!")
TOKEN2CHAR2('%', "%");
TOKEN2CHAR2('&', "&");
TOKEN2CHAR2('*', "*");
TOKEN2CHAR2('+', "+");
TOKEN2CHAR2('-', "-");
TOKEN2CHAR2('/', "/");
TOKEN2CHAR2('<', "<");
TOKEN2CHAR2('=', "=");
TOKEN2CHAR2('>', ">");
TOKEN2CHAR2('?', "?");
TOKEN2CHAR2('^', "^");
TOKEN2CHAR2('|', "|");
TOKEN2CHAR2('~', "~");
TOKEN2CHAR2(':', ":");
TOKEN2CHAR2(',', ",");
TOKEN2CHAR2('.', ".");
TOKEN2CHAR2(';', ";");
TOKEN2CHAR2('`', "`");
TOKEN2CHAR2('\n', "nl");
TOKEN2CHAR2('{', "\"{\"");
TOKEN2CHAR2('}', "\"}\"");
TOKEN2CHAR2('[', "\"[\"");
TOKEN2CHAR2(']', "\"]\"");
TOKEN2CHAR2('(', "\"(\"");
TOKEN2CHAR2(')', "\")\"");
TOKEN2CHAR2('\\', "backslash");
TOKEN2CHAR(keyword_class);
TOKEN2CHAR(keyword_module);
TOKEN2CHAR(keyword_def);
TOKEN2CHAR(keyword_undef);
TOKEN2CHAR(keyword_begin);
TOKEN2CHAR(keyword_rescue);
TOKEN2CHAR(keyword_ensure);
TOKEN2CHAR(keyword_end);
TOKEN2CHAR(keyword_if);
TOKEN2CHAR(keyword_unless);
TOKEN2CHAR(keyword_then);
TOKEN2CHAR(keyword_elsif);
TOKEN2CHAR(keyword_else);
TOKEN2CHAR(keyword_case);
TOKEN2CHAR(keyword_when);
TOKEN2CHAR(keyword_while);
TOKEN2CHAR(keyword_until);
TOKEN2CHAR(keyword_for);
TOKEN2CHAR(keyword_break);
TOKEN2CHAR(keyword_next);
TOKEN2CHAR(keyword_redo);
TOKEN2CHAR(keyword_retry);
TOKEN2CHAR(keyword_in);
TOKEN2CHAR(keyword_do);
TOKEN2CHAR(keyword_do_cond);
TOKEN2CHAR(keyword_do_block);
TOKEN2CHAR(keyword_do_LAMBDA);
TOKEN2CHAR(keyword_return);
TOKEN2CHAR(keyword_yield);
TOKEN2CHAR(keyword_super);
TOKEN2CHAR(keyword_self);
TOKEN2CHAR(keyword_nil);
TOKEN2CHAR(keyword_true);
TOKEN2CHAR(keyword_false);
TOKEN2CHAR(keyword_and);
TOKEN2CHAR(keyword_or);
TOKEN2CHAR(keyword_not);
TOKEN2CHAR(modifier_if);
TOKEN2CHAR(modifier_unless);
TOKEN2CHAR(modifier_while);
TOKEN2CHAR(modifier_until);
TOKEN2CHAR(modifier_rescue);
TOKEN2CHAR(keyword_alias);
TOKEN2CHAR(keyword_defined);
TOKEN2CHAR(keyword_BEGIN);
TOKEN2CHAR(keyword_END);
TOKEN2CHAR(keyword__LINE__);
TOKEN2CHAR(keyword__FILE__);
TOKEN2CHAR(keyword__ENCODING__);
TOKEN2CHAR(tIDENTIFIER);
TOKEN2CHAR(tFID);
TOKEN2CHAR(tGVAR);
TOKEN2CHAR(tIVAR);
TOKEN2CHAR(tCONSTANT);
TOKEN2CHAR(tCVAR);
TOKEN2CHAR(tLABEL);
TOKEN2CHAR(tINTEGER);
TOKEN2CHAR(tFLOAT);
TOKEN2CHAR(tRATIONAL);
TOKEN2CHAR(tIMAGINARY);
TOKEN2CHAR(tCHAR);
TOKEN2CHAR(tNTH_REF);
TOKEN2CHAR(tBACK_REF);
TOKEN2CHAR(tSTRING_CONTENT);
TOKEN2CHAR(tREGEXP_END);
TOKEN2CHAR(tDUMNY_END);
TOKEN2CHAR(tSP);
TOKEN2CHAR(tUPLUS);
TOKEN2CHAR(tUMINUS);
TOKEN2CHAR(tPOW);
TOKEN2CHAR(tCMP);
TOKEN2CHAR(tEQ);
TOKEN2CHAR(tEQQ);
TOKEN2CHAR(tNEQ);
TOKEN2CHAR(tGEQ);
TOKEN2CHAR(tLEQ);
TOKEN2CHAR(tANDOP);
TOKEN2CHAR(tOROP);
TOKEN2CHAR(tMATCH);
TOKEN2CHAR(tNMATCH);
TOKEN2CHAR(tDOT2);
TOKEN2CHAR(tDOT3);
TOKEN2CHAR(tBDOT2);
TOKEN2CHAR(tBDOT3);
TOKEN2CHAR(tAREF);
TOKEN2CHAR(tASET);
TOKEN2CHAR(tLSHFT);
TOKEN2CHAR(tRSHFT);
TOKEN2CHAR(tANDDOT);
TOKEN2CHAR(tCOLON2);
TOKEN2CHAR(tCOLON3);
TOKEN2CHAR(tOP_ASGN);
TOKEN2CHAR(tASSOC);
TOKEN2CHAR(tLPAREN);
TOKEN2CHAR(tLPAREN_ARG);
TOKEN2CHAR(tRPAREN);
TOKEN2CHAR(tLBRACK);
TOKEN2CHAR(tLBRACE);
TOKEN2CHAR(tLBRACE_ARG);
TOKEN2CHAR(tSTAR);
TOKEN2CHAR(tDSTAR);
TOKEN2CHAR(tAMPER);
TOKEN2CHAR(tLAMBDA);
TOKEN2CHAR(tSYMBEG);
TOKEN2CHAR(tSTRING_BEG);
TOKEN2CHAR(tXSTRING_BEG);
TOKEN2CHAR(tREGEXP_BEG);
TOKEN2CHAR(tWORDS_BEG);
TOKEN2CHAR(tQWORDS_BEG);
TOKEN2CHAR(tSYMBOLS_BEG);
TOKEN2CHAR(tQSYMBOLS_BEG);
TOKEN2CHAR(tSTRING_END);
TOKEN2CHAR(tSTRING_DEND);
TOKEN2CHAR(tSTRING_DBEG);
TOKEN2CHAR(tSTRING_DVAR);
TOKEN2CHAR(tLAMBEG);
TOKEN2CHAR(tLABEL_END);
TOKEN2CHAR(tIGNORED_NL);
TOKEN2CHAR(tCOMMENT);
TOKEN2CHAR(tEMBDOC_BEG);
TOKEN2CHAR(tEMBDOC);
TOKEN2CHAR(tEMBDOC_END);
TOKEN2CHAR(tHEREDOC_BEG);
TOKEN2CHAR(tHEREDOC_END);
TOKEN2CHAR(k__END__);
TOKEN2CHAR(tLOWEST);
TOKEN2CHAR(tUMINUS_NUM);
TOKEN2CHAR(tLAST_TOKEN);
#undef TOKEN2CHAR
#undef TOKEN2CHAR2