Ruby 3.5.0dev (2025-02-20 revision 34098b669c0cbc024cd08e686891f1dfe0a10aaf)
rubyparser.h (34098b669c0cbc024cd08e686891f1dfe0a10aaf)
1#ifndef RUBY_RUBYPARSER_H
2#define RUBY_RUBYPARSER_H 1
3/*
4 * This is a header file for librubyparser interface
5 */
6
7#include <stdarg.h> /* for va_list */
8#include <assert.h>
9
10#ifdef UNIVERSAL_PARSER
11
12#define rb_encoding const void
13#define OnigCodePoint unsigned int
14#include "parser_st.h"
15#ifndef RUBY_RUBY_H
16#include "parser_value.h"
17#endif
18
19#else
20
21#include "ruby/encoding.h"
22
23#endif
24
25#ifndef FLEX_ARY_LEN
26/* From internal/compilers.h */
27/* A macro for defining a flexible array, like: VALUE ary[FLEX_ARY_LEN]; */
28#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
29# define FLEX_ARY_LEN /* VALUE ary[]; */
30#elif defined(__GNUC__) && !defined(__STRICT_ANSI__)
31# define FLEX_ARY_LEN 0 /* VALUE ary[0]; */
32#else
33# define FLEX_ARY_LEN 1 /* VALUE ary[1]; */
34#endif
35#endif
36
37#if defined(__GNUC__)
38# if defined(__MINGW_PRINTF_FORMAT)
39# define RUBYPARSER_ATTRIBUTE_FORMAT(string_index, argument_index) __attribute__((format(__MINGW_PRINTF_FORMAT, string_index, argument_index)))
40# else
41# define RUBYPARSER_ATTRIBUTE_FORMAT(string_index, argument_index) __attribute__((format(printf, string_index, argument_index)))
42# endif
43#elif defined(__clang__)
44# define RUBYPARSER_ATTRIBUTE_FORMAT(string_index, argument_index) __attribute__((__format__(__printf__, string_index, argument_index)))
45#else
46# define RUBYPARSER_ATTRIBUTE_FORMAT(string_index, argument_index)
47#endif
48
49/*
50 * Parser String
51 */
52enum rb_parser_string_coderange_type {
54 RB_PARSER_ENC_CODERANGE_UNKNOWN = 0,
55 RB_PARSER_ENC_CODERANGE_7BIT = 1,
56 RB_PARSER_ENC_CODERANGE_VALID = 2,
57 RB_PARSER_ENC_CODERANGE_BROKEN = 3
58};
59
60typedef struct rb_parser_string {
61 enum rb_parser_string_coderange_type coderange;
62 rb_encoding *enc;
63 /* Length of the string, not including terminating NUL character. */
64 long len;
65 /* Pointer to the contents of the string. */
66 char *ptr;
68
69enum rb_parser_shareability {
70 rb_parser_shareable_none,
71 rb_parser_shareable_literal,
72 rb_parser_shareable_copy,
73 rb_parser_shareable_everything,
74};
75
76typedef void* rb_parser_input_data;
77
78/*
79 * AST Node
80 */
81enum node_type {
82 NODE_SCOPE,
83 NODE_BLOCK,
84 NODE_IF,
85 NODE_UNLESS,
86 NODE_CASE,
87 NODE_CASE2,
88 NODE_CASE3,
89 NODE_WHEN,
90 NODE_IN,
91 NODE_WHILE,
92 NODE_UNTIL,
93 NODE_ITER,
94 NODE_FOR,
95 NODE_FOR_MASGN,
96 NODE_BREAK,
97 NODE_NEXT,
98 NODE_REDO,
99 NODE_RETRY,
100 NODE_BEGIN,
101 NODE_RESCUE,
102 NODE_RESBODY,
103 NODE_ENSURE,
104 NODE_AND,
105 NODE_OR,
106 NODE_MASGN,
107 NODE_LASGN,
108 NODE_DASGN,
109 NODE_GASGN,
110 NODE_IASGN,
111 NODE_CDECL,
112 NODE_CVASGN,
113 NODE_OP_ASGN1,
114 NODE_OP_ASGN2,
115 NODE_OP_ASGN_AND,
116 NODE_OP_ASGN_OR,
117 NODE_OP_CDECL,
118 NODE_CALL,
119 NODE_OPCALL,
120 NODE_FCALL,
121 NODE_VCALL,
122 NODE_QCALL,
123 NODE_SUPER,
124 NODE_ZSUPER,
125 NODE_LIST,
126 NODE_ZLIST,
127 NODE_HASH,
128 NODE_RETURN,
129 NODE_YIELD,
130 NODE_LVAR,
131 NODE_DVAR,
132 NODE_GVAR,
133 NODE_IVAR,
134 NODE_CONST,
135 NODE_CVAR,
136 NODE_NTH_REF,
137 NODE_BACK_REF,
138 NODE_MATCH,
139 NODE_MATCH2,
140 NODE_MATCH3,
141 NODE_INTEGER,
142 NODE_FLOAT,
143 NODE_RATIONAL,
144 NODE_IMAGINARY,
145 NODE_STR,
146 NODE_DSTR,
147 NODE_XSTR,
148 NODE_DXSTR,
149 NODE_EVSTR,
150 NODE_REGX,
151 NODE_DREGX,
152 NODE_ONCE,
153 NODE_ARGS,
154 NODE_ARGS_AUX,
155 NODE_OPT_ARG,
156 NODE_KW_ARG,
157 NODE_POSTARG,
158 NODE_ARGSCAT,
159 NODE_ARGSPUSH,
160 NODE_SPLAT,
161 NODE_BLOCK_PASS,
162 NODE_DEFN,
163 NODE_DEFS,
164 NODE_ALIAS,
165 NODE_VALIAS,
166 NODE_UNDEF,
167 NODE_CLASS,
168 NODE_MODULE,
169 NODE_SCLASS,
170 NODE_COLON2,
171 NODE_COLON3,
172 NODE_DOT2,
173 NODE_DOT3,
174 NODE_FLIP2,
175 NODE_FLIP3,
176 NODE_SELF,
177 NODE_NIL,
178 NODE_TRUE,
179 NODE_FALSE,
180 NODE_ERRINFO,
181 NODE_DEFINED,
182 NODE_POSTEXE,
183 NODE_SYM,
184 NODE_DSYM,
185 NODE_ATTRASGN,
186 NODE_LAMBDA,
187 NODE_ARYPTN,
188 NODE_HSHPTN,
189 NODE_FNDPTN,
190 NODE_ERROR,
191 NODE_LINE,
192 NODE_FILE,
193 NODE_ENCODING,
194 NODE_LAST
195};
196
197typedef struct rb_ast_id_table {
198 int size;
199 ID ids[FLEX_ARY_LEN];
201
203 int lineno;
204 int column;
206
211#define YYLTYPE rb_code_location_t
212#define YYLTYPE_IS_DECLARED 1
213
214typedef struct rb_parser_ast_token {
215 int id;
216 const char *type_name;
220
221/*
222 * Array-like object for parser
223 */
224typedef void* rb_parser_ary_data;
225
226enum rb_parser_ary_data_type {
227 PARSER_ARY_DATA_AST_TOKEN = 1,
228 PARSER_ARY_DATA_SCRIPT_LINE,
229 PARSER_ARY_DATA_NODE
230};
231
232typedef struct rb_parser_ary {
233 enum rb_parser_ary_data_type data_type;
234 rb_parser_ary_data *data;
235 long len; // current size
236 long capa; // capacity
238
239/* Header part of AST Node */
240typedef struct RNode {
241 VALUE flags;
242 rb_code_location_t nd_loc;
243 int node_id;
244} NODE;
245
246typedef struct RNode_SCOPE {
247 NODE node;
248
249 rb_ast_id_table_t *nd_tbl;
250 struct RNode *nd_body;
251 struct RNode_ARGS *nd_args;
253
254typedef struct RNode_BLOCK {
255 NODE node;
256
257 struct RNode *nd_head;
258 struct RNode *nd_end;
259 struct RNode *nd_next;
261
262typedef struct RNode_IF {
263 NODE node;
264
265 struct RNode *nd_cond;
266 struct RNode *nd_body;
267 struct RNode *nd_else;
268 rb_code_location_t if_keyword_loc;
269 rb_code_location_t then_keyword_loc;
270 rb_code_location_t end_keyword_loc;
272
273typedef struct RNode_UNLESS {
274 NODE node;
275
276 struct RNode *nd_cond;
277 struct RNode *nd_body;
278 struct RNode *nd_else;
279 rb_code_location_t keyword_loc;
280 rb_code_location_t then_keyword_loc;
281 rb_code_location_t end_keyword_loc;
283
284typedef struct RNode_CASE {
285 NODE node;
286
287 struct RNode *nd_head;
288 struct RNode *nd_body;
289 rb_code_location_t case_keyword_loc;
290 rb_code_location_t end_keyword_loc;
292
293typedef struct RNode_CASE2 {
294 NODE node;
295
296 struct RNode *nd_head;
297 struct RNode *nd_body;
298 rb_code_location_t case_keyword_loc;
299 rb_code_location_t end_keyword_loc;
301
302typedef struct RNode_CASE3 {
303 NODE node;
304
305 struct RNode *nd_head;
306 struct RNode *nd_body;
307 rb_code_location_t case_keyword_loc;
308 rb_code_location_t end_keyword_loc;
310
311typedef struct RNode_WHEN {
312 NODE node;
313
314 struct RNode *nd_head;
315 struct RNode *nd_body;
316 struct RNode *nd_next;
317 rb_code_location_t keyword_loc;
318 rb_code_location_t then_keyword_loc;
320
321typedef struct RNode_IN {
322 NODE node;
323
324 struct RNode *nd_head;
325 struct RNode *nd_body;
326 struct RNode *nd_next;
328
329typedef struct RNode_LOOP {
330 NODE node;
331
332 struct RNode *nd_cond;
333 struct RNode *nd_body;
334 long nd_state;
335 rb_code_location_t keyword_loc;
336 rb_code_location_t closing_loc;
338
339typedef struct RNode_ITER {
340 NODE node;
341
342 struct RNode *nd_body;
343 struct RNode *nd_iter;
345
346typedef struct RNode_FOR {
347 NODE node;
348
349 struct RNode *nd_body;
350 struct RNode *nd_iter;
351 rb_code_location_t for_keyword_loc;
352 rb_code_location_t in_keyword_loc;
353 rb_code_location_t do_keyword_loc;
354 rb_code_location_t end_keyword_loc;
356
357typedef struct RNode_FOR_MASGN {
358 NODE node;
359
360 struct RNode *nd_var;
362
363typedef struct RNode_EXITS {
364 NODE node;
365
366 struct RNode *nd_chain;
367 struct RNode *nd_stts;
368 rb_code_location_t keyword_loc;
370
371typedef struct RNode_RETRY {
372 NODE node;
374
375typedef struct RNode_BEGIN {
376 NODE node;
377
378 struct RNode *nd_body;
380
381typedef struct RNode_RESCUE {
382 NODE node;
383
384 struct RNode *nd_head;
385 struct RNode *nd_resq;
386 struct RNode *nd_else;
388
389typedef struct RNode_RESBODY {
390 NODE node;
391
392 struct RNode *nd_args;
393 struct RNode *nd_exc_var;
394 struct RNode *nd_body;
395 struct RNode *nd_next;
397
398typedef struct RNode_ENSURE {
399 NODE node;
400
401 struct RNode *nd_head;
402 struct RNode *nd_ensr;
404
405typedef struct {
406 NODE node;
407
408 struct RNode *nd_1st;
409 struct RNode *nd_2nd;
410 rb_code_location_t operator_loc;
412
413typedef struct RNode_MASGN {
414 NODE node;
415
416 struct RNode *nd_head;
417 struct RNode *nd_value;
418 struct RNode *nd_args;
420
421typedef struct RNode_LASGN {
422 NODE node;
423
424 ID nd_vid;
425 struct RNode *nd_value;
427
428typedef struct RNode_DASGN {
429 NODE node;
430
431 ID nd_vid;
432 struct RNode *nd_value;
434
435typedef struct RNode_GASGN {
436 NODE node;
437
438 ID nd_vid;
439 struct RNode *nd_value;
441
442typedef struct RNode_IASGN {
443 NODE node;
444
445 ID nd_vid;
446 struct RNode *nd_value;
448
449typedef struct RNode_CDECL {
450 NODE node;
451
452 ID nd_vid;
453 struct RNode *nd_value;
454 struct RNode *nd_else;
455 enum rb_parser_shareability shareability;
457
458typedef struct RNode_CVASGN {
459 NODE node;
460
461 ID nd_vid;
462 struct RNode *nd_value;
464
465typedef struct RNode_OP_ASGN1 {
466 NODE node;
467
468 struct RNode *nd_recv;
469 ID nd_mid;
470 struct RNode *nd_index;
471 struct RNode *nd_rvalue;
472 rb_code_location_t call_operator_loc;
473 rb_code_location_t opening_loc;
474 rb_code_location_t closing_loc;
475 rb_code_location_t binary_operator_loc;
477
478typedef struct RNode_OP_ASGN2 {
479 NODE node;
480
481 struct RNode *nd_recv;
482 struct RNode *nd_value;
483 ID nd_vid;
484 ID nd_mid;
485 bool nd_aid;
486 rb_code_location_t call_operator_loc;
487 rb_code_location_t message_loc;
488 rb_code_location_t binary_operator_loc;
490
491typedef struct RNode_OP_ASGN_AND {
492 NODE node;
493
494 struct RNode *nd_head;
495 struct RNode *nd_value;
497
498typedef struct RNode_OP_ASGN_OR {
499 NODE node;
500
501 struct RNode *nd_head;
502 struct RNode *nd_value;
504
505typedef struct RNode_OP_CDECL {
506 NODE node;
507
508 struct RNode *nd_head;
509 struct RNode *nd_value;
510 ID nd_aid;
511 enum rb_parser_shareability shareability;
513
514typedef struct RNode_CALL {
515 NODE node;
516
517 struct RNode *nd_recv;
518 ID nd_mid;
519 struct RNode *nd_args;
521
522typedef struct RNode_OPCALL {
523 NODE node;
524
525 struct RNode *nd_recv;
526 ID nd_mid;
527 struct RNode *nd_args;
529
530typedef struct RNode_FCALL {
531 NODE node;
532
533 ID nd_mid;
534 struct RNode *nd_args;
536
537typedef struct RNode_VCALL {
538 NODE node;
539
540 ID nd_mid;
542
543typedef struct RNode_QCALL {
544 NODE node;
545
546 struct RNode *nd_recv;
547 ID nd_mid;
548 struct RNode *nd_args;
550
551typedef struct RNode_SUPER {
552 NODE node;
553
554 struct RNode *nd_args;
555 rb_code_location_t keyword_loc;
556 rb_code_location_t lparen_loc;
557 rb_code_location_t rparen_loc;
559
560typedef struct RNode_ZSUPER {
561 NODE node;
563
564/*
565
566 Structure of LIST:
567
568 LIST +--> LIST
569 * head --> element | * head
570 * alen (length of list) | * nd_end (point to the last LIST)
571 * next -----------------+ * next
572
573*/
574typedef struct RNode_LIST {
575 NODE node;
576
577 struct RNode *nd_head; /* element */
578 union {
579 long nd_alen;
580 struct RNode *nd_end; /* Second list node has this structure */
581 } as;
582 struct RNode *nd_next; /* next list node */
584
585typedef struct RNode_ZLIST {
586 NODE node;
588
589typedef struct RNode_HASH {
590 NODE node;
591
592 struct RNode *nd_head;
593 long nd_brace;
595
596typedef struct RNode_RETURN {
597 NODE node;
598
599 struct RNode *nd_stts;
600 rb_code_location_t keyword_loc;
602
603typedef struct RNode_YIELD {
604 NODE node;
605
606 struct RNode *nd_head;
607 rb_code_location_t keyword_loc;
608 rb_code_location_t lparen_loc;
609 rb_code_location_t rparen_loc;
611
612typedef struct RNode_LVAR {
613 NODE node;
614
615 ID nd_vid;
617
618typedef struct RNode_DVAR {
619 NODE node;
620
621 ID nd_vid;
623
624typedef struct RNode_GVAR {
625 NODE node;
626
627 ID nd_vid;
629
630typedef struct RNode_IVAR {
631 NODE node;
632
633 ID nd_vid;
635
636typedef struct RNode_CONST {
637 NODE node;
638
639 ID nd_vid;
641
642typedef struct RNode_CVAR {
643 NODE node;
644
645 ID nd_vid;
647
648typedef struct RNode_NTH_REF {
649 NODE node;
650
651 long nd_nth;
653
654typedef struct RNode_BACK_REF {
655 NODE node;
656
657 long nd_nth;
659
660typedef struct RNode_MATCH2 {
661 NODE node;
662
663 struct RNode *nd_recv;
664 struct RNode *nd_value;
665 struct RNode *nd_args;
667
668typedef struct RNode_MATCH3 {
669 NODE node;
670
671 struct RNode *nd_recv;
672 struct RNode *nd_value;
674
675typedef struct RNode_INTEGER {
676 NODE node;
677
678 char *val;
679 int minus;
680 int base;
682
683typedef struct RNode_FLOAT {
684 NODE node;
685
686 char *val;
687 int minus;
689
690typedef struct RNode_RATIONAL {
691 NODE node;
692
693 char *val;
694 int minus;
695 int base;
696 int seen_point;
698
699enum rb_numeric_type {
700 integer_literal,
701 float_literal,
702 rational_literal
703};
704
705typedef struct RNode_IMAGINARY {
706 NODE node;
707
708 char *val;
709 int minus;
710 int base;
711 int seen_point;
712 enum rb_numeric_type type;
714
715typedef struct RNode_STR {
716 NODE node;
717
718 struct rb_parser_string *string;
720
721/* NODE_DSTR, NODE_DXSTR, NODE_DREGX, NODE_DSYM */
722typedef struct RNode_DSTR {
723 NODE node;
724
725 struct rb_parser_string *string;
726 union {
727 long nd_alen;
728 long nd_cflag;
729 struct RNode *nd_end; /* Second dstr node has this structure. See also RNode_LIST */
730 } as;
731 struct RNode_LIST *nd_next;
733
735
737
738typedef struct RNode_EVSTR {
739 NODE node;
740
741 struct RNode *nd_body;
742 rb_code_location_t opening_loc;
743 rb_code_location_t closing_loc;
745
746typedef struct RNode_REGX { /* also RNode_MATCH */
747 NODE node;
748
749 struct rb_parser_string *string;
750 int options;
751 rb_code_location_t opening_loc;
752 rb_code_location_t content_loc;
753 rb_code_location_t closing_loc;
755
757
758typedef struct RNode_ONCE {
759 NODE node;
760
761 struct RNode *nd_body;
763
765 NODE *pre_init;
766 NODE *post_init;
767
768 int pre_args_num; /* count of mandatory pre-arguments */
769 int post_args_num; /* count of mandatory post-arguments */
770
771 ID first_post_arg;
772
773 ID rest_arg;
774 ID block_arg;
775
776 struct RNode_KW_ARG *kw_args;
777 NODE *kw_rest_arg;
778
779 struct RNode_OPT_ARG *opt_args;
780 unsigned int no_kwarg: 1;
781 unsigned int ruby2_keywords: 1;
782 unsigned int forwarding: 1;
783};
784
785typedef struct RNode_ARGS {
786 NODE node;
787
788 struct rb_args_info nd_ainfo;
790
791typedef struct RNode_ARGS_AUX {
792 NODE node;
793
794 ID nd_pid;
795 int nd_plen;
796 struct RNode *nd_next;
798
799typedef struct RNode_OPT_ARG {
800 NODE node;
801
802 struct RNode *nd_body;
803 struct RNode_OPT_ARG *nd_next;
805
806typedef struct RNode_KW_ARG {
807 NODE node;
808
809 struct RNode *nd_body;
810 struct RNode_KW_ARG *nd_next;
812
813typedef struct RNode_POSTARG {
814 NODE node;
815
816 struct RNode *nd_1st;
817 struct RNode *nd_2nd;
819
820typedef struct RNode_ARGSCAT {
821 NODE node;
822
823 struct RNode *nd_head;
824 struct RNode *nd_body;
826
827typedef struct RNode_ARGSPUSH {
828 NODE node;
829
830 struct RNode *nd_head;
831 struct RNode *nd_body;
833
834typedef struct RNode_SPLAT {
835 NODE node;
836
837 struct RNode *nd_head;
838 rb_code_location_t operator_loc;
840
841typedef struct RNode_BLOCK_PASS {
842 NODE node;
843
844 struct RNode *nd_head;
845 struct RNode *nd_body;
846 unsigned int forwarding: 1;
847 rb_code_location_t operator_loc;
849
850typedef struct RNode_DEFN {
851 NODE node;
852
853 ID nd_mid;
854 struct RNode *nd_defn;
856
857typedef struct RNode_DEFS {
858 NODE node;
859
860 struct RNode *nd_recv;
861 ID nd_mid;
862 struct RNode *nd_defn;
864
865typedef struct RNode_ALIAS {
866 NODE node;
867
868 struct RNode *nd_1st;
869 struct RNode *nd_2nd;
870 rb_code_location_t keyword_loc;
872
873typedef struct RNode_VALIAS {
874 NODE node;
875
876 ID nd_alias;
877 ID nd_orig;
878 rb_code_location_t keyword_loc;
880
881typedef struct RNode_UNDEF {
882 NODE node;
883
884 rb_parser_ary_t *nd_undefs;
885 rb_code_location_t keyword_loc;
887
888typedef struct RNode_CLASS {
889 NODE node;
890
891 struct RNode *nd_cpath;
892 struct RNode *nd_body;
893 struct RNode *nd_super;
895
896typedef struct RNode_MODULE {
897 NODE node;
898
899 struct RNode *nd_cpath;
900 struct RNode *nd_body;
902
903typedef struct RNode_SCLASS {
904 NODE node;
905
906 struct RNode *nd_recv;
907 struct RNode *nd_body;
909
910typedef struct RNode_COLON2 {
911 NODE node;
912
913 struct RNode *nd_head;
914 ID nd_mid;
916
917typedef struct RNode_COLON3 {
918 NODE node;
919
920 ID nd_mid;
922
923/* NODE_DOT2, NODE_DOT3, NODE_FLIP2, NODE_FLIP3 */
924typedef struct RNode_DOTS {
925 NODE node;
926
927 struct RNode *nd_beg;
928 struct RNode *nd_end;
929 rb_code_location_t operator_loc;
931
932typedef struct RNode_SELF {
933 NODE node;
934
935 long nd_state; /* Default 1. See NEW_SELF. */
937
938typedef struct RNode_NIL {
939 NODE node;
941
942typedef struct RNode_TRUE {
943 NODE node;
945
946typedef struct RNode_FALSE {
947 NODE node;
949
950typedef struct RNode_ERRINFO {
951 NODE node;
953
954typedef struct RNode_DEFINED {
955 NODE node;
956
957 struct RNode *nd_head;
959
960typedef struct RNode_POSTEXE {
961 NODE node;
962
963 struct RNode *nd_body;
965
966typedef struct RNode_SYM {
967 NODE node;
968
969 struct rb_parser_string *string;
971
973
974typedef struct RNode_ATTRASGN {
975 NODE node;
976
977 struct RNode *nd_recv;
978 ID nd_mid;
979 struct RNode *nd_args;
981
982typedef struct RNode_LAMBDA {
983 NODE node;
984
985 struct RNode *nd_body;
986 rb_code_location_t operator_loc;
987 rb_code_location_t opening_loc;
988 rb_code_location_t closing_loc;
990
991typedef struct RNode_ARYPTN {
992 NODE node;
993
994 struct RNode *nd_pconst;
995 NODE *pre_args;
996 NODE *rest_arg;
997 NODE *post_args;
999
1000typedef struct RNode_HSHPTN {
1001 NODE node;
1002
1003 struct RNode *nd_pconst;
1004 struct RNode *nd_pkwargs;
1005 struct RNode *nd_pkwrestarg;
1007
1008typedef struct RNode_FNDPTN {
1009 NODE node;
1010
1011 struct RNode *nd_pconst;
1012 NODE *pre_rest_arg;
1013 NODE *args;
1014 NODE *post_rest_arg;
1016
1017typedef struct RNode_LINE {
1018 NODE node;
1020
1021typedef struct RNode_FILE {
1022 NODE node;
1023
1024 struct rb_parser_string *path;
1026
1027typedef struct RNode_ENCODING {
1028 NODE node;
1029 rb_encoding *enc;
1031
1032typedef struct RNode_ERROR {
1033 NODE node;
1035
1036#define RNODE(obj) ((NODE *)(obj))
1037
1038#define RNODE_SCOPE(node) ((rb_node_scope_t *)(node))
1039#define RNODE_BLOCK(node) ((rb_node_block_t *)(node))
1040#define RNODE_IF(node) ((rb_node_if_t *)(node))
1041#define RNODE_UNLESS(node) ((rb_node_unless_t *)(node))
1042#define RNODE_CASE(node) ((rb_node_case_t *)(node))
1043#define RNODE_CASE2(node) ((rb_node_case2_t *)(node))
1044#define RNODE_CASE3(node) ((rb_node_case3_t *)(node))
1045#define RNODE_WHEN(node) ((rb_node_when_t *)(node))
1046#define RNODE_IN(node) ((rb_node_in_t *)(node))
1047#define RNODE_WHILE(node) ((rb_node_while_t *)(node))
1048#define RNODE_UNTIL(node) ((rb_node_until_t *)(node))
1049#define RNODE_ITER(node) ((rb_node_iter_t *)(node))
1050#define RNODE_FOR(node) ((rb_node_for_t *)(node))
1051#define RNODE_FOR_MASGN(node) ((rb_node_for_masgn_t *)(node))
1052#define RNODE_BREAK(node) ((rb_node_break_t *)(node))
1053#define RNODE_NEXT(node) ((rb_node_next_t *)(node))
1054#define RNODE_REDO(node) ((rb_node_redo_t *)(node))
1055#define RNODE_RETRY(node) ((rb_node_retry_t *)(node))
1056#define RNODE_BEGIN(node) ((rb_node_begin_t *)(node))
1057#define RNODE_RESCUE(node) ((rb_node_rescue_t *)(node))
1058#define RNODE_RESBODY(node) ((rb_node_resbody_t *)(node))
1059#define RNODE_ENSURE(node) ((rb_node_ensure_t *)(node))
1060#define RNODE_AND(node) ((rb_node_and_t *)(node))
1061#define RNODE_OR(node) ((rb_node_or_t *)(node))
1062#define RNODE_MASGN(node) ((rb_node_masgn_t *)(node))
1063#define RNODE_LASGN(node) ((rb_node_lasgn_t *)(node))
1064#define RNODE_DASGN(node) ((rb_node_dasgn_t *)(node))
1065#define RNODE_GASGN(node) ((rb_node_gasgn_t *)(node))
1066#define RNODE_IASGN(node) ((rb_node_iasgn_t *)(node))
1067#define RNODE_CDECL(node) ((rb_node_cdecl_t *)(node))
1068#define RNODE_CVASGN(node) ((rb_node_cvasgn_t *)(node))
1069#define RNODE_OP_ASGN1(node) ((rb_node_op_asgn1_t *)(node))
1070#define RNODE_OP_ASGN2(node) ((rb_node_op_asgn2_t *)(node))
1071#define RNODE_OP_ASGN_AND(node) ((rb_node_op_asgn_and_t *)(node))
1072#define RNODE_OP_ASGN_OR(node) ((rb_node_op_asgn_or_t *)(node))
1073#define RNODE_OP_CDECL(node) ((rb_node_op_cdecl_t *)(node))
1074#define RNODE_CALL(node) ((rb_node_call_t *)(node))
1075#define RNODE_OPCALL(node) ((rb_node_opcall_t *)(node))
1076#define RNODE_FCALL(node) ((rb_node_fcall_t *)(node))
1077#define RNODE_VCALL(node) ((rb_node_vcall_t *)(node))
1078#define RNODE_QCALL(node) ((rb_node_qcall_t *)(node))
1079#define RNODE_SUPER(node) ((rb_node_super_t *)(node))
1080#define RNODE_ZSUPER(node) ((rb_node_zsuper_t *)(node))
1081#define RNODE_LIST(node) ((rb_node_list_t *)(node))
1082#define RNODE_ZLIST(node) ((rb_node_zlist_t *)(node))
1083#define RNODE_HASH(node) ((rb_node_hash_t *)(node))
1084#define RNODE_RETURN(node) ((rb_node_return_t *)(node))
1085#define RNODE_YIELD(node) ((rb_node_yield_t *)(node))
1086#define RNODE_LVAR(node) ((rb_node_lvar_t *)(node))
1087#define RNODE_DVAR(node) ((rb_node_dvar_t *)(node))
1088#define RNODE_GVAR(node) ((rb_node_gvar_t *)(node))
1089#define RNODE_IVAR(node) ((rb_node_ivar_t *)(node))
1090#define RNODE_CONST(node) ((rb_node_const_t *)(node))
1091#define RNODE_CVAR(node) ((rb_node_cvar_t *)(node))
1092#define RNODE_NTH_REF(node) ((rb_node_nth_ref_t *)(node))
1093#define RNODE_BACK_REF(node) ((rb_node_back_ref_t *)(node))
1094#define RNODE_MATCH(node) ((rb_node_match_t *)(node))
1095#define RNODE_MATCH2(node) ((rb_node_match2_t *)(node))
1096#define RNODE_MATCH3(node) ((rb_node_match3_t *)(node))
1097#define RNODE_INTEGER(node) ((rb_node_integer_t *)(node))
1098#define RNODE_FLOAT(node) ((rb_node_float_t *)(node))
1099#define RNODE_RATIONAL(node) ((rb_node_rational_t *)(node))
1100#define RNODE_IMAGINARY(node) ((rb_node_imaginary_t *)(node))
1101#define RNODE_STR(node) ((rb_node_str_t *)(node))
1102#define RNODE_DSTR(node) ((rb_node_dstr_t *)(node))
1103#define RNODE_XSTR(node) ((rb_node_xstr_t *)(node))
1104#define RNODE_DXSTR(node) ((rb_node_dxstr_t *)(node))
1105#define RNODE_EVSTR(node) ((rb_node_evstr_t *)(node))
1106#define RNODE_REGX(node) ((rb_node_regx_t *)(node))
1107#define RNODE_DREGX(node) ((rb_node_dregx_t *)(node))
1108#define RNODE_ONCE(node) ((rb_node_once_t *)(node))
1109#define RNODE_ARGS(node) ((rb_node_args_t *)(node))
1110#define RNODE_ARGS_AUX(node) ((rb_node_args_aux_t *)(node))
1111#define RNODE_OPT_ARG(node) ((rb_node_opt_arg_t *)(node))
1112#define RNODE_KW_ARG(node) ((rb_node_kw_arg_t *)(node))
1113#define RNODE_POSTARG(node) ((rb_node_postarg_t *)(node))
1114#define RNODE_ARGSCAT(node) ((rb_node_argscat_t *)(node))
1115#define RNODE_ARGSPUSH(node) ((rb_node_argspush_t *)(node))
1116#define RNODE_SPLAT(node) ((rb_node_splat_t *)(node))
1117#define RNODE_BLOCK_PASS(node) ((rb_node_block_pass_t *)(node))
1118#define RNODE_DEFN(node) ((rb_node_defn_t *)(node))
1119#define RNODE_DEFS(node) ((rb_node_defs_t *)(node))
1120#define RNODE_ALIAS(node) ((rb_node_alias_t *)(node))
1121#define RNODE_VALIAS(node) ((rb_node_valias_t *)(node))
1122#define RNODE_UNDEF(node) ((rb_node_undef_t *)(node))
1123#define RNODE_CLASS(node) ((rb_node_class_t *)(node))
1124#define RNODE_MODULE(node) ((rb_node_module_t *)(node))
1125#define RNODE_SCLASS(node) ((rb_node_sclass_t *)(node))
1126#define RNODE_COLON2(node) ((rb_node_colon2_t *)(node))
1127#define RNODE_COLON3(node) ((rb_node_colon3_t *)(node))
1128#define RNODE_DOT2(node) ((rb_node_dot2_t *)(node))
1129#define RNODE_DOT3(node) ((rb_node_dot3_t *)(node))
1130#define RNODE_FLIP2(node) ((rb_node_flip2_t *)(node))
1131#define RNODE_FLIP3(node) ((rb_node_flip3_t *)(node))
1132#define RNODE_SELF(node) ((rb_node_self_t *)(node))
1133#define RNODE_NIL(node) ((rb_node_nil_t *)(node))
1134#define RNODE_TRUE(node) ((rb_node_true_t *)(node))
1135#define RNODE_FALSE(node) ((rb_node_false_t *)(node))
1136#define RNODE_ERRINFO(node) ((rb_node_errinfo_t *)(node))
1137#define RNODE_DEFINED(node) ((rb_node_defined_t *)(node))
1138#define RNODE_POSTEXE(node) ((rb_node_postexe_t *)(node))
1139#define RNODE_SYM(node) ((rb_node_sym_t *)(node))
1140#define RNODE_DSYM(node) ((rb_node_dsym_t *)(node))
1141#define RNODE_ATTRASGN(node) ((rb_node_attrasgn_t *)(node))
1142#define RNODE_LAMBDA(node) ((rb_node_lambda_t *)(node))
1143#define RNODE_ARYPTN(node) ((rb_node_aryptn_t *)(node))
1144#define RNODE_HSHPTN(node) ((rb_node_hshptn_t *)(node))
1145#define RNODE_FNDPTN(node) ((rb_node_fndptn_t *)(node))
1146#define RNODE_LINE(node) ((rb_node_line_t *)(node))
1147#define RNODE_FILE(node) ((rb_node_file_t *)(node))
1148#define RNODE_ENCODING(node) ((rb_node_encoding_t *)(node))
1149
1150/* FL : 0..4: T_TYPES, 5: KEEP_WB, 6: PROMOTED, 7: FINALIZE, 8: UNUSED, 9: UNUSED, 10: EXIVAR, 11: FREEZE */
1151/* NODE_FL: 0..4: UNUSED, 5: UNUSED, 6: UNUSED, 7: NODE_FL_NEWLINE,
1152 * 8..14: nd_type,
1153 * 15..: nd_line
1154 */
1155#define NODE_FL_NEWLINE (((VALUE)1)<<7)
1156
1157#define NODE_TYPESHIFT 8
1158#define NODE_TYPEMASK (((VALUE)0x7f)<<NODE_TYPESHIFT)
1159
1160#define nd_fl_newline(n) ((n)->flags & NODE_FL_NEWLINE)
1161#define nd_set_fl_newline(n) ((n)->flags |= NODE_FL_NEWLINE)
1162#define nd_unset_fl_newline(n) ((n)->flags &= ~NODE_FL_NEWLINE)
1163
1164#define nd_type(n) ((int) ((RNODE(n)->flags & NODE_TYPEMASK)>>NODE_TYPESHIFT))
1165#define nd_set_type(n,t) \
1166 rb_node_set_type(n, t)
1167#define nd_init_type(n,t) \
1168 (n)->flags=(((n)->flags&~NODE_TYPEMASK)|((((unsigned long)(t))<<NODE_TYPESHIFT)&NODE_TYPEMASK))
1169
1170typedef struct node_buffer_struct node_buffer_t;
1171
1172#ifdef UNIVERSAL_PARSER
1173typedef struct rb_parser_config_struct rb_parser_config_t;
1174#endif
1175
1176typedef struct rb_ast_body_struct {
1177 const NODE *root;
1178 rb_parser_ary_t *script_lines;
1179 int line_count;
1180 signed int frozen_string_literal:2; /* -1: not specified, 0: false, 1: true */
1181 signed int coverage_enabled:2; /* -1: not specified, 0: false, 1: true */
1183typedef struct rb_ast_struct {
1184 node_buffer_t *node_buffer;
1185 rb_ast_body_t body;
1186#ifdef UNIVERSAL_PARSER
1187 const rb_parser_config_t *config;
1188#endif
1189} rb_ast_t;
1190
1191
1192
1193/*
1194 * Parser Interface
1195 */
1196
1197
1198typedef struct parser_params rb_parser_t;
1199#ifndef INTERNAL_IMEMO_H
1201#endif
1202
1203typedef NODE *(*rb_parser_assignable_func)(struct parser_params *p, ID id, NODE *val, const rb_code_location_t *loc);
1204
1205#ifdef UNIVERSAL_PARSER
1206typedef struct rb_parser_config_struct {
1207 /* Memory */
1208 void *(*malloc)(size_t size);
1209 void *(*calloc)(size_t number, size_t size);
1210 void *(*realloc)(void *ptr, size_t newsiz);
1211 void (*free)(void *ptr);
1212 void *(*alloc_n)(size_t nelems, size_t elemsiz);
1213 void *(*alloc)(size_t elemsiz);
1214 void *(*realloc_n)(void *ptr, size_t newelems, size_t newsiz);
1215 void *(*zalloc)(size_t elemsiz);
1216 void *(*rb_memmove)(void *dest, const void *src, size_t t, size_t n);
1217 void *(*nonempty_memcpy)(void *dest, const void *src, size_t t, size_t n);
1218 void *(*xmalloc_mul_add)(size_t x, size_t y, size_t z);
1219
1220 // VALUE rb_suppress_tracing(VALUE (*func)(VALUE), VALUE arg);
1221 VALUE (*compile_callback)(VALUE (*func)(VALUE), VALUE arg);
1222 NODE *(*reg_named_capture_assign)(struct parser_params* p, VALUE regexp, const rb_code_location_t *loc, rb_parser_assignable_func assignable);
1223
1224 /* Variable */
1225 VALUE (*attr_get)(VALUE obj, ID id);
1226
1227 /* Array */
1228 VALUE (*ary_new_from_args)(long n, ...);
1229 VALUE (*ary_unshift)(VALUE ary, VALUE item);
1230
1231 /* Symbol */
1232 ID (*make_temporary_id)(size_t n);
1233 int (*is_local_id)(ID);
1234 int (*is_attrset_id)(ID);
1235 int (*is_global_name_punct)(const int c);
1236 int (*id_type)(ID id);
1237 ID (*id_attrset)(ID);
1238 ID (*intern)(const char *name);
1239 ID (*intern2)(const char *name, long len);
1240 ID (*intern3)(const char *name, long len, rb_encoding *enc);
1241 ID (*intern_str)(VALUE str);
1242 int (*is_notop_id)(ID);
1243 int (*enc_symname_type)(const char *name, long len, rb_encoding *enc, unsigned int allowed_attrset);
1244 const char *(*id2name)(ID id);
1245 VALUE (*id2str)(ID id);
1246 VALUE (*id2sym)(ID x);
1247
1248 /* String */
1249 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 3)
1250 VALUE (*str_catf)(VALUE str, const char *format, ...);
1251 VALUE (*str_cat_cstr)(VALUE str, const char *ptr);
1252 VALUE (*str_resize)(VALUE str, long len);
1253 VALUE (*str_new)(const char *ptr, long len);
1254 VALUE (*str_new_cstr)(const char *ptr);
1255 VALUE (*str_to_interned_str)(VALUE);
1256 VALUE (*enc_str_new)(const char *ptr, long len, rb_encoding *enc);
1257 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 0)
1258 VALUE (*str_vcatf)(VALUE str, const char *fmt, va_list ap);
1259 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 1, 2)
1260 VALUE (*rb_sprintf)(const char *format, ...);
1261 char *(*rstring_ptr)(VALUE str);
1262 long (*rstring_len)(VALUE str);
1263
1264 /* Numeric */
1265 VALUE (*int2num)(int v);
1266
1267 /* IO */
1268 int (*stderr_tty_p)(void);
1269 void (*write_error_str)(VALUE mesg);
1270 VALUE (*io_write)(VALUE io, VALUE str);
1271 VALUE (*io_flush)(VALUE io);
1272 VALUE (*io_puts)(int argc, const VALUE *argv, VALUE out);
1273
1274 /* IO (Ractor) */
1275 VALUE (*debug_output_stdout)(void);
1276 VALUE (*debug_output_stderr)(void);
1277
1278 /* Encoding */
1279 int (*is_usascii_enc)(rb_encoding *enc);
1280 int (*enc_isalnum)(OnigCodePoint c, rb_encoding *enc);
1281 int (*enc_precise_mbclen)(const char *p, const char *e, rb_encoding *enc);
1282 int (*mbclen_charfound_p)(int len);
1283 int (*mbclen_charfound_len)(int len);
1284 const char *(*enc_name)(rb_encoding *enc);
1285 char *(*enc_prev_char)(const char *s, const char *p, const char *e, rb_encoding *enc);
1286 rb_encoding* (*enc_get)(VALUE obj);
1287 int (*enc_asciicompat)(rb_encoding *enc);
1288 rb_encoding *(*utf8_encoding)(void);
1289 rb_encoding *(*ascii8bit_encoding)(void);
1290 int (*enc_codelen)(int c, rb_encoding *enc);
1291 int (*enc_mbcput)(unsigned int c, void *buf, rb_encoding *enc);
1292 int (*enc_find_index)(const char *name);
1293 rb_encoding *(*enc_from_index)(int idx);
1294 int (*enc_isspace)(OnigCodePoint c, rb_encoding *enc);
1295 int (*enc_mbminlen)(rb_encoding *enc);
1296 bool (*enc_isascii)(OnigCodePoint c, rb_encoding *enc);
1297 OnigCodePoint (*enc_mbc_to_codepoint)(const char *p, const char *e, rb_encoding *enc);
1298
1299 /* Compile */
1300 // int rb_local_defined(ID id, const rb_iseq_t *iseq);
1301 int (*local_defined)(ID, const void*);
1302 // int rb_dvar_defined(ID id, const rb_iseq_t *iseq);
1303 int (*dvar_defined)(ID, const void*);
1304
1305 /* Error (Exception) */
1306 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 6, 0)
1307 VALUE (*syntax_error_append)(VALUE, VALUE, int, int, rb_encoding*, const char*, va_list);
1308 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 3)
1309 void (*raise)(VALUE exc, const char *fmt, ...);
1310 VALUE (*syntax_error_new)(void);
1311
1312 /* Eval */
1313 VALUE (*errinfo)(void);
1314 void (*set_errinfo)(VALUE err);
1315 VALUE (*make_exception)(int argc, const VALUE *argv);
1316
1317 /* GC */
1318 void (*sized_xfree)(void *x, size_t size);
1319 void *(*sized_realloc_n)(void *ptr, size_t new_count, size_t element_size, size_t old_count);
1320 void (*gc_guard)(VALUE);
1321 void (*gc_mark)(VALUE);
1322
1323 /* Re */
1324 VALUE (*reg_compile)(VALUE str, int options, const char *sourcefile, int sourceline);
1325 VALUE (*reg_check_preprocess)(VALUE str);
1326 int (*memcicmp)(const void *x, const void *y, long len);
1327
1328 /* Error */
1329 void (*compile_warn)(const char *file, int line, const char *fmt, ...) RUBYPARSER_ATTRIBUTE_FORMAT(3, 4);
1330 void (*compile_warning)(const char *file, int line, const char *fmt, ...) RUBYPARSER_ATTRIBUTE_FORMAT(3, 4);
1331 void (*bug)(const char *fmt, ...) RUBYPARSER_ATTRIBUTE_FORMAT(1, 2);
1332 void (*fatal)(const char *fmt, ...) RUBYPARSER_ATTRIBUTE_FORMAT(1, 2);
1333 VALUE (*verbose)(void);
1334 int *(*errno_ptr)(void);
1335
1336 /* VM */
1337 VALUE (*make_backtrace)(void);
1338
1339 /* Util */
1340 unsigned long (*scan_hex)(const char *start, size_t len, size_t *retlen);
1341 unsigned long (*scan_oct)(const char *start, size_t len, size_t *retlen);
1342 unsigned long (*scan_digits)(const char *str, ssize_t len, int base, size_t *retlen, int *overflow);
1343 double (*strtod)(const char *s00, char **se);
1344
1345 /* Misc */
1346 int (*rtest)(VALUE obj);
1347 int (*nil_p)(VALUE obj);
1348 VALUE qnil;
1349 VALUE qfalse;
1350 VALUE (*eArgError)(void);
1351 int (*long2int)(long);
1352
1353 /* For Ripper */
1354 int enc_coderange_7bit;
1355 int enc_coderange_unknown;
1356 VALUE (*static_id2sym)(ID id);
1357 long (*str_coderange_scan_restartable)(const char *s, const char *e, rb_encoding *enc, int *cr);
1358} rb_parser_config_t;
1359
1360#undef rb_encoding
1361#undef OnigCodePoint
1362#endif /* UNIVERSAL_PARSER */
1363
1364RUBY_SYMBOL_EXPORT_BEGIN
1365void rb_ruby_parser_free(void *ptr);
1366
1367#ifdef UNIVERSAL_PARSER
1368rb_parser_t *rb_ruby_parser_allocate(const rb_parser_config_t *config);
1369rb_parser_t *rb_ruby_parser_new(const rb_parser_config_t *config);
1370#endif
1371
1372RUBY_SYMBOL_EXPORT_END
1373
1374#endif /* RUBY_RUBYPARSER_H */
#define RBIMPL_ATTR_FORMAT(x, y, z)
Wraps (or simulates) __attribute__((format))
Definition format.h:29
#define scan_hex(s, l, e)
Old name of ruby_scan_hex.
Definition util.h:108
#define scan_oct(s, l, e)
Old name of ruby_scan_oct.
Definition util.h:85
Encoding relates APIs.
int len
Length of the buffer.
Definition io.h:8
#define strtod(s, e)
Just another name of ruby_strtod.
Definition util.h:223
Defines old _.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40