Ruby: ruby_parser.c Source File

Ruby 4.0.5p0 (2026-05-20 revision 64336ffd0ee9e1f4c05891695a3d7b49cb709721)

ruby_parser.c
1/* This is a wrapper for parse.y */
2
3#include "internal/parse.h"
4#include "internal/re.h"
5#include "internal/ruby_parser.h"
6
7#include "node.h"
8#include "rubyparser.h"
9#include "internal/error.h"
10
11#ifdef UNIVERSAL_PARSER
12
13#include "internal.h"
14#include "internal/array.h"
15#include "internal/bignum.h"
16#include "internal/compile.h"
17#include "internal/complex.h"
18#include "internal/encoding.h"
19#include "internal/gc.h"
20#include "internal/hash.h"
21#include "internal/io.h"
22#include "internal/rational.h"
23#include "internal/re.h"
24#include "internal/string.h"
25#include "internal/symbol.h"
26#include "internal/thread.h"
27
28#include "ruby/ractor.h"
29#include "ruby/ruby.h"
30#include "ruby/util.h"
31#include "internal.h"
32#include "vm_core.h"
33#include "symbol.h"
34
35#define parser_encoding const void
36
37 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 6, 0)
38static VALUE
39syntax_error_append(VALUE exc, VALUE file, int line, int column,
40 parser_encoding *enc, const char *fmt, va_list args)
41{
42 return rb_syntax_error_append(exc, file, line, column, enc, fmt, args);
43}
44
45static int
46local_defined(ID id, const void *p)
47{
48 return rb_local_defined(id, (const rb_iseq_t *)p);
49}
50
51static int
52dvar_defined(ID id, const void *p)
53{
54 return rb_dvar_defined(id, (const rb_iseq_t *)p);
55}
56
57static int
58is_usascii_enc(parser_encoding *enc)
59{
60 return rb_is_usascii_enc(enc);
61}
62
63static int
64is_local_id2(ID id)
65{
66 return is_local_id(id);
67}
68
69static int
70is_attrset_id2(ID id)
71{
72 return is_attrset_id(id);
73}
74
75static int
76is_notop_id2(ID id)
77{
78 return is_notop_id(id);
79}
80
81static VALUE
82enc_str_new(const char *ptr, long len, parser_encoding *enc)
83{
84 return rb_enc_str_new(ptr, len, enc);
85}
86
87static int
88enc_isalnum(OnigCodePoint c, parser_encoding *enc)
89{
90 return rb_enc_isalnum(c, enc);
91}
92
93static int
94enc_precise_mbclen(const char *p, const char *e, parser_encoding *enc)
95{
96 return rb_enc_precise_mbclen(p, e, enc);
97}
98
99static int
100mbclen_charfound_p(int len)
101{
102 return MBCLEN_CHARFOUND_P(len);
103}
104
105static int
106mbclen_charfound_len(int len)
107{
108 return MBCLEN_CHARFOUND_LEN(len);
109}
110
111static const char *
112enc_name(parser_encoding *enc)
113{
114 return rb_enc_name(enc);
115}
116
117static char *
118enc_prev_char(const char *s, const char *p, const char *e, parser_encoding *enc)
119{
120 return rb_enc_prev_char(s, p, e, enc);
121}
122
123static parser_encoding *
124enc_get(VALUE obj)
125{
126 return rb_enc_get(obj);
127}
128
129static int
130enc_asciicompat(parser_encoding *enc)
131{
132 return rb_enc_asciicompat(enc);
133}
134
135static parser_encoding *
136utf8_encoding(void)
137{
138 return rb_utf8_encoding();
139}
140
141static parser_encoding *
142ascii8bit_encoding(void)
143{
144 return rb_ascii8bit_encoding();
145}
146
147static int
148enc_codelen(int c, parser_encoding *enc)
149{
150 return rb_enc_codelen(c, enc);
151}
152
153static int
154enc_mbcput(unsigned int c, void *buf, parser_encoding *enc)
155{
156 return rb_enc_mbcput(c, buf, enc);
157}
158
159static parser_encoding *
160enc_from_index(int idx)
161{
162 return rb_enc_from_index(idx);
163}
164
165static int
166enc_isspace(OnigCodePoint c, parser_encoding *enc)
167{
168 return rb_enc_isspace(c, enc);
169}
170
171static ID
172intern3(const char *name, long len, parser_encoding *enc)
173{
174 return rb_intern3(name, len, enc);
175}
176
177static int
178enc_symname_type(const char *name, long len, parser_encoding *enc, unsigned int allowed_attrset)
179{
180 return rb_enc_symname_type(name, len, enc, allowed_attrset);
181}
182
183typedef struct {
184 struct parser_params *parser;
185 rb_encoding *enc;
186 NODE *succ_block;
187 const rb_code_location_t *loc;
188 rb_parser_assignable_func assignable;
189} reg_named_capture_assign_t;
190
191static int
192reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
193 int back_num, int *back_refs, OnigRegex regex, void *arg0)
194{
195 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
196 struct parser_params* p = arg->parser;
197 rb_encoding *enc = arg->enc;
198 const rb_code_location_t *loc = arg->loc;
199 long len = name_end - name;
200 const char *s = (const char *)name;
201
202 return rb_reg_named_capture_assign_iter_impl(p, s, len, enc, &arg->succ_block, loc, arg->assignable);
203}
204
205static NODE *
206reg_named_capture_assign(struct parser_params* p, VALUE regexp, const rb_code_location_t *loc,
207 rb_parser_assignable_func assignable)
208{
209 reg_named_capture_assign_t arg;
210
211 arg.parser = p;
212 arg.enc = rb_enc_get(regexp);
213 arg.succ_block = 0;
214 arg.loc = loc;
215 arg.assignable = assignable;
216 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
217
218 if (!arg.succ_block) return 0;
219 return RNODE_BLOCK(arg.succ_block)->nd_next;
220}
221
222static int
223rtest(VALUE obj)
224{
225 return (int)RB_TEST(obj);
226}
227
228static int
229nil_p(VALUE obj)
230{
231 return (int)NIL_P(obj);
232}
233
234static VALUE
235syntax_error_new(void)
236{
237 return rb_class_new_instance(0, 0, rb_eSyntaxError);
238}
239
240static void *
241memmove2(void *dest, const void *src, size_t t, size_t n)
242{
243 return memmove(dest, src, rbimpl_size_mul_or_raise(t, n));
244}
245
246static void *
247nonempty_memcpy(void *dest, const void *src, size_t t, size_t n)
248{
249 return ruby_nonempty_memcpy(dest, src, rbimpl_size_mul_or_raise(t, n));
250}
251
252static VALUE
253ruby_verbose2(void)
254{
255 return ruby_verbose;
256}
257
258static int *
259rb_errno_ptr2(void)
260{
261 return rb_errno_ptr();
262}
263
264static void *
265zalloc(size_t elemsiz)
266{
267 return ruby_xcalloc(1, elemsiz);
268}
269
270static void
271gc_guard(VALUE obj)
272{
273 RB_GC_GUARD(obj);
274}
275
276static VALUE
277arg_error(void)
278{
279 return rb_eArgError;
280}
281
282static VALUE
283static_id2sym(ID id)
284{
285 return (((VALUE)(id)<<RUBY_SPECIAL_SHIFT)|SYMBOL_FLAG);
286}
287
288static long
289str_coderange_scan_restartable(const char *s, const char *e, parser_encoding *enc, int *cr)
290{
291 return rb_str_coderange_scan_restartable(s, e, enc, cr);
292}
293
294static int
295enc_mbminlen(parser_encoding *enc)
296{
297 return rb_enc_mbminlen(enc);
298}
299
300static bool
301enc_isascii(OnigCodePoint c, parser_encoding *enc)
302{
303 return rb_enc_isascii(c, enc);
304}
305
306static OnigCodePoint
307enc_mbc_to_codepoint(const char *p, const char *e, parser_encoding *enc)
308{
309 const OnigUChar *up = RBIMPL_CAST((const OnigUChar *)p);
310 const OnigUChar *ue = RBIMPL_CAST((const OnigUChar *)e);
311
312 return ONIGENC_MBC_TO_CODE((rb_encoding *)enc, up, ue);
313}
314
315extern VALUE rb_eArgError;
316
317static const rb_parser_config_t rb_global_parser_config = {
318 .malloc = ruby_xmalloc,
319 .calloc = ruby_xcalloc,
320 .realloc = ruby_xrealloc,
321 .free = ruby_xfree,
322 .alloc_n = ruby_xmalloc2,
323 .alloc = ruby_xmalloc,
324 .realloc_n = ruby_xrealloc2,
325 .zalloc = zalloc,
326 .rb_memmove = memmove2,
327 .nonempty_memcpy = nonempty_memcpy,
328 .xmalloc_mul_add = rb_xmalloc_mul_add,
329
330 .compile_callback = rb_suppress_tracing,
331 .reg_named_capture_assign = reg_named_capture_assign,
332
333 .attr_get = rb_attr_get,
334
335 .ary_new_from_args = rb_ary_new_from_args,
336 .ary_unshift = rb_ary_unshift,
337
338 .make_temporary_id = rb_make_temporary_id,
339 .is_local_id = is_local_id2,
340 .is_attrset_id = is_attrset_id2,
341 .is_global_name_punct = is_global_name_punct,
342 .id_type = id_type,
343 .id_attrset = rb_id_attrset,
344 .intern = rb_intern,
345 .intern2 = rb_intern2,
346 .intern3 = intern3,
347 .intern_str = rb_intern_str,
348 .is_notop_id = is_notop_id2,
349 .enc_symname_type = enc_symname_type,
350 .id2name = rb_id2name,
351 .id2str = rb_id2str,
352 .id2sym = rb_id2sym,
353
354 .str_catf = rb_str_catf,
355 .str_cat_cstr = rb_str_cat_cstr,
356 .str_resize = rb_str_resize,
357 .str_new = rb_str_new,
358 .str_new_cstr = rb_str_new_cstr,
359 .str_to_interned_str = rb_str_to_interned_str,
360 .enc_str_new = enc_str_new,
361 .str_vcatf = rb_str_vcatf,
362 .rb_sprintf = rb_sprintf,
363 .rstring_ptr = RSTRING_PTR,
364 .rstring_len = RSTRING_LEN,
365
366 .int2num = rb_int2num_inline,
367
368 .stderr_tty_p = rb_stderr_tty_p,
369 .write_error_str = rb_write_error_str,
370 .io_write = rb_io_write,
371 .io_flush = rb_io_flush,
372 .io_puts = rb_io_puts,
373
374 .debug_output_stdout = rb_ractor_stdout,
375 .debug_output_stderr = rb_ractor_stderr,
376
377 .is_usascii_enc = is_usascii_enc,
378 .enc_isalnum = enc_isalnum,
379 .enc_precise_mbclen = enc_precise_mbclen,
380 .mbclen_charfound_p = mbclen_charfound_p,
381 .mbclen_charfound_len = mbclen_charfound_len,
382 .enc_name = enc_name,
383 .enc_prev_char = enc_prev_char,
384 .enc_get = enc_get,
385 .enc_asciicompat = enc_asciicompat,
386 .utf8_encoding = utf8_encoding,
387 .ascii8bit_encoding = ascii8bit_encoding,
388 .enc_codelen = enc_codelen,
389 .enc_mbcput = enc_mbcput,
390 .enc_find_index = rb_enc_find_index,
391 .enc_from_index = enc_from_index,
392 .enc_isspace = enc_isspace,
393 .enc_coderange_7bit = ENC_CODERANGE_7BIT,
394 .enc_coderange_unknown = ENC_CODERANGE_UNKNOWN,
395 .enc_mbminlen = enc_mbminlen,
396 .enc_isascii = enc_isascii,
397 .enc_mbc_to_codepoint = enc_mbc_to_codepoint,
398
399 .local_defined = local_defined,
400 .dvar_defined = dvar_defined,
401
402 .syntax_error_append = syntax_error_append,
403 .raise = rb_raise,
404 .syntax_error_new = syntax_error_new,
405
406 .errinfo = rb_errinfo,
407 .set_errinfo = rb_set_errinfo,
408 .make_exception = rb_make_exception,
409
410 .sized_xfree = ruby_sized_xfree,
411 .sized_realloc_n = ruby_sized_realloc_n,
412 .gc_guard = gc_guard,
413 .gc_mark = rb_gc_mark,
414
415 .reg_compile = rb_reg_compile,
416 .reg_check_preprocess = rb_reg_check_preprocess,
417 .memcicmp = rb_memcicmp,
418
419 .compile_warn = rb_compile_warn,
420 .compile_warning = rb_compile_warning,
421 .bug = rb_bug,
422 .fatal = rb_fatal,
423 .verbose = ruby_verbose2,
424 .errno_ptr = rb_errno_ptr2,
425
426 .make_backtrace = rb_make_backtrace,
427
428 .scan_hex = ruby_scan_hex,
429 .scan_oct = ruby_scan_oct,
430 .scan_digits = ruby_scan_digits,
431 .strtod = ruby_strtod,
432
433 .rtest = rtest,
434 .nil_p = nil_p,
435 .qnil = Qnil,
436 .qfalse = Qfalse,
437 .eArgError = arg_error,
438 .long2int = rb_long2int,
439
440 /* For Ripper */
441 .static_id2sym = static_id2sym,
442 .str_coderange_scan_restartable = str_coderange_scan_restartable,
443};
444#endif
445
446enum lex_type {
447 lex_type_str,
448 lex_type_io,
449 lex_type_array,
450 lex_type_generic,
451};
452
453 struct ruby_parser {
454 rb_parser_t *parser_params;
455 enum lex_type type;
456 union {
457 struct lex_pointer_string lex_str;
458 struct {
459 VALUE file;
460 } lex_io;
461 struct {
462 VALUE ary;
463 } lex_array;
464 } data;
465};
466
467static void
468parser_mark(void *ptr)
469{
470 struct ruby_parser *parser = (struct ruby_parser*)ptr;
471 rb_ruby_parser_mark(parser->parser_params);
472
473 switch (parser->type) {
474 case lex_type_str:
475 rb_gc_mark(parser->data.lex_str.str);
476 break;
477 case lex_type_io:
478 rb_gc_mark(parser->data.lex_io.file);
479 break;
480 case lex_type_array:
481 rb_gc_mark(parser->data.lex_array.ary);
482 break;
483 case lex_type_generic:
484 /* noop. Caller of rb_parser_compile_generic should mark the objects. */
485 break;
486 }
487}
488
489static void
490parser_free(void *ptr)
491{
492 struct ruby_parser *parser = (struct ruby_parser*)ptr;
493 rb_ruby_parser_free(parser->parser_params);
494 xfree(parser);
495}
496
497static size_t
498parser_memsize(const void *ptr)
499{
500 struct ruby_parser *parser = (struct ruby_parser*)ptr;
501 return rb_ruby_parser_memsize(parser->parser_params);
502}
503
504static const rb_data_type_t ruby_parser_data_type = {
505 "parser",
506 {
507 parser_mark,
508 parser_free,
509 parser_memsize,
510 },
511 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
512};
513
514#ifdef UNIVERSAL_PARSER
515const rb_parser_config_t *
516rb_ruby_parser_config(void)
517{
518 return &rb_global_parser_config;
519}
520
521rb_parser_t *
522rb_parser_params_new(void)
523{
524 return rb_ruby_parser_new(&rb_global_parser_config);
525}
526#else
527rb_parser_t *
528rb_parser_params_new(void)
529{
530 return rb_ruby_parser_new();
531}
532#endif /* UNIVERSAL_PARSER */
533
534 VALUE
535rb_parser_new(void)
536{
537 struct ruby_parser *parser;
538 rb_parser_t *parser_params;
539
540 /*
541 * Create parser_params ahead of vparser because
542 * rb_ruby_parser_new can run GC so if create vparser
543 * first, parser_mark tries to mark not initialized parser_params.
544 */
545 parser_params = rb_parser_params_new();
546 VALUE vparser = TypedData_Make_Struct(0, struct ruby_parser,
547 &ruby_parser_data_type, parser);
548 parser->parser_params = parser_params;
549
550 return vparser;
551}
552
553void
554rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
555{
556 struct ruby_parser *parser;
557
558 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
559 rb_ruby_parser_set_options(parser->parser_params, print, loop, chomp, split);
560}
561
562 VALUE
563rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
564{
565 struct ruby_parser *parser;
566
567 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
568 rb_ruby_parser_set_context(parser->parser_params, base, main);
569 return vparser;
570}
571
572void
573rb_parser_set_script_lines(VALUE vparser)
574{
575 struct ruby_parser *parser;
576
577 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
578 rb_ruby_parser_set_script_lines(parser->parser_params);
579}
580
581void
582rb_parser_error_tolerant(VALUE vparser)
583{
584 struct ruby_parser *parser;
585
586 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
587 rb_ruby_parser_error_tolerant(parser->parser_params);
588}
589
590void
591rb_parser_keep_tokens(VALUE vparser)
592{
593 struct ruby_parser *parser;
594
595 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
596 rb_ruby_parser_keep_tokens(parser->parser_params);
597}
598
599rb_parser_string_t *
600rb_parser_lex_get_str(struct parser_params *p, struct lex_pointer_string *ptr_str)
601{
602 char *beg, *end, *start;
603 long len;
604 VALUE s = ptr_str->str;
605
606 beg = RSTRING_PTR(s);
607 len = RSTRING_LEN(s);
608 start = beg;
609 if (ptr_str->ptr) {
610 if (len == ptr_str->ptr) return 0;
611 beg += ptr_str->ptr;
612 len -= ptr_str->ptr;
613 }
614 end = memchr(beg, '\n', len);
615 if (end) len = ++end - beg;
616 ptr_str->ptr += len;
617 return rb_str_to_parser_string(p, rb_str_subseq(s, beg - start, len));
618}
619
620static rb_parser_string_t *
621lex_get_str(struct parser_params *p, rb_parser_input_data input, int line_count)
622{
623 return rb_parser_lex_get_str(p, (struct lex_pointer_string *)input);
624}
625
626static void parser_aset_script_lines_for(VALUE path, rb_parser_ary_t *lines);
627
628static rb_ast_t*
629parser_compile(rb_parser_t *p, rb_parser_lex_gets_func *gets, VALUE fname, rb_parser_input_data input, int line)
630{
631 rb_ast_t *ast = rb_parser_compile(p, gets, fname, input, line);
632 parser_aset_script_lines_for(fname, ast->body.script_lines);
633 return ast;
634}
635
636static rb_ast_t*
637parser_compile_string0(struct ruby_parser *parser, VALUE fname, VALUE s, int line)
638{
639 VALUE str = rb_str_new_frozen(s);
640
641 parser->type = lex_type_str;
642 parser->data.lex_str.str = str;
643 parser->data.lex_str.ptr = 0;
644
645 return parser_compile(parser->parser_params, lex_get_str, fname, (rb_parser_input_data)&parser->data, line);
646}
647
648static rb_encoding *
649must_be_ascii_compatible(VALUE s)
650{
651 rb_encoding *enc = rb_enc_get(s);
652 if (!rb_enc_asciicompat(enc)) {
653 rb_raise(rb_eArgError, "invalid source encoding");
654 }
655 return enc;
656}
657
658static rb_ast_t*
659parser_compile_string_path(struct ruby_parser *parser, VALUE f, VALUE s, int line)
660{
661 must_be_ascii_compatible(s);
662 return parser_compile_string0(parser, f, s, line);
663}
664
665static rb_ast_t*
666parser_compile_string(struct ruby_parser *parser, const char *f, VALUE s, int line)
667{
668 return parser_compile_string_path(parser, rb_filesystem_str_new_cstr(f), s, line);
669}
670
671 VALUE rb_io_gets_internal(VALUE io);
672
673static rb_parser_string_t *
674lex_io_gets(struct parser_params *p, rb_parser_input_data input, int line_count)
675{
676 VALUE io = (VALUE)input;
677 VALUE line = rb_io_gets_internal(io);
678 if (NIL_P(line)) return 0;
679 return rb_str_to_parser_string(p, line);
680}
681
682static rb_parser_string_t *
683lex_gets_array(struct parser_params *p, rb_parser_input_data data, int index)
684{
685 VALUE array = (VALUE)data;
686 VALUE str = rb_ary_entry(array, index);
687 if (!NIL_P(str)) {
688 StringValue(str);
689 if (!rb_enc_asciicompat(rb_enc_get(str))) {
690 rb_raise(rb_eArgError, "invalid source encoding");
691 }
692 return rb_str_to_parser_string(p, str);
693 }
694 else {
695 return 0;
696 }
697}
698
699static rb_ast_t*
700parser_compile_file_path(struct ruby_parser *parser, VALUE fname, VALUE file, int start)
701{
702 parser->type = lex_type_io;
703 parser->data.lex_io.file = file;
704
705 return parser_compile(parser->parser_params, lex_io_gets, fname, (rb_parser_input_data)file, start);
706}
707
708static rb_ast_t*
709parser_compile_array(struct ruby_parser *parser, VALUE fname, VALUE array, int start)
710{
711 parser->type = lex_type_array;
712 parser->data.lex_array.ary = array;
713
714 return parser_compile(parser->parser_params, lex_gets_array, fname, (rb_parser_input_data)array, start);
715}
716
717static rb_ast_t*
718parser_compile_generic(struct ruby_parser *parser, rb_parser_lex_gets_func *lex_gets, VALUE fname, VALUE input, int start)
719{
720 parser->type = lex_type_generic;
721
722 return parser_compile(parser->parser_params, lex_gets, fname, (rb_parser_input_data)input, start);
723}
724
725static void
726ast_free(void *ptr)
727{
728 rb_ast_t *ast = (rb_ast_t *)ptr;
729 rb_ast_free(ast);
730}
731
732static const rb_data_type_t ast_data_type = {
733 "AST",
734 {
735 NULL,
736 ast_free,
737 NULL, // No dsize() because this object does not appear in ObjectSpace.
738 },
739 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
740};
741
742static VALUE
743ast_alloc(void)
744{
745 return TypedData_Wrap_Struct(0, &ast_data_type, NULL);
746}
747
748 VALUE
749rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
750{
751 struct ruby_parser *parser;
752 VALUE ast_value = ast_alloc();
753
754 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
755 DATA_PTR(ast_value) = parser_compile_file_path(parser, fname, file, start);
756 RB_GC_GUARD(vparser);
757
758 return ast_value;
759}
760
761 VALUE
762rb_parser_compile_array(VALUE vparser, VALUE fname, VALUE array, int start)
763{
764 struct ruby_parser *parser;
765 VALUE ast_value = ast_alloc();
766
767 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
768 DATA_PTR(ast_value) = parser_compile_array(parser, fname, array, start);
769 RB_GC_GUARD(vparser);
770
771 return ast_value;
772}
773
774 VALUE
775rb_parser_compile_generic(VALUE vparser, rb_parser_lex_gets_func *lex_gets, VALUE fname, VALUE input, int start)
776{
777 struct ruby_parser *parser;
778 VALUE ast_value = ast_alloc();
779
780 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
781 DATA_PTR(ast_value) = parser_compile_generic(parser, lex_gets, fname, input, start);
782 RB_GC_GUARD(vparser);
783
784 return ast_value;
785}
786
787 VALUE
788rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
789{
790 struct ruby_parser *parser;
791 VALUE ast_value = ast_alloc();
792
793 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
794 DATA_PTR(ast_value) = parser_compile_string(parser, f, s, line);
795 RB_GC_GUARD(vparser);
796
797 return ast_value;
798}
799
800 VALUE
801rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
802{
803 struct ruby_parser *parser;
804 VALUE ast_value = ast_alloc();
805
806 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
807 DATA_PTR(ast_value) = parser_compile_string_path(parser, f, s, line);
808 RB_GC_GUARD(vparser);
809
810 return ast_value;
811}
812
813 VALUE
814rb_parser_encoding(VALUE vparser)
815{
816 struct ruby_parser *parser;
817
818 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
819 return rb_enc_from_encoding(rb_ruby_parser_encoding(parser->parser_params));
820}
821
822 VALUE
823rb_parser_end_seen_p(VALUE vparser)
824{
825 struct ruby_parser *parser;
826
827 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
828 return RBOOL(rb_ruby_parser_end_seen_p(parser->parser_params));
829}
830
831 VALUE
832rb_parser_set_yydebug(VALUE vparser, VALUE flag)
833{
834 struct ruby_parser *parser;
835
836 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
837 rb_ruby_parser_set_yydebug(parser->parser_params, RTEST(flag));
838 return flag;
839}
840
841void
842rb_set_script_lines_for(VALUE vparser, VALUE path)
843{
844 struct ruby_parser *parser;
845 VALUE hash;
846 ID script_lines;
847 CONST_ID(script_lines, "SCRIPT_LINES__");
848 if (!rb_const_defined_at(rb_cObject, script_lines)) return;
849 hash = rb_const_get_at(rb_cObject, script_lines);
850 if (RB_TYPE_P(hash, T_HASH)) {
851 rb_hash_aset(hash, path, Qtrue);
852 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
853 rb_ruby_parser_set_script_lines(parser->parser_params);
854 }
855}
856
857 VALUE
858rb_parser_build_script_lines_from(rb_parser_ary_t *lines)
859{
860 int i;
861 if (!lines) return Qnil;
862 if (lines->data_type != PARSER_ARY_DATA_SCRIPT_LINE) {
863 rb_bug("unexpected rb_parser_ary_data_type (%d) for script lines", lines->data_type);
864 }
865 VALUE script_lines = rb_ary_new_capa(lines->len);
866 for (i = 0; i < lines->len; i++) {
867 rb_parser_string_t *str = (rb_parser_string_t *)lines->data[i];
868 rb_ary_push(script_lines, rb_enc_str_new(str->ptr, str->len, str->enc));
869 }
870 return script_lines;
871}
872
873 VALUE
874rb_str_new_parser_string(rb_parser_string_t *str)
875{
876 VALUE string = rb_enc_literal_str(str->ptr, str->len, str->enc);
877 rb_enc_str_coderange(string);
878 return string;
879}
880
881 VALUE
882rb_str_new_mutable_parser_string(rb_parser_string_t *str)
883{
884 return rb_enc_str_new(str->ptr, str->len, str->enc);
885}
886
887static VALUE
888negative_numeric(VALUE val)
889{
890 if (FIXNUM_P(val)) {
891 return LONG2FIX(-FIX2LONG(val));
892 }
893 if (SPECIAL_CONST_P(val)) {
894#if USE_FLONUM
895 if (FLONUM_P(val)) {
896 return DBL2NUM(-RFLOAT_VALUE(val));
897 }
898#endif
899 goto unknown;
900 }
901 switch (BUILTIN_TYPE(val)) {
902 case T_BIGNUM:
903 BIGNUM_NEGATE(val);
904 val = rb_big_norm(val);
905 break;
906 case T_RATIONAL:
907 RATIONAL_SET_NUM(val, negative_numeric(RRATIONAL(val)->num));
908 break;
909 case T_COMPLEX:
910 RCOMPLEX_SET_REAL(val, negative_numeric(RCOMPLEX(val)->real));
911 RCOMPLEX_SET_IMAG(val, negative_numeric(RCOMPLEX(val)->imag));
912 break;
913 case T_FLOAT:
914 val = DBL2NUM(-RFLOAT_VALUE(val));
915 break;
916 unknown:
917 default:
918 rb_bug("unknown literal type (%s) passed to negative_numeric",
919 rb_builtin_class_name(val));
920 break;
921 }
922 return val;
923}
924
925static VALUE
926integer_value(const char *val, int base)
927{
928 return rb_cstr_to_inum(val, base, FALSE);
929}
930
931static VALUE
932rational_value(const char *node_val, int base, int seen_point)
933{
934 VALUE lit;
935 char* val = strdup(node_val);
936 if (seen_point > 0) {
937 int len = (int)(strlen(val));
938 char *point = &val[seen_point];
939 size_t fraclen = len-seen_point-1;
940 memmove(point, point+1, fraclen+1);
941
942 lit = rb_rational_new(integer_value(val, base), rb_int_positive_pow(10, fraclen));
943 }
944 else {
945 lit = rb_rational_raw1(integer_value(val, base));
946 }
947
948 free(val);
949
950 return lit;
951}
952
953 VALUE
954rb_node_integer_literal_val(const NODE *n)
955{
956 const rb_node_integer_t *node = RNODE_INTEGER(n);
957 VALUE val = integer_value(node->val, node->base);
958 if (node->minus) {
959 val = negative_numeric(val);
960 }
961 return val;
962}
963
964 VALUE
965rb_node_float_literal_val(const NODE *n)
966{
967 const rb_node_float_t *node = RNODE_FLOAT(n);
968 double d = strtod(node->val, 0);
969 if (node->minus) {
970 d = -d;
971 }
972 VALUE val = DBL2NUM(d);
973 return val;
974}
975
976 VALUE
977rb_node_rational_literal_val(const NODE *n)
978{
979 VALUE lit;
980 const rb_node_rational_t *node = RNODE_RATIONAL(n);
981
982 lit = rational_value(node->val, node->base, node->seen_point);
983
984 if (node->minus) {
985 lit = negative_numeric(lit);
986 }
987
988 return lit;
989}
990
991 VALUE
992rb_node_imaginary_literal_val(const NODE *n)
993{
994 VALUE lit;
995 const rb_node_imaginary_t *node = RNODE_IMAGINARY(n);
996
997 enum rb_numeric_type type = node->type;
998
999 switch (type) {
1000 case integer_literal:
1001 lit = integer_value(node->val, node->base);
1002 break;
1003 case float_literal:{
1004 double d = strtod(node->val, 0);
1005 lit = DBL2NUM(d);
1006 break;
1007 }
1008 case rational_literal:
1009 lit = rational_value(node->val, node->base, node->seen_point);
1010 break;
1011 default:
1012 rb_bug("unreachable");
1013 }
1014
1015 lit = rb_complex_raw(INT2FIX(0), lit);
1016
1017 if (node->minus) {
1018 lit = negative_numeric(lit);
1019 }
1020 return lit;
1021}
1022
1023 VALUE
1024rb_node_str_string_val(const NODE *node)
1025{
1026 rb_parser_string_t *str = RNODE_STR(node)->string;
1027 return rb_str_new_parser_string(str);
1028}
1029
1030 VALUE
1031rb_node_sym_string_val(const NODE *node)
1032{
1033 rb_parser_string_t *str = RNODE_SYM(node)->string;
1034 return ID2SYM(rb_intern3(str->ptr, str->len, str->enc));
1035}
1036
1037 VALUE
1038rb_node_dstr_string_val(const NODE *node)
1039{
1040 rb_parser_string_t *str = RNODE_DSTR(node)->string;
1041 return str ? rb_str_new_parser_string(str) : Qnil;
1042}
1043
1044 VALUE
1045rb_node_dregx_string_val(const NODE *node)
1046{
1047 rb_parser_string_t *str = RNODE_DREGX(node)->string;
1048 return rb_str_new_parser_string(str);
1049}
1050
1051 VALUE
1052rb_node_regx_string_val(const NODE *node)
1053{
1054 rb_node_regx_t *node_reg = RNODE_REGX(node);
1055 rb_parser_string_t *string = node_reg->string;
1056 VALUE str = rb_enc_str_new(string->ptr, string->len, string->enc);
1057
1058 return rb_reg_compile(str, node_reg->options, NULL, 0);
1059}
1060
1061 VALUE
1062rb_node_line_lineno_val(const NODE *node)
1063{
1064 return INT2FIX(node->nd_loc.beg_pos.lineno);
1065}
1066
1067 VALUE
1068rb_node_file_path_val(const NODE *node)
1069{
1070 return rb_str_new_parser_string(RNODE_FILE(node)->path);
1071}
1072
1073 VALUE
1074rb_node_encoding_val(const NODE *node)
1075{
1076 return rb_enc_from_encoding(RNODE_ENCODING(node)->enc);
1077}
1078
1079static void
1080parser_aset_script_lines_for(VALUE path, rb_parser_ary_t *lines)
1081{
1082 VALUE hash, script_lines;
1083 ID script_lines_id;
1084 if (NIL_P(path) || !lines) return;
1085 CONST_ID(script_lines_id, "SCRIPT_LINES__");
1086 if (!rb_const_defined_at(rb_cObject, script_lines_id)) return;
1087 hash = rb_const_get_at(rb_cObject, script_lines_id);
1088 if (!RB_TYPE_P(hash, T_HASH)) return;
1089 if (rb_hash_lookup(hash, path) == Qnil) return;
1090 script_lines = rb_parser_build_script_lines_from(lines);
1091 rb_hash_aset(hash, path, script_lines);
1092}
1093
1094 VALUE
1095rb_ruby_ast_new(const NODE *const root)
1096{
1097 rb_ast_t *ast;
1098 VALUE ast_value = TypedData_Make_Struct(0, rb_ast_t, &ast_data_type, ast);
1099#ifdef UNIVERSAL_PARSER
1100 ast->config = &rb_global_parser_config;
1101#endif
1102 ast->body = (rb_ast_body_t){
1103 .root = root,
1104 .frozen_string_literal = -1,
1105 .coverage_enabled = -1,
1106 .script_lines = NULL,
1107 .line_count = 0,
1108 };
1109 return ast_value;
1110}
1111
1112rb_ast_t *
1113rb_ruby_ast_data_get(VALUE ast_value)
1114{
1115 rb_ast_t *ast;
1116 if (NIL_P(ast_value)) return NULL;
1117 TypedData_Get_Struct(ast_value, rb_ast_t, &ast_data_type, ast);
1118 return ast;
1119}
RBIMPL_ATTR_FORMAT
#define RBIMPL_ATTR_FORMAT(x, y, z)
Wraps (or simulates) __attribute__((format)).
Definition format.h:29
T_COMPLEX
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
Definition value_type.h:59
ENC_CODERANGE_7BIT
#define ENC_CODERANGE_7BIT
Old name of RUBY_ENC_CODERANGE_7BIT.
Definition coderange.h:180
RFLOAT_VALUE
#define RFLOAT_VALUE
Old name of rb_float_value.
Definition double.h:28
xfree
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
INT2FIX
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
T_FLOAT
#define T_FLOAT
Old name of RUBY_T_FLOAT.
Definition value_type.h:64
ID2SYM
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
T_BIGNUM
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
Definition value_type.h:57
SPECIAL_CONST_P
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
Definition special_consts.h:56
ENC_CODERANGE_UNKNOWN
#define ENC_CODERANGE_UNKNOWN
Old name of RUBY_ENC_CODERANGE_UNKNOWN.
Definition coderange.h:179
LONG2FIX
#define LONG2FIX
Old name of RB_INT2FIX.
Definition long.h:49
SYMBOL_FLAG
#define SYMBOL_FLAG
Old name of RUBY_SYMBOL_FLAG.
Definition special_consts.h:69
T_RATIONAL
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
Definition value_type.h:76
T_HASH
#define T_HASH
Old name of RUBY_T_HASH.
Definition value_type.h:65
MBCLEN_CHARFOUND_LEN
#define MBCLEN_CHARFOUND_LEN(ret)
Old name of ONIGENC_MBCLEN_CHARFOUND_LEN.
Definition encoding.h:517
FLONUM_P
#define FLONUM_P
Old name of RB_FLONUM_P.
Definition special_consts.h:67
Qtrue
#define Qtrue
Old name of RUBY_Qtrue.
Definition special_consts.h:61
Qnil
#define Qnil
Old name of RUBY_Qnil.
Definition special_consts.h:60
Qfalse
#define Qfalse
Old name of RUBY_Qfalse.
Definition special_consts.h:59
FIX2LONG
#define FIX2LONG
Old name of RB_FIX2LONG.
Definition long.h:46
NIL_P
#define NIL_P
Old name of RB_NIL_P.
Definition special_consts.h:55
MBCLEN_CHARFOUND_P
#define MBCLEN_CHARFOUND_P(ret)
Old name of ONIGENC_MBCLEN_CHARFOUND_P.
Definition encoding.h:516
DBL2NUM
#define DBL2NUM
Old name of rb_float_new.
Definition double.h:29
BUILTIN_TYPE
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
FIXNUM_P
#define FIXNUM_P
Old name of RB_FIXNUM_P.
Definition special_consts.h:53
CONST_ID
#define CONST_ID
Old name of RUBY_CONST_ID.
Definition symbol.h:47
ruby_verbose
#define ruby_verbose
This variable controls whether the interpreter is in debug mode.
Definition error.h:475
rb_eSyntaxError
VALUE rb_eSyntaxError
SyntaxError exception.
Definition error.c:1448
rb_class_new_instance
VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass)
Allocates, then initialises an instance of the given class.
Definition object.c:2249
rb_utf8_encoding
rb_encoding * rb_utf8_encoding(void)
Queries the encoding that represents UTF-8.
Definition encoding.c:1535
rb_ascii8bit_encoding
rb_encoding * rb_ascii8bit_encoding(void)
Queries the encoding that represents ASCII-8BIT a.k.a.
Definition encoding.c:1523
rb_enc_str_coderange
int rb_enc_str_coderange(VALUE str)
Scans the passed string to collect its code range.
Definition string.c:947
rb_str_coderange_scan_restartable
long rb_str_coderange_scan_restartable(const char *str, const char *end, rb_encoding *enc, int *cr)
Scans the passed string until it finds something odd.
Definition string.c:831
rb_ary_new_capa
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
rb_ary_push
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
rb_ary_entry
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
rb_int_positive_pow
VALUE rb_int_positive_pow(long x, unsigned long y)
Raises the passed x to the power of y.
Definition numeric.c:4694
rb_rational_new
VALUE rb_rational_new(VALUE num, VALUE den)
Constructs a Rational, with reduction.
Definition rational.c:2000
rb_rational_raw1
#define rb_rational_raw1(x)
Shorthand of (x/1)r.
Definition rational.h:51
rb_str_subseq
VALUE rb_str_subseq(VALUE str, long beg, long len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
Definition string.c:3155
rb_str_new
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
Definition string.h:1499
rb_str_new_frozen
VALUE rb_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
Definition string.c:1518
rb_filesystem_str_new_cstr
VALUE rb_filesystem_str_new_cstr(const char *ptr)
Identical to rb_filesystem_str_new(), except it assumes the passed pointer is a pointer to a C string...
Definition string.c:1429
rb_str_cat_cstr
#define rb_str_cat_cstr(buf, str)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1657
rb_str_new_cstr
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1515
rb_const_get_at
VALUE rb_const_get_at(VALUE space, ID name)
Identical to rb_const_defined_at(), except it returns the actual defined value.
Definition variable.c:3467
rb_const_defined_at
int rb_const_defined_at(VALUE space, ID name)
Identical to rb_const_defined(), except it doesn't look for parent classes.
Definition variable.c:3799
rb_id2sym
VALUE rb_id2sym(ID id)
Allocates an instance of rb_cSymbol that has the given id.
Definition symbol.c:974
len
int len
Length of the buffer.
Definition io.h:8
ractor.h
util.h
strtod
#define strtod(s, e)
Just another name of ruby_strtod.
Definition util.h:223
ruby_strtod
double ruby_strtod(const char *str, char **endptr)
Our own locale-insensitive version of strtod(3).
strdup
#define strdup(s)
Just another name of ruby_strdup.
Definition util.h:187
rb_int2num_inline
static VALUE rb_int2num_inline(int v)
Converts a C's int into an instance of rb_cInteger.
Definition int.h:239
rb_long2int
#define rb_long2int
Just another name of rb_long2int_inline.
Definition long.h:62
RB_GC_GUARD
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:167
ruby::backward::cxxanyargs::type
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition cxxanyargs.hpp:56
DATA_PTR
#define DATA_PTR(obj)
Convenient getter macro.
Definition rdata.h:67
RREGEXP_PTR
#define RREGEXP_PTR(obj)
Convenient accessor macro.
Definition rregexp.h:45
StringValue
#define StringValue(v)
Ensures that the parameter object is a String.
Definition rstring.h:66
TypedData_Get_Struct
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
Definition rtypeddata.h:649
TypedData_Wrap_Struct
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
Definition rtypeddata.h:461
rb_data_type_t
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:205
TypedData_Make_Struct
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
Definition rtypeddata.h:508
ruby.h
rb_errno_ptr
int * rb_errno_ptr(void)
The location of errno.
Definition eval.c:2286
RB_TEST
static bool RB_TEST(VALUE obj)
Emulates Ruby's "if" statement.
Definition special_consts.h:138
RUBY_SPECIAL_SHIFT
@ RUBY_SPECIAL_SHIFT
Least significant 8 bits are reserved.
Definition special_consts.h:119
RTEST
#define RTEST
This is an old name of RB_TEST.
Definition special_consts.h:51
lex_pointer_string
Definition ruby_parser.h:13
parser_params
Definition parse.c:552
rb_iseq_struct
Definition vm_core.h:572
reg_named_capture_assign_t
Definition parse.c:26619
ruby_parser
Definition ruby_parser.c:453
ID
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
RB_TYPE_P
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376