Ruby 3.3.0p0 (2023-12-25 revision 5124f9ac7513eb590c37717337c430cb93caa151)
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 {
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
113RBIMPL_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;
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{
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{
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{
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
520VALUE rb_io_gets_internal(VALUE io);
521extern VALUE rb_mRubyVMFrozenCore;
522VALUE 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_compatible = enc_compatible;
683 config->enc_from_encoding = enc_from_encoding;
684 config->encoding_get = encoding_get;
685 config->encoding_set = encoding_set;
686 config->encoding_is_ascii8bit = encoding_is_ascii8bit;
687 config->usascii_encoding = usascii_encoding;
688
689 config->ractor_make_shareable = rb_ractor_make_shareable;
690
691 config->local_defined = local_defined;
692 config->dvar_defined = dvar_defined;
693
694 config->literal_cmp = literal_cmp;
695 config->literal_hash = literal_hash;
696
697 config->builtin_class_name = rb_builtin_class_name;
698 config->syntax_error_append = syntax_error_append;
699 config->raise = rb_raise;
700 config->syntax_error_new = syntax_error_new;
701
702 config->errinfo = rb_errinfo;
703 config->set_errinfo = rb_set_errinfo;
704 config->exc_raise = rb_exc_raise;
705 config->make_exception = rb_make_exception;
706
707 config->sized_xfree = ruby_sized_xfree;
708 config->sized_realloc_n = ruby_sized_realloc_n;
709 config->obj_write = obj_write;
710 config->obj_written = obj_written;
711 config->gc_register_mark_object = rb_gc_register_mark_object;
712 config->gc_guard = gc_guard;
713 config->gc_mark = rb_gc_mark;
714 config->gc_mark_movable = rb_gc_mark_movable;
715 config->gc_location = rb_gc_location;
716
717 config->reg_compile = rb_reg_compile;
718 config->reg_check_preprocess = rb_reg_check_preprocess;
719 config->memcicmp = rb_memcicmp;
720
721 config->compile_warn = rb_compile_warn;
722 config->compile_warning = rb_compile_warning;
723 config->bug = rb_bug;
724 config->fatal = rb_fatal;
725 config->verbose = ruby_verbose2;
726
727 config->make_backtrace = rb_make_backtrace;
728
729 config->scan_hex = ruby_scan_hex;
730 config->scan_oct = ruby_scan_oct;
731 config->scan_digits = ruby_scan_digits;
732 config->strtod = ruby_strtod;
733
734 config->rbool = rbool;
735 config->undef_p = undef_p;
736 config->rtest = rtest;
737 config->nil_p = nil_p;
738 config->flonum_p = flonum_p;
739 config->qnil = Qnil;
740 config->qtrue = Qtrue;
741 config->qfalse = Qfalse;
742 config->qundef = Qundef;
743 config->eArgError = rb_eArgError;
744 config->mRubyVMFrozenCore = rb_mRubyVMFrozenCore;
745 config->long2int = rb_long2int;
746 config->special_const_p = special_const_p;
747 config->builtin_type = builtin_type;
748
749 config->node_case_when_optimizable_literal = rb_node_case_when_optimizable_literal;
750
751 /* For Ripper */
752 config->static_id2sym = static_id2sym;
753 config->str_coderange_scan_restartable = str_coderange_scan_restartable;
754}
755
756VALUE
757rb_parser_new(void)
758{
759 struct ruby_parser *parser;
760 rb_parser_config_t *config;
762
763 config = rb_ruby_parser_config_new(ruby_xmalloc);
764 rb_parser_config_initialize(config);
765
766 /*
767 * Create parser_params ahead of vparser because
768 * rb_ruby_parser_new can run GC so if create vparser
769 * first, parser_mark tries to mark not initialized parser_params.
770 */
771 parser_params = rb_ruby_parser_new(config);
772 VALUE vparser = TypedData_Make_Struct(0, struct ruby_parser,
773 &ruby_parser_data_type, parser);
774 parser->parser_params = parser_params;
775
776 return vparser;
777}
778
779void
780rb_parser_set_options(VALUE vparser, int print, int loop, int chomp, int split)
781{
782 struct ruby_parser *parser;
783
784 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
785 rb_ruby_parser_set_options(parser->parser_params, print, loop, chomp, split);
786}
787
788VALUE
789rb_parser_set_context(VALUE vparser, const struct rb_iseq_struct *base, int main)
790{
791 struct ruby_parser *parser;
792
793 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
794 rb_ruby_parser_set_context(parser->parser_params, base, main);
795 return vparser;
796}
797
798void
799rb_parser_set_script_lines(VALUE vparser, VALUE lines)
800{
801 struct ruby_parser *parser;
802
803 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
804 rb_ruby_parser_set_script_lines(parser->parser_params, lines);
805}
806
807void
808rb_parser_error_tolerant(VALUE vparser)
809{
810 struct ruby_parser *parser;
811
812 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
813 rb_ruby_parser_error_tolerant(parser->parser_params);
814}
815
817rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
818{
819 struct ruby_parser *parser;
820 rb_ast_t *ast;
821
822 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
823 ast = rb_ruby_parser_compile_file_path(parser->parser_params, fname, file, start);
824 RB_GC_GUARD(vparser);
825
826 return ast;
827}
828
829void
830rb_parser_keep_tokens(VALUE vparser)
831{
832 struct ruby_parser *parser;
833
834 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
835 rb_ruby_parser_keep_tokens(parser->parser_params);
836}
837
839rb_parser_compile_generic(VALUE vparser, VALUE (*lex_gets)(VALUE, int), VALUE fname, VALUE input, int start)
840{
841 struct ruby_parser *parser;
842 rb_ast_t *ast;
843
844 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
845 ast = rb_ruby_parser_compile_generic(parser->parser_params, lex_gets, fname, input, start);
846 RB_GC_GUARD(vparser);
847
848 return ast;
849}
850
852rb_parser_compile_string(VALUE vparser, const char *f, VALUE s, int line)
853{
854 struct ruby_parser *parser;
855 rb_ast_t *ast;
856
857 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
858 ast = rb_ruby_parser_compile_string(parser->parser_params, f, s, line);
859 RB_GC_GUARD(vparser);
860
861 return ast;
862}
863
865rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
866{
867 struct ruby_parser *parser;
868 rb_ast_t *ast;
869
870 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
871 ast = rb_ruby_parser_compile_string_path(parser->parser_params, f, s, line);
872 RB_GC_GUARD(vparser);
873
874 return ast;
875}
876
877VALUE
878rb_parser_encoding(VALUE vparser)
879{
880 struct ruby_parser *parser;
881
882 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
883 return rb_ruby_parser_encoding(parser->parser_params);
884}
885
886VALUE
887rb_parser_end_seen_p(VALUE vparser)
888{
889 struct ruby_parser *parser;
890
891 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
892 return RBOOL(rb_ruby_parser_end_seen_p(parser->parser_params));
893}
894
895VALUE
896rb_parser_set_yydebug(VALUE vparser, VALUE flag)
897{
898 struct ruby_parser *parser;
899
900 TypedData_Get_Struct(vparser, struct ruby_parser, &ruby_parser_data_type, parser);
901 rb_ruby_parser_set_yydebug(parser->parser_params, RTEST(flag));
902 return flag;
903}
904
905#else
906
907/* For "ISO C requires a translation unit to contain at least one declaration" */
908void
909rb_parser_dummy(void)
910{}
911#endif
#define RBIMPL_ATTR_FORMAT(x, y, z)
Wraps (or simulates) __attribute__((format))
Definition format.h:29
#define ENC_CODERANGE_7BIT
Old name of RUBY_ENC_CODERANGE_7BIT.
Definition coderange.h:180
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define OBJ_FREEZE_RAW
Old name of RB_OBJ_FREEZE_RAW.
Definition fl_type.h:136
#define T_NODE
Old name of RUBY_T_NODE.
Definition value_type.h:73
#define ENC_CODERANGE_UNKNOWN
Old name of RUBY_ENC_CODERANGE_UNKNOWN.
Definition coderange.h:179
#define ENCODING_GET(obj)
Old name of RB_ENCODING_GET.
Definition encoding.h:108
#define SYMBOL_FLAG
Old name of RUBY_SYMBOL_FLAG.
#define Qtrue
Old name of RUBY_Qtrue.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define NIL_P
Old name of RB_NIL_P.
#define MBCLEN_CHARFOUND_P(ret)
Old name of ONIGENC_MBCLEN_CHARFOUND_P.
Definition encoding.h:515
#define ENCODING_SET(obj, i)
Old name of RB_ENCODING_SET.
Definition encoding.h:106
#define ENCODING_IS_ASCII8BIT(obj)
Old name of RB_ENCODING_IS_ASCII8BIT.
Definition encoding.h:109
#define rb_ary_new2
Old name of rb_ary_new_capa.
Definition array.h:651
#define ruby_verbose
This variable controls whether the interpreter is in debug mode.
Definition error.h:471
VALUE rb_eSyntaxError
SyntaxError exception.
Definition error.c:1361
VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass)
Allocates, then initialises an instance of the given class.
Definition object.c:2090
#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
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:619
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
#define rb_rational_raw1(x)
Shorthand of (x/1)r.
Definition rational.h:51
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
Definition string.h:1498
#define rb_str_buf_cat
Just another name of rb_str_cat.
Definition string.h:1681
#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
ID rb_sym2id(VALUE obj)
Converts an instance of rb_cSymbol into an ID.
Definition symbol.c:897
int len
Length of the buffer.
Definition io.h:8
double ruby_strtod(const char *str, char **endptr)
Our own locale-insensitive version of strtod(3).
static int rb_num2int_inline(VALUE x)
Converts an instance of rb_cNumeric into C's int.
Definition int.h:158
static VALUE rb_int2num_inline(int v)
Converts a C's int into an instance of rb_cInteger.
Definition int.h:239
static long rb_fix2long(VALUE x)
Converts a Fixnum into C's long.
Definition long.h:231
#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:161
static long rb_array_len(VALUE a)
Queries the length of the array.
Definition rarray.h:255
#define RARRAY_AREF(a, i)
Definition rarray.h:403
#define RREGEXP_PTR(obj)
Convenient accessor macro.
Definition rregexp.h:45
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:2631
#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_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.
static bool RB_FIXNUM_P(VALUE obj)
Checks if the given object is a so-called Fixnum.
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
@ RUBY_SPECIAL_SHIFT
Least significant 8 bits are reserved.
static bool RB_UNDEF_P(VALUE obj)
Checks if the given object is undef.
#define RTEST
This is an old name of RB_TEST.
static bool RB_FLONUM_P(VALUE obj)
Checks if the given object is a so-called Flonum.
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 enum ruby_value_type RB_BUILTIN_TYPE(VALUE obj)
Queries the type of the object.
Definition value_type.h:181
static bool RB_SYMBOL_P(VALUE obj)
Queries if the object is an instance of rb_cSymbol.
Definition value_type.h:306