Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

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
41 static VALUE RaccBug;
42 static VALUE CparseParams;
43
44 static ID id_yydebug;
45 static ID id_nexttoken;
46 static ID id_onerror;
47 static ID id_noreduce;
48 static ID id_errstatus;
49
50 static ID id_d_shift;
51 static ID id_d_reduce;
52 static ID id_d_accept;
53 static ID id_d_read_token;
54 static ID id_d_next_state;
55 static 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
79 static ID value_to_id _((VALUE v));
80 static inline long num_to_long _((VALUE n));
81
82 static ID
83 value_to_id(VALUE v)
84 {
85 if (! SYMBOL_P(v)) {
86 rb_raise(rb_eTypeError, "not symbol");
87 }
88 return SYM2ID(v);
89 }
90
91 static inline long
92 num_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
104 static VALUE get_stack_tail _((VALUE stack, long len));
105 static void cut_stack_tail _((VALUE stack, long len));
106
107 static VALUE
108 get_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
115 static void
116 cut_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
194 static VALUE racc_cparse _((VALUE parser, VALUE arg, VALUE sysdebug));
195 static VALUE racc_yyparse _((VALUE parser, VALUE lexer, VALUE lexmid,
196 VALUE arg, VALUE sysdebug));
197
198 static void call_lexer _((struct cparse_params *v));
199 static VALUE lexer_i _((RB_BLOCK_CALL_FUNC_ARGLIST(block_args, data)));
200
201 static VALUE assert_array _((VALUE a));
202 static long assert_integer _((VALUE n));
203 static VALUE assert_hash _((VALUE h));
204 static VALUE initialize_params _((VALUE vparams, VALUE parser, VALUE arg,
205 VALUE lexer, VALUE lexmid));
206 static void cparse_params_mark _((void *ptr));
207 static size_t cparse_params_memsize _((const void *ptr));
208
209 static void parse_main _((struct cparse_params *v,
210 VALUE tok, VALUE val, int resume));
211 static void extract_user_token _((struct cparse_params *v,
212 VALUE block_args, VALUE *tok, VALUE *val));
213 static void shift _((struct cparse_params* v, long act, VALUE tok, VALUE val));
214 static int reduce _((struct cparse_params* v, long act));
215 static 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
228 static 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
241 static VALUE
242 racc_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
259 static VALUE
260 racc_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
284 static void
285 call_lexer(struct cparse_params *v)
286 {
287 rb_block_call(v->lexer, v->lexmid, 0, NULL, lexer_i, v->value_v);
288 }
289 #else
290 static VALUE
291 lexer_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
299 static void
300 call_lexer(struct cparse_params *v)
301 {
302 rb_iterate(lexer_iter, v->value_v, lexer_i, v->value_v);
303 }
304 #endif
305
306 static VALUE
307 lexer_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
321 static VALUE
322 assert_array(VALUE a)
323 {
324 Check_Type(a, T_ARRAY);
325 return a;
326 }
327
328 static VALUE
329 assert_hash(VALUE h)
330 {
331 Check_Type(h, T_HASH);
332 return h;
333 }
334
335 static long
336 assert_integer(VALUE n)
337 {
338 return NUM2LONG(n);
339 }
340
341 static VALUE
342 initialize_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
403 static void
404 cparse_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
428 static size_t
429 cparse_params_memsize(const void *ptr)
430 {
431 return sizeof(struct cparse_params);
432 }
433
434 static void
435 extract_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
478 static void
479 parse_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 = Qfalse;
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
687 static void
688 shift(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
700 static int
701 reduce(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
710 static VALUE
711 reduce0(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
819 void
820 Init_cparse(void)
821 {
822 #undef rb_intern
823 #define rb_intern(str) rb_intern_const(str)
824 VALUE Racc, Parser;
825 ID id_racc = rb_intern("Racc");
826
827 if (rb_const_defined(rb_cObject, id_racc)) {
828 Racc = rb_const_get(rb_cObject, id_racc);
829 Parser = rb_const_get_at(Racc, rb_intern("Parser"));
830 }
831 else {
832 Racc = rb_define_module("Racc");
833 Parser = rb_define_class_under(Racc, "Parser", rb_cObject);
834 }
835 rb_define_private_method(Parser, "_racc_do_parse_c", racc_cparse, 2);
836 rb_define_private_method(Parser, "_racc_yyparse_c", racc_yyparse, 4);
837 rb_define_const(Parser, "Racc_Runtime_Core_Version_C",
838 rb_str_new2(RACC_VERSION));
839 rb_define_const(Parser, "Racc_Runtime_Core_Id_C",
840 rb_str_new2("$originalId: cparse.c,v 1.8 2006/07/06 11:39:46 aamine Exp $"));
841
842 CparseParams = rb_define_class_under(Racc, "CparseParams", rb_cObject);
843 rb_undef_alloc_func(CparseParams);
844 rb_undef_method(CparseParams, "initialize");
845 rb_undef_method(CparseParams, "initialize_copy");
846
847 RaccBug = rb_eRuntimeError;
848
849 id_yydebug = rb_intern("@yydebug");
850 id_nexttoken = rb_intern("next_token");
851 id_onerror = rb_intern("on_error");
852 id_noreduce = rb_intern("_reduce_none");
853 id_errstatus = rb_intern("@racc_error_status");
854
855 id_d_shift = rb_intern("racc_shift");
856 id_d_reduce = rb_intern("racc_reduce");
857 id_d_accept = rb_intern("racc_accept");
858 id_d_read_token = rb_intern("racc_read_token");
859 id_d_next_state = rb_intern("racc_next_state");
860 id_d_e_pop = rb_intern("racc_e_pop");
861 }
ID
unsigned long ID
Definition: ruby.h:103
PUSH
#define PUSH(s, i)
Definition: cparse.c:126
rb_id2name
const char * rb_id2name(ID)
Definition: symbol.c:801
TypedData_Make_Struct
#define TypedData_Make_Struct(klass, type, data_type, sval)
Definition: ruby.h:1244
cparse_params::lex_is_iterator
int lex_is_iterator
Definition: cparse.c:142
rb_intern
#define rb_intern(str)
Check_Type
#define Check_Type(v, t)
Definition: ruby.h:595
GET_TAIL
#define GET_TAIL(s, len)
Definition: cparse.c:130
CP_FIN_CANTPOP
#define CP_FIN_CANTPOP
Definition: cparse.c:182
cparse_params
Definition: cparse.c:137
rb_str_new2
#define rb_str_new2
Definition: intern.h:903
LAST_I
#define LAST_I(s)
Definition: cparse.c:128
rb_const_defined
int rb_const_defined(VALUE, ID)
Definition: variable.c:2682
cparse_params::goto_check
VALUE goto_check
Definition: cparse.c:155
CP_FIN_EOT
#define CP_FIN_EOT
Definition: cparse.c:181
cparse_params::action_check
VALUE action_check
Definition: cparse.c:150
INT2FIX
#define INT2FIX(i)
Definition: ruby.h:263
cparse_params::use_result_var
int use_result_var
Definition: cparse.c:176
cparse_params::goto_default
VALUE goto_default
Definition: cparse.c:156
cparse_params::sys_debug
int sys_debug
Definition: cparse.c:185
NUM2LONG
#define NUM2LONG(x)
Definition: ruby.h:679
REDUCE
#define REDUCE(v, act)
Definition: cparse.c:464
cparse_params::tstack
VALUE tstack
Definition: cparse.c:167
SHIFT
#define SHIFT(v, act, tok, val)
Definition: cparse.c:463
rb_hash_aref
VALUE rb_hash_aref(VALUE hash, VALUE key)
Definition: hash.c:1964
D_printf
#define D_printf(fmt, arg)
Definition: cparse.c:222
VALUE
unsigned long VALUE
Definition: ruby.h:102
rb_eArgError
VALUE rb_eArgError
Definition: error.c:923
RB_TYPE_P
#define RB_TYPE_P(obj, type)
Definition: ruby.h:560
cparse_params::action_pointer
VALUE action_pointer
Definition: cparse.c:152
rb_define_module
VALUE rb_define_module(const char *name)
Definition: class.c:772
rb_const_get_at
VALUE rb_const_get_at(VALUE, ID)
Definition: variable.c:2393
cparse_params::token_table
VALUE token_table
Definition: cparse.c:161
ERROR_TOKEN
#define ERROR_TOKEN
Definition: cparse.c:30
cparse_params::nt_base
long nt_base
Definition: cparse.c:159
Qundef
#define Qundef
Definition: ruby.h:470
ptr
struct RIMemo * ptr
Definition: debug.c:74
Qfalse
#define Qfalse
Definition: ruby.h:467
POP
#define POP(s)
Definition: cparse.c:127
CP_FIN_ACCEPT
#define CP_FIN_ACCEPT
Definition: cparse.c:180
NULL
#define NULL
Definition: _sdbm.c:101
PRIsVALUE
#define PRIsVALUE
Definition: ruby.h:166
error
const rb_iseq_t const char * error
Definition: rb_mjit_min_header-2.7.0.h:13506
rb_ary_pop
VALUE rb_ary_pop(VALUE ary)
Definition: array.c:1241
RB_BLOCK_CALL_FUNC_ARGLIST
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Definition: ruby.h:1964
rb_undef_method
void rb_undef_method(VALUE klass, const char *name)
Definition: class.c:1575
v
int VALUE v
Definition: rb_mjit_min_header-2.7.0.h:12332
RUBY_TYPED_DEFAULT_FREE
#define RUBY_TYPED_DEFAULT_FREE
Definition: ruby.h:1203
cparse_params::action_table
VALUE action_table
Definition: cparse.c:149
cparse_params::retval
VALUE retval
Definition: cparse.c:178
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2669
rb_ary_entry
VALUE rb_ary_entry(VALUE ary, long offset)
Definition: array.c:1512
rb_ivar_get
VALUE rb_ivar_get(VALUE, ID)
Definition: variable.c:1070
cparse_params::state
VALUE state
Definition: cparse.c:164
LONG2NUM
#define LONG2NUM(x)
Definition: ruby.h:1644
cparse_params::ruleno
long ruleno
Definition: cparse.c:171
rb_obj_class
VALUE rb_obj_class(VALUE)
Equivalent to Object#class in Ruby.
Definition: object.c:217
cparse_params::errstatus
long errstatus
Definition: cparse.c:173
cparse_params::goto_table
VALUE goto_table
Definition: cparse.c:154
cparse_params::i
long i
Definition: cparse.c:187
cparse_params::lexer
VALUE lexer
Definition: cparse.c:143
FALSE
#define FALSE
Definition: cparse.c:17
i
uint32_t i
Definition: rb_mjit_min_header-2.7.0.h:5464
TRUE
#define TRUE
Definition: cparse.c:20
ruby::backward::cxxanyargs::rb_catch
VALUE rb_catch(const char *q, type *w, VALUE e)
An equivalent of Kernel#catch.
Definition: cxxanyargs.hpp:269
cparse_params::curstate
long curstate
Definition: cparse.c:165
ruby.h
rb_block_call_func
VALUE rb_block_call_func(RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg))
Definition: ruby.h:1966
cparse_params::fin
long fin
Definition: cparse.c:179
rb_eTypeError
VALUE rb_eTypeError
Definition: error.c:922
AREF
#define AREF(s, idx)
Definition: cparse.c:97
rb_ary_subseq
#define rb_ary_subseq(ary, beg, len)
Definition: cparse.c:76
rb_eRuntimeError
VALUE rb_eRuntimeError
Definition: error.c:920
cparse_params::t
VALUE t
Definition: cparse.c:168
cparse_params::value_v
VALUE value_v
Definition: cparse.c:138
vERROR_TOKEN
#define vERROR_TOKEN
Definition: cparse.c:34
cparse_params::vstack
VALUE vstack
Definition: cparse.c:166
tok
#define tok(p)
Definition: ripper.c:13096
rb_iter_break
void rb_iter_break(void)
Definition: vm.c:1546
T_HASH
#define T_HASH
Definition: ruby.h:531
Init_cparse
void Init_cparse(void)
Definition: cparse.c:820
NEW_STACK
#define NEW_STACK()
Definition: cparse.c:125
cparse_params::debug
int debug
Definition: cparse.c:184
RARRAY_LEN
#define RARRAY_LEN(a)
Definition: ruby.h:1070
rb_cObject
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:2010
n
const char size_t n
Definition: rb_mjit_min_header-2.7.0.h:5456
rb_funcall
#define rb_funcall(recv, mid, argc,...)
Definition: rb_mjit_min_header-2.7.0.h:6585
T_ARRAY
#define T_ARRAY
Definition: ruby.h:530
arg
VALUE arg
Definition: rb_mjit_min_header-2.7.0.h:5601
CUT_TAIL
#define CUT_TAIL(s, len)
Definition: cparse.c:131
RUBY_TYPED_FREE_IMMEDIATELY
#define RUBY_TYPED_FREE_IMMEDIATELY
Definition: ruby.h:1207
D_puts
#define D_puts(msg)
Definition: cparse.c:221
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:178
NIL_P
#define NIL_P(v)
Definition: ruby.h:482
rb_define_const
void rb_define_const(VALUE, const char *, VALUE)
Definition: variable.c:2880
cparse_params::action_default
VALUE action_default
Definition: cparse.c:151
rb_data_type_struct
Definition: ruby.h:1148
rb_str_new
#define rb_str_new(str, len)
Definition: rb_mjit_min_header-2.7.0.h:6116
rb_gc_mark
void rb_gc_mark(VALUE ptr)
Definition: gc.c:5212
rb_check_typeddata
void * rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
Definition: error.c:889
_
#define _(args)
Definition: dln.h:28
cparse_params::shift_n
long shift_n
Definition: cparse.c:169
len
uint8_t len
Definition: escape.c:17
SYMBOL_P
#define SYMBOL_P(x)
Definition: ruby.h:413
ruby::backward::cxxanyargs::rb_iterate
VALUE rb_iterate(VALUE(*q)(VALUE), VALUE w, type *e, VALUE r)
Old way to implement iterators.
Definition: cxxanyargs.hpp:160
FINAL_TOKEN
#define FINAL_TOKEN
Definition: cparse.c:31
RACC_VERSION
#define RACC_VERSION
Definition: cparse.c:27
rb_ivar_set
VALUE rb_ivar_set(VALUE, ID, VALUE)
Definition: variable.c:1300
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:698
cparse_params::nerr
long nerr
Definition: cparse.c:174
rb_ary_new
VALUE rb_ary_new(void)
Definition: array.c:723
NUM2INT
#define NUM2INT(x)
Definition: ruby.h:715
Qnil
#define Qnil
Definition: ruby.h:469
h
size_t st_index_t h
Definition: rb_mjit_min_header-2.7.0.h:5462
rb_undef_alloc_func
void rb_undef_alloc_func(VALUE)
Definition: vm_method.c:722
cparse_params::reduce_n
long reduce_n
Definition: cparse.c:170
RB_GC_GUARD
#define RB_GC_GUARD(v)
Definition: ruby.h:585
cparse_params::goto_pointer
VALUE goto_pointer
Definition: cparse.c:157
vFINAL_TOKEN
#define vFINAL_TOKEN
Definition: cparse.c:35
rb_define_private_method
void rb_define_private_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1569
cparse_params::reduce_table
VALUE reduce_table
Definition: cparse.c:160
cparse_params::lexmid
ID lexmid
Definition: cparse.c:144
RTEST
#define RTEST(v)
Definition: ruby.h:481
SYM2ID
#define SYM2ID(x)
Definition: ruby.h:415
rb_iv_set
VALUE rb_iv_set(VALUE, const char *, VALUE)
Definition: variable.c:3307
cparse_params::parser
VALUE parser
Definition: cparse.c:140
rb_const_get
VALUE rb_const_get(VALUE, ID)
Definition: variable.c:2387