Ruby: ruby_parser.c Source File

Ruby 3.3.6p108 (2024-11-05 revision 75015d4c1f6965b5e85e96fb309f1f2129f933c0)

ruby_parser.c
1/* This is a wrapper for parse.y */
2#ifdef UNIVERSAL_PARSER
3
4#include "internal.h"
5#include "internal/array.h"
6#include "internal/bignum.h"
7#include "internal/compile.h"
8#include "internal/complex.h"
9#include "internal/encoding.h"
10#include "internal/error.h"
11#include "internal/gc.h"
12#include "internal/hash.h"
13#include "internal/io.h"
14#include "internal/parse.h"
15#include "internal/rational.h"
16#include "internal/re.h"
17#include "internal/ruby_parser.h"
18#include "internal/string.h"
19#include "internal/symbol.h"
20#include "internal/thread.h"
21
22#include "ruby/ractor.h"
23#include "ruby/ruby.h"
24#include "ruby/util.h"
25#include "node.h"
26#include "internal.h"
27#include "vm_core.h"
28#include "symbol.h"
29
30struct ruby_parser {
31 rb_parser_t *parser_params;
32};
33
34static void
35parser_mark(void *ptr)
36{
37 struct ruby_parser *parser = (struct ruby_parser*)ptr;
38 rb_ruby_parser_mark(parser->parser_params);
39}
40
41static void
42parser_free(void *ptr)
43{
44 struct ruby_parser *parser = (struct ruby_parser*)ptr;
45 rb_ruby_parser_free(parser->parser_params);
46}
47
48static size_t
49parser_memsize(const void *ptr)
50{
51 struct ruby_parser *parser = (struct ruby_parser*)ptr;
52 return rb_ruby_parser_memsize(parser->parser_params);
53}
54
55static const rb_data_type_t ruby_parser_data_type = {
56 "parser",
57 {
58 parser_mark,
59 parser_free,
60 parser_memsize,
61 },
62 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
63};
64
65static void
66bignum_negate(VALUE b)
67{
68 BIGNUM_NEGATE(b);
69}
70
71static int
72is_ascii_string2(VALUE str)
73{
74 return is_ascii_string(str);
75}
76
77static void
78rational_set_num(VALUE r, VALUE n)
79{
80 RATIONAL_SET_NUM(r, n);
81}
82
83static VALUE
84rational_get_num(VALUE obj)
85{
86 return RRATIONAL(obj)->num;
87}
88
89static void
90rcomplex_set_real(VALUE cmp, VALUE r)
91{
92 RCOMPLEX_SET_REAL(cmp, r);
93}
94
95static void
96rcomplex_set_imag(VALUE cmp, VALUE i)
97{
98 RCOMPLEX_SET_IMAG(cmp, i);
99}
100
101static VALUE
102rcomplex_get_real(VALUE obj)
103{
104 return RCOMPLEX(obj)->real;
105}
106
107static VALUE
108rcomplex_get_imag(VALUE obj)
109{
110 return RCOMPLEX(obj)->imag;
111}
112
113 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 6, 0)
114static VALUE
115syntax_error_append(VALUE exc, VALUE file, int line, int column,
116 void *enc, const char *fmt, va_list args)
117{
118 return rb_syntax_error_append(exc, file, line, column, (rb_encoding *)enc, fmt, args);
119}
120
121static int
122local_defined(ID id, const void *p)
123{
124 return rb_local_defined(id, (const rb_iseq_t *)p);
125}
126
127static int
128dvar_defined(ID id, const void *p)
129{
130 return rb_dvar_defined(id, (const rb_iseq_t *)p);
131}
132
133static bool
134hash_literal_key_p(VALUE k)
135{
136 switch (OBJ_BUILTIN_TYPE(k)) {
137 case T_NODE:
138 return false;
139 default:
140 return true;
141 }
142}
143
144static int
145literal_cmp(VALUE val, VALUE lit)
146{
147 if (val == lit) return 0;
148 if (!hash_literal_key_p(val) || !hash_literal_key_p(lit)) return -1;
149 return rb_iseq_cdhash_cmp(val, lit);
150}
151
152static st_index_t
153literal_hash(VALUE a)
154{
155 if (!hash_literal_key_p(a)) return (st_index_t)a;
156 return rb_iseq_cdhash_hash(a);
157}
158
159static int
160is_usascii_enc(void *enc)
161{
162 return rb_is_usascii_enc((rb_encoding *)enc);
163}
164
165static int
166is_local_id2(ID id)
167{
168 return is_local_id(id);
169}
170
171static int
172is_attrset_id2(ID id)
173{
174 return is_attrset_id(id);
175}
176
177static int
178is_notop_id2(ID id)
179{
180 return is_notop_id(id);
181}
182
183static VALUE
184enc_str_new(const char *ptr, long len, void *enc)
185{
186 return rb_enc_str_new(ptr, len, (rb_encoding *)enc);
187}
188
189static int
190enc_isalnum(OnigCodePoint c, void *enc)
191{
192 return rb_enc_isalnum(c, (rb_encoding *)enc);
193}
194
195static int
196enc_precise_mbclen(const char *p, const char *e, void *enc)
197{
198 return rb_enc_precise_mbclen(p, e, (rb_encoding *)enc);
199}
200
201static int
202mbclen_charfound_p(int len)
203{
204 return MBCLEN_CHARFOUND_P(len);
205}
206
207static const char *
208enc_name(void *enc)
209{
210 return rb_enc_name((rb_encoding *)enc);
211}
212
213static char *
214enc_prev_char(const char *s, const char *p, const char *e, void *enc)
215{
216 return rb_enc_prev_char(s, p, e, (rb_encoding *)enc);
217}
218
219static void *
220enc_get(VALUE obj)
221{
222 return (void *)rb_enc_get(obj);
223}
224
225static int
226enc_asciicompat(void *enc)
227{
228 return rb_enc_asciicompat((rb_encoding *)enc);
229}
230
231static void *
232utf8_encoding(void)
233{
234 return (void *)rb_utf8_encoding();
235}
236
237static VALUE
238enc_associate(VALUE obj, void *enc)
239{
240 return rb_enc_associate(obj, (rb_encoding *)enc);
241}
242
243static void *
244ascii8bit_encoding(void)
245{
246 return (void *)rb_ascii8bit_encoding();
247}
248
249static int
250enc_codelen(int c, void *enc)
251{
252 return rb_enc_codelen(c, (rb_encoding *)enc);
253}
254
255static VALUE
256enc_str_buf_cat(VALUE str, const char *ptr, long len, void *enc)
257{
258 return rb_enc_str_buf_cat(str, ptr, len, (rb_encoding *)enc);
259}
260
261static int
262enc_mbcput(unsigned int c, void *buf, void *enc)
263{
264 return rb_enc_mbcput(c, buf, (rb_encoding *)enc);
265}
266
267static void *
268enc_from_index(int idx)
269{
270 return (void *)rb_enc_from_index(idx);
271}
272
273static int
274enc_isspace(OnigCodePoint c, void *enc)
275{
276 return rb_enc_isspace(c, (rb_encoding *)enc);
277}
278
279static ID
280intern3(const char *name, long len, void *enc)
281{
282 return rb_intern3(name, len, (rb_encoding *)enc);
283}
284
285static void *
286enc_compatible(VALUE str1, VALUE str2)
287{
288 return (void *)rb_enc_compatible(str1, str2);
289}
290
291static VALUE
292enc_from_encoding(void *enc)
293{
294 return rb_enc_from_encoding((rb_encoding *)enc);
295}
296
297static int
298encoding_get(VALUE obj)
299{
300 return ENCODING_GET(obj);
301}
302
303static void
304encoding_set(VALUE obj, int encindex)
305{
306 ENCODING_SET(obj, encindex);
307}
308
309static int
310encoding_is_ascii8bit(VALUE obj)
311{
312 return ENCODING_IS_ASCII8BIT(obj);
313}
314
315static void *
316usascii_encoding(void)
317{
318 return (void *)rb_usascii_encoding();
319}
320
321static int
322enc_symname_type(const char *name, long len, void *enc, unsigned int allowed_attrset)
323{
324 return rb_enc_symname_type(name, len, (rb_encoding *)enc, allowed_attrset);
325}
326
327typedef struct {
328 struct parser_params *parser;
329 rb_encoding *enc;
330 NODE *succ_block;
331 const rb_code_location_t *loc;
332} reg_named_capture_assign_t;
333
334static int
335reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end,
336 int back_num, int *back_refs, OnigRegex regex, void *arg0)
337{
338 reg_named_capture_assign_t *arg = (reg_named_capture_assign_t*)arg0;
339 struct parser_params* p = arg->parser;
340 rb_encoding *enc = arg->enc;
341 const rb_code_location_t *loc = arg->loc;
342 long len = name_end - name;
343 const char *s = (const char *)name;
344
345 return rb_reg_named_capture_assign_iter_impl(p, s, len, (void *)enc, &arg->succ_block, loc);
346}
347
348static NODE *
349reg_named_capture_assign(struct parser_params* p, VALUE regexp, const rb_code_location_t *loc)
350{
351 reg_named_capture_assign_t arg;
352
353 arg.parser = p;
354 arg.enc = rb_enc_get(regexp);
355 arg.succ_block = 0;
356 arg.loc = loc;
357 onig_foreach_name(RREGEXP_PTR(regexp), reg_named_capture_assign_iter, &arg);
358
359 if (!arg.succ_block) return 0;
360 return RNODE_BLOCK(arg.succ_block)->nd_next;
361}
362
363static VALUE
364rbool(VALUE v)
365{
366 return RBOOL(v);
367}
368
369static int
370undef_p(VALUE v)
371{
372 return RB_UNDEF_P(v);
373}
374
375static int
376rtest(VALUE obj)
377{
378 return (int)RB_TEST(obj);
379}
380
381static int
382nil_p(VALUE obj)
383{
384 return (int)NIL_P(obj);
385}
386
387static int
388flonum_p(VALUE obj)
389{
390 return (int)RB_FLONUM_P(obj);
391}
392
393static VALUE
394int2fix(long i)
395{
396 return INT2FIX(i);
397}
398
399static VALUE
400syntax_error_new(void)
401{
402 return rb_class_new_instance(0, 0, rb_eSyntaxError);
403}
404
405static int
406obj_frozen(VALUE obj)
407{
408 return (int)RB_OBJ_FROZEN(obj);
409}
410
411static VALUE
412obj_write(VALUE old, VALUE *slot, VALUE young)
413{
414 return RB_OBJ_WRITE(old, slot, young);
415}
416
417static VALUE
418obj_written(VALUE old, VALUE slot, VALUE young)
419{
420 return RB_OBJ_WRITTEN(old, slot, young);
421}
422
423static VALUE
424default_rs(void)
425{
426 return rb_default_rs;
427}
428
429static VALUE
430rational_raw1(VALUE x)
431{
432 return rb_rational_raw1(x);
433}
434
435static void *
436memmove2(void *dest, const void *src, size_t t, size_t n)
437{
438 return memmove(dest, src, rbimpl_size_mul_or_raise(t, n));
439}
440
441static void *
442nonempty_memcpy(void *dest, const void *src, size_t t, size_t n)
443{
444 return ruby_nonempty_memcpy(dest, src, rbimpl_size_mul_or_raise(t, n));
445}
446
447static VALUE
448ruby_verbose2(void)
449{
450 return ruby_verbose;
451}
452
453static int
454type_p(VALUE obj, int t)
455{
456 return (int)RB_TYPE_P(obj, t);
457}
458
459static int
460fixnum_p(VALUE obj)
461{
462 return (int)RB_FIXNUM_P(obj);
463}
464
465static int
466symbol_p(VALUE obj)
467{
468 return (int)RB_SYMBOL_P(obj);
469}
470
471static void *
472zalloc(size_t elemsiz)
473{
474 return ruby_xcalloc(1, elemsiz);
475}
476
477static void
478gc_guard(VALUE obj)
479{
480 RB_GC_GUARD(obj);
481}
482
483static rb_imemo_tmpbuf_t *
484tmpbuf_parser_heap(void *buf, rb_imemo_tmpbuf_t *old_heap, size_t cnt)
485{
486 return rb_imemo_tmpbuf_parser_heap(buf, old_heap, cnt);
487}
488
489static int
490special_const_p(VALUE obj)
491{
492 return (int)RB_SPECIAL_CONST_P(obj);
493}
494
495static int
496builtin_type(VALUE obj)
497{
498 return (int)RB_BUILTIN_TYPE(obj);
499}
500
501static rb_ast_t *
502ast_new(VALUE nb)
503{
504 rb_ast_t *ast = (rb_ast_t *)rb_imemo_new(imemo_ast, 0, 0, 0, nb);
505 return ast;
506}
507
508static VALUE
509static_id2sym(ID id)
510{
511 return (((VALUE)(id)<<RUBY_SPECIAL_SHIFT)|SYMBOL_FLAG);
512}
513
514static long
515str_coderange_scan_restartable(const char *s, const char *e, void *enc, int *cr)
516{
517 return rb_str_coderange_scan_restartable(s, e, (rb_encoding *)enc, cr);
518}
519
520 VALUE rb_io_gets_internal(VALUE io);
521extern VALUE rb_mRubyVMFrozenCore;
522 VALUE rb_node_case_when_optimizable_literal(const NODE *const node);
523
524void
525rb_parser_config_initialize(rb_parser_config_t *config)
526{
527 config->counter = 0;
528
529 config->malloc = ruby_xmalloc;
530 config->calloc = ruby_xcalloc;
531 config->realloc = ruby_xrealloc;
532 config->free = ruby_xfree;
533 config->alloc_n = ruby_xmalloc2;
534 config->alloc = ruby_xmalloc;
535 config->realloc_n = ruby_xrealloc2;
536 config->zalloc = zalloc;
537 config->rb_memmove = memmove2;
538 config->nonempty_memcpy = nonempty_memcpy;
539 config->xmalloc_mul_add = rb_xmalloc_mul_add;
540
541 config->tmpbuf_parser_heap = tmpbuf_parser_heap;
542 config->ast_new = ast_new;
543
544 config->compile_callback = rb_suppress_tracing;
545 config->reg_named_capture_assign = reg_named_capture_assign;
546
547 config->obj_freeze = rb_obj_freeze;
548 config->obj_hide = rb_obj_hide;
549 config->obj_frozen = obj_frozen;
550 config->type_p = type_p;
551 config->obj_freeze_raw = OBJ_FREEZE_RAW;
552
553 config->fixnum_p = fixnum_p;
554 config->symbol_p = symbol_p;
555
556 config->attr_get = rb_attr_get;
557
558 config->ary_new = rb_ary_new;
559 config->ary_push = rb_ary_push;
560 config->ary_new_from_args = rb_ary_new_from_args;
561 config->ary_pop = rb_ary_pop;
562 config->ary_last = rb_ary_last;
563 config->ary_unshift = rb_ary_unshift;
564 config->ary_new2 = rb_ary_new2;
565 config->ary_entry = rb_ary_entry;
566 config->ary_join = rb_ary_join;
567 config->ary_reverse = rb_ary_reverse;
568 config->ary_clear = rb_ary_clear;
569 config->array_len = rb_array_len;
570 config->array_aref = RARRAY_AREF;
571
572 config->sym_intern_ascii_cstr = rb_sym_intern_ascii_cstr;
573 config->make_temporary_id = rb_make_temporary_id;
574 config->is_local_id = is_local_id2;
575 config->is_attrset_id = is_attrset_id2;
576 config->is_global_name_punct = is_global_name_punct;
577 config->id_type = id_type;
578 config->id_attrset = rb_id_attrset;
579 config->intern = rb_intern;
580 config->intern2 = rb_intern2;
581 config->intern3 = intern3;
582 config->intern_str = rb_intern_str;
583 config->is_notop_id = is_notop_id2;
584 config->enc_symname_type = enc_symname_type;
585 config->str_intern = rb_str_intern;
586 config->id2name = rb_id2name;
587 config->id2str = rb_id2str;
588 config->id2sym = rb_id2sym;
589 config->sym2id = rb_sym2id;
590
591 config->str_catf = rb_str_catf;
592 config->str_cat_cstr = rb_str_cat_cstr;
593 config->str_subseq = rb_str_subseq;
594 config->str_dup = rb_str_dup;
595 config->str_new_frozen = rb_str_new_frozen;
596 config->str_buf_new = rb_str_buf_new;
597 config->str_buf_cat = rb_str_buf_cat;
598 config->str_modify = rb_str_modify;
599 config->str_set_len = rb_str_set_len;
600 config->str_cat = rb_str_cat;
601 config->str_resize = rb_str_resize;
602 config->str_new = rb_str_new;
603 config->str_new_cstr = rb_str_new_cstr;
604 config->fstring = rb_fstring;
605 config->is_ascii_string = is_ascii_string2;
606 config->enc_str_new = enc_str_new;
607 config->enc_str_buf_cat = enc_str_buf_cat;
608 config->str_buf_append = rb_str_buf_append;
609 config->str_vcatf = rb_str_vcatf;
610 config->string_value_cstr = rb_string_value_cstr;
611 config->rb_sprintf = rb_sprintf;
612 config->rstring_ptr = RSTRING_PTR;
613 config->rstring_end = RSTRING_END;
614 config->rstring_len = RSTRING_LEN;
615 config->filesystem_str_new_cstr = rb_filesystem_str_new_cstr;
616 config->obj_as_string = rb_obj_as_string;
617
618 config->hash_clear = rb_hash_clear;
619 config->hash_new = rb_hash_new;
620 config->hash_aset = rb_hash_aset;
621 config->hash_lookup = rb_hash_lookup;
622 config->hash_delete = rb_hash_delete;
623 config->ident_hash_new = rb_ident_hash_new;
624
625 config->int2fix = int2fix;
626
627 config->bignum_negate = bignum_negate;
628 config->big_norm = rb_big_norm;
629 config->cstr_to_inum = rb_cstr_to_inum;
630
631 config->float_new = rb_float_new;
632 config->float_value = rb_float_value;
633
634 config->num2int = rb_num2int_inline;
635 config->int_positive_pow = rb_int_positive_pow;
636 config->int2num = rb_int2num_inline;
637 config->fix2long = rb_fix2long;
638
639 config->rational_new = rb_rational_new;
640 config->rational_raw1 = rational_raw1;
641 config->rational_set_num = rational_set_num;
642 config->rational_get_num = rational_get_num;
643
644 config->complex_raw = rb_complex_raw;
645 config->rcomplex_set_real = rcomplex_set_real;
646 config->rcomplex_set_imag = rcomplex_set_imag;
647 config->rcomplex_get_real = rcomplex_get_real;
648 config->rcomplex_get_imag = rcomplex_get_imag;
649
650 config->stderr_tty_p = rb_stderr_tty_p;
651 config->write_error_str = rb_write_error_str;
652 config->default_rs = default_rs;
653 config->io_write = rb_io_write;
654 config->io_flush = rb_io_flush;
655 config->io_puts = rb_io_puts;
656 config->io_gets_internal= rb_io_gets_internal;
657
658 config->debug_output_stdout = rb_ractor_stdout;
659 config->debug_output_stderr = rb_ractor_stderr;
660
661 config->is_usascii_enc = is_usascii_enc;
662 config->enc_isalnum = enc_isalnum;
663 config->enc_precise_mbclen = enc_precise_mbclen;
664 config->mbclen_charfound_p = mbclen_charfound_p;
665 config->enc_name = enc_name;
666 config->enc_prev_char = enc_prev_char;
667 config->enc_get = enc_get;
668 config->enc_asciicompat = enc_asciicompat;
669 config->utf8_encoding = utf8_encoding;
670 config->enc_associate = enc_associate;
671 config->ascii8bit_encoding = ascii8bit_encoding;
672 config->enc_codelen = enc_codelen;
673 config->enc_mbcput = enc_mbcput;
674 config->char_to_option_kcode = rb_char_to_option_kcode;
675 config->ascii8bit_encindex = rb_ascii8bit_encindex;
676 config->enc_find_index = rb_enc_find_index;
677 config->enc_from_index = enc_from_index;
678 config->enc_associate_index = rb_enc_associate_index;
679 config->enc_isspace = enc_isspace;
680 config->enc_coderange_7bit = ENC_CODERANGE_7BIT;
681 config->enc_coderange_unknown = ENC_CODERANGE_UNKNOWN;
682 config->enc_coderange_broken = ENC_CODERANGE_BROKEN;
683 config->enc_compatible = enc_compatible;
684 config->enc_from_encoding = enc_from_encoding;
685 config->encoding_get = encoding_get;
686 config->encoding_set = encoding_set;
687 config->encoding_is_ascii8bit = encoding_is_ascii8bit;
688 config->usascii_encoding = usascii_encoding;
689 config->enc_str_coderange = rb_enc_str_coderange;
690
691 config->ractor_make_shareable = rb_ractor_make_shareable;
692
693 config->local_defined = local_defined;
694 config->dvar_defined = dvar_defined;
695
696 config->literal_cmp = literal_cmp;
697 config->literal_hash = literal_hash;
698
699 config->builtin_class_name = rb_builtin_class_name;
700 config->syntax_error_append = syntax_error_append;
701 config->raise = rb_raise;
702 config->syntax_error_new = syntax_error_new;
703
704 config->errinfo = rb_errinfo;
705 config->set_errinfo = rb_set_errinfo;
706 config->exc_raise = rb_exc_raise;
707 config->make_exception = rb_make_exception;
708
709 config->sized_xfree = ruby_sized_xfree;
710 config->sized_realloc_n = ruby_sized_realloc_n;
711 config->obj_write = obj_write;
712 config->obj_written = obj_written;
713 config->gc_register_mark_object = rb_gc_register_mark_object;
714 config->gc_guard = gc_guard;
715 config->gc_mark = rb_gc_mark;
716 config->gc_mark_movable = rb_gc_mark_movable;
717 config->gc_location = rb_gc_location;
718
719 config->reg_compile = rb_reg_compile;
720 config->reg_check_preprocess = rb_reg_check_preprocess;
721 config->memcicmp = rb_memcicmp;
722
723 config->compile_warn = rb_compile_warn;
724 config->compile_warning = rb_compile_warning;
725 config->bug = rb_bug;
726 config->fatal = rb_fatal;
727 config->verbose = ruby_verbose2;
728
729 config->make_backtrace = rb_make_backtrace;
730
731 config->scan_hex = ruby_scan_hex;
732 config->scan_oct = ruby_scan_oct;
733 config->scan_digits = ruby_scan_digits;
734 config->strtod = ruby_strtod;
735
736 config->rbool = rbool;
737 config->undef_p = undef_p;
738 config->rtest = rtest;
739 config->nil_p = nil_p;
740 config->flonum_p = flonum_p;
741 config->qnil = Qnil;
742 config->qtrue = Qtrue;
743 config->qfalse = Qfalse;
744 config->qundef = Qundef;
745 config->eArgError = rb_eArgError;
746 config->mRubyVMFrozenCore = rb_mRubyVMFrozenCore;
747 config->long2int = rb_long2int;
748 config->special_const_p = special_const_p;
749 config->builtin_type = builtin_type;
750
751 config->node_case_when_optimizable_literal = rb_node_case_when_optimizable_literal;
752
753 /* For Ripper */
754 config->static_id2sym = static_id2sym;
755 config->str_coderange_scan_restartable = str_coderange_scan_restartable;
756}
757
758 VALUE
759rb_parser_new(void)
760{
761 struct ruby_parser *parser;
762 rb_parser_config_t *config;
763 rb_parser_t *parser_params;
764
765 config = rb_ruby_parser_config_new(ruby_xmalloc);
766 rb_parser_config_initialize(config);
767
768 /*
769 * Create parser_params ahead of vparser because
770 * rb_ruby_parser_new can run GC so if create vparser
771 * first, parser_mark tries to mark not initialized parser_params.
772 */
773 parser_params = rb_ruby_parser_new(config);
774 VALUE vparser = TypedData_Make_Struct(0, struct ruby_parser,
775 &ruby_parser_data_type, parser);
776 parser->parser_params = parser_params;
777
778 return vparser;
779}
780
781void
782rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
783{
784 struct ruby_parser *parser;
785
786 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
787 rb_ruby_parser_set_options(parser->parser_params, print, loop, chomp, split);
788}
789
790 VALUE
791rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
792{
793 struct ruby_parser *parser;
794
795 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
796 rb_ruby_parser_set_context(parser->parser_params, base, main);
797 return vparser;
798}
799
800void
801rb_parser_set_script_lines(VALUE vparser, VALUE lines)
802{
803 struct ruby_parser *parser;
804
805 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
806 rb_ruby_parser_set_script_lines(parser->parser_params, lines);
807}
808
809void
810rb_parser_error_tolerant(VALUE vparser)
811{
812 struct ruby_parser *parser;
813
814 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
815 rb_ruby_parser_error_tolerant(parser->parser_params);
816}
817
818 rb_ast_t*
819rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
820{
821 struct ruby_parser *parser;
822 rb_ast_t *ast;
823
824 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
825 ast = rb_ruby_parser_compile_file_path(parser->parser_params, fname, file, start);
826 RB_GC_GUARD(vparser);
827
828 return ast;
829}
830
831void
832rb_parser_keep_tokens(VALUE vparser)
833{
834 struct ruby_parser *parser;
835
836 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
837 rb_ruby_parser_keep_tokens(parser->parser_params);
838}
839
840 rb_ast_t*
841rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
842{
843 struct ruby_parser *parser;
844 rb_ast_t *ast;
845
846 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
847 ast = rb_ruby_parser_compile_generic(parser->parser_params, lex_gets, fname, input, start);
848 RB_GC_GUARD(vparser);
849
850 return ast;
851}
852
853 rb_ast_t*
854rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
855{
856 struct ruby_parser *parser;
857 rb_ast_t *ast;
858
859 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
860 ast = rb_ruby_parser_compile_string(parser->parser_params, f, s, line);
861 RB_GC_GUARD(vparser);
862
863 return ast;
864}
865
866 rb_ast_t*
867rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
868{
869 struct ruby_parser *parser;
870 rb_ast_t *ast;
871
872 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
873 ast = rb_ruby_parser_compile_string_path(parser->parser_params, f, s, line);
874 RB_GC_GUARD(vparser);
875
876 return ast;
877}
878
879 VALUE
880rb_parser_encoding(VALUE vparser)
881{
882 struct ruby_parser *parser;
883
884 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
885 return rb_ruby_parser_encoding(parser->parser_params);
886}
887
888 VALUE
889rb_parser_end_seen_p(VALUE vparser)
890{
891 struct ruby_parser *parser;
892
893 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
894 return RBOOL(rb_ruby_parser_end_seen_p(parser->parser_params));
895}
896
897 VALUE
898rb_parser_set_yydebug(VALUE vparser, VALUE flag)
899{
900 struct ruby_parser *parser;
901
902 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
903 rb_ruby_parser_set_yydebug(parser->parser_params, RTEST(flag));
904 return flag;
905}
906
907#else
908
909/* For "ISO C requires a translation unit to contain at least one declaration" */
910void
911rb_parser_dummy(void)
912{}
913#endif
RBIMPL_ATTR_FORMAT
#define RBIMPL_ATTR_FORMAT(x, y, z)
Wraps (or simulates) __attribute__((format))
Definition format.h:29
ENC_CODERANGE_7BIT
#define ENC_CODERANGE_7BIT
Old name of RUBY_ENC_CODERANGE_7BIT.
Definition coderange.h:180
Qundef
#define Qundef
Old name of RUBY_Qundef.
Definition special_consts.h:62
INT2FIX
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
OBJ_FREEZE_RAW
#define OBJ_FREEZE_RAW
Old name of RB_OBJ_FREEZE_RAW.
Definition fl_type.h:136
T_NODE
#define T_NODE
Old name of RUBY_T_NODE.
Definition value_type.h:73
ENC_CODERANGE_UNKNOWN
#define ENC_CODERANGE_UNKNOWN
Old name of RUBY_ENC_CODERANGE_UNKNOWN.
Definition coderange.h:179
ENCODING_GET
#define ENCODING_GET(obj)
Old name of RB_ENCODING_GET.
Definition encoding.h:108
SYMBOL_FLAG
#define SYMBOL_FLAG
Old name of RUBY_SYMBOL_FLAG.
Definition special_consts.h:69
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
ENC_CODERANGE_BROKEN
#define ENC_CODERANGE_BROKEN
Old name of RUBY_ENC_CODERANGE_BROKEN.
Definition coderange.h:182
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:515
ENCODING_SET
#define ENCODING_SET(obj, i)
Old name of RB_ENCODING_SET.
Definition encoding.h:106
ENCODING_IS_ASCII8BIT
#define ENCODING_IS_ASCII8BIT(obj)
Old name of RB_ENCODING_IS_ASCII8BIT.
Definition encoding.h:109
rb_ary_new2
#define rb_ary_new2
Old name of rb_ary_new_capa.
Definition array.h:651
ruby_verbose
#define ruby_verbose
This variable controls whether the interpreter is in debug mode.
Definition error.h:471
rb_eSyntaxError
VALUE rb_eSyntaxError
SyntaxError exception.
Definition error.c:1361
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:2099
RB_OBJ_WRITTEN
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
Definition gc.h:631
RB_OBJ_WRITE
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:619
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:653
rb_rational_raw1
#define rb_rational_raw1(x)
Shorthand of (x/1)r.
Definition rational.h:51
rb_str_new
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
Definition string.h:1498
rb_str_buf_cat
#define rb_str_buf_cat
Just another name of rb_str_cat.
Definition string.h:1681
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:1656
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:1514
rb_sym2id
ID rb_sym2id(VALUE obj)
Converts an instance of rb_cSymbol into an ID.
Definition symbol.c:900
len
int len
Length of the buffer.
Definition io.h:8
ractor.h
util.h
ruby_strtod
double ruby_strtod(const char *str, char **endptr)
Our own locale-insensitive version of strtod(3).
rb_num2int_inline
static int rb_num2int_inline(VALUE x)
Converts an instance of rb_cNumeric into C's int.
Definition int.h:158
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_fix2long
static long rb_fix2long(VALUE x)
Converts a Fixnum into C's long.
Definition long.h:231
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:161
rb_array_len
static long rb_array_len(VALUE a)
Queries the length of the array.
Definition rarray.h:255
RARRAY_AREF
#define RARRAY_AREF(a, i)
Definition rarray.h:403
RREGEXP_PTR
#define RREGEXP_PTR(obj)
Convenient accessor macro.
Definition rregexp.h:45
rb_string_value_cstr
char * rb_string_value_cstr(volatile VALUE *ptr)
Identical to rb_string_value_ptr(), except it additionally checks for the contents for viability as a...
Definition string.c:2658
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:515
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:497
ruby.h
RB_TEST
static bool RB_TEST(VALUE obj)
Emulates Ruby's "if" statement.
Definition special_consts.h:138
RB_FIXNUM_P
static bool RB_FIXNUM_P(VALUE obj)
Checks if the given object is a so-called Fixnum.
Definition special_consts.h:247
RB_SPECIAL_CONST_P
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
Definition special_consts.h:327
RUBY_SPECIAL_SHIFT
@ RUBY_SPECIAL_SHIFT
Least significant 8 bits are reserved.
Definition special_consts.h:119
RB_UNDEF_P
static bool RB_UNDEF_P(VALUE obj)
Checks if the given object is undef.
Definition special_consts.h:189
RTEST
#define RTEST
This is an old name of RB_TEST.
Definition special_consts.h:51
RB_FLONUM_P
static bool RB_FLONUM_P(VALUE obj)
Checks if the given object is a so-called Flonum.
Definition special_consts.h:288
OnigEncodingTypeST
Definition onigmo.h:160
RNode
Definition rubyparser.h:166
parser_params
Definition parse.c:481
rb_ast_struct
Definition rubyparser.h:1096
rb_code_location_struct
Definition rubyparser.h:160
rb_data_type_struct
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:200
rb_imemo_tmpbuf_struct
Definition imemo.h:91
rb_iseq_struct
Definition vm_core.h:534
re_pattern_buffer
Definition onigmo.h:756
reg_named_capture_assign_t
Definition parse.c:25376
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_BUILTIN_TYPE
static enum ruby_value_type RB_BUILTIN_TYPE(VALUE obj)
Queries the type of the object.
Definition value_type.h:181
RB_SYMBOL_P
static bool RB_SYMBOL_P(VALUE obj)
Queries if the object is an instance of rb_cSymbol.
Definition value_type.h:306