Ruby
2.7.1p83(2020-03-31revisiona0c7c23c9cec0d0ffcba012279cd652d28ad5bf3)
|
Go to the documentation of this file.
27 #define RACC_VERSION "1.4.15"
29 #define DEFAULT_TOKEN -1
33 #define vDEFAULT_TOKEN INT2FIX(DEFAULT_TOKEN)
34 #define vERROR_TOKEN INT2FIX(ERROR_TOKEN)
35 #define vFINAL_TOKEN INT2FIX(FINAL_TOKEN)
42 static VALUE CparseParams;
45 static ID id_nexttoken;
47 static ID id_noreduce;
48 static ID id_errstatus;
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;
63 # define ID2SYM(i) ULONG2NUM(i)
66 # define SYM2ID(v) ((ID)NUM2ULONG(v))
69 # define SYMBOL_P(v) FIXNUM_P(v)
72 # define LONG2NUM(i) INT2NUM(i)
75 #ifndef HAVE_RB_ARY_SUBSEQ
76 # define rb_ary_subseq(ary, beg, len) rb_ary_new4(len, RARRAY_PTR(ary) + beg)
80 static inline long num_to_long
_((
VALUE n));
97 #define AREF(s, idx) \
98 ((0 <= idx && idx < RARRAY_LEN(s)) ? rb_ary_entry(s, idx) : Qnil)
105 static void cut_stack_tail
_((
VALUE stack,
long len));
108 get_stack_tail(
VALUE stack,
long len)
116 cut_stack_tail(
VALUE stack,
long len)
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)
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)
180 #define CP_FIN_ACCEPT 1
182 #define CP_FIN_CANTPOP 3
202 static long assert_integer
_((
VALUE n));
206 static void cparse_params_mark
_((
void *
ptr));
207 static size_t cparse_params_memsize
_((
const void *
ptr));
218 # define D_puts(msg) if (v->sys_debug) puts(msg)
219 # define D_printf(fmt,arg) if (v->sys_debug) printf(fmt,arg)
222 # define D_printf(fmt,arg)
225 #undef RUBY_UNTYPED_DATA_WARNING
226 #define RUBY_UNTYPED_DATA_WARNING 1
233 cparse_params_memsize,
235 #ifdef RUBY_TYPED_FREE_IMMEDIATELY
248 &cparse_params_type,
v);
249 D_puts(
"starting cparse");
250 v->sys_debug =
RTEST(sysdebug);
252 v->lex_is_iterator =
FALSE;
266 &cparse_params_type,
v);
267 v->sys_debug =
RTEST(sysdebug);
268 D_puts(
"start C yyparse");
270 v->lex_is_iterator =
TRUE;
271 D_puts(
"params initialized");
283 #ifdef HAVE_RB_BLOCK_CALL
291 lexer_iter(
VALUE data)
302 rb_iterate(lexer_iter,
v->value_v, lexer_i,
v->value_v);
314 extract_user_token(
v, block_args, &
tok, &val);
315 parse_main(
v,
tok, val, 1);
322 assert_array(
VALUE a)
346 v->value_v = vparams;
350 v->lexmid = value_to_id(
lexmid);
374 v->use_result_var =
TRUE;
390 v->lex_is_iterator =
FALSE;
404 cparse_params_mark(
void *
ptr)
429 cparse_params_memsize(
const void *
ptr)
438 if (
NIL_P(block_args)) {
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",
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",
460 *val =
AREF(block_args, 1);
463 #define SHIFT(v,act,tok,val) shift(v,act,tok,val)
464 #define REDUCE(v,act) do {\
465 switch (reduce(v,act)) { \
471 D_puts("u accept"); \
492 D_puts(
"---- enter new loop ----");
496 tmp =
AREF(
v->action_pointer,
v->curstate);
497 if (
NIL_P(tmp))
goto notfound;
498 D_puts(
"(act) pointer[k1] ok");
501 D_printf(
"read_next=%d\n", read_next);
503 if (
v->lex_is_iterator) {
515 extract_user_token(
v, tmp, &
tok, &val);
532 if (
i < 0)
goto notfound;
534 act_value =
AREF(
v->action_table,
i);
535 if (
NIL_P(act_value))
goto notfound;
537 D_printf(
"(act) table[i]=%ld\n", act);
539 tmp =
AREF(
v->action_check,
i);
540 if (
NIL_P(tmp))
goto notfound;
541 if (
NUM2LONG(tmp) !=
v->curstate)
goto notfound;
550 D_puts(
"(act) not found: use default");
551 act_value =
AREF(
v->action_default,
v->curstate);
557 if (act > 0 && act < v->
shift_n) {
559 if (
v->errstatus > 0) {
566 else if (act < 0 && act > -(
v->reduce_n)) {
570 else if (act == -(
v->reduce_n)) {
575 else if (act ==
v->shift_n) {
580 rb_raise(RaccBug,
"[Racc Bug] unknown act value %ld", act);
599 D_printf(
"error detected, status=%ld\n",
v->errstatus);
600 if (
v->errstatus == 0) {
603 3,
v->t, val,
v->vstack);
606 if (
v->errstatus == 3) {
621 tmp =
AREF(
v->action_pointer,
v->curstate);
622 if (
NIL_P(tmp))
goto error_pop;
623 D_puts(
"(err) pointer[k1] ok");
627 if (
i < 0)
goto error_pop;
629 act_value =
AREF(
v->action_table,
i);
630 if (
NIL_P(act_value)) {
631 D_puts(
"(err) table[i] == nil");
635 D_printf(
"(err) table[i]=%ld\n", act);
637 tmp =
AREF(
v->action_check,
i);
639 D_puts(
"(err) check[i] == nil");
643 D_puts(
"(err) check[i] != k1");
647 D_puts(
"(err) found: can handle error token");
651 D_puts(
"(err) act not found: can't handle error token; pop");
660 v->curstate = num_to_long(
LAST_I(
v->state));
664 3,
v->state,
v->tstack,
v->vstack);
669 if (act > 0 && act < v->
shift_n) {
673 else if (act < 0 && act > -(
v->reduce_n)) {
677 else if (act ==
v->shift_n) {
682 rb_raise(RaccBug,
"[Racc Bug] unknown act value %ld", act);
684 goto error_recovered;
690 PUSH(
v->vstack, val);
694 3,
tok,
v->tstack,
v->vstack);
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));
714 VALUE reduce_to, reduce_len, method_id;
725 mid = value_to_id(method_id);
730 if (mid != id_noreduce)
736 if (mid != id_noreduce) {
750 if (mid != id_noreduce) {
751 if (
v->use_result_var) {
753 3, tmp_v,
v->vstack, tmp);
757 2, tmp_v,
v->vstack);
762 PUSH(
v->vstack, tmp);
764 PUSH(
v->tstack, reduce_to);
766 4, tmp_t, reduce_to,
v->tstack,
v->vstack);
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;
777 tmp =
AREF(
v->goto_pointer, k1);
778 if (
NIL_P(tmp))
goto notfound;
782 if (
i < 0)
goto notfound;
784 goto_state =
AREF(
v->goto_table,
i);
785 if (
NIL_P(goto_state)) {
786 D_puts(
"(goto) table[i] == nil");
791 tmp =
AREF(
v->goto_check,
i);
793 D_puts(
"(goto) check[i] == nil");
797 D_puts(
"(goto) check[i] != table[i]");
804 PUSH(
v->state, goto_state);
809 D_puts(
"(goto) not found: use default");
811 goto_state =
AREF(
v->goto_default, k1);
823 #define rb_intern(str) rb_intern_const(str)
840 rb_str_new2(
"$originalId: cparse.c,v 1.8 2006/07/06 11:39:46 aamine Exp $"));
853 id_errstatus =
rb_intern(
"@racc_error_status");
858 id_d_read_token =
rb_intern(
"racc_read_token");
859 id_d_next_state =
rb_intern(
"racc_next_state");
const char * rb_id2name(ID)
#define TypedData_Make_Struct(klass, type, data_type, sval)
int rb_const_defined(VALUE, ID)
#define SHIFT(v, act, tok, val)
VALUE rb_hash_aref(VALUE hash, VALUE key)
#define D_printf(fmt, arg)
#define RB_TYPE_P(obj, type)
VALUE rb_define_module(const char *name)
VALUE rb_const_get_at(VALUE, ID)
VALUE rb_ary_pop(VALUE ary)
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
void rb_undef_method(VALUE klass, const char *name)
#define RUBY_TYPED_DEFAULT_FREE
void rb_raise(VALUE exc, const char *fmt,...)
VALUE rb_ary_entry(VALUE ary, long offset)
VALUE rb_ivar_get(VALUE, ID)
VALUE rb_obj_class(VALUE)
Equivalent to Object#class in Ruby.
VALUE rb_catch(const char *q, type *w, VALUE e)
An equivalent of Kernel#catch.
VALUE rb_block_call_func(RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg))
#define rb_ary_subseq(ary, beg, len)
RUBY_EXTERN VALUE rb_cObject
#define RUBY_TYPED_FREE_IMMEDIATELY
VALUE rb_block_call(VALUE q, ID w, int e, const VALUE *r, type *t, VALUE y)
Call a method with a block.
void rb_define_const(VALUE, const char *, VALUE)
void rb_gc_mark(VALUE ptr)
void * rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
VALUE rb_iterate(VALUE(*q)(VALUE), VALUE w, type *e, VALUE r)
Old way to implement iterators.
VALUE rb_ivar_set(VALUE, ID, VALUE)
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
void rb_undef_alloc_func(VALUE)
void rb_define_private_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
VALUE rb_iv_set(VALUE, const char *, VALUE)
VALUE rb_const_get(VALUE, ID)