2002-08-06 19:42:04 -04:00
|
|
|
/*
|
2002-03-22 02:20:31 -05:00
|
|
|
|
2006-07-02 05:43:11 -04:00
|
|
|
cparse.c -- Racc Runtime Core
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2006-07-02 05:43:11 -04:00
|
|
|
Copyright (c) 1999-2006 Minero Aoki
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2002-03-22 02:20:31 -05:00
|
|
|
This library is free software.
|
|
|
|
You can distribute/modify this program under the same terms of ruby.
|
|
|
|
|
2006-07-06 08:50:51 -04:00
|
|
|
$originalId: cparse.c,v 1.8 2006/07/06 11:39:46 aamine Exp $
|
2002-03-22 02:20:31 -05:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2007-06-09 23:06:15 -04:00
|
|
|
#include "ruby/ruby.h"
|
2002-08-05 09:38:49 -04:00
|
|
|
|
2010-03-11 17:17:20 -05:00
|
|
|
#ifndef FALSE
|
|
|
|
#define FALSE 0
|
|
|
|
#endif
|
|
|
|
#ifndef TRUE
|
|
|
|
#define TRUE 1
|
|
|
|
#endif
|
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
/* -----------------------------------------------------------------------
|
|
|
|
Important Constants
|
|
|
|
----------------------------------------------------------------------- */
|
|
|
|
|
2006-07-02 05:43:11 -04:00
|
|
|
#define RACC_VERSION "1.4.5"
|
2002-03-22 02:20:31 -05:00
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
#define DEFAULT_TOKEN -1
|
|
|
|
#define ERROR_TOKEN 1
|
|
|
|
#define FINAL_TOKEN 0
|
|
|
|
|
|
|
|
#define vDEFAULT_TOKEN INT2FIX(DEFAULT_TOKEN)
|
|
|
|
#define vERROR_TOKEN INT2FIX(ERROR_TOKEN)
|
|
|
|
#define vFINAL_TOKEN INT2FIX(FINAL_TOKEN)
|
2002-03-22 02:20:31 -05:00
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
/* -----------------------------------------------------------------------
|
2003-11-03 08:55:01 -05:00
|
|
|
File Local Variables
|
2002-08-05 09:38:49 -04:00
|
|
|
----------------------------------------------------------------------- */
|
2002-03-22 02:20:31 -05:00
|
|
|
|
|
|
|
static VALUE RaccBug;
|
|
|
|
static VALUE CparseParams;
|
|
|
|
|
|
|
|
static ID id_yydebug;
|
|
|
|
static ID id_nexttoken;
|
|
|
|
static ID id_onerror;
|
|
|
|
static ID id_noreduce;
|
|
|
|
static ID id_errstatus;
|
|
|
|
|
|
|
|
static ID id_d_shift;
|
|
|
|
static ID id_d_reduce;
|
|
|
|
static ID id_d_accept;
|
|
|
|
static ID id_d_read_token;
|
|
|
|
static ID id_d_next_state;
|
|
|
|
static ID id_d_e_pop;
|
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
/* -----------------------------------------------------------------------
|
|
|
|
Utils
|
|
|
|
----------------------------------------------------------------------- */
|
|
|
|
|
2006-07-02 05:43:11 -04:00
|
|
|
/* For backward compatibility */
|
|
|
|
#ifndef ID2SYM
|
|
|
|
# define ID2SYM(i) ULONG2NUM(i)
|
|
|
|
#endif
|
|
|
|
#ifndef SYM2ID
|
|
|
|
# define SYM2ID(v) ((ID)NUM2ULONG(v))
|
|
|
|
#endif
|
|
|
|
#ifndef SYMBOL_P
|
|
|
|
# define SYMBOL_P(v) FIXNUM_P(v)
|
|
|
|
#endif
|
|
|
|
#ifndef LONG2NUM
|
|
|
|
# define LONG2NUM(i) INT2NUM(i)
|
|
|
|
#endif
|
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
static ID value_to_id _((VALUE v));
|
|
|
|
static inline long num_to_long _((VALUE n));
|
|
|
|
|
2002-03-22 02:20:31 -05:00
|
|
|
static ID
|
2006-06-20 14:02:17 -04:00
|
|
|
value_to_id(VALUE v)
|
2002-03-22 02:20:31 -05:00
|
|
|
{
|
|
|
|
if (! SYMBOL_P(v)) {
|
|
|
|
rb_raise(rb_eTypeError, "not symbol");
|
|
|
|
}
|
|
|
|
return SYM2ID(v);
|
|
|
|
}
|
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
static inline long
|
2006-06-20 14:02:17 -04:00
|
|
|
num_to_long(VALUE n)
|
2002-08-05 09:38:49 -04:00
|
|
|
{
|
|
|
|
return NUM2LONG(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define AREF(s, idx) \
|
2013-06-10 16:06:53 -04:00
|
|
|
((0 <= idx && idx < RARRAY_LEN(s)) ? rb_ary_entry(s, idx) : Qnil)
|
2002-08-05 09:38:49 -04:00
|
|
|
|
|
|
|
/* -----------------------------------------------------------------------
|
|
|
|
Parser Stack Interfaces
|
|
|
|
----------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static VALUE get_stack_tail _((VALUE stack, long len));
|
|
|
|
static void cut_stack_tail _((VALUE stack, long len));
|
|
|
|
|
2002-03-22 02:20:31 -05:00
|
|
|
static VALUE
|
2006-06-20 14:02:17 -04:00
|
|
|
get_stack_tail(VALUE stack, long len)
|
2002-03-22 02:20:31 -05:00
|
|
|
{
|
|
|
|
if (len < 0) return Qnil; /* system error */
|
2006-09-02 10:42:08 -04:00
|
|
|
if (len > RARRAY_LEN(stack)) len = RARRAY_LEN(stack);
|
2013-06-10 16:06:53 -04:00
|
|
|
return rb_ary_subseq(stack, RARRAY_LEN(stack) - len, len);
|
2002-03-22 02:20:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-06-20 14:02:17 -04:00
|
|
|
cut_stack_tail(VALUE stack, long len)
|
2002-03-22 02:20:31 -05:00
|
|
|
{
|
|
|
|
while (len > 0) {
|
|
|
|
rb_ary_pop(stack);
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define STACK_INIT_LEN 64
|
2002-08-05 09:38:49 -04:00
|
|
|
#define NEW_STACK() rb_ary_new2(STACK_INIT_LEN)
|
2006-09-02 10:42:08 -04:00
|
|
|
#define PUSH(s, i) rb_ary_store(s, RARRAY_LEN(s), i)
|
2002-08-05 09:38:49 -04:00
|
|
|
#define POP(s) rb_ary_pop(s)
|
2002-03-22 02:20:31 -05:00
|
|
|
#define LAST_I(s) \
|
2013-06-10 16:06:53 -04:00
|
|
|
((RARRAY_LEN(s) > 0) ? rb_ary_entry(s, RARRAY_LEN(s) - 1) : Qnil)
|
2002-08-05 09:38:49 -04:00
|
|
|
#define GET_TAIL(s, len) get_stack_tail(s, len)
|
|
|
|
#define CUT_TAIL(s, len) cut_stack_tail(s, len)
|
2002-03-22 02:20:31 -05:00
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
/* -----------------------------------------------------------------------
|
|
|
|
struct cparse_params
|
|
|
|
----------------------------------------------------------------------- */
|
2002-03-22 02:20:31 -05:00
|
|
|
|
|
|
|
struct cparse_params {
|
2002-08-05 09:38:49 -04:00
|
|
|
VALUE value_v; /* VALUE version of this struct */
|
|
|
|
|
|
|
|
VALUE parser; /* parser object */
|
2002-03-22 02:20:31 -05:00
|
|
|
|
2003-11-03 08:55:01 -05:00
|
|
|
int lex_is_iterator;
|
|
|
|
VALUE lexer; /* scanner object */
|
|
|
|
ID lexmid; /* name of scanner method (must be an iterator) */
|
2002-03-22 02:20:31 -05:00
|
|
|
|
2003-11-03 08:55:01 -05:00
|
|
|
/* State transition tables (immutable)
|
|
|
|
Data structure is from Dragon Book 4.9 */
|
2002-08-05 09:38:49 -04:00
|
|
|
/* action table */
|
2002-03-22 02:20:31 -05:00
|
|
|
VALUE action_table;
|
|
|
|
VALUE action_check;
|
|
|
|
VALUE action_default;
|
|
|
|
VALUE action_pointer;
|
2002-08-05 09:38:49 -04:00
|
|
|
/* goto table */
|
2002-03-22 02:20:31 -05:00
|
|
|
VALUE goto_table;
|
|
|
|
VALUE goto_check;
|
|
|
|
VALUE goto_default;
|
|
|
|
VALUE goto_pointer;
|
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
long nt_base; /* NonTerminal BASE index */
|
|
|
|
VALUE reduce_table; /* reduce data table */
|
|
|
|
VALUE token_table; /* token conversion table */
|
|
|
|
|
|
|
|
/* parser stacks and parameters */
|
2002-03-22 02:20:31 -05:00
|
|
|
VALUE state;
|
|
|
|
long curstate;
|
|
|
|
VALUE vstack;
|
|
|
|
VALUE tstack;
|
|
|
|
VALUE t;
|
|
|
|
long shift_n;
|
|
|
|
long reduce_n;
|
|
|
|
long ruleno;
|
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
long errstatus; /* nonzero in error recovering mode */
|
|
|
|
long nerr; /* number of error */
|
2002-03-22 02:20:31 -05:00
|
|
|
|
2003-11-03 08:55:01 -05:00
|
|
|
int use_result_var;
|
2002-03-22 02:20:31 -05:00
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
VALUE retval; /* return value of parser routine */
|
|
|
|
long fin; /* parse result status */
|
2002-03-22 02:20:31 -05:00
|
|
|
#define CP_FIN_ACCEPT 1
|
|
|
|
#define CP_FIN_EOT 2
|
|
|
|
#define CP_FIN_CANTPOP 3
|
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
int debug; /* user level debug */
|
|
|
|
int sys_debug; /* system level debug */
|
2002-03-22 02:20:31 -05:00
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
long i; /* table index */
|
2002-03-22 02:20:31 -05:00
|
|
|
};
|
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
/* -----------------------------------------------------------------------
|
|
|
|
Parser Main Routines
|
|
|
|
----------------------------------------------------------------------- */
|
2002-03-22 02:20:31 -05:00
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
static VALUE racc_cparse _((VALUE parser, VALUE arg, VALUE sysdebug));
|
|
|
|
static VALUE racc_yyparse _((VALUE parser, VALUE lexer, VALUE lexmid,
|
|
|
|
VALUE arg, VALUE sysdebug));
|
2002-03-22 02:20:31 -05:00
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
static void call_lexer _((struct cparse_params *v));
|
|
|
|
static VALUE lexer_i _((VALUE block_args, VALUE data, VALUE self));
|
2002-03-22 02:20:31 -05:00
|
|
|
|
2003-11-03 08:55:01 -05:00
|
|
|
static VALUE assert_array _((VALUE a));
|
|
|
|
static long assert_integer _((VALUE n));
|
|
|
|
static VALUE assert_hash _((VALUE h));
|
2006-07-06 08:50:51 -04:00
|
|
|
static VALUE initialize_params _((VALUE vparams, VALUE parser, VALUE arg,
|
2002-08-05 09:38:49 -04:00
|
|
|
VALUE lexer, VALUE lexmid));
|
2006-07-06 08:50:51 -04:00
|
|
|
static void cparse_params_mark _((void *ptr));
|
2002-08-05 09:38:49 -04:00
|
|
|
|
|
|
|
static void parse_main _((struct cparse_params *v,
|
|
|
|
VALUE tok, VALUE val, int resume));
|
|
|
|
static void extract_user_token _((struct cparse_params *v,
|
|
|
|
VALUE block_args, VALUE *tok, VALUE *val));
|
|
|
|
static void shift _((struct cparse_params* v, long act, VALUE tok, VALUE val));
|
|
|
|
static int reduce _((struct cparse_params* v, long act));
|
|
|
|
static VALUE reduce0 _((VALUE block_args, VALUE data, VALUE self));
|
2002-03-22 02:20:31 -05:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2003-11-03 08:55:01 -05:00
|
|
|
# define D_puts(msg) if (v->sys_debug) puts(msg)
|
|
|
|
# define D_printf(fmt,arg) if (v->sys_debug) printf(fmt,arg)
|
2002-03-22 02:20:31 -05:00
|
|
|
#else
|
2003-11-03 08:55:01 -05:00
|
|
|
# define D_puts(msg)
|
|
|
|
# define D_printf(fmt,arg)
|
2002-03-22 02:20:31 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static VALUE
|
2006-06-20 14:02:17 -04:00
|
|
|
racc_cparse(VALUE parser, VALUE arg, VALUE sysdebug)
|
2002-03-22 02:20:31 -05:00
|
|
|
{
|
2006-07-06 08:50:51 -04:00
|
|
|
volatile VALUE vparams;
|
|
|
|
struct cparse_params *v;
|
2002-03-22 02:20:31 -05:00
|
|
|
|
2006-07-06 08:50:51 -04:00
|
|
|
vparams = Data_Make_Struct(CparseParams, struct cparse_params,
|
|
|
|
cparse_params_mark, -1, v);
|
2003-11-03 08:55:01 -05:00
|
|
|
D_puts("starting cparse");
|
|
|
|
v->sys_debug = RTEST(sysdebug);
|
2006-07-06 08:50:51 -04:00
|
|
|
vparams = initialize_params(vparams, parser, arg, Qnil, Qnil);
|
2010-03-11 17:17:20 -05:00
|
|
|
v->lex_is_iterator = FALSE;
|
2003-11-03 08:55:01 -05:00
|
|
|
parse_main(v, Qnil, Qnil, 0);
|
|
|
|
|
|
|
|
return v->retval;
|
2002-03-22 02:20:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-06-20 14:02:17 -04:00
|
|
|
racc_yyparse(VALUE parser, VALUE lexer, VALUE lexmid, VALUE arg, VALUE sysdebug)
|
2002-03-22 02:20:31 -05:00
|
|
|
{
|
2006-07-06 08:50:51 -04:00
|
|
|
volatile VALUE vparams;
|
|
|
|
struct cparse_params *v;
|
2003-11-03 08:55:01 -05:00
|
|
|
|
2006-07-06 08:50:51 -04:00
|
|
|
vparams = Data_Make_Struct(CparseParams, struct cparse_params,
|
|
|
|
cparse_params_mark, -1, v);
|
2003-11-03 08:55:01 -05:00
|
|
|
v->sys_debug = RTEST(sysdebug);
|
|
|
|
D_puts("start C yyparse");
|
2006-07-06 08:50:51 -04:00
|
|
|
vparams = initialize_params(vparams, parser, arg, lexer, lexmid);
|
2010-03-11 17:17:20 -05:00
|
|
|
v->lex_is_iterator = TRUE;
|
2003-11-03 08:55:01 -05:00
|
|
|
D_puts("params initialized");
|
|
|
|
parse_main(v, Qnil, Qnil, 0);
|
|
|
|
call_lexer(v);
|
|
|
|
if (!v->fin) {
|
2002-03-22 02:20:31 -05:00
|
|
|
rb_raise(rb_eArgError, "%s() is finished before EndOfToken",
|
2003-11-03 08:55:01 -05:00
|
|
|
rb_id2name(v->lexmid));
|
2002-03-22 02:20:31 -05:00
|
|
|
}
|
|
|
|
|
2003-11-03 08:55:01 -05:00
|
|
|
return v->retval;
|
2002-03-22 02:20:31 -05:00
|
|
|
}
|
|
|
|
|
2006-07-02 05:43:11 -04:00
|
|
|
#ifdef HAVE_RB_BLOCK_CALL
|
2002-08-05 09:38:49 -04:00
|
|
|
static void
|
2006-06-20 14:02:17 -04:00
|
|
|
call_lexer(struct cparse_params *v)
|
2002-08-05 09:38:49 -04:00
|
|
|
{
|
2006-07-02 05:43:11 -04:00
|
|
|
rb_block_call(v->lexer, v->lexmid, 0, NULL, lexer_i, v->value_v);
|
2002-08-05 09:38:49 -04:00
|
|
|
}
|
2006-07-02 05:43:11 -04:00
|
|
|
#else
|
2002-03-22 02:20:31 -05:00
|
|
|
static VALUE
|
2006-06-20 14:02:17 -04:00
|
|
|
lexer_iter(VALUE data)
|
2002-03-22 02:20:31 -05:00
|
|
|
{
|
|
|
|
struct cparse_params *v;
|
|
|
|
|
|
|
|
Data_Get_Struct(data, struct cparse_params, v);
|
2002-08-05 09:38:49 -04:00
|
|
|
rb_funcall(v->lexer, v->lexmid, 0);
|
2002-03-22 02:20:31 -05:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2006-07-02 05:43:11 -04:00
|
|
|
static void
|
|
|
|
call_lexer(struct cparse_params *v)
|
|
|
|
{
|
|
|
|
rb_iterate(lexer_iter, v->value_v, lexer_i, v->value_v);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-03-22 02:20:31 -05:00
|
|
|
static VALUE
|
2006-06-20 14:02:17 -04:00
|
|
|
lexer_i(VALUE block_args, VALUE data, VALUE self)
|
2002-03-22 02:20:31 -05:00
|
|
|
{
|
|
|
|
struct cparse_params *v;
|
|
|
|
VALUE tok, val;
|
|
|
|
|
|
|
|
Data_Get_Struct(data, struct cparse_params, v);
|
|
|
|
if (v->fin)
|
|
|
|
rb_raise(rb_eArgError, "extra token after EndOfToken");
|
2002-08-05 09:38:49 -04:00
|
|
|
extract_user_token(v, block_args, &tok, &val);
|
|
|
|
parse_main(v, tok, val, 1);
|
2002-03-22 02:20:31 -05:00
|
|
|
if (v->fin && v->fin != CP_FIN_ACCEPT)
|
2010-04-22 04:04:13 -04:00
|
|
|
rb_iter_break();
|
2002-03-22 02:20:31 -05:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
static VALUE
|
2006-06-20 14:02:17 -04:00
|
|
|
assert_array(VALUE a)
|
2002-03-22 02:20:31 -05:00
|
|
|
{
|
2002-08-05 09:38:49 -04:00
|
|
|
Check_Type(a, T_ARRAY);
|
|
|
|
return a;
|
2002-03-22 02:20:31 -05:00
|
|
|
}
|
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
static VALUE
|
2006-06-20 14:02:17 -04:00
|
|
|
assert_hash(VALUE h)
|
2002-03-22 02:20:31 -05:00
|
|
|
{
|
2002-08-05 09:38:49 -04:00
|
|
|
Check_Type(h, T_HASH);
|
|
|
|
return h;
|
|
|
|
}
|
2002-03-22 02:20:31 -05:00
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
static long
|
2006-06-20 14:02:17 -04:00
|
|
|
assert_integer(VALUE n)
|
2002-08-05 09:38:49 -04:00
|
|
|
{
|
|
|
|
return NUM2LONG(n);
|
|
|
|
}
|
2002-03-22 02:20:31 -05:00
|
|
|
|
2006-07-06 08:50:51 -04:00
|
|
|
static VALUE
|
|
|
|
initialize_params(VALUE vparams, VALUE parser, VALUE arg, VALUE lexer, VALUE lexmid)
|
2002-08-05 09:38:49 -04:00
|
|
|
{
|
2006-07-06 08:50:51 -04:00
|
|
|
struct cparse_params *v;
|
2002-03-22 02:20:31 -05:00
|
|
|
|
2006-07-06 08:50:51 -04:00
|
|
|
Data_Get_Struct(vparams, struct cparse_params, v);
|
|
|
|
v->value_v = vparams;
|
2002-03-22 02:20:31 -05:00
|
|
|
v->parser = parser;
|
2002-08-05 09:38:49 -04:00
|
|
|
v->lexer = lexer;
|
|
|
|
if (! NIL_P(lexmid))
|
|
|
|
v->lexmid = value_to_id(lexmid);
|
2002-03-22 02:20:31 -05:00
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
v->debug = RTEST(rb_ivar_get(parser, id_yydebug));
|
2002-03-22 02:20:31 -05:00
|
|
|
|
|
|
|
Check_Type(arg, T_ARRAY);
|
2006-09-02 10:42:08 -04:00
|
|
|
if (!(13 <= RARRAY_LEN(arg) && RARRAY_LEN(arg) <= 14))
|
|
|
|
rb_raise(RaccBug, "[Racc Bug] wrong arg.size %ld", RARRAY_LEN(arg));
|
2013-06-10 16:06:53 -04:00
|
|
|
v->action_table = assert_array (rb_ary_entry(arg, 0));
|
|
|
|
v->action_check = assert_array (rb_ary_entry(arg, 1));
|
|
|
|
v->action_default = assert_array (rb_ary_entry(arg, 2));
|
|
|
|
v->action_pointer = assert_array (rb_ary_entry(arg, 3));
|
|
|
|
v->goto_table = assert_array (rb_ary_entry(arg, 4));
|
|
|
|
v->goto_check = assert_array (rb_ary_entry(arg, 5));
|
|
|
|
v->goto_default = assert_array (rb_ary_entry(arg, 6));
|
|
|
|
v->goto_pointer = assert_array (rb_ary_entry(arg, 7));
|
|
|
|
v->nt_base = assert_integer(rb_ary_entry(arg, 8));
|
|
|
|
v->reduce_table = assert_array (rb_ary_entry(arg, 9));
|
|
|
|
v->token_table = assert_hash (rb_ary_entry(arg, 10));
|
|
|
|
v->shift_n = assert_integer(rb_ary_entry(arg, 11));
|
|
|
|
v->reduce_n = assert_integer(rb_ary_entry(arg, 12));
|
2006-09-02 10:42:08 -04:00
|
|
|
if (RARRAY_LEN(arg) > 13) {
|
2013-06-10 16:06:53 -04:00
|
|
|
v->use_result_var = RTEST(rb_ary_entry(arg, 13));
|
2002-03-22 02:20:31 -05:00
|
|
|
}
|
|
|
|
else {
|
2010-03-11 17:17:20 -05:00
|
|
|
v->use_result_var = TRUE;
|
2002-03-22 02:20:31 -05:00
|
|
|
}
|
2002-08-05 09:38:49 -04:00
|
|
|
|
|
|
|
v->tstack = v->debug ? NEW_STACK() : Qnil;
|
|
|
|
v->vstack = NEW_STACK();
|
|
|
|
v->state = NEW_STACK();
|
2002-03-22 02:20:31 -05:00
|
|
|
v->curstate = 0;
|
|
|
|
PUSH(v->state, INT2FIX(0));
|
2002-08-05 09:38:49 -04:00
|
|
|
v->t = INT2FIX(FINAL_TOKEN + 1); /* must not init to FINAL_TOKEN */
|
2002-03-22 02:20:31 -05:00
|
|
|
v->nerr = 0;
|
|
|
|
v->errstatus = 0;
|
|
|
|
rb_ivar_set(parser, id_errstatus, LONG2NUM(v->errstatus));
|
|
|
|
|
|
|
|
v->retval = Qnil;
|
|
|
|
v->fin = 0;
|
|
|
|
|
2010-03-11 17:17:20 -05:00
|
|
|
v->lex_is_iterator = FALSE;
|
2006-07-06 08:50:51 -04:00
|
|
|
|
|
|
|
rb_iv_set(parser, "@vstack", v->vstack);
|
|
|
|
if (v->debug) {
|
|
|
|
rb_iv_set(parser, "@tstack", v->tstack);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_iv_set(parser, "@tstack", Qnil);
|
|
|
|
}
|
|
|
|
|
|
|
|
return vparams;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cparse_params_mark(void *ptr)
|
|
|
|
{
|
|
|
|
struct cparse_params *v = (struct cparse_params*)ptr;
|
|
|
|
|
|
|
|
rb_gc_mark(v->value_v);
|
|
|
|
rb_gc_mark(v->parser);
|
|
|
|
rb_gc_mark(v->lexer);
|
|
|
|
rb_gc_mark(v->action_table);
|
|
|
|
rb_gc_mark(v->action_check);
|
|
|
|
rb_gc_mark(v->action_default);
|
|
|
|
rb_gc_mark(v->action_pointer);
|
|
|
|
rb_gc_mark(v->goto_table);
|
|
|
|
rb_gc_mark(v->goto_check);
|
|
|
|
rb_gc_mark(v->goto_default);
|
|
|
|
rb_gc_mark(v->goto_pointer);
|
|
|
|
rb_gc_mark(v->reduce_table);
|
|
|
|
rb_gc_mark(v->token_table);
|
|
|
|
rb_gc_mark(v->state);
|
|
|
|
rb_gc_mark(v->vstack);
|
|
|
|
rb_gc_mark(v->tstack);
|
|
|
|
rb_gc_mark(v->t);
|
|
|
|
rb_gc_mark(v->retval);
|
2002-03-22 02:20:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-07-02 05:43:11 -04:00
|
|
|
extract_user_token(struct cparse_params *v, VALUE block_args,
|
|
|
|
VALUE *tok, VALUE *val)
|
2002-03-22 02:20:31 -05:00
|
|
|
{
|
2002-08-05 09:38:49 -04:00
|
|
|
if (NIL_P(block_args)) {
|
2002-03-22 02:20:31 -05:00
|
|
|
/* EOF */
|
2002-08-05 09:38:49 -04:00
|
|
|
*tok = Qfalse;
|
|
|
|
*val = rb_str_new("$", 1);
|
2002-03-22 02:20:31 -05:00
|
|
|
return;
|
|
|
|
}
|
2002-08-05 09:38:49 -04:00
|
|
|
|
2012-05-23 03:13:21 -04:00
|
|
|
if (!RB_TYPE_P(block_args, T_ARRAY)) {
|
2002-03-22 02:20:31 -05:00
|
|
|
rb_raise(rb_eTypeError,
|
|
|
|
"%s() %s %s (must be Array[2])",
|
2003-11-03 08:55:01 -05:00
|
|
|
v->lex_is_iterator ? rb_id2name(v->lexmid) : "next_token",
|
|
|
|
v->lex_is_iterator ? "yielded" : "returned",
|
2002-08-05 09:38:49 -04:00
|
|
|
rb_class2name(CLASS_OF(block_args)));
|
2002-03-22 02:20:31 -05:00
|
|
|
}
|
2006-09-02 10:42:08 -04:00
|
|
|
if (RARRAY_LEN(block_args) != 2) {
|
2002-03-22 02:20:31 -05:00
|
|
|
rb_raise(rb_eArgError,
|
|
|
|
"%s() %s wrong size of array (%ld for 2)",
|
2003-11-03 08:55:01 -05:00
|
|
|
v->lex_is_iterator ? rb_id2name(v->lexmid) : "next_token",
|
|
|
|
v->lex_is_iterator ? "yielded" : "returned",
|
2006-09-02 10:42:08 -04:00
|
|
|
RARRAY_LEN(block_args));
|
2002-08-05 09:38:49 -04:00
|
|
|
}
|
|
|
|
*tok = AREF(block_args, 0);
|
|
|
|
*val = AREF(block_args, 1);
|
2002-03-22 02:20:31 -05:00
|
|
|
}
|
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
#define SHIFT(v,act,tok,val) shift(v,act,tok,val)
|
|
|
|
#define REDUCE(v,act) do {\
|
|
|
|
switch (reduce(v,act)) { \
|
|
|
|
case 0: /* normal */ \
|
|
|
|
break; \
|
|
|
|
case 1: /* yyerror */ \
|
|
|
|
goto user_yyerror; \
|
|
|
|
case 2: /* yyaccept */ \
|
2003-11-03 08:55:01 -05:00
|
|
|
D_puts("u accept"); \
|
2002-08-05 09:38:49 -04:00
|
|
|
goto accept; \
|
|
|
|
default: \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2002-03-22 02:20:31 -05:00
|
|
|
static void
|
2006-06-20 14:02:17 -04:00
|
|
|
parse_main(struct cparse_params *v, VALUE tok, VALUE val, int resume)
|
2002-03-22 02:20:31 -05:00
|
|
|
{
|
2003-11-03 08:55:01 -05:00
|
|
|
long i; /* table index */
|
|
|
|
long act; /* action type */
|
|
|
|
VALUE act_value; /* action type, VALUE version */
|
|
|
|
int read_next = 1; /* true if we need to read next token */
|
2002-08-05 09:38:49 -04:00
|
|
|
VALUE tmp;
|
2002-03-22 02:20:31 -05:00
|
|
|
|
|
|
|
if (resume)
|
|
|
|
goto resume;
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2002-03-22 02:20:31 -05:00
|
|
|
while (1) {
|
2003-11-03 08:55:01 -05:00
|
|
|
D_puts("");
|
|
|
|
D_puts("---- enter new loop ----");
|
|
|
|
D_puts("");
|
2002-03-22 02:20:31 -05:00
|
|
|
|
2003-11-03 08:55:01 -05:00
|
|
|
D_printf("(act) k1=%ld\n", v->curstate);
|
2002-03-22 02:20:31 -05:00
|
|
|
tmp = AREF(v->action_pointer, v->curstate);
|
2002-08-05 09:38:49 -04:00
|
|
|
if (NIL_P(tmp)) goto notfound;
|
2003-11-03 08:55:01 -05:00
|
|
|
D_puts("(act) pointer[k1] ok");
|
2002-08-05 09:38:49 -04:00
|
|
|
i = NUM2LONG(tmp);
|
|
|
|
|
2003-11-03 08:55:01 -05:00
|
|
|
D_printf("read_next=%d\n", read_next);
|
|
|
|
if (read_next && (v->t != vFINAL_TOKEN)) {
|
|
|
|
if (v->lex_is_iterator) {
|
|
|
|
D_puts("resuming...");
|
|
|
|
if (v->fin) rb_raise(rb_eArgError, "token given after EOF");
|
|
|
|
v->i = i; /* save i */
|
|
|
|
return;
|
|
|
|
resume:
|
|
|
|
D_puts("resumed");
|
|
|
|
i = v->i; /* load i */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
D_puts("next_token");
|
|
|
|
tmp = rb_funcall(v->parser, id_nexttoken, 0);
|
|
|
|
extract_user_token(v, tmp, &tok, &val);
|
|
|
|
}
|
|
|
|
/* convert token */
|
2006-07-06 08:50:51 -04:00
|
|
|
v->t = rb_hash_aref(v->token_table, tok);
|
|
|
|
if (NIL_P(v->t)) {
|
|
|
|
v->t = vERROR_TOKEN;
|
|
|
|
}
|
2003-11-03 08:55:01 -05:00
|
|
|
D_printf("(act) t(k2)=%ld\n", NUM2LONG(v->t));
|
|
|
|
if (v->debug) {
|
|
|
|
rb_funcall(v->parser, id_d_read_token,
|
|
|
|
3, v->t, tok, val);
|
2002-03-22 02:20:31 -05:00
|
|
|
}
|
|
|
|
}
|
2003-11-03 08:55:01 -05:00
|
|
|
read_next = 0;
|
2002-03-22 02:20:31 -05:00
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
i += NUM2LONG(v->t);
|
2003-11-03 08:55:01 -05:00
|
|
|
D_printf("(act) i=%ld\n", i);
|
2002-08-05 09:38:49 -04:00
|
|
|
if (i < 0) goto notfound;
|
|
|
|
|
2003-11-03 08:55:01 -05:00
|
|
|
act_value = AREF(v->action_table, i);
|
|
|
|
if (NIL_P(act_value)) goto notfound;
|
|
|
|
act = NUM2LONG(act_value);
|
|
|
|
D_printf("(act) table[i]=%ld\n", act);
|
2002-08-05 09:38:49 -04:00
|
|
|
|
|
|
|
tmp = AREF(v->action_check, i);
|
|
|
|
if (NIL_P(tmp)) goto notfound;
|
|
|
|
if (NUM2LONG(tmp) != v->curstate) goto notfound;
|
2003-11-03 08:55:01 -05:00
|
|
|
D_printf("(act) check[i]=%ld\n", NUM2LONG(tmp));
|
2002-08-05 09:38:49 -04:00
|
|
|
|
2003-11-03 08:55:01 -05:00
|
|
|
D_puts("(act) found");
|
2002-08-05 09:38:49 -04:00
|
|
|
act_fixed:
|
2003-11-03 08:55:01 -05:00
|
|
|
D_printf("act=%ld\n", act);
|
2002-08-05 09:38:49 -04:00
|
|
|
goto handle_act;
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
notfound:
|
2003-11-03 08:55:01 -05:00
|
|
|
D_puts("(act) not found: use default");
|
|
|
|
act_value = AREF(v->action_default, v->curstate);
|
|
|
|
act = NUM2LONG(act_value);
|
2002-08-05 09:38:49 -04:00
|
|
|
goto act_fixed;
|
2002-03-22 02:20:31 -05:00
|
|
|
|
2003-11-03 08:55:01 -05:00
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
handle_act:
|
2002-03-22 02:20:31 -05:00
|
|
|
if (act > 0 && act < v->shift_n) {
|
2003-11-03 08:55:01 -05:00
|
|
|
D_puts("shift");
|
2002-03-22 02:20:31 -05:00
|
|
|
if (v->errstatus > 0) {
|
|
|
|
v->errstatus--;
|
|
|
|
rb_ivar_set(v->parser, id_errstatus, LONG2NUM(v->errstatus));
|
|
|
|
}
|
|
|
|
SHIFT(v, act, v->t, val);
|
|
|
|
read_next = 1;
|
|
|
|
}
|
|
|
|
else if (act < 0 && act > -(v->reduce_n)) {
|
2003-11-03 08:55:01 -05:00
|
|
|
D_puts("reduce");
|
2002-03-22 02:20:31 -05:00
|
|
|
REDUCE(v, act);
|
|
|
|
}
|
|
|
|
else if (act == -(v->reduce_n)) {
|
2002-08-05 09:38:49 -04:00
|
|
|
goto error;
|
2003-11-03 08:55:01 -05:00
|
|
|
error_recovered:
|
2002-08-06 19:42:04 -04:00
|
|
|
; /* goto label requires stmt */
|
2002-03-22 02:20:31 -05:00
|
|
|
}
|
|
|
|
else if (act == v->shift_n) {
|
2003-11-03 08:55:01 -05:00
|
|
|
D_puts("accept");
|
2002-08-05 09:38:49 -04:00
|
|
|
goto accept;
|
2002-03-22 02:20:31 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(RaccBug, "[Racc Bug] unknown act value %ld", act);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v->debug) {
|
|
|
|
rb_funcall(v->parser, id_d_next_state,
|
|
|
|
2, LONG2NUM(v->curstate), v->state);
|
|
|
|
}
|
|
|
|
}
|
2002-08-05 09:38:49 -04:00
|
|
|
/* not reach */
|
|
|
|
|
|
|
|
|
|
|
|
accept:
|
|
|
|
if (v->debug) rb_funcall(v->parser, id_d_accept, 0);
|
2013-06-10 16:06:53 -04:00
|
|
|
v->retval = rb_ary_entry(v->vstack, 0);
|
2002-08-05 09:38:49 -04:00
|
|
|
v->fin = CP_FIN_ACCEPT;
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
error:
|
2003-11-03 08:55:01 -05:00
|
|
|
D_printf("error detected, status=%ld\n", v->errstatus);
|
2002-08-05 09:38:49 -04:00
|
|
|
if (v->errstatus == 0) {
|
|
|
|
v->nerr++;
|
|
|
|
rb_funcall(v->parser, id_onerror,
|
|
|
|
3, v->t, val, v->vstack);
|
|
|
|
}
|
|
|
|
user_yyerror:
|
|
|
|
if (v->errstatus == 3) {
|
|
|
|
if (v->t == vFINAL_TOKEN) {
|
|
|
|
v->retval = Qfalse;
|
|
|
|
v->fin = CP_FIN_EOT;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
read_next = 1;
|
|
|
|
}
|
|
|
|
v->errstatus = 3;
|
|
|
|
rb_ivar_set(v->parser, id_errstatus, LONG2NUM(v->errstatus));
|
|
|
|
|
|
|
|
/* check if we can shift/reduce error token */
|
2003-11-03 08:55:01 -05:00
|
|
|
D_printf("(err) k1=%ld\n", v->curstate);
|
|
|
|
D_printf("(err) k2=%d (error)\n", ERROR_TOKEN);
|
2002-08-06 19:42:04 -04:00
|
|
|
while (1) {
|
|
|
|
tmp = AREF(v->action_pointer, v->curstate);
|
2003-11-03 08:55:01 -05:00
|
|
|
if (NIL_P(tmp)) goto error_pop;
|
|
|
|
D_puts("(err) pointer[k1] ok");
|
2002-08-05 09:38:49 -04:00
|
|
|
|
2002-08-06 19:42:04 -04:00
|
|
|
i = NUM2LONG(tmp) + ERROR_TOKEN;
|
2003-11-03 08:55:01 -05:00
|
|
|
D_printf("(err) i=%ld\n", i);
|
|
|
|
if (i < 0) goto error_pop;
|
2002-08-05 09:38:49 -04:00
|
|
|
|
2003-11-03 08:55:01 -05:00
|
|
|
act_value = AREF(v->action_table, i);
|
|
|
|
if (NIL_P(act_value)) {
|
|
|
|
D_puts("(err) table[i] == nil");
|
|
|
|
goto error_pop;
|
2002-08-06 19:42:04 -04:00
|
|
|
}
|
2003-11-03 08:55:01 -05:00
|
|
|
act = NUM2LONG(act_value);
|
|
|
|
D_printf("(err) table[i]=%ld\n", act);
|
2002-08-05 09:38:49 -04:00
|
|
|
|
2002-08-06 19:42:04 -04:00
|
|
|
tmp = AREF(v->action_check, i);
|
|
|
|
if (NIL_P(tmp)) {
|
2003-11-03 08:55:01 -05:00
|
|
|
D_puts("(err) check[i] == nil");
|
|
|
|
goto error_pop;
|
2002-08-06 19:42:04 -04:00
|
|
|
}
|
|
|
|
if (NUM2LONG(tmp) != v->curstate) {
|
2003-11-03 08:55:01 -05:00
|
|
|
D_puts("(err) check[i] != k1");
|
|
|
|
goto error_pop;
|
2002-08-06 19:42:04 -04:00
|
|
|
}
|
|
|
|
|
2003-11-03 08:55:01 -05:00
|
|
|
D_puts("(err) found: can handle error token");
|
2002-08-06 19:42:04 -04:00
|
|
|
break;
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2003-11-03 08:55:01 -05:00
|
|
|
error_pop:
|
|
|
|
D_puts("(err) act not found: can't handle error token; pop");
|
2002-08-06 19:42:04 -04:00
|
|
|
|
2006-09-02 10:42:08 -04:00
|
|
|
if (RARRAY_LEN(v->state) <= 1) {
|
2002-08-06 19:42:04 -04:00
|
|
|
v->retval = Qnil;
|
|
|
|
v->fin = CP_FIN_CANTPOP;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
POP(v->state);
|
|
|
|
POP(v->vstack);
|
|
|
|
v->curstate = num_to_long(LAST_I(v->state));
|
|
|
|
if (v->debug) {
|
|
|
|
POP(v->tstack);
|
|
|
|
rb_funcall(v->parser, id_d_e_pop,
|
|
|
|
3, v->state, v->tstack, v->vstack);
|
|
|
|
}
|
2002-08-05 09:38:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* shift/reduce error token */
|
|
|
|
if (act > 0 && act < v->shift_n) {
|
2003-11-03 08:55:01 -05:00
|
|
|
D_puts("e shift");
|
2002-08-05 09:38:49 -04:00
|
|
|
SHIFT(v, act, ERROR_TOKEN, val);
|
|
|
|
}
|
|
|
|
else if (act < 0 && act > -(v->reduce_n)) {
|
2003-11-03 08:55:01 -05:00
|
|
|
D_puts("e reduce");
|
2002-08-05 09:38:49 -04:00
|
|
|
REDUCE(v, act);
|
|
|
|
}
|
|
|
|
else if (act == v->shift_n) {
|
2003-11-03 08:55:01 -05:00
|
|
|
D_puts("e accept");
|
2002-08-05 09:38:49 -04:00
|
|
|
goto accept;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(RaccBug, "[Racc Bug] unknown act value %ld", act);
|
|
|
|
}
|
2003-11-03 08:55:01 -05:00
|
|
|
goto error_recovered;
|
2002-03-22 02:20:31 -05:00
|
|
|
}
|
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
static void
|
2006-06-20 14:02:17 -04:00
|
|
|
shift(struct cparse_params *v, long act, VALUE tok, VALUE val)
|
2002-08-05 09:38:49 -04:00
|
|
|
{
|
|
|
|
PUSH(v->vstack, val);
|
|
|
|
if (v->debug) {
|
|
|
|
PUSH(v->tstack, tok);
|
|
|
|
rb_funcall(v->parser, id_d_shift,
|
|
|
|
3, tok, v->tstack, v->vstack);
|
|
|
|
}
|
|
|
|
v->curstate = act;
|
|
|
|
PUSH(v->state, LONG2NUM(v->curstate));
|
|
|
|
}
|
2002-03-22 02:20:31 -05:00
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
static int
|
2006-06-20 14:02:17 -04:00
|
|
|
reduce(struct cparse_params *v, long act)
|
2002-03-22 02:20:31 -05:00
|
|
|
{
|
2002-08-05 09:38:49 -04:00
|
|
|
VALUE code;
|
|
|
|
v->ruleno = -act * 3;
|
2006-07-02 05:43:11 -04:00
|
|
|
code = rb_catch("racc_jump", reduce0, v->value_v);
|
2002-08-05 09:38:49 -04:00
|
|
|
v->errstatus = num_to_long(rb_ivar_get(v->parser, id_errstatus));
|
|
|
|
return NUM2INT(code);
|
2002-03-22 02:20:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2006-06-20 14:02:17 -04:00
|
|
|
reduce0(VALUE val, VALUE data, VALUE self)
|
2002-03-22 02:20:31 -05:00
|
|
|
{
|
|
|
|
struct cparse_params *v;
|
|
|
|
VALUE reduce_to, reduce_len, method_id;
|
|
|
|
long len;
|
|
|
|
ID mid;
|
2006-07-02 05:43:11 -04:00
|
|
|
VALUE tmp, tmp_t = Qundef, tmp_v = Qundef;
|
2002-03-22 02:20:31 -05:00
|
|
|
long i, k1, k2;
|
2002-08-05 09:38:49 -04:00
|
|
|
VALUE goto_state;
|
2002-03-22 02:20:31 -05:00
|
|
|
|
|
|
|
Data_Get_Struct(data, struct cparse_params, v);
|
2013-06-10 16:06:53 -04:00
|
|
|
reduce_len = rb_ary_entry(v->reduce_table, v->ruleno);
|
|
|
|
reduce_to = rb_ary_entry(v->reduce_table, v->ruleno+1);
|
|
|
|
method_id = rb_ary_entry(v->reduce_table, v->ruleno+2);
|
2002-03-22 02:20:31 -05:00
|
|
|
len = NUM2LONG(reduce_len);
|
|
|
|
mid = value_to_id(method_id);
|
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
/* call action */
|
2002-03-22 02:20:31 -05:00
|
|
|
if (len == 0) {
|
|
|
|
tmp = Qnil;
|
|
|
|
if (mid != id_noreduce)
|
|
|
|
tmp_v = rb_ary_new();
|
|
|
|
if (v->debug)
|
|
|
|
tmp_t = rb_ary_new();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (mid != id_noreduce) {
|
|
|
|
tmp_v = GET_TAIL(v->vstack, len);
|
2013-06-10 16:06:53 -04:00
|
|
|
tmp = rb_ary_entry(tmp_v, 0);
|
2002-03-22 02:20:31 -05:00
|
|
|
}
|
|
|
|
else {
|
2013-06-10 16:06:53 -04:00
|
|
|
tmp = rb_ary_entry(v->vstack, RARRAY_LEN(v->vstack) - len);
|
2002-03-22 02:20:31 -05:00
|
|
|
}
|
|
|
|
CUT_TAIL(v->vstack, len);
|
|
|
|
if (v->debug) {
|
|
|
|
tmp_t = GET_TAIL(v->tstack, len);
|
|
|
|
CUT_TAIL(v->tstack, len);
|
|
|
|
}
|
|
|
|
CUT_TAIL(v->state, len);
|
|
|
|
}
|
|
|
|
if (mid != id_noreduce) {
|
|
|
|
if (v->use_result_var) {
|
|
|
|
tmp = rb_funcall(v->parser, mid,
|
|
|
|
3, tmp_v, v->vstack, tmp);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tmp = rb_funcall(v->parser, mid,
|
|
|
|
2, tmp_v, v->vstack);
|
|
|
|
}
|
|
|
|
}
|
2002-08-05 09:38:49 -04:00
|
|
|
|
|
|
|
/* then push result */
|
2002-03-22 02:20:31 -05:00
|
|
|
PUSH(v->vstack, tmp);
|
|
|
|
if (v->debug) {
|
|
|
|
PUSH(v->tstack, reduce_to);
|
|
|
|
rb_funcall(v->parser, id_d_reduce,
|
|
|
|
4, tmp_t, reduce_to, v->tstack, v->vstack);
|
|
|
|
}
|
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
/* calculate transition state */
|
2006-09-02 10:42:08 -04:00
|
|
|
if (RARRAY_LEN(v->state) == 0)
|
2004-03-29 02:54:38 -05:00
|
|
|
rb_raise(RaccBug, "state stack unexpectedly empty");
|
2002-08-05 09:38:49 -04:00
|
|
|
k2 = num_to_long(LAST_I(v->state));
|
|
|
|
k1 = num_to_long(reduce_to) - v->nt_base;
|
2003-11-03 08:55:01 -05:00
|
|
|
D_printf("(goto) k1=%ld\n", k1);
|
|
|
|
D_printf("(goto) k2=%ld\n", k2);
|
2002-03-22 02:20:31 -05:00
|
|
|
|
|
|
|
tmp = AREF(v->goto_pointer, k1);
|
2002-08-05 09:38:49 -04:00
|
|
|
if (NIL_P(tmp)) goto notfound;
|
|
|
|
|
|
|
|
i = NUM2LONG(tmp) + k2;
|
2003-11-03 08:55:01 -05:00
|
|
|
D_printf("(goto) i=%ld\n", i);
|
2002-08-05 09:38:49 -04:00
|
|
|
if (i < 0) goto notfound;
|
|
|
|
|
|
|
|
goto_state = AREF(v->goto_table, i);
|
|
|
|
if (NIL_P(goto_state)) {
|
2003-11-03 08:55:01 -05:00
|
|
|
D_puts("(goto) table[i] == nil");
|
2002-08-05 09:38:49 -04:00
|
|
|
goto notfound;
|
2002-03-22 02:20:31 -05:00
|
|
|
}
|
2003-11-03 08:55:01 -05:00
|
|
|
D_printf("(goto) table[i]=%ld (goto_state)\n", NUM2LONG(goto_state));
|
2002-03-22 02:20:31 -05:00
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
tmp = AREF(v->goto_check, i);
|
|
|
|
if (NIL_P(tmp)) {
|
2003-11-03 08:55:01 -05:00
|
|
|
D_puts("(goto) check[i] == nil");
|
2002-08-05 09:38:49 -04:00
|
|
|
goto notfound;
|
|
|
|
}
|
|
|
|
if (tmp != LONG2NUM(k1)) {
|
2003-11-03 08:55:01 -05:00
|
|
|
D_puts("(goto) check[i] != table[i]");
|
2002-08-05 09:38:49 -04:00
|
|
|
goto notfound;
|
|
|
|
}
|
2003-11-03 08:55:01 -05:00
|
|
|
D_printf("(goto) check[i]=%ld\n", NUM2LONG(tmp));
|
2002-03-22 02:20:31 -05:00
|
|
|
|
2003-11-03 08:55:01 -05:00
|
|
|
D_puts("(goto) found");
|
2002-08-05 09:38:49 -04:00
|
|
|
transit:
|
|
|
|
PUSH(v->state, goto_state);
|
|
|
|
v->curstate = NUM2LONG(goto_state);
|
2002-03-22 02:20:31 -05:00
|
|
|
return INT2FIX(0);
|
2002-08-05 09:38:49 -04:00
|
|
|
|
|
|
|
notfound:
|
2003-11-03 08:55:01 -05:00
|
|
|
D_puts("(goto) not found: use default");
|
2002-08-05 09:38:49 -04:00
|
|
|
/* overwrite `goto-state' by default value */
|
|
|
|
goto_state = AREF(v->goto_default, k1);
|
|
|
|
goto transit;
|
2002-03-22 02:20:31 -05:00
|
|
|
}
|
|
|
|
|
2002-08-05 09:38:49 -04:00
|
|
|
/* -----------------------------------------------------------------------
|
|
|
|
Ruby Interface
|
|
|
|
----------------------------------------------------------------------- */
|
|
|
|
|
2002-03-22 02:20:31 -05:00
|
|
|
void
|
2006-07-02 05:43:11 -04:00
|
|
|
Init_cparse(void)
|
2002-03-22 02:20:31 -05:00
|
|
|
{
|
2003-11-03 08:55:01 -05:00
|
|
|
VALUE Racc, Parser;
|
2002-03-22 02:20:31 -05:00
|
|
|
ID id_racc = rb_intern("Racc");
|
|
|
|
|
|
|
|
if (rb_const_defined(rb_cObject, id_racc)) {
|
|
|
|
Racc = rb_const_get(rb_cObject, id_racc);
|
|
|
|
Parser = rb_const_get_at(Racc, rb_intern("Parser"));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Racc = rb_define_module("Racc");
|
|
|
|
Parser = rb_define_class_under(Racc, "Parser", rb_cObject);
|
|
|
|
}
|
|
|
|
rb_define_private_method(Parser, "_racc_do_parse_c", racc_cparse, 2);
|
|
|
|
rb_define_private_method(Parser, "_racc_yyparse_c", racc_yyparse, 4);
|
|
|
|
rb_define_const(Parser, "Racc_Runtime_Core_Version_C",
|
2002-08-05 09:38:49 -04:00
|
|
|
rb_str_new2(RACC_VERSION));
|
2002-03-22 02:20:31 -05:00
|
|
|
rb_define_const(Parser, "Racc_Runtime_Core_Id_C",
|
2006-07-06 08:50:51 -04:00
|
|
|
rb_str_new2("$originalId: cparse.c,v 1.8 2006/07/06 11:39:46 aamine Exp $"));
|
2002-03-22 02:20:31 -05:00
|
|
|
|
|
|
|
CparseParams = rb_define_class_under(Racc, "CparseParams", rb_cObject);
|
|
|
|
|
|
|
|
RaccBug = rb_eRuntimeError;
|
|
|
|
|
|
|
|
id_yydebug = rb_intern("@yydebug");
|
|
|
|
id_nexttoken = rb_intern("next_token");
|
|
|
|
id_onerror = rb_intern("on_error");
|
|
|
|
id_noreduce = rb_intern("_reduce_none");
|
|
|
|
id_errstatus = rb_intern("@racc_error_status");
|
|
|
|
|
|
|
|
id_d_shift = rb_intern("racc_shift");
|
|
|
|
id_d_reduce = rb_intern("racc_reduce");
|
|
|
|
id_d_accept = rb_intern("racc_accept");
|
|
|
|
id_d_read_token = rb_intern("racc_read_token");
|
|
|
|
id_d_next_state = rb_intern("racc_next_state");
|
|
|
|
id_d_e_pop = rb_intern("racc_e_pop");
|
|
|
|
}
|