1 /* This is a wrapper for parse.y */
3 #include "internal/parse.h"
4 #include "internal/re.h"
5 #include "internal/ruby_parser.h"
8 #include "rubyparser.h"
9 #include "internal/error.h"
11 #ifdef UNIVERSAL_PARSER
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"
28 #include "ruby/ractor.h"
29 #include "ruby/ruby.h"
30 #include "ruby/util.h"
35 #define parser_encoding const void
38 is_ascii_string2(VALUE str
)
40 return is_ascii_string(str
);
43 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT
, 6, 0)
45 syntax_error_append(VALUE exc
, VALUE file
, int line
, int column
,
46 parser_encoding
*enc
, const char *fmt
, va_list args
)
48 return rb_syntax_error_append(exc
, file
, line
, column
, enc
, fmt
, args
);
52 local_defined(ID id
, const void *p
)
54 return rb_local_defined(id
, (const rb_iseq_t
*)p
);
58 dvar_defined(ID id
, const void *p
)
60 return rb_dvar_defined(id
, (const rb_iseq_t
*)p
);
64 is_usascii_enc(parser_encoding
*enc
)
66 return rb_is_usascii_enc(enc
);
72 return is_local_id(id
);
78 return is_attrset_id(id
);
84 return is_notop_id(id
);
88 enc_str_new(const char *ptr
, long len
, parser_encoding
*enc
)
90 return rb_enc_str_new(ptr
, len
, enc
);
94 enc_isalnum(OnigCodePoint c
, parser_encoding
*enc
)
96 return rb_enc_isalnum(c
, enc
);
100 enc_precise_mbclen(const char *p
, const char *e
, parser_encoding
*enc
)
102 return rb_enc_precise_mbclen(p
, e
, enc
);
106 mbclen_charfound_p(int len
)
108 return MBCLEN_CHARFOUND_P(len
);
112 mbclen_charfound_len(int len
)
114 return MBCLEN_CHARFOUND_LEN(len
);
118 enc_name(parser_encoding
*enc
)
120 return rb_enc_name(enc
);
124 enc_prev_char(const char *s
, const char *p
, const char *e
, parser_encoding
*enc
)
126 return rb_enc_prev_char(s
, p
, e
, enc
);
129 static parser_encoding
*
132 return rb_enc_get(obj
);
136 enc_asciicompat(parser_encoding
*enc
)
138 return rb_enc_asciicompat(enc
);
141 static parser_encoding
*
144 return rb_utf8_encoding();
148 enc_associate(VALUE obj
, parser_encoding
*enc
)
150 return rb_enc_associate(obj
, enc
);
153 static parser_encoding
*
154 ascii8bit_encoding(void)
156 return rb_ascii8bit_encoding();
160 enc_codelen(int c
, parser_encoding
*enc
)
162 return rb_enc_codelen(c
, enc
);
166 enc_mbcput(unsigned int c
, void *buf
, parser_encoding
*enc
)
168 return rb_enc_mbcput(c
, buf
, enc
);
171 static parser_encoding
*
172 enc_from_index(int idx
)
174 return rb_enc_from_index(idx
);
178 enc_isspace(OnigCodePoint c
, parser_encoding
*enc
)
180 return rb_enc_isspace(c
, enc
);
184 intern3(const char *name
, long len
, parser_encoding
*enc
)
186 return rb_intern3(name
, len
, enc
);
189 static parser_encoding
*
190 usascii_encoding(void)
192 return rb_usascii_encoding();
196 enc_symname_type(const char *name
, long len
, parser_encoding
*enc
, unsigned int allowed_attrset
)
198 return rb_enc_symname_type(name
, len
, enc
, allowed_attrset
);
202 struct parser_params
*parser
;
205 const rb_code_location_t
*loc
;
206 } reg_named_capture_assign_t
;
209 reg_named_capture_assign_iter(const OnigUChar
*name
, const OnigUChar
*name_end
,
210 int back_num
, int *back_refs
, OnigRegex regex
, void *arg0
)
212 reg_named_capture_assign_t
*arg
= (reg_named_capture_assign_t
*)arg0
;
213 struct parser_params
* p
= arg
->parser
;
214 rb_encoding
*enc
= arg
->enc
;
215 const rb_code_location_t
*loc
= arg
->loc
;
216 long len
= name_end
- name
;
217 const char *s
= (const char *)name
;
219 return rb_reg_named_capture_assign_iter_impl(p
, s
, len
, enc
, &arg
->succ_block
, loc
);
223 reg_named_capture_assign(struct parser_params
* p
, VALUE regexp
, const rb_code_location_t
*loc
)
225 reg_named_capture_assign_t arg
;
228 arg
.enc
= rb_enc_get(regexp
);
231 onig_foreach_name(RREGEXP_PTR(regexp
), reg_named_capture_assign_iter
, &arg
);
233 if (!arg
.succ_block
) return 0;
234 return RNODE_BLOCK(arg
.succ_block
)->nd_next
;
240 return (int)RB_TEST(obj
);
246 return (int)NIL_P(obj
);
250 syntax_error_new(void)
252 return rb_class_new_instance(0, 0, rb_eSyntaxError
);
256 memmove2(void *dest
, const void *src
, size_t t
, size_t n
)
258 return memmove(dest
, src
, rbimpl_size_mul_or_raise(t
, n
));
262 nonempty_memcpy(void *dest
, const void *src
, size_t t
, size_t n
)
264 return ruby_nonempty_memcpy(dest
, src
, rbimpl_size_mul_or_raise(t
, n
));
276 return rb_errno_ptr();
280 zalloc(size_t elemsiz
)
282 return ruby_xcalloc(1, elemsiz
);
300 return (((VALUE
)(id
)<<RUBY_SPECIAL_SHIFT
)|SYMBOL_FLAG
);
304 str_coderange_scan_restartable(const char *s
, const char *e
, parser_encoding
*enc
, int *cr
)
306 return rb_str_coderange_scan_restartable(s
, e
, enc
, cr
);
310 enc_mbminlen(parser_encoding
*enc
)
312 return rb_enc_mbminlen(enc
);
316 enc_isascii(OnigCodePoint c
, parser_encoding
*enc
)
318 return rb_enc_isascii(c
, enc
);
322 enc_mbc_to_codepoint(const char *p
, const char *e
, parser_encoding
*enc
)
324 const OnigUChar
*up
= RBIMPL_CAST((const OnigUChar
*)p
);
325 const OnigUChar
*ue
= RBIMPL_CAST((const OnigUChar
*)e
);
327 return ONIGENC_MBC_TO_CODE((rb_encoding
*)enc
, up
, ue
);
330 extern VALUE rb_eArgError
;
332 static const rb_parser_config_t rb_global_parser_config
= {
333 .malloc
= ruby_xmalloc
,
334 .calloc
= ruby_xcalloc
,
335 .realloc
= ruby_xrealloc
,
337 .alloc_n
= ruby_xmalloc2
,
338 .alloc
= ruby_xmalloc
,
339 .realloc_n
= ruby_xrealloc2
,
341 .rb_memmove
= memmove2
,
342 .nonempty_memcpy
= nonempty_memcpy
,
343 .xmalloc_mul_add
= rb_xmalloc_mul_add
,
345 .compile_callback
= rb_suppress_tracing
,
346 .reg_named_capture_assign
= reg_named_capture_assign
,
348 .attr_get
= rb_attr_get
,
350 .ary_new
= rb_ary_new
,
351 .ary_push
= rb_ary_push
,
352 .ary_new_from_args
= rb_ary_new_from_args
,
353 .ary_unshift
= rb_ary_unshift
,
355 .make_temporary_id
= rb_make_temporary_id
,
356 .is_local_id
= is_local_id2
,
357 .is_attrset_id
= is_attrset_id2
,
358 .is_global_name_punct
= is_global_name_punct
,
360 .id_attrset
= rb_id_attrset
,
362 .intern2
= rb_intern2
,
364 .intern_str
= rb_intern_str
,
365 .is_notop_id
= is_notop_id2
,
366 .enc_symname_type
= enc_symname_type
,
367 .id2name
= rb_id2name
,
372 .str_catf
= rb_str_catf
,
373 .str_cat_cstr
= rb_str_cat_cstr
,
374 .str_resize
= rb_str_resize
,
375 .str_new
= rb_str_new
,
376 .str_new_cstr
= rb_str_new_cstr
,
377 .str_to_interned_str
= rb_str_to_interned_str
,
378 .is_ascii_string
= is_ascii_string2
,
379 .enc_str_new
= enc_str_new
,
380 .str_vcatf
= rb_str_vcatf
,
381 .rb_sprintf
= rb_sprintf
,
382 .rstring_ptr
= RSTRING_PTR
,
383 .rstring_end
= RSTRING_END
,
384 .rstring_len
= RSTRING_LEN
,
385 .obj_as_string
= rb_obj_as_string
,
387 .int2num
= rb_int2num_inline
,
389 .stderr_tty_p
= rb_stderr_tty_p
,
390 .write_error_str
= rb_write_error_str
,
391 .io_write
= rb_io_write
,
392 .io_flush
= rb_io_flush
,
393 .io_puts
= rb_io_puts
,
395 .debug_output_stdout
= rb_ractor_stdout
,
396 .debug_output_stderr
= rb_ractor_stderr
,
398 .is_usascii_enc
= is_usascii_enc
,
399 .enc_isalnum
= enc_isalnum
,
400 .enc_precise_mbclen
= enc_precise_mbclen
,
401 .mbclen_charfound_p
= mbclen_charfound_p
,
402 .mbclen_charfound_len
= mbclen_charfound_len
,
403 .enc_name
= enc_name
,
404 .enc_prev_char
= enc_prev_char
,
406 .enc_asciicompat
= enc_asciicompat
,
407 .utf8_encoding
= utf8_encoding
,
408 .enc_associate
= enc_associate
,
409 .ascii8bit_encoding
= ascii8bit_encoding
,
410 .enc_codelen
= enc_codelen
,
411 .enc_mbcput
= enc_mbcput
,
412 .enc_find_index
= rb_enc_find_index
,
413 .enc_from_index
= enc_from_index
,
414 .enc_isspace
= enc_isspace
,
415 .enc_coderange_7bit
= ENC_CODERANGE_7BIT
,
416 .enc_coderange_unknown
= ENC_CODERANGE_UNKNOWN
,
417 .usascii_encoding
= usascii_encoding
,
418 .enc_mbminlen
= enc_mbminlen
,
419 .enc_isascii
= enc_isascii
,
420 .enc_mbc_to_codepoint
= enc_mbc_to_codepoint
,
422 .local_defined
= local_defined
,
423 .dvar_defined
= dvar_defined
,
425 .syntax_error_append
= syntax_error_append
,
427 .syntax_error_new
= syntax_error_new
,
429 .errinfo
= rb_errinfo
,
430 .set_errinfo
= rb_set_errinfo
,
431 .exc_raise
= rb_exc_raise
,
432 .make_exception
= rb_make_exception
,
434 .sized_xfree
= ruby_sized_xfree
,
435 .sized_realloc_n
= ruby_sized_realloc_n
,
436 .gc_guard
= gc_guard
,
437 .gc_mark
= rb_gc_mark
,
439 .reg_compile
= rb_reg_compile
,
440 .reg_check_preprocess
= rb_reg_check_preprocess
,
441 .memcicmp
= rb_memcicmp
,
443 .compile_warn
= rb_compile_warn
,
444 .compile_warning
= rb_compile_warning
,
447 .verbose
= ruby_verbose2
,
448 .errno_ptr
= rb_errno_ptr2
,
450 .make_backtrace
= rb_make_backtrace
,
452 .scan_hex
= ruby_scan_hex
,
453 .scan_oct
= ruby_scan_oct
,
454 .scan_digits
= ruby_scan_digits
,
455 .strtod
= ruby_strtod
,
461 .eArgError
= arg_error
,
462 .long2int
= rb_long2int
,
465 .static_id2sym
= static_id2sym
,
466 .str_coderange_scan_restartable
= str_coderange_scan_restartable
,
478 rb_parser_t
*parser_params
;
481 struct lex_pointer_string lex_str
;
492 parser_mark(void *ptr
)
494 struct ruby_parser
*parser
= (struct ruby_parser
*)ptr
;
495 rb_ruby_parser_mark(parser
->parser_params
);
497 switch (parser
->type
) {
499 rb_gc_mark(parser
->data
.lex_str
.str
);
502 rb_gc_mark(parser
->data
.lex_io
.file
);
505 rb_gc_mark(parser
->data
.lex_array
.ary
);
507 case lex_type_generic
:
508 /* noop. Caller of rb_parser_compile_generic should mark the objects. */
514 parser_free(void *ptr
)
516 struct ruby_parser
*parser
= (struct ruby_parser
*)ptr
;
517 rb_ruby_parser_free(parser
->parser_params
);
522 parser_memsize(const void *ptr
)
524 struct ruby_parser
*parser
= (struct ruby_parser
*)ptr
;
525 return rb_ruby_parser_memsize(parser
->parser_params
);
528 static const rb_data_type_t ruby_parser_data_type
= {
535 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
538 #ifdef UNIVERSAL_PARSER
539 const rb_parser_config_t
*
540 rb_ruby_parser_config(void)
542 return &rb_global_parser_config
;
546 rb_parser_params_new(void)
548 return rb_ruby_parser_new(&rb_global_parser_config
);
552 rb_parser_params_new(void)
554 return rb_ruby_parser_new();
556 #endif /* UNIVERSAL_PARSER */
561 struct ruby_parser
*parser
;
562 rb_parser_t
*parser_params
;
565 * Create parser_params ahead of vparser because
566 * rb_ruby_parser_new can run GC so if create vparser
567 * first, parser_mark tries to mark not initialized parser_params.
569 parser_params
= rb_parser_params_new();
570 VALUE vparser
= TypedData_Make_Struct(0, struct ruby_parser
,
571 &ruby_parser_data_type
, parser
);
572 parser
->parser_params
= parser_params
;
578 rb_parser_set_options(VALUE vparser
, int print
, int loop
, int chomp
, int split
)
580 struct ruby_parser
*parser
;
582 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
583 rb_ruby_parser_set_options(parser
->parser_params
, print
, loop
, chomp
, split
);
587 rb_parser_set_context(VALUE vparser
, const struct rb_iseq_struct
*base
, int main
)
589 struct ruby_parser
*parser
;
591 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
592 rb_ruby_parser_set_context(parser
->parser_params
, base
, main
);
597 rb_parser_set_script_lines(VALUE vparser
)
599 struct ruby_parser
*parser
;
601 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
602 rb_ruby_parser_set_script_lines(parser
->parser_params
);
606 rb_parser_error_tolerant(VALUE vparser
)
608 struct ruby_parser
*parser
;
610 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
611 rb_ruby_parser_error_tolerant(parser
->parser_params
);
615 rb_parser_keep_tokens(VALUE vparser
)
617 struct ruby_parser
*parser
;
619 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
620 rb_ruby_parser_keep_tokens(parser
->parser_params
);
624 rb_parser_lex_get_str(struct parser_params
*p
, struct lex_pointer_string
*ptr_str
)
626 char *beg
, *end
, *start
;
628 VALUE s
= ptr_str
->str
;
630 beg
= RSTRING_PTR(s
);
631 len
= RSTRING_LEN(s
);
634 if (len
== ptr_str
->ptr
) return 0;
638 end
= memchr(beg
, '\n', len
);
639 if (end
) len
= ++end
- beg
;
641 return rb_str_to_parser_string(p
, rb_str_subseq(s
, beg
- start
, len
));
644 static rb_parser_string_t
*
645 lex_get_str(struct parser_params
*p
, rb_parser_input_data input
, int line_count
)
647 return rb_parser_lex_get_str(p
, (struct lex_pointer_string
*)input
);
650 static void parser_aset_script_lines_for(VALUE path
, rb_parser_ary_t
*lines
);
653 parser_compile(rb_parser_t
*p
, rb_parser_lex_gets_func
*gets
, VALUE fname
, rb_parser_input_data input
, int line
)
655 rb_ast_t
*ast
= rb_parser_compile(p
, gets
, fname
, input
, line
);
656 parser_aset_script_lines_for(fname
, ast
->body
.script_lines
);
661 parser_compile_string0(struct ruby_parser
*parser
, VALUE fname
, VALUE s
, int line
)
663 VALUE str
= rb_str_new_frozen(s
);
665 parser
->type
= lex_type_str
;
666 parser
->data
.lex_str
.str
= str
;
667 parser
->data
.lex_str
.ptr
= 0;
669 return parser_compile(parser
->parser_params
, lex_get_str
, fname
, (rb_parser_input_data
)&parser
->data
, line
);
673 must_be_ascii_compatible(VALUE s
)
675 rb_encoding
*enc
= rb_enc_get(s
);
676 if (!rb_enc_asciicompat(enc
)) {
677 rb_raise(rb_eArgError
, "invalid source encoding");
683 parser_compile_string_path(struct ruby_parser
*parser
, VALUE f
, VALUE s
, int line
)
685 must_be_ascii_compatible(s
);
686 return parser_compile_string0(parser
, f
, s
, line
);
690 parser_compile_string(struct ruby_parser
*parser
, const char *f
, VALUE s
, int line
)
692 return parser_compile_string_path(parser
, rb_filesystem_str_new_cstr(f
), s
, line
);
695 VALUE
rb_io_gets_internal(VALUE io
);
697 static rb_parser_string_t
*
698 lex_io_gets(struct parser_params
*p
, rb_parser_input_data input
, int line_count
)
700 VALUE io
= (VALUE
)input
;
701 VALUE line
= rb_io_gets_internal(io
);
702 if (NIL_P(line
)) return 0;
703 return rb_str_to_parser_string(p
, line
);
706 static rb_parser_string_t
*
707 lex_gets_array(struct parser_params
*p
, rb_parser_input_data data
, int index
)
709 VALUE array
= (VALUE
)data
;
710 VALUE str
= rb_ary_entry(array
, index
);
713 if (!rb_enc_asciicompat(rb_enc_get(str
))) {
714 rb_raise(rb_eArgError
, "invalid source encoding");
716 return rb_str_to_parser_string(p
, str
);
724 parser_compile_file_path(struct ruby_parser
*parser
, VALUE fname
, VALUE file
, int start
)
726 parser
->type
= lex_type_io
;
727 parser
->data
.lex_io
.file
= file
;
729 return parser_compile(parser
->parser_params
, lex_io_gets
, fname
, (rb_parser_input_data
)file
, start
);
733 parser_compile_array(struct ruby_parser
*parser
, VALUE fname
, VALUE array
, int start
)
735 parser
->type
= lex_type_array
;
736 parser
->data
.lex_array
.ary
= array
;
738 return parser_compile(parser
->parser_params
, lex_gets_array
, fname
, (rb_parser_input_data
)array
, start
);
742 parser_compile_generic(struct ruby_parser
*parser
, rb_parser_lex_gets_func
*lex_gets
, VALUE fname
, VALUE input
, int start
)
744 parser
->type
= lex_type_generic
;
746 return parser_compile(parser
->parser_params
, lex_gets
, fname
, (rb_parser_input_data
)input
, start
);
752 rb_ast_t
*ast
= (rb_ast_t
*)ptr
;
756 static const rb_data_type_t ast_data_type
= {
761 NULL
, // No dsize() because this object does not appear in ObjectSpace.
763 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
769 return TypedData_Wrap_Struct(0, &ast_data_type
, NULL
);
773 rb_parser_compile_file_path(VALUE vparser
, VALUE fname
, VALUE file
, int start
)
775 struct ruby_parser
*parser
;
776 VALUE ast_value
= ast_alloc();
778 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
779 DATA_PTR(ast_value
) = parser_compile_file_path(parser
, fname
, file
, start
);
780 RB_GC_GUARD(vparser
);
786 rb_parser_compile_array(VALUE vparser
, VALUE fname
, VALUE array
, int start
)
788 struct ruby_parser
*parser
;
789 VALUE ast_value
= ast_alloc();
791 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
792 DATA_PTR(ast_value
) = parser_compile_array(parser
, fname
, array
, start
);
793 RB_GC_GUARD(vparser
);
799 rb_parser_compile_generic(VALUE vparser
, rb_parser_lex_gets_func
*lex_gets
, VALUE fname
, VALUE input
, int start
)
801 struct ruby_parser
*parser
;
802 VALUE ast_value
= ast_alloc();
804 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
805 DATA_PTR(ast_value
) = parser_compile_generic(parser
, lex_gets
, fname
, input
, start
);
806 RB_GC_GUARD(vparser
);
812 rb_parser_compile_string(VALUE vparser
, const char *f
, VALUE s
, int line
)
814 struct ruby_parser
*parser
;
815 VALUE ast_value
= ast_alloc();
817 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
818 DATA_PTR(ast_value
) = parser_compile_string(parser
, f
, s
, line
);
819 RB_GC_GUARD(vparser
);
825 rb_parser_compile_string_path(VALUE vparser
, VALUE f
, VALUE s
, int line
)
827 struct ruby_parser
*parser
;
828 VALUE ast_value
= ast_alloc();
830 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
831 DATA_PTR(ast_value
) = parser_compile_string_path(parser
, f
, s
, line
);
832 RB_GC_GUARD(vparser
);
838 rb_parser_encoding(VALUE vparser
)
840 struct ruby_parser
*parser
;
842 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
843 return rb_enc_from_encoding(rb_ruby_parser_encoding(parser
->parser_params
));
847 rb_parser_end_seen_p(VALUE vparser
)
849 struct ruby_parser
*parser
;
851 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
852 return RBOOL(rb_ruby_parser_end_seen_p(parser
->parser_params
));
856 rb_parser_set_yydebug(VALUE vparser
, VALUE flag
)
858 struct ruby_parser
*parser
;
860 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
861 rb_ruby_parser_set_yydebug(parser
->parser_params
, RTEST(flag
));
866 rb_set_script_lines_for(VALUE vparser
, VALUE path
)
868 struct ruby_parser
*parser
;
871 CONST_ID(script_lines
, "SCRIPT_LINES__");
872 if (!rb_const_defined_at(rb_cObject
, script_lines
)) return;
873 hash
= rb_const_get_at(rb_cObject
, script_lines
);
874 if (RB_TYPE_P(hash
, T_HASH
)) {
875 rb_hash_aset(hash
, path
, Qtrue
);
876 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
877 rb_ruby_parser_set_script_lines(parser
->parser_params
);
882 rb_parser_build_script_lines_from(rb_parser_ary_t
*lines
)
885 if (!lines
) return Qnil
;
886 if (lines
->data_type
!= PARSER_ARY_DATA_SCRIPT_LINE
) {
887 rb_bug("unexpected rb_parser_ary_data_type (%d) for script lines", lines
->data_type
);
889 VALUE script_lines
= rb_ary_new_capa(lines
->len
);
890 for (i
= 0; i
< lines
->len
; i
++) {
891 rb_parser_string_t
*str
= (rb_parser_string_t
*)lines
->data
[i
];
892 rb_ary_push(script_lines
, rb_enc_str_new(str
->ptr
, str
->len
, str
->enc
));
898 rb_str_new_parser_string(rb_parser_string_t
*str
)
900 VALUE string
= rb_enc_literal_str(str
->ptr
, str
->len
, str
->enc
);
901 rb_enc_str_coderange(string
);
906 rb_str_new_mutable_parser_string(rb_parser_string_t
*str
)
908 return rb_enc_str_new(str
->ptr
, str
->len
, str
->enc
);
912 negative_numeric(VALUE val
)
915 return LONG2FIX(-FIX2LONG(val
));
917 if (SPECIAL_CONST_P(val
)) {
920 return DBL2NUM(-RFLOAT_VALUE(val
));
925 switch (BUILTIN_TYPE(val
)) {
928 val
= rb_big_norm(val
);
931 RATIONAL_SET_NUM(val
, negative_numeric(RRATIONAL(val
)->num
));
934 RCOMPLEX_SET_REAL(val
, negative_numeric(RCOMPLEX(val
)->real
));
935 RCOMPLEX_SET_IMAG(val
, negative_numeric(RCOMPLEX(val
)->imag
));
938 val
= DBL2NUM(-RFLOAT_VALUE(val
));
942 rb_bug("unknown literal type (%s) passed to negative_numeric",
943 rb_builtin_class_name(val
));
950 integer_value(const char *val
, int base
)
952 return rb_cstr_to_inum(val
, base
, FALSE
);
956 rational_value(const char *node_val
, int base
, int seen_point
)
959 char* val
= strdup(node_val
);
960 if (seen_point
> 0) {
961 int len
= (int)(strlen(val
));
962 char *point
= &val
[seen_point
];
963 size_t fraclen
= len
-seen_point
-1;
964 memmove(point
, point
+1, fraclen
+1);
966 lit
= rb_rational_new(integer_value(val
, base
), rb_int_positive_pow(10, fraclen
));
969 lit
= rb_rational_raw1(integer_value(val
, base
));
978 rb_node_integer_literal_val(const NODE
*n
)
980 const rb_node_integer_t
*node
= RNODE_INTEGER(n
);
981 VALUE val
= integer_value(node
->val
, node
->base
);
983 val
= negative_numeric(val
);
989 rb_node_float_literal_val(const NODE
*n
)
991 const rb_node_float_t
*node
= RNODE_FLOAT(n
);
992 double d
= strtod(node
->val
, 0);
996 VALUE val
= DBL2NUM(d
);
1001 rb_node_rational_literal_val(const NODE
*n
)
1004 const rb_node_rational_t
*node
= RNODE_RATIONAL(n
);
1006 lit
= rational_value(node
->val
, node
->base
, node
->seen_point
);
1009 lit
= negative_numeric(lit
);
1016 rb_node_imaginary_literal_val(const NODE
*n
)
1019 const rb_node_imaginary_t
*node
= RNODE_IMAGINARY(n
);
1021 enum rb_numeric_type type
= node
->type
;
1024 case integer_literal
:
1025 lit
= integer_value(node
->val
, node
->base
);
1027 case float_literal
:{
1028 double d
= strtod(node
->val
, 0);
1032 case rational_literal
:
1033 lit
= rational_value(node
->val
, node
->base
, node
->seen_point
);
1036 rb_bug("unreachable");
1039 lit
= rb_complex_raw(INT2FIX(0), lit
);
1042 lit
= negative_numeric(lit
);
1048 rb_node_str_string_val(const NODE
*node
)
1050 rb_parser_string_t
*str
= RNODE_STR(node
)->string
;
1051 return rb_str_new_parser_string(str
);
1055 rb_node_sym_string_val(const NODE
*node
)
1057 rb_parser_string_t
*str
= RNODE_SYM(node
)->string
;
1058 return ID2SYM(rb_intern3(str
->ptr
, str
->len
, str
->enc
));
1062 rb_node_dstr_string_val(const NODE
*node
)
1064 rb_parser_string_t
*str
= RNODE_DSTR(node
)->string
;
1065 return str
? rb_str_new_parser_string(str
) : Qnil
;
1069 rb_node_dregx_string_val(const NODE
*node
)
1071 rb_parser_string_t
*str
= RNODE_DREGX(node
)->string
;
1072 return rb_str_new_parser_string(str
);
1076 rb_node_regx_string_val(const NODE
*node
)
1078 rb_node_regx_t
*node_reg
= RNODE_REGX(node
);
1079 rb_parser_string_t
*string
= node_reg
->string
;
1080 VALUE str
= rb_enc_str_new(string
->ptr
, string
->len
, string
->enc
);
1082 return rb_reg_compile(str
, node_reg
->options
, NULL
, 0);
1086 rb_node_line_lineno_val(const NODE
*node
)
1088 return INT2FIX(node
->nd_loc
.beg_pos
.lineno
);
1092 rb_node_file_path_val(const NODE
*node
)
1094 return rb_str_new_parser_string(RNODE_FILE(node
)->path
);
1098 rb_node_encoding_val(const NODE
*node
)
1100 return rb_enc_from_encoding(RNODE_ENCODING(node
)->enc
);
1104 parser_aset_script_lines_for(VALUE path
, rb_parser_ary_t
*lines
)
1106 VALUE hash
, script_lines
;
1108 if (NIL_P(path
) || !lines
) return;
1109 CONST_ID(script_lines_id
, "SCRIPT_LINES__");
1110 if (!rb_const_defined_at(rb_cObject
, script_lines_id
)) return;
1111 hash
= rb_const_get_at(rb_cObject
, script_lines_id
);
1112 if (!RB_TYPE_P(hash
, T_HASH
)) return;
1113 if (rb_hash_lookup(hash
, path
) == Qnil
) return;
1114 script_lines
= rb_parser_build_script_lines_from(lines
);
1115 rb_hash_aset(hash
, path
, script_lines
);
1119 rb_ruby_ast_new(const NODE
*const root
)
1122 VALUE ast_value
= TypedData_Make_Struct(0, rb_ast_t
, &ast_data_type
, ast
);
1123 #ifdef UNIVERSAL_PARSER
1124 ast
->config
= &rb_global_parser_config
;
1126 ast
->body
= (rb_ast_body_t
){
1128 .frozen_string_literal
= -1,
1129 .coverage_enabled
= -1,
1130 .script_lines
= NULL
,
1137 rb_ruby_ast_data_get(VALUE ast_value
)
1140 if (NIL_P(ast_value
)) return NULL
;
1141 TypedData_Get_Struct(ast_value
, rb_ast_t
, &ast_data_type
, ast
);