Ruby: ext/racc/cparse/cparse.c Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

cparse.c
Go to the documentation of this file.
1/*
2
3 cparse.c -- Racc Runtime Core
4
5 Copyright (c) 1999-2006 Minero Aoki
6
7 This library is free software.
8 You can distribute/modify this program under the same terms of ruby.
9
10 $originalId: cparse.c,v 1.8 2006/07/06 11:39:46 aamine Exp $
11
12*/
13
14#include <ruby.h>
15
16#ifndef FALSE
17#define FALSE 0
18#endif
19#ifndef TRUE
20#define TRUE 1
21#endif
22
23/* -----------------------------------------------------------------------
24 Important Constants
25----------------------------------------------------------------------- */
26
27#define RACC_VERSION "1.4.15"
28
29#define DEFAULT_TOKEN -1
30#define ERROR_TOKEN 1
31#define FINAL_TOKEN 0
32
33#define vDEFAULT_TOKEN INT2FIX(DEFAULT_TOKEN)
34#define vERROR_TOKEN INT2FIX(ERROR_TOKEN)
35#define vFINAL_TOKEN INT2FIX(FINAL_TOKEN)
36
37/* -----------------------------------------------------------------------
38 File Local Variables
39----------------------------------------------------------------------- */
40
41static VALUE RaccBug;
42static VALUE CparseParams;
43
44static ID id_yydebug;
45static ID id_nexttoken;
46static ID id_onerror;
47static ID id_noreduce;
48static ID id_errstatus;
49
50static ID id_d_shift;
51static ID id_d_reduce;
52static ID id_d_accept;
53static ID id_d_read_token;
54static ID id_d_next_state;
55static ID id_d_e_pop;
56
57/* -----------------------------------------------------------------------
58 Utils
59----------------------------------------------------------------------- */
60
61/* For backward compatibility */
62#ifndef ID2SYM
63# define ID2SYM(i) ULONG2NUM(i)
64#endif
65#ifndef SYM2ID
66# define SYM2ID(v) ((ID)NUM2ULONG(v))
67#endif
68#ifndef SYMBOL_P
69# define SYMBOL_P(v) FIXNUM_P(v)
70#endif
71#ifndef LONG2NUM
72# define LONG2NUM(i) INT2NUM(i)
73#endif
74
75#ifndef HAVE_RB_ARY_SUBSEQ
76# define rb_ary_subseq(ary, beg, len) rb_ary_new4(len, RARRAY_PTR(ary) + beg)
77#endif
78
79static ID value_to_id _((VALUE v));
80static inline long num_to_long _((VALUE n));
81
82static ID
83value_to_id(VALUE v)
84{
85 if (! SYMBOL_P(v)) {
86 rb_raise(rb_eTypeError, "not symbol");
87 }
88 return SYM2ID(v);
89}
90
91static inline long
92num_to_long(VALUE n)
93{
94 return NUM2LONG(n);
95}
96
97#define AREF(s, idx) \
98 ((0 <= idx && idx < RARRAY_LEN(s)) ? rb_ary_entry(s, idx) : Qnil)
99
100/* -----------------------------------------------------------------------
101 Parser Stack Interfaces
102----------------------------------------------------------------------- */
103
104static VALUE get_stack_tail _((VALUE stack, long len));
105static void cut_stack_tail _((VALUE stack, long len));
106
107static VALUE
108get_stack_tail(VALUE stack, long len)
109{
110 if (len < 0) return Qnil; /* system error */
111 if (len > RARRAY_LEN(stack)) len = RARRAY_LEN(stack);
112 return rb_ary_subseq(stack, RARRAY_LEN(stack) - len, len);
113}
114
115static void
116cut_stack_tail(VALUE stack, long len)
117{
118 while (len > 0) {
119 rb_ary_pop(stack);
120 len--;
121 }
122}
123
124#define STACK_INIT_LEN 64
125#define NEW_STACK() rb_ary_new2(STACK_INIT_LEN)
126#define PUSH(s, i) rb_ary_store(s, RARRAY_LEN(s), i)
127#define POP(s) rb_ary_pop(s)
128#define LAST_I(s) \
129 ((RARRAY_LEN(s) > 0) ? rb_ary_entry(s, RARRAY_LEN(s) - 1) : Qnil)
130#define GET_TAIL(s, len) get_stack_tail(s, len)
131#define CUT_TAIL(s, len) cut_stack_tail(s, len)
132
133/* -----------------------------------------------------------------------
134 struct cparse_params
135----------------------------------------------------------------------- */
136
137 struct cparse_params {
138 VALUE value_v; /* VALUE version of this struct */
139
140 VALUE parser; /* parser object */
141
142 int lex_is_iterator;
143 VALUE lexer; /* scanner object */
144 ID lexmid; /* name of scanner method (must be an iterator) */
145
146 /* State transition tables (immutable)
147 Data structure is from Dragon Book 4.9 */
148 /* action table */
149 VALUE action_table;
150 VALUE action_check;
151 VALUE action_default;
152 VALUE action_pointer;
153 /* goto table */
154 VALUE goto_table;
155 VALUE goto_check;
156 VALUE goto_default;
157 VALUE goto_pointer;
158
159 long nt_base; /* NonTerminal BASE index */
160 VALUE reduce_table; /* reduce data table */
161 VALUE token_table; /* token conversion table */
162
163 /* parser stacks and parameters */
164 VALUE state;
165 long curstate;
166 VALUE vstack;
167 VALUE tstack;
168 VALUE t;
169 long shift_n;
170 long reduce_n;
171 long ruleno;
172
173 long errstatus; /* nonzero in error recovering mode */
174 long nerr; /* number of error */
175
176 int use_result_var;
177
178 VALUE retval; /* return value of parser routine */
179 long fin; /* parse result status */
180#define CP_FIN_ACCEPT 1
181#define CP_FIN_EOT 2
182#define CP_FIN_CANTPOP 3
183
184 int debug; /* user level debug */
185 int sys_debug; /* system level debug */
186
187 long i; /* table index */
188};
189
190/* -----------------------------------------------------------------------
191 Parser Main Routines
192----------------------------------------------------------------------- */
193
194static VALUE racc_cparse _((VALUE parser, VALUE arg, VALUE sysdebug));
195static VALUE racc_yyparse _((VALUE parser, VALUE lexer, VALUE lexmid,
196 VALUE arg, VALUE sysdebug));
197
198static void call_lexer _((struct cparse_params *v));
199static VALUE lexer_i _((RB_BLOCK_CALL_FUNC_ARGLIST(block_args, data)));
200
201static VALUE assert_array _((VALUE a));
202static long assert_integer _((VALUE n));
203static VALUE assert_hash _((VALUE h));
204static VALUE initialize_params _((VALUE vparams, VALUE parser, VALUE arg,
205 VALUE lexer, VALUE lexmid));
206static void cparse_params_mark _((void *ptr));
207static size_t cparse_params_memsize _((const void *ptr));
208
209static void parse_main _((struct cparse_params *v,
210 VALUE tok, VALUE val, int resume));
211static void extract_user_token _((struct cparse_params *v,
212 VALUE block_args, VALUE *tok, VALUE *val));
213static void shift _((struct cparse_params* v, long act, VALUE tok, VALUE val));
214static int reduce _((struct cparse_params* v, long act));
215static rb_block_call_func reduce0;
216
217#ifdef DEBUG
218# define D_puts(msg) if (v->sys_debug) puts(msg)
219# define D_printf(fmt,arg) if (v->sys_debug) printf(fmt,arg)
220#else
221# define D_puts(msg)
222# define D_printf(fmt,arg)
223#endif
224
225#undef RUBY_UNTYPED_DATA_WARNING
226#define RUBY_UNTYPED_DATA_WARNING 1
227
228static const rb_data_type_t cparse_params_type = {
229 "racc/cparse",
230 {
231 cparse_params_mark,
232 RUBY_TYPED_DEFAULT_FREE,
233 cparse_params_memsize,
234 },
235#ifdef RUBY_TYPED_FREE_IMMEDIATELY
236 0, 0,
237 RUBY_TYPED_FREE_IMMEDIATELY,
238#endif
239};
240
241static VALUE
242racc_cparse(VALUE parser, VALUE arg, VALUE sysdebug)
243{
244 VALUE vparams;
245 struct cparse_params *v;
246
247 vparams = TypedData_Make_Struct(CparseParams, struct cparse_params,
248 &cparse_params_type, v);
249 D_puts("starting cparse");
250 v->sys_debug = RTEST(sysdebug);
251 vparams = initialize_params(vparams, parser, arg, Qnil, Qnil);
252 v->lex_is_iterator = FALSE;
253 parse_main(v, Qnil, Qnil, 0);
254
255 RB_GC_GUARD(vparams);
256 return v->retval;
257}
258
259static VALUE
260racc_yyparse(VALUE parser, VALUE lexer, VALUE lexmid, VALUE arg, VALUE sysdebug)
261{
262 VALUE vparams;
263 struct cparse_params *v;
264
265 vparams = TypedData_Make_Struct(CparseParams, struct cparse_params,
266 &cparse_params_type, v);
267 v->sys_debug = RTEST(sysdebug);
268 D_puts("start C yyparse");
269 vparams = initialize_params(vparams, parser, arg, lexer, lexmid);
270 v->lex_is_iterator = TRUE;
271 D_puts("params initialized");
272 parse_main(v, Qnil, Qnil, 0);
273 call_lexer(v);
274 if (!v->fin) {
275 rb_raise(rb_eArgError, "%s() is finished before EndOfToken",
276 rb_id2name(v->lexmid));
277 }
278
279 RB_GC_GUARD(vparams);
280 return v->retval;
281}
282
283#ifdef HAVE_RB_BLOCK_CALL
284static void
285call_lexer(struct cparse_params *v)
286{
287 rb_block_call(v->lexer, v->lexmid, 0, NULL, lexer_i, v->value_v);
288}
289#else
290static VALUE
291lexer_iter(VALUE data)
292{
293 struct cparse_params *v = rb_check_typeddata(data, &cparse_params_type);
294
295 rb_funcall(v->lexer, v->lexmid, 0);
296 return Qnil;
297}
298
299static void
300call_lexer(struct cparse_params *v)
301{
302 rb_iterate(lexer_iter, v->value_v, lexer_i, v->value_v);
303}
304#endif
305
306static VALUE
307lexer_i(RB_BLOCK_CALL_FUNC_ARGLIST(block_args, data))
308{
309 struct cparse_params *v = rb_check_typeddata(data, &cparse_params_type);
310 VALUE tok, val;
311
312 if (v->fin)
313 rb_raise(rb_eArgError, "extra token after EndOfToken");
314 extract_user_token(v, block_args, &tok, &val);
315 parse_main(v, tok, val, 1);
316 if (v->fin && v->fin != CP_FIN_ACCEPT)
317 rb_iter_break();
318 return Qnil;
319}
320
321static VALUE
322assert_array(VALUE a)
323{
324 Check_Type(a, T_ARRAY);
325 return a;
326}
327
328static VALUE
329assert_hash(VALUE h)
330{
331 Check_Type(h, T_HASH);
332 return h;
333}
334
335static long
336assert_integer(VALUE n)
337{
338 return NUM2LONG(n);
339}
340
341static VALUE
342initialize_params(VALUE vparams, VALUE parser, VALUE arg, VALUE lexer, VALUE lexmid)
343{
344 struct cparse_params *v = rb_check_typeddata(vparams, &cparse_params_type);
345
346 v->value_v = vparams;
347 v->parser = parser;
348 v->lexer = lexer;
349 if (! NIL_P(lexmid))
350 v->lexmid = value_to_id(lexmid);
351
352 v->debug = RTEST(rb_ivar_get(parser, id_yydebug));
353
354 Check_Type(arg, T_ARRAY);
355 if (!(13 <= RARRAY_LEN(arg) && RARRAY_LEN(arg) <= 14))
356 rb_raise(RaccBug, "[Racc Bug] wrong arg.size %ld", RARRAY_LEN(arg));
357 v->action_table = assert_array (rb_ary_entry(arg, 0));
358 v->action_check = assert_array (rb_ary_entry(arg, 1));
359 v->action_default = assert_array (rb_ary_entry(arg, 2));
360 v->action_pointer = assert_array (rb_ary_entry(arg, 3));
361 v->goto_table = assert_array (rb_ary_entry(arg, 4));
362 v->goto_check = assert_array (rb_ary_entry(arg, 5));
363 v->goto_default = assert_array (rb_ary_entry(arg, 6));
364 v->goto_pointer = assert_array (rb_ary_entry(arg, 7));
365 v->nt_base = assert_integer(rb_ary_entry(arg, 8));
366 v->reduce_table = assert_array (rb_ary_entry(arg, 9));
367 v->token_table = assert_hash (rb_ary_entry(arg, 10));
368 v->shift_n = assert_integer(rb_ary_entry(arg, 11));
369 v->reduce_n = assert_integer(rb_ary_entry(arg, 12));
370 if (RARRAY_LEN(arg) > 13) {
371 v->use_result_var = RTEST(rb_ary_entry(arg, 13));
372 }
373 else {
374 v->use_result_var = TRUE;
375 }
376
377 v->tstack = v->debug ? NEW_STACK() : Qnil;
378 v->vstack = NEW_STACK();
379 v->state = NEW_STACK();
380 v->curstate = 0;
381 PUSH(v->state, INT2FIX(0));
382 v->t = INT2FIX(FINAL_TOKEN + 1); /* must not init to FINAL_TOKEN */
383 v->nerr = 0;
384 v->errstatus = 0;
385 rb_ivar_set(parser, id_errstatus, LONG2NUM(v->errstatus));
386
387 v->retval = Qnil;
388 v->fin = 0;
389
390 v->lex_is_iterator = FALSE;
391
392 rb_iv_set(parser, "@vstack", v->vstack);
393 if (v->debug) {
394 rb_iv_set(parser, "@tstack", v->tstack);
395 }
396 else {
397 rb_iv_set(parser, "@tstack", Qnil);
398 }
399
400 return vparams;
401}
402
403static void
404cparse_params_mark(void *ptr)
405{
406 struct cparse_params *v = (struct cparse_params*)ptr;
407
408 rb_gc_mark(v->value_v);
409 rb_gc_mark(v->parser);
410 rb_gc_mark(v->lexer);
411 rb_gc_mark(v->action_table);
412 rb_gc_mark(v->action_check);
413 rb_gc_mark(v->action_default);
414 rb_gc_mark(v->action_pointer);
415 rb_gc_mark(v->goto_table);
416 rb_gc_mark(v->goto_check);
417 rb_gc_mark(v->goto_default);
418 rb_gc_mark(v->goto_pointer);
419 rb_gc_mark(v->reduce_table);
420 rb_gc_mark(v->token_table);
421 rb_gc_mark(v->state);
422 rb_gc_mark(v->vstack);
423 rb_gc_mark(v->tstack);
424 rb_gc_mark(v->t);
425 rb_gc_mark(v->retval);
426}
427
428static size_t
429cparse_params_memsize(const void *ptr)
430{
431 return sizeof(struct cparse_params);
432}
433
434static void
435extract_user_token(struct cparse_params *v, VALUE block_args,
436 VALUE *tok, VALUE *val)
437{
438 if (NIL_P(block_args)) {
439 /* EOF */
440 *tok = Qfalse;
441 *val = rb_str_new("$", 1);
442 return;
443 }
444
445 if (!RB_TYPE_P(block_args, T_ARRAY)) {
446 rb_raise(rb_eTypeError,
447 "%s() %s %"PRIsVALUE" (must be Array[2])",
448 v->lex_is_iterator ? rb_id2name(v->lexmid) : "next_token",
449 v->lex_is_iterator ? "yielded" : "returned",
450 rb_obj_class(block_args));
451 }
452 if (RARRAY_LEN(block_args) != 2) {
453 rb_raise(rb_eArgError,
454 "%s() %s wrong size of array (%ld for 2)",
455 v->lex_is_iterator ? rb_id2name(v->lexmid) : "next_token",
456 v->lex_is_iterator ? "yielded" : "returned",
457 RARRAY_LEN(block_args));
458 }
459 *tok = AREF(block_args, 0);
460 *val = AREF(block_args, 1);
461}
462
463#define SHIFT(v,act,tok,val) shift(v,act,tok,val)
464#define REDUCE(v,act) do {\
465 switch (reduce(v,act)) { \
466 case 0: /* normal */ \
467 break; \
468 case 1: /* yyerror */ \
469 goto user_yyerror; \
470 case 2: /* yyaccept */ \
471 D_puts("u accept"); \
472 goto accept; \
473 default: \
474 break; \
475 } \
476} while (0)
477
478static void
479parse_main(struct cparse_params *v, VALUE tok, VALUE val, int resume)
480{
481 long i; /* table index */
482 long act; /* action type */
483 VALUE act_value; /* action type, VALUE version */
484 int read_next = 1; /* true if we need to read next token */
485 VALUE tmp;
486
487 if (resume)
488 goto resume;
489
490 while (1) {
491 D_puts("");
492 D_puts("---- enter new loop ----");
493 D_puts("");
494
495 D_printf("(act) k1=%ld\n", v->curstate);
496 tmp = AREF(v->action_pointer, v->curstate);
497 if (NIL_P(tmp)) goto notfound;
498 D_puts("(act) pointer[k1] ok");
499 i = NUM2LONG(tmp);
500
501 D_printf("read_next=%d\n", read_next);
502 if (read_next && (v->t != vFINAL_TOKEN)) {
503 if (v->lex_is_iterator) {
504 D_puts("resuming...");
505 if (v->fin) rb_raise(rb_eArgError, "token given after EOF");
506 v->i = i; /* save i */
507 return;
508 resume:
509 D_puts("resumed");
510 i = v->i; /* load i */
511 }
512 else {
513 D_puts("next_token");
514 tmp = rb_funcall(v->parser, id_nexttoken, 0);
515 extract_user_token(v, tmp, &tok, &val);
516 }
517 /* convert token */
518 v->t = rb_hash_aref(v->token_table, tok);
519 if (NIL_P(v->t)) {
520 v->t = vERROR_TOKEN;
521 }
522 D_printf("(act) t(k2)=%ld\n", NUM2LONG(v->t));
523 if (v->debug) {
524 rb_funcall(v->parser, id_d_read_token,
525 3, v->t, tok, val);
526 }
527 }
528 read_next = 0;
529
530 i += NUM2LONG(v->t);
531 D_printf("(act) i=%ld\n", i);
532 if (i < 0) goto notfound;
533
534 act_value = AREF(v->action_table, i);
535 if (NIL_P(act_value)) goto notfound;
536 act = NUM2LONG(act_value);
537 D_printf("(act) table[i]=%ld\n", act);
538
539 tmp = AREF(v->action_check, i);
540 if (NIL_P(tmp)) goto notfound;
541 if (NUM2LONG(tmp) != v->curstate) goto notfound;
542 D_printf("(act) check[i]=%ld\n", NUM2LONG(tmp));
543
544 D_puts("(act) found");
545 act_fixed:
546 D_printf("act=%ld\n", act);
547 goto handle_act;
548
549 notfound:
550 D_puts("(act) not found: use default");
551 act_value = AREF(v->action_default, v->curstate);
552 act = NUM2LONG(act_value);
553 goto act_fixed;
554
555
556 handle_act:
557 if (act > 0 && act < v->shift_n) {
558 D_puts("shift");
559 if (v->errstatus > 0) {
560 v->errstatus--;
561 rb_ivar_set(v->parser, id_errstatus, LONG2NUM(v->errstatus));
562 }
563 SHIFT(v, act, v->t, val);
564 read_next = 1;
565 }
566 else if (act < 0 && act > -(v->reduce_n)) {
567 D_puts("reduce");
568 REDUCE(v, act);
569 }
570 else if (act == -(v->reduce_n)) {
571 goto error;
572 error_recovered:
573 ; /* goto label requires stmt */
574 }
575 else if (act == v->shift_n) {
576 D_puts("accept");
577 goto accept;
578 }
579 else {
580 rb_raise(RaccBug, "[Racc Bug] unknown act value %ld", act);
581 }
582
583 if (v->debug) {
584 rb_funcall(v->parser, id_d_next_state,
585 2, LONG2NUM(v->curstate), v->state);
586 }
587 }
588 /* not reach */
589
590
591 accept:
592 if (v->debug) rb_funcall(v->parser, id_d_accept, 0);
593 v->retval = rb_ary_entry(v->vstack, 0);
594 v->fin = CP_FIN_ACCEPT;
595 return;
596
597
598 error:
599 D_printf("error detected, status=%ld\n", v->errstatus);
600 if (v->errstatus == 0) {
601 v->nerr++;
602 rb_funcall(v->parser, id_onerror,
603 3, v->t, val, v->vstack);
604 }
605 user_yyerror:
606 if (v->errstatus == 3) {
607 if (v->t == vFINAL_TOKEN) {
608 v->retval = Qnil;
609 v->fin = CP_FIN_EOT;
610 return;
611 }
612 read_next = 1;
613 }
614 v->errstatus = 3;
615 rb_ivar_set(v->parser, id_errstatus, LONG2NUM(v->errstatus));
616
617 /* check if we can shift/reduce error token */
618 D_printf("(err) k1=%ld\n", v->curstate);
619 D_printf("(err) k2=%d (error)\n", ERROR_TOKEN);
620 while (1) {
621 tmp = AREF(v->action_pointer, v->curstate);
622 if (NIL_P(tmp)) goto error_pop;
623 D_puts("(err) pointer[k1] ok");
624
625 i = NUM2LONG(tmp) + ERROR_TOKEN;
626 D_printf("(err) i=%ld\n", i);
627 if (i < 0) goto error_pop;
628
629 act_value = AREF(v->action_table, i);
630 if (NIL_P(act_value)) {
631 D_puts("(err) table[i] == nil");
632 goto error_pop;
633 }
634 act = NUM2LONG(act_value);
635 D_printf("(err) table[i]=%ld\n", act);
636
637 tmp = AREF(v->action_check, i);
638 if (NIL_P(tmp)) {
639 D_puts("(err) check[i] == nil");
640 goto error_pop;
641 }
642 if (NUM2LONG(tmp) != v->curstate) {
643 D_puts("(err) check[i] != k1");
644 goto error_pop;
645 }
646
647 D_puts("(err) found: can handle error token");
648 break;
649
650 error_pop:
651 D_puts("(err) act not found: can't handle error token; pop");
652
653 if (RARRAY_LEN(v->state) <= 1) {
654 v->retval = Qnil;
655 v->fin = CP_FIN_CANTPOP;
656 return;
657 }
658 POP(v->state);
659 POP(v->vstack);
660 v->curstate = num_to_long(LAST_I(v->state));
661 if (v->debug) {
662 POP(v->tstack);
663 rb_funcall(v->parser, id_d_e_pop,
664 3, v->state, v->tstack, v->vstack);
665 }
666 }
667
668 /* shift/reduce error token */
669 if (act > 0 && act < v->shift_n) {
670 D_puts("e shift");
671 SHIFT(v, act, ERROR_TOKEN, val);
672 }
673 else if (act < 0 && act > -(v->reduce_n)) {
674 D_puts("e reduce");
675 REDUCE(v, act);
676 }
677 else if (act == v->shift_n) {
678 D_puts("e accept");
679 goto accept;
680 }
681 else {
682 rb_raise(RaccBug, "[Racc Bug] unknown act value %ld", act);
683 }
684 goto error_recovered;
685}
686
687static void
688shift(struct cparse_params *v, long act, VALUE tok, VALUE val)
689{
690 PUSH(v->vstack, val);
691 if (v->debug) {
692 PUSH(v->tstack, tok);
693 rb_funcall(v->parser, id_d_shift,
694 3, tok, v->tstack, v->vstack);
695 }
696 v->curstate = act;
697 PUSH(v->state, LONG2NUM(v->curstate));
698}
699
700static int
701reduce(struct cparse_params *v, long act)
702{
703 VALUE code;
704 v->ruleno = -act * 3;
705 code = rb_catch("racc_jump", reduce0, v->value_v);
706 v->errstatus = num_to_long(rb_ivar_get(v->parser, id_errstatus));
707 return NUM2INT(code);
708}
709
710static VALUE
711reduce0(RB_BLOCK_CALL_FUNC_ARGLIST(_, data))
712{
713 struct cparse_params *v = rb_check_typeddata(data, &cparse_params_type);
714 VALUE reduce_to, reduce_len, method_id;
715 long len;
716 ID mid;
717 VALUE tmp, tmp_t = Qundef, tmp_v = Qundef;
718 long i, k1, k2;
719 VALUE goto_state;
720
721 reduce_len = rb_ary_entry(v->reduce_table, v->ruleno);
722 reduce_to = rb_ary_entry(v->reduce_table, v->ruleno+1);
723 method_id = rb_ary_entry(v->reduce_table, v->ruleno+2);
724 len = NUM2LONG(reduce_len);
725 mid = value_to_id(method_id);
726
727 /* call action */
728 if (len == 0) {
729 tmp = Qnil;
730 if (mid != id_noreduce)
731 tmp_v = rb_ary_new();
732 if (v->debug)
733 tmp_t = rb_ary_new();
734 }
735 else {
736 if (mid != id_noreduce) {
737 tmp_v = GET_TAIL(v->vstack, len);
738 tmp = rb_ary_entry(tmp_v, 0);
739 }
740 else {
741 tmp = rb_ary_entry(v->vstack, RARRAY_LEN(v->vstack) - len);
742 }
743 CUT_TAIL(v->vstack, len);
744 if (v->debug) {
745 tmp_t = GET_TAIL(v->tstack, len);
746 CUT_TAIL(v->tstack, len);
747 }
748 CUT_TAIL(v->state, len);
749 }
750 if (mid != id_noreduce) {
751 if (v->use_result_var) {
752 tmp = rb_funcall(v->parser, mid,
753 3, tmp_v, v->vstack, tmp);
754 }
755 else {
756 tmp = rb_funcall(v->parser, mid,
757 2, tmp_v, v->vstack);
758 }
759 }
760
761 /* then push result */
762 PUSH(v->vstack, tmp);
763 if (v->debug) {
764 PUSH(v->tstack, reduce_to);
765 rb_funcall(v->parser, id_d_reduce,
766 4, tmp_t, reduce_to, v->tstack, v->vstack);
767 }
768
769 /* calculate transition state */
770 if (RARRAY_LEN(v->state) == 0)
771 rb_raise(RaccBug, "state stack unexpectedly empty");
772 k2 = num_to_long(LAST_I(v->state));
773 k1 = num_to_long(reduce_to) - v->nt_base;
774 D_printf("(goto) k1=%ld\n", k1);
775 D_printf("(goto) k2=%ld\n", k2);
776
777 tmp = AREF(v->goto_pointer, k1);
778 if (NIL_P(tmp)) goto notfound;
779
780 i = NUM2LONG(tmp) + k2;
781 D_printf("(goto) i=%ld\n", i);
782 if (i < 0) goto notfound;
783
784 goto_state = AREF(v->goto_table, i);
785 if (NIL_P(goto_state)) {
786 D_puts("(goto) table[i] == nil");
787 goto notfound;
788 }
789 D_printf("(goto) table[i]=%ld (goto_state)\n", NUM2LONG(goto_state));
790
791 tmp = AREF(v->goto_check, i);
792 if (NIL_P(tmp)) {
793 D_puts("(goto) check[i] == nil");
794 goto notfound;
795 }
796 if (tmp != LONG2NUM(k1)) {
797 D_puts("(goto) check[i] != table[i]");
798 goto notfound;
799 }
800 D_printf("(goto) check[i]=%ld\n", NUM2LONG(tmp));
801
802 D_puts("(goto) found");
803 transit:
804 PUSH(v->state, goto_state);
805 v->curstate = NUM2LONG(goto_state);
806 return INT2FIX(0);
807
808 notfound:
809 D_puts("(goto) not found: use default");
810 /* overwrite `goto-state' by default value */
811 goto_state = AREF(v->goto_default, k1);
812 goto transit;
813}
814
815/* -----------------------------------------------------------------------
816 Ruby Interface
817----------------------------------------------------------------------- */
818
819void
820Init_cparse(void)
821{
822#ifdef HAVE_RB_EXT_RACTOR_SAFE
823 rb_ext_ractor_safe(true);
824#endif
825
826 VALUE Racc, Parser;
827 ID id_racc = rb_intern_const("Racc");
828
829 if (rb_const_defined(rb_cObject, id_racc)) {
830 Racc = rb_const_get(rb_cObject, id_racc);
831 Parser = rb_const_get_at(Racc, rb_intern_const("Parser"));
832 }
833 else {
834 Racc = rb_define_module("Racc");
835 Parser = rb_define_class_under(Racc, "Parser", rb_cObject);
836 }
837 rb_define_private_method(Parser, "_racc_do_parse_c", racc_cparse, 2);
838 rb_define_private_method(Parser, "_racc_yyparse_c", racc_yyparse, 4);
839 rb_define_const(Parser, "Racc_Runtime_Core_Version_C",
840 rb_str_new2(RACC_VERSION));
841 rb_define_const(Parser, "Racc_Runtime_Core_Id_C",
842 rb_str_new2("$originalId: cparse.c,v 1.8 2006/07/06 11:39:46 aamine Exp $"));
843
844 CparseParams = rb_define_class_under(Racc, "CparseParams", rb_cObject);
845 rb_undef_alloc_func(CparseParams);
846 rb_undef_method(CparseParams, "initialize");
847 rb_undef_method(CparseParams, "initialize_copy");
848
849 RaccBug = rb_eRuntimeError;
850
851 id_yydebug = rb_intern_const("@yydebug");
852 id_nexttoken = rb_intern_const("next_token");
853 id_onerror = rb_intern_const("on_error");
854 id_noreduce = rb_intern_const("_reduce_none");
855 id_errstatus = rb_intern_const("@racc_error_status");
856
857 id_d_shift = rb_intern_const("racc_shift");
858 id_d_reduce = rb_intern_const("racc_reduce");
859 id_d_accept = rb_intern_const("racc_accept");
860 id_d_read_token = rb_intern_const("racc_read_token");
861 id_d_next_state = rb_intern_const("racc_next_state");
862 id_d_e_pop = rb_intern_const("racc_e_pop");
863}
rb_ary_new
VALUE rb_ary_new(void)
Definition: array.c:749
rb_ary_pop
VALUE rb_ary_pop(VALUE ary)
Definition: array.c:1350
rb_ary_entry
VALUE rb_ary_entry(VALUE ary, long offset)
Definition: array.c:1672
CP_FIN_ACCEPT
#define CP_FIN_ACCEPT
Definition: cparse.c:180
PUSH
#define PUSH(s, i)
Definition: cparse.c:126
LAST_I
#define LAST_I(s)
Definition: cparse.c:128
ERROR_TOKEN
#define ERROR_TOKEN
Definition: cparse.c:30
SHIFT
#define SHIFT(v, act, tok, val)
Definition: cparse.c:463
AREF
#define AREF(s, idx)
Definition: cparse.c:97
D_puts
#define D_puts(msg)
Definition: cparse.c:221
RACC_VERSION
#define RACC_VERSION
Definition: cparse.c:27
vFINAL_TOKEN
#define vFINAL_TOKEN
Definition: cparse.c:35
CP_FIN_CANTPOP
#define CP_FIN_CANTPOP
Definition: cparse.c:182
GET_TAIL
#define GET_TAIL(s, len)
Definition: cparse.c:130
Init_cparse
void Init_cparse(void)
Definition: cparse.c:820
REDUCE
#define REDUCE(v, act)
Definition: cparse.c:464
D_printf
#define D_printf(fmt, arg)
Definition: cparse.c:222
NEW_STACK
#define NEW_STACK()
Definition: cparse.c:125
POP
#define POP(s)
Definition: cparse.c:127
FINAL_TOKEN
#define FINAL_TOKEN
Definition: cparse.c:31
CP_FIN_EOT
#define CP_FIN_EOT
Definition: cparse.c:181
rb_ary_subseq
#define rb_ary_subseq(ary, beg, len)
Definition: cparse.c:76
vERROR_TOKEN
#define vERROR_TOKEN
Definition: cparse.c:34
CUT_TAIL
#define CUT_TAIL(s, len)
Definition: cparse.c:131
rb_define_private_method
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
Definition: cxxanyargs.hpp:660
ptr
struct RIMemo * ptr
Definition: debug.c:88
len
uint8_t len
Definition: escape.c:17
PRIsVALUE
#define PRIsVALUE
Definition: function.c:10
rb_gc_mark
void rb_gc_mark(VALUE ptr)
Definition: gc.c:6112
rb_define_class_under
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:797
rb_define_module
VALUE rb_define_module(const char *name)
Definition: class.c:871
rb_undef_method
void rb_undef_method(VALUE klass, const char *name)
Definition: class.c:1777
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2917
rb_eTypeError
VALUE rb_eTypeError
Definition: error.c:1057
rb_eRuntimeError
VALUE rb_eRuntimeError
Definition: error.c:1055
rb_check_typeddata
void * rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
Definition: error.c:1024
rb_eArgError
VALUE rb_eArgError
Definition: error.c:1058
rb_cObject
VALUE rb_cObject
Object class.
Definition: object.c:49
rb_obj_class
VALUE rb_obj_class(VALUE)
Definition: object.c:245
rb_hash_aref
VALUE rb_hash_aref(VALUE hash, VALUE key)
Definition: hash.c:2046
rb_iter_break
void rb_iter_break(void)
Definition: vm.c:1786
rb_funcall
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:1077
rb_ext_ractor_safe
void rb_ext_ractor_safe(bool flag)
Definition: load.c:1058
rb_str_new2
#define rb_str_new2
Definition: string.h:276
rb_str_new
#define rb_str_new(str, len)
Definition: string.h:213
rb_const_get
VALUE rb_const_get(VALUE, ID)
Definition: variable.c:2624
rb_ivar_get
VALUE rb_ivar_get(VALUE, ID)
Definition: variable.c:1234
rb_const_get_at
VALUE rb_const_get_at(VALUE, ID)
Definition: variable.c:2630
rb_ivar_set
VALUE rb_ivar_set(VALUE, ID, VALUE)
Definition: variable.c:1493
rb_const_defined
int rb_const_defined(VALUE, ID)
Definition: variable.c:2928
rb_undef_alloc_func
void rb_undef_alloc_func(VALUE)
Definition: vm_method.c:954
rb_id2name
const char * rb_id2name(ID)
Definition: symbol.c:944
SYM2ID
#define SYM2ID
Definition: symbol.h:45
rb_iv_set
VALUE rb_iv_set(VALUE, const char *, VALUE)
Definition: variable.c:3580
rb_define_const
void rb_define_const(VALUE, const char *, VALUE)
Definition: variable.c:3150
NUM2INT
#define NUM2INT
Definition: int.h:44
RB_BLOCK_CALL_FUNC_ARGLIST
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Definition: iterator.h:31
rb_block_call_func
VALUE rb_block_call_func(RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg))
Definition: iterator.h:33
INT2FIX
#define INT2FIX
Definition: long.h:48
LONG2NUM
#define LONG2NUM
Definition: long.h:50
NUM2LONG
#define NUM2LONG
Definition: long.h:51
RB_GC_GUARD
#define RB_GC_GUARD(v)
Definition: memory.h:91
ruby::backward::cxxanyargs::rb_catch
VALUE rb_catch(const char *q, type *w, VALUE e)
An equivalent of Kernel#catch.
Definition: cxxanyargs.hpp:329
ruby::backward::cxxanyargs::rb_block_call
VALUE rb_block_call(VALUE q, ID w, int e, const VALUE *r, type *t, VALUE y)
Call a method with a block.
Definition: cxxanyargs.hpp:230
ruby::backward::cxxanyargs::rb_iterate
VALUE rb_iterate(onearg_type *q, VALUE w, type *e, VALUE r)
Old way to implement iterators.
Definition: cxxanyargs.hpp:204
TRUE
#define TRUE
Definition: nkf.h:175
FALSE
#define FALSE
Definition: nkf.h:174
RARRAY_LEN
#define RARRAY_LEN
Definition: rarray.h:52
NULL
#define NULL
Definition: regenc.h:69
tok
#define tok(p)
Definition: ripper.c:14208
RUBY_TYPED_DEFAULT_FREE
#define RUBY_TYPED_DEFAULT_FREE
Definition: rtypeddata.h:44
RUBY_TYPED_FREE_IMMEDIATELY
@ RUBY_TYPED_FREE_IMMEDIATELY
Definition: rtypeddata.h:62
TypedData_Make_Struct
#define TypedData_Make_Struct(klass, type, data_type, sval)
Definition: rtypeddata.h:122
ruby.h
Qundef
#define Qundef
Definition: special_consts.h:53
RTEST
#define RTEST
Definition: special_consts.h:42
Qnil
#define Qnil
Definition: special_consts.h:51
Qfalse
#define Qfalse
Definition: special_consts.h:50
NIL_P
#define NIL_P
Definition: special_consts.h:46
_
#define _(args)
Definition: stdarg.h:31
code
Definition: inftree9.h:24
cparse_params
Definition: cparse.c:137
cparse_params::tstack
VALUE tstack
Definition: cparse.c:167
cparse_params::shift_n
long shift_n
Definition: cparse.c:169
cparse_params::debug
int debug
Definition: cparse.c:184
cparse_params::fin
long fin
Definition: cparse.c:179
cparse_params::ruleno
long ruleno
Definition: cparse.c:171
cparse_params::state
VALUE state
Definition: cparse.c:164
cparse_params::use_result_var
int use_result_var
Definition: cparse.c:176
cparse_params::retval
VALUE retval
Definition: cparse.c:178
cparse_params::curstate
long curstate
Definition: cparse.c:165
cparse_params::token_table
VALUE token_table
Definition: cparse.c:161
cparse_params::action_table
VALUE action_table
Definition: cparse.c:149
cparse_params::parser
VALUE parser
Definition: cparse.c:140
cparse_params::errstatus
long errstatus
Definition: cparse.c:173
cparse_params::action_pointer
VALUE action_pointer
Definition: cparse.c:152
cparse_params::action_default
VALUE action_default
Definition: cparse.c:151
cparse_params::sys_debug
int sys_debug
Definition: cparse.c:185
cparse_params::lexer
VALUE lexer
Definition: cparse.c:143
cparse_params::nerr
long nerr
Definition: cparse.c:174
cparse_params::goto_default
VALUE goto_default
Definition: cparse.c:156
cparse_params::vstack
VALUE vstack
Definition: cparse.c:166
cparse_params::goto_check
VALUE goto_check
Definition: cparse.c:155
cparse_params::reduce_table
VALUE reduce_table
Definition: cparse.c:160
cparse_params::action_check
VALUE action_check
Definition: cparse.c:150
cparse_params::nt_base
long nt_base
Definition: cparse.c:159
cparse_params::lexmid
ID lexmid
Definition: cparse.c:144
cparse_params::reduce_n
long reduce_n
Definition: cparse.c:170
cparse_params::t
VALUE t
Definition: cparse.c:168
cparse_params::goto_pointer
VALUE goto_pointer
Definition: cparse.c:157
cparse_params::value_v
VALUE value_v
Definition: cparse.c:138
cparse_params::i
long i
Definition: cparse.c:187
cparse_params::lex_is_iterator
int lex_is_iterator
Definition: cparse.c:142
cparse_params::goto_table
VALUE goto_table
Definition: cparse.c:154
rb_data_type_struct
Definition: rtypeddata.h:70
error
void error(const char *msg)
Definition: untgz.c:593
VALUE
unsigned long VALUE
Definition: value.h:38
ID
unsigned long ID
Definition: value.h:39
T_HASH
#define T_HASH
Definition: value_type.h:64
T_ARRAY
#define T_ARRAY
Definition: value_type.h:55
SYMBOL_P
#define SYMBOL_P
Definition: value_type.h:87