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
37 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT
, 6, 0)
39 syntax_error_append(VALUE exc
, VALUE file
, int line
, int column
,
40 parser_encoding
*enc
, const char *fmt
, va_list args
)
42 return rb_syntax_error_append(exc
, file
, line
, column
, enc
, fmt
, args
);
46 local_defined(ID id
, const void *p
)
48 return rb_local_defined(id
, (const rb_iseq_t
*)p
);
52 dvar_defined(ID id
, const void *p
)
54 return rb_dvar_defined(id
, (const rb_iseq_t
*)p
);
58 is_usascii_enc(parser_encoding
*enc
)
60 return rb_is_usascii_enc(enc
);
66 return is_local_id(id
);
72 return is_attrset_id(id
);
78 return is_notop_id(id
);
82 enc_str_new(const char *ptr
, long len
, parser_encoding
*enc
)
84 return rb_enc_str_new(ptr
, len
, enc
);
88 enc_isalnum(OnigCodePoint c
, parser_encoding
*enc
)
90 return rb_enc_isalnum(c
, enc
);
94 enc_precise_mbclen(const char *p
, const char *e
, parser_encoding
*enc
)
96 return rb_enc_precise_mbclen(p
, e
, enc
);
100 mbclen_charfound_p(int len
)
102 return MBCLEN_CHARFOUND_P(len
);
106 mbclen_charfound_len(int len
)
108 return MBCLEN_CHARFOUND_LEN(len
);
112 enc_name(parser_encoding
*enc
)
114 return rb_enc_name(enc
);
118 enc_prev_char(const char *s
, const char *p
, const char *e
, parser_encoding
*enc
)
120 return rb_enc_prev_char(s
, p
, e
, enc
);
123 static parser_encoding
*
126 return rb_enc_get(obj
);
130 enc_asciicompat(parser_encoding
*enc
)
132 return rb_enc_asciicompat(enc
);
135 static parser_encoding
*
138 return rb_utf8_encoding();
142 enc_associate(VALUE obj
, parser_encoding
*enc
)
144 return rb_enc_associate(obj
, enc
);
147 static parser_encoding
*
148 ascii8bit_encoding(void)
150 return rb_ascii8bit_encoding();
154 enc_codelen(int c
, parser_encoding
*enc
)
156 return rb_enc_codelen(c
, enc
);
160 enc_mbcput(unsigned int c
, void *buf
, parser_encoding
*enc
)
162 return rb_enc_mbcput(c
, buf
, enc
);
165 static parser_encoding
*
166 enc_from_index(int idx
)
168 return rb_enc_from_index(idx
);
172 enc_isspace(OnigCodePoint c
, parser_encoding
*enc
)
174 return rb_enc_isspace(c
, enc
);
178 intern3(const char *name
, long len
, parser_encoding
*enc
)
180 return rb_intern3(name
, len
, enc
);
184 enc_symname_type(const char *name
, long len
, parser_encoding
*enc
, unsigned int allowed_attrset
)
186 return rb_enc_symname_type(name
, len
, enc
, allowed_attrset
);
190 struct parser_params
*parser
;
193 const rb_code_location_t
*loc
;
194 rb_parser_assignable_func assignable
;
195 } reg_named_capture_assign_t
;
198 reg_named_capture_assign_iter(const OnigUChar
*name
, const OnigUChar
*name_end
,
199 int back_num
, int *back_refs
, OnigRegex regex
, void *arg0
)
201 reg_named_capture_assign_t
*arg
= (reg_named_capture_assign_t
*)arg0
;
202 struct parser_params
* p
= arg
->parser
;
203 rb_encoding
*enc
= arg
->enc
;
204 const rb_code_location_t
*loc
= arg
->loc
;
205 long len
= name_end
- name
;
206 const char *s
= (const char *)name
;
208 return rb_reg_named_capture_assign_iter_impl(p
, s
, len
, enc
, &arg
->succ_block
, loc
, arg
->assignable
);
212 reg_named_capture_assign(struct parser_params
* p
, VALUE regexp
, const rb_code_location_t
*loc
,
213 rb_parser_assignable_func assignable
)
215 reg_named_capture_assign_t arg
;
218 arg
.enc
= rb_enc_get(regexp
);
221 arg
.assignable
= assignable
;
222 onig_foreach_name(RREGEXP_PTR(regexp
), reg_named_capture_assign_iter
, &arg
);
224 if (!arg
.succ_block
) return 0;
225 return RNODE_BLOCK(arg
.succ_block
)->nd_next
;
231 return (int)RB_TEST(obj
);
237 return (int)NIL_P(obj
);
241 syntax_error_new(void)
243 return rb_class_new_instance(0, 0, rb_eSyntaxError
);
247 memmove2(void *dest
, const void *src
, size_t t
, size_t n
)
249 return memmove(dest
, src
, rbimpl_size_mul_or_raise(t
, n
));
253 nonempty_memcpy(void *dest
, const void *src
, size_t t
, size_t n
)
255 return ruby_nonempty_memcpy(dest
, src
, rbimpl_size_mul_or_raise(t
, n
));
267 return rb_errno_ptr();
271 zalloc(size_t elemsiz
)
273 return ruby_xcalloc(1, elemsiz
);
291 return (((VALUE
)(id
)<<RUBY_SPECIAL_SHIFT
)|SYMBOL_FLAG
);
295 str_coderange_scan_restartable(const char *s
, const char *e
, parser_encoding
*enc
, int *cr
)
297 return rb_str_coderange_scan_restartable(s
, e
, enc
, cr
);
301 enc_mbminlen(parser_encoding
*enc
)
303 return rb_enc_mbminlen(enc
);
307 enc_isascii(OnigCodePoint c
, parser_encoding
*enc
)
309 return rb_enc_isascii(c
, enc
);
313 enc_mbc_to_codepoint(const char *p
, const char *e
, parser_encoding
*enc
)
315 const OnigUChar
*up
= RBIMPL_CAST((const OnigUChar
*)p
);
316 const OnigUChar
*ue
= RBIMPL_CAST((const OnigUChar
*)e
);
318 return ONIGENC_MBC_TO_CODE((rb_encoding
*)enc
, up
, ue
);
321 extern VALUE rb_eArgError
;
323 static const rb_parser_config_t rb_global_parser_config
= {
324 .malloc
= ruby_xmalloc
,
325 .calloc
= ruby_xcalloc
,
326 .realloc
= ruby_xrealloc
,
328 .alloc_n
= ruby_xmalloc2
,
329 .alloc
= ruby_xmalloc
,
330 .realloc_n
= ruby_xrealloc2
,
332 .rb_memmove
= memmove2
,
333 .nonempty_memcpy
= nonempty_memcpy
,
334 .xmalloc_mul_add
= rb_xmalloc_mul_add
,
336 .compile_callback
= rb_suppress_tracing
,
337 .reg_named_capture_assign
= reg_named_capture_assign
,
339 .attr_get
= rb_attr_get
,
341 .ary_new_from_args
= rb_ary_new_from_args
,
342 .ary_unshift
= rb_ary_unshift
,
344 .make_temporary_id
= rb_make_temporary_id
,
345 .is_local_id
= is_local_id2
,
346 .is_attrset_id
= is_attrset_id2
,
347 .is_global_name_punct
= is_global_name_punct
,
349 .id_attrset
= rb_id_attrset
,
351 .intern2
= rb_intern2
,
353 .intern_str
= rb_intern_str
,
354 .is_notop_id
= is_notop_id2
,
355 .enc_symname_type
= enc_symname_type
,
356 .id2name
= rb_id2name
,
360 .str_catf
= rb_str_catf
,
361 .str_cat_cstr
= rb_str_cat_cstr
,
362 .str_resize
= rb_str_resize
,
363 .str_new
= rb_str_new
,
364 .str_new_cstr
= rb_str_new_cstr
,
365 .str_to_interned_str
= rb_str_to_interned_str
,
366 .enc_str_new
= enc_str_new
,
367 .str_vcatf
= rb_str_vcatf
,
368 .rb_sprintf
= rb_sprintf
,
369 .rstring_ptr
= RSTRING_PTR
,
370 .rstring_len
= RSTRING_LEN
,
372 .int2num
= rb_int2num_inline
,
374 .stderr_tty_p
= rb_stderr_tty_p
,
375 .write_error_str
= rb_write_error_str
,
376 .io_write
= rb_io_write
,
377 .io_flush
= rb_io_flush
,
378 .io_puts
= rb_io_puts
,
380 .debug_output_stdout
= rb_ractor_stdout
,
381 .debug_output_stderr
= rb_ractor_stderr
,
383 .is_usascii_enc
= is_usascii_enc
,
384 .enc_isalnum
= enc_isalnum
,
385 .enc_precise_mbclen
= enc_precise_mbclen
,
386 .mbclen_charfound_p
= mbclen_charfound_p
,
387 .mbclen_charfound_len
= mbclen_charfound_len
,
388 .enc_name
= enc_name
,
389 .enc_prev_char
= enc_prev_char
,
391 .enc_asciicompat
= enc_asciicompat
,
392 .utf8_encoding
= utf8_encoding
,
393 .enc_associate
= enc_associate
,
394 .ascii8bit_encoding
= ascii8bit_encoding
,
395 .enc_codelen
= enc_codelen
,
396 .enc_mbcput
= enc_mbcput
,
397 .enc_find_index
= rb_enc_find_index
,
398 .enc_from_index
= enc_from_index
,
399 .enc_isspace
= enc_isspace
,
400 .enc_coderange_7bit
= ENC_CODERANGE_7BIT
,
401 .enc_coderange_unknown
= ENC_CODERANGE_UNKNOWN
,
402 .enc_mbminlen
= enc_mbminlen
,
403 .enc_isascii
= enc_isascii
,
404 .enc_mbc_to_codepoint
= enc_mbc_to_codepoint
,
406 .local_defined
= local_defined
,
407 .dvar_defined
= dvar_defined
,
409 .syntax_error_append
= syntax_error_append
,
411 .syntax_error_new
= syntax_error_new
,
413 .errinfo
= rb_errinfo
,
414 .set_errinfo
= rb_set_errinfo
,
415 .make_exception
= rb_make_exception
,
417 .sized_xfree
= ruby_sized_xfree
,
418 .sized_realloc_n
= ruby_sized_realloc_n
,
419 .gc_guard
= gc_guard
,
420 .gc_mark
= rb_gc_mark
,
422 .reg_compile
= rb_reg_compile
,
423 .reg_check_preprocess
= rb_reg_check_preprocess
,
424 .memcicmp
= rb_memcicmp
,
426 .compile_warn
= rb_compile_warn
,
427 .compile_warning
= rb_compile_warning
,
430 .verbose
= ruby_verbose2
,
431 .errno_ptr
= rb_errno_ptr2
,
433 .make_backtrace
= rb_make_backtrace
,
435 .scan_hex
= ruby_scan_hex
,
436 .scan_oct
= ruby_scan_oct
,
437 .scan_digits
= ruby_scan_digits
,
438 .strtod
= ruby_strtod
,
444 .eArgError
= arg_error
,
445 .long2int
= rb_long2int
,
448 .static_id2sym
= static_id2sym
,
449 .str_coderange_scan_restartable
= str_coderange_scan_restartable
,
461 rb_parser_t
*parser_params
;
464 struct lex_pointer_string lex_str
;
475 parser_mark(void *ptr
)
477 struct ruby_parser
*parser
= (struct ruby_parser
*)ptr
;
478 rb_ruby_parser_mark(parser
->parser_params
);
480 switch (parser
->type
) {
482 rb_gc_mark(parser
->data
.lex_str
.str
);
485 rb_gc_mark(parser
->data
.lex_io
.file
);
488 rb_gc_mark(parser
->data
.lex_array
.ary
);
490 case lex_type_generic
:
491 /* noop. Caller of rb_parser_compile_generic should mark the objects. */
497 parser_free(void *ptr
)
499 struct ruby_parser
*parser
= (struct ruby_parser
*)ptr
;
500 rb_ruby_parser_free(parser
->parser_params
);
505 parser_memsize(const void *ptr
)
507 struct ruby_parser
*parser
= (struct ruby_parser
*)ptr
;
508 return rb_ruby_parser_memsize(parser
->parser_params
);
511 static const rb_data_type_t ruby_parser_data_type
= {
518 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
521 #ifdef UNIVERSAL_PARSER
522 const rb_parser_config_t
*
523 rb_ruby_parser_config(void)
525 return &rb_global_parser_config
;
529 rb_parser_params_new(void)
531 return rb_ruby_parser_new(&rb_global_parser_config
);
535 rb_parser_params_new(void)
537 return rb_ruby_parser_new();
539 #endif /* UNIVERSAL_PARSER */
544 struct ruby_parser
*parser
;
545 rb_parser_t
*parser_params
;
548 * Create parser_params ahead of vparser because
549 * rb_ruby_parser_new can run GC so if create vparser
550 * first, parser_mark tries to mark not initialized parser_params.
552 parser_params
= rb_parser_params_new();
553 VALUE vparser
= TypedData_Make_Struct(0, struct ruby_parser
,
554 &ruby_parser_data_type
, parser
);
555 parser
->parser_params
= parser_params
;
561 rb_parser_set_options(VALUE vparser
, int print
, int loop
, int chomp
, int split
)
563 struct ruby_parser
*parser
;
565 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
566 rb_ruby_parser_set_options(parser
->parser_params
, print
, loop
, chomp
, split
);
570 rb_parser_set_context(VALUE vparser
, const struct rb_iseq_struct
*base
, int main
)
572 struct ruby_parser
*parser
;
574 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
575 rb_ruby_parser_set_context(parser
->parser_params
, base
, main
);
580 rb_parser_set_script_lines(VALUE vparser
)
582 struct ruby_parser
*parser
;
584 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
585 rb_ruby_parser_set_script_lines(parser
->parser_params
);
589 rb_parser_error_tolerant(VALUE vparser
)
591 struct ruby_parser
*parser
;
593 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
594 rb_ruby_parser_error_tolerant(parser
->parser_params
);
598 rb_parser_keep_tokens(VALUE vparser
)
600 struct ruby_parser
*parser
;
602 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
603 rb_ruby_parser_keep_tokens(parser
->parser_params
);
607 rb_parser_lex_get_str(struct parser_params
*p
, struct lex_pointer_string
*ptr_str
)
609 char *beg
, *end
, *start
;
611 VALUE s
= ptr_str
->str
;
613 beg
= RSTRING_PTR(s
);
614 len
= RSTRING_LEN(s
);
617 if (len
== ptr_str
->ptr
) return 0;
621 end
= memchr(beg
, '\n', len
);
622 if (end
) len
= ++end
- beg
;
624 return rb_str_to_parser_string(p
, rb_str_subseq(s
, beg
- start
, len
));
627 static rb_parser_string_t
*
628 lex_get_str(struct parser_params
*p
, rb_parser_input_data input
, int line_count
)
630 return rb_parser_lex_get_str(p
, (struct lex_pointer_string
*)input
);
633 static void parser_aset_script_lines_for(VALUE path
, rb_parser_ary_t
*lines
);
636 parser_compile(rb_parser_t
*p
, rb_parser_lex_gets_func
*gets
, VALUE fname
, rb_parser_input_data input
, int line
)
638 rb_ast_t
*ast
= rb_parser_compile(p
, gets
, fname
, input
, line
);
639 parser_aset_script_lines_for(fname
, ast
->body
.script_lines
);
644 parser_compile_string0(struct ruby_parser
*parser
, VALUE fname
, VALUE s
, int line
)
646 VALUE str
= rb_str_new_frozen(s
);
648 parser
->type
= lex_type_str
;
649 parser
->data
.lex_str
.str
= str
;
650 parser
->data
.lex_str
.ptr
= 0;
652 return parser_compile(parser
->parser_params
, lex_get_str
, fname
, (rb_parser_input_data
)&parser
->data
, line
);
656 must_be_ascii_compatible(VALUE s
)
658 rb_encoding
*enc
= rb_enc_get(s
);
659 if (!rb_enc_asciicompat(enc
)) {
660 rb_raise(rb_eArgError
, "invalid source encoding");
666 parser_compile_string_path(struct ruby_parser
*parser
, VALUE f
, VALUE s
, int line
)
668 must_be_ascii_compatible(s
);
669 return parser_compile_string0(parser
, f
, s
, line
);
673 parser_compile_string(struct ruby_parser
*parser
, const char *f
, VALUE s
, int line
)
675 return parser_compile_string_path(parser
, rb_filesystem_str_new_cstr(f
), s
, line
);
678 VALUE
rb_io_gets_internal(VALUE io
);
680 static rb_parser_string_t
*
681 lex_io_gets(struct parser_params
*p
, rb_parser_input_data input
, int line_count
)
683 VALUE io
= (VALUE
)input
;
684 VALUE line
= rb_io_gets_internal(io
);
685 if (NIL_P(line
)) return 0;
686 return rb_str_to_parser_string(p
, line
);
689 static rb_parser_string_t
*
690 lex_gets_array(struct parser_params
*p
, rb_parser_input_data data
, int index
)
692 VALUE array
= (VALUE
)data
;
693 VALUE str
= rb_ary_entry(array
, index
);
696 if (!rb_enc_asciicompat(rb_enc_get(str
))) {
697 rb_raise(rb_eArgError
, "invalid source encoding");
699 return rb_str_to_parser_string(p
, str
);
707 parser_compile_file_path(struct ruby_parser
*parser
, VALUE fname
, VALUE file
, int start
)
709 parser
->type
= lex_type_io
;
710 parser
->data
.lex_io
.file
= file
;
712 return parser_compile(parser
->parser_params
, lex_io_gets
, fname
, (rb_parser_input_data
)file
, start
);
716 parser_compile_array(struct ruby_parser
*parser
, VALUE fname
, VALUE array
, int start
)
718 parser
->type
= lex_type_array
;
719 parser
->data
.lex_array
.ary
= array
;
721 return parser_compile(parser
->parser_params
, lex_gets_array
, fname
, (rb_parser_input_data
)array
, start
);
725 parser_compile_generic(struct ruby_parser
*parser
, rb_parser_lex_gets_func
*lex_gets
, VALUE fname
, VALUE input
, int start
)
727 parser
->type
= lex_type_generic
;
729 return parser_compile(parser
->parser_params
, lex_gets
, fname
, (rb_parser_input_data
)input
, start
);
735 rb_ast_t
*ast
= (rb_ast_t
*)ptr
;
739 static const rb_data_type_t ast_data_type
= {
744 NULL
, // No dsize() because this object does not appear in ObjectSpace.
746 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
752 return TypedData_Wrap_Struct(0, &ast_data_type
, NULL
);
756 rb_parser_compile_file_path(VALUE vparser
, VALUE fname
, VALUE file
, int start
)
758 struct ruby_parser
*parser
;
759 VALUE ast_value
= ast_alloc();
761 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
762 DATA_PTR(ast_value
) = parser_compile_file_path(parser
, fname
, file
, start
);
763 RB_GC_GUARD(vparser
);
769 rb_parser_compile_array(VALUE vparser
, VALUE fname
, VALUE array
, int start
)
771 struct ruby_parser
*parser
;
772 VALUE ast_value
= ast_alloc();
774 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
775 DATA_PTR(ast_value
) = parser_compile_array(parser
, fname
, array
, start
);
776 RB_GC_GUARD(vparser
);
782 rb_parser_compile_generic(VALUE vparser
, rb_parser_lex_gets_func
*lex_gets
, VALUE fname
, VALUE input
, int start
)
784 struct ruby_parser
*parser
;
785 VALUE ast_value
= ast_alloc();
787 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
788 DATA_PTR(ast_value
) = parser_compile_generic(parser
, lex_gets
, fname
, input
, start
);
789 RB_GC_GUARD(vparser
);
795 rb_parser_compile_string(VALUE vparser
, const char *f
, VALUE s
, int line
)
797 struct ruby_parser
*parser
;
798 VALUE ast_value
= ast_alloc();
800 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
801 DATA_PTR(ast_value
) = parser_compile_string(parser
, f
, s
, line
);
802 RB_GC_GUARD(vparser
);
808 rb_parser_compile_string_path(VALUE vparser
, VALUE f
, VALUE s
, int line
)
810 struct ruby_parser
*parser
;
811 VALUE ast_value
= ast_alloc();
813 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
814 DATA_PTR(ast_value
) = parser_compile_string_path(parser
, f
, s
, line
);
815 RB_GC_GUARD(vparser
);
821 rb_parser_encoding(VALUE vparser
)
823 struct ruby_parser
*parser
;
825 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
826 return rb_enc_from_encoding(rb_ruby_parser_encoding(parser
->parser_params
));
830 rb_parser_end_seen_p(VALUE vparser
)
832 struct ruby_parser
*parser
;
834 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
835 return RBOOL(rb_ruby_parser_end_seen_p(parser
->parser_params
));
839 rb_parser_set_yydebug(VALUE vparser
, VALUE flag
)
841 struct ruby_parser
*parser
;
843 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
844 rb_ruby_parser_set_yydebug(parser
->parser_params
, RTEST(flag
));
849 rb_set_script_lines_for(VALUE vparser
, VALUE path
)
851 struct ruby_parser
*parser
;
854 CONST_ID(script_lines
, "SCRIPT_LINES__");
855 if (!rb_const_defined_at(rb_cObject
, script_lines
)) return;
856 hash
= rb_const_get_at(rb_cObject
, script_lines
);
857 if (RB_TYPE_P(hash
, T_HASH
)) {
858 rb_hash_aset(hash
, path
, Qtrue
);
859 TypedData_Get_Struct(vparser
, struct ruby_parser
, &ruby_parser_data_type
, parser
);
860 rb_ruby_parser_set_script_lines(parser
->parser_params
);
865 rb_parser_build_script_lines_from(rb_parser_ary_t
*lines
)
868 if (!lines
) return Qnil
;
869 if (lines
->data_type
!= PARSER_ARY_DATA_SCRIPT_LINE
) {
870 rb_bug("unexpected rb_parser_ary_data_type (%d) for script lines", lines
->data_type
);
872 VALUE script_lines
= rb_ary_new_capa(lines
->len
);
873 for (i
= 0; i
< lines
->len
; i
++) {
874 rb_parser_string_t
*str
= (rb_parser_string_t
*)lines
->data
[i
];
875 rb_ary_push(script_lines
, rb_enc_str_new(str
->ptr
, str
->len
, str
->enc
));
881 rb_str_new_parser_string(rb_parser_string_t
*str
)
883 VALUE string
= rb_enc_literal_str(str
->ptr
, str
->len
, str
->enc
);
884 rb_enc_str_coderange(string
);
889 rb_str_new_mutable_parser_string(rb_parser_string_t
*str
)
891 return rb_enc_str_new(str
->ptr
, str
->len
, str
->enc
);
895 negative_numeric(VALUE val
)
898 return LONG2FIX(-FIX2LONG(val
));
900 if (SPECIAL_CONST_P(val
)) {
903 return DBL2NUM(-RFLOAT_VALUE(val
));
908 switch (BUILTIN_TYPE(val
)) {
911 val
= rb_big_norm(val
);
914 RATIONAL_SET_NUM(val
, negative_numeric(RRATIONAL(val
)->num
));
917 RCOMPLEX_SET_REAL(val
, negative_numeric(RCOMPLEX(val
)->real
));
918 RCOMPLEX_SET_IMAG(val
, negative_numeric(RCOMPLEX(val
)->imag
));
921 val
= DBL2NUM(-RFLOAT_VALUE(val
));
925 rb_bug("unknown literal type (%s) passed to negative_numeric",
926 rb_builtin_class_name(val
));
933 integer_value(const char *val
, int base
)
935 return rb_cstr_to_inum(val
, base
, FALSE
);
939 rational_value(const char *node_val
, int base
, int seen_point
)
942 char* val
= strdup(node_val
);
943 if (seen_point
> 0) {
944 int len
= (int)(strlen(val
));
945 char *point
= &val
[seen_point
];
946 size_t fraclen
= len
-seen_point
-1;
947 memmove(point
, point
+1, fraclen
+1);
949 lit
= rb_rational_new(integer_value(val
, base
), rb_int_positive_pow(10, fraclen
));
952 lit
= rb_rational_raw1(integer_value(val
, base
));
961 rb_node_integer_literal_val(const NODE
*n
)
963 const rb_node_integer_t
*node
= RNODE_INTEGER(n
);
964 VALUE val
= integer_value(node
->val
, node
->base
);
966 val
= negative_numeric(val
);
972 rb_node_float_literal_val(const NODE
*n
)
974 const rb_node_float_t
*node
= RNODE_FLOAT(n
);
975 double d
= strtod(node
->val
, 0);
979 VALUE val
= DBL2NUM(d
);
984 rb_node_rational_literal_val(const NODE
*n
)
987 const rb_node_rational_t
*node
= RNODE_RATIONAL(n
);
989 lit
= rational_value(node
->val
, node
->base
, node
->seen_point
);
992 lit
= negative_numeric(lit
);
999 rb_node_imaginary_literal_val(const NODE
*n
)
1002 const rb_node_imaginary_t
*node
= RNODE_IMAGINARY(n
);
1004 enum rb_numeric_type type
= node
->type
;
1007 case integer_literal
:
1008 lit
= integer_value(node
->val
, node
->base
);
1010 case float_literal
:{
1011 double d
= strtod(node
->val
, 0);
1015 case rational_literal
:
1016 lit
= rational_value(node
->val
, node
->base
, node
->seen_point
);
1019 rb_bug("unreachable");
1022 lit
= rb_complex_raw(INT2FIX(0), lit
);
1025 lit
= negative_numeric(lit
);
1031 rb_node_str_string_val(const NODE
*node
)
1033 rb_parser_string_t
*str
= RNODE_STR(node
)->string
;
1034 return rb_str_new_parser_string(str
);
1038 rb_node_sym_string_val(const NODE
*node
)
1040 rb_parser_string_t
*str
= RNODE_SYM(node
)->string
;
1041 return ID2SYM(rb_intern3(str
->ptr
, str
->len
, str
->enc
));
1045 rb_node_dstr_string_val(const NODE
*node
)
1047 rb_parser_string_t
*str
= RNODE_DSTR(node
)->string
;
1048 return str
? rb_str_new_parser_string(str
) : Qnil
;
1052 rb_node_dregx_string_val(const NODE
*node
)
1054 rb_parser_string_t
*str
= RNODE_DREGX(node
)->string
;
1055 return rb_str_new_parser_string(str
);
1059 rb_node_regx_string_val(const NODE
*node
)
1061 rb_node_regx_t
*node_reg
= RNODE_REGX(node
);
1062 rb_parser_string_t
*string
= node_reg
->string
;
1063 VALUE str
= rb_enc_str_new(string
->ptr
, string
->len
, string
->enc
);
1065 return rb_reg_compile(str
, node_reg
->options
, NULL
, 0);
1069 rb_node_line_lineno_val(const NODE
*node
)
1071 return INT2FIX(node
->nd_loc
.beg_pos
.lineno
);
1075 rb_node_file_path_val(const NODE
*node
)
1077 return rb_str_new_parser_string(RNODE_FILE(node
)->path
);
1081 rb_node_encoding_val(const NODE
*node
)
1083 return rb_enc_from_encoding(RNODE_ENCODING(node
)->enc
);
1087 parser_aset_script_lines_for(VALUE path
, rb_parser_ary_t
*lines
)
1089 VALUE hash
, script_lines
;
1091 if (NIL_P(path
) || !lines
) return;
1092 CONST_ID(script_lines_id
, "SCRIPT_LINES__");
1093 if (!rb_const_defined_at(rb_cObject
, script_lines_id
)) return;
1094 hash
= rb_const_get_at(rb_cObject
, script_lines_id
);
1095 if (!RB_TYPE_P(hash
, T_HASH
)) return;
1096 if (rb_hash_lookup(hash
, path
) == Qnil
) return;
1097 script_lines
= rb_parser_build_script_lines_from(lines
);
1098 rb_hash_aset(hash
, path
, script_lines
);
1102 rb_ruby_ast_new(const NODE
*const root
)
1105 VALUE ast_value
= TypedData_Make_Struct(0, rb_ast_t
, &ast_data_type
, ast
);
1106 #ifdef UNIVERSAL_PARSER
1107 ast
->config
= &rb_global_parser_config
;
1109 ast
->body
= (rb_ast_body_t
){
1111 .frozen_string_literal
= -1,
1112 .coverage_enabled
= -1,
1113 .script_lines
= NULL
,
1120 rb_ruby_ast_data_get(VALUE ast_value
)
1123 if (NIL_P(ast_value
)) return NULL
;
1124 TypedData_Get_Struct(ast_value
, rb_ast_t
, &ast_data_type
, ast
);