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