racc 1.4.9-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. data/.gemtest +0 -0
  2. data/COPYING +515 -0
  3. data/ChangeLog +846 -0
  4. data/DEPENDS +4 -0
  5. data/Manifest.txt +105 -0
  6. data/README.ja.rdoc +96 -0
  7. data/README.rdoc +86 -0
  8. data/Rakefile +51 -0
  9. data/TODO +5 -0
  10. data/bin/racc +308 -0
  11. data/bin/racc2y +195 -0
  12. data/bin/y2racc +339 -0
  13. data/ext/racc/MANIFEST +4 -0
  14. data/ext/racc/cparse.c +824 -0
  15. data/ext/racc/depend +1 -0
  16. data/ext/racc/extconf.rb +5 -0
  17. data/fastcache/extconf.rb +2 -0
  18. data/fastcache/fastcache.c +185 -0
  19. data/lib/racc.rb +6 -0
  20. data/lib/racc/compat.rb +40 -0
  21. data/lib/racc/debugflags.rb +59 -0
  22. data/lib/racc/exception.rb +15 -0
  23. data/lib/racc/grammar.rb +1115 -0
  24. data/lib/racc/grammarfileparser.rb +559 -0
  25. data/lib/racc/info.rb +16 -0
  26. data/lib/racc/iset.rb +91 -0
  27. data/lib/racc/logfilegenerator.rb +211 -0
  28. data/lib/racc/parser-text.rb +479 -0
  29. data/lib/racc/parser.rb +474 -0
  30. data/lib/racc/parserfilegenerator.rb +512 -0
  31. data/lib/racc/pre-setup +13 -0
  32. data/lib/racc/sourcetext.rb +34 -0
  33. data/lib/racc/state.rb +971 -0
  34. data/lib/racc/statetransitiontable.rb +316 -0
  35. data/lib/racc/static.rb +5 -0
  36. data/misc/dist.sh +31 -0
  37. data/rdoc/en/NEWS.en.rdoc +282 -0
  38. data/rdoc/en/command.en.html +78 -0
  39. data/rdoc/en/debug.en.rdoc +20 -0
  40. data/rdoc/en/grammar.en.rdoc +230 -0
  41. data/rdoc/en/index.en.html +10 -0
  42. data/rdoc/en/parser.en.rdoc +74 -0
  43. data/rdoc/en/usage.en.rdoc +83 -0
  44. data/rdoc/ja/NEWS.ja.rdoc +307 -0
  45. data/rdoc/ja/command.ja.html +94 -0
  46. data/rdoc/ja/debug.ja.rdoc +36 -0
  47. data/rdoc/ja/grammar.ja.rdoc +348 -0
  48. data/rdoc/ja/index.ja.html +10 -0
  49. data/rdoc/ja/parser.ja.rdoc +125 -0
  50. data/rdoc/ja/usage.ja.html +414 -0
  51. data/sample/array.y +67 -0
  52. data/sample/array2.y +59 -0
  53. data/sample/calc-ja.y +66 -0
  54. data/sample/calc.y +65 -0
  55. data/sample/conflict.y +15 -0
  56. data/sample/hash.y +60 -0
  57. data/sample/lalr.y +17 -0
  58. data/sample/lists.y +57 -0
  59. data/sample/syntax.y +46 -0
  60. data/sample/yyerr.y +46 -0
  61. data/setup.rb +1587 -0
  62. data/tasks/doc.rb +12 -0
  63. data/tasks/email.rb +55 -0
  64. data/test/assets/chk.y +126 -0
  65. data/test/assets/conf.y +16 -0
  66. data/test/assets/digraph.y +29 -0
  67. data/test/assets/echk.y +118 -0
  68. data/test/assets/err.y +60 -0
  69. data/test/assets/expect.y +7 -0
  70. data/test/assets/firstline.y +4 -0
  71. data/test/assets/ichk.y +102 -0
  72. data/test/assets/intp.y +546 -0
  73. data/test/assets/mailp.y +437 -0
  74. data/test/assets/newsyn.y +25 -0
  75. data/test/assets/noend.y +4 -0
  76. data/test/assets/nonass.y +41 -0
  77. data/test/assets/normal.y +27 -0
  78. data/test/assets/norule.y +4 -0
  79. data/test/assets/nullbug1.y +25 -0
  80. data/test/assets/nullbug2.y +15 -0
  81. data/test/assets/opt.y +123 -0
  82. data/test/assets/percent.y +35 -0
  83. data/test/assets/recv.y +97 -0
  84. data/test/assets/rrconf.y +14 -0
  85. data/test/assets/scan.y +72 -0
  86. data/test/assets/syntax.y +50 -0
  87. data/test/assets/unterm.y +5 -0
  88. data/test/assets/useless.y +12 -0
  89. data/test/assets/yyerr.y +46 -0
  90. data/test/bench.y +36 -0
  91. data/test/helper.rb +91 -0
  92. data/test/infini.y +8 -0
  93. data/test/scandata/brace +7 -0
  94. data/test/scandata/gvar +1 -0
  95. data/test/scandata/normal +4 -0
  96. data/test/scandata/percent +18 -0
  97. data/test/scandata/slash +10 -0
  98. data/test/src.intp +34 -0
  99. data/test/start.y +20 -0
  100. data/test/test_chk_y.rb +51 -0
  101. data/test/test_grammar_file_parser.rb +15 -0
  102. data/test/test_racc_command.rb +155 -0
  103. data/test/test_scan_y.rb +51 -0
  104. data/test/testscanner.rb +51 -0
  105. data/web/racc.en.rhtml +42 -0
  106. data/web/racc.ja.rhtml +51 -0
  107. metadata +233 -0
data/ext/racc/MANIFEST ADDED
@@ -0,0 +1,4 @@
1
+ MANIFEST
2
+ cparse.c
3
+ depend
4
+ extconf.rb
data/ext/racc/cparse.c ADDED
@@ -0,0 +1,824 @@
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
+ /* -----------------------------------------------------------------------
17
+ Important Constants
18
+ ----------------------------------------------------------------------- */
19
+
20
+ #define RACC_VERSION "1.4.9"
21
+
22
+ #define DEFAULT_TOKEN -1
23
+ #define ERROR_TOKEN 1
24
+ #define FINAL_TOKEN 0
25
+
26
+ #define vDEFAULT_TOKEN INT2FIX(DEFAULT_TOKEN)
27
+ #define vERROR_TOKEN INT2FIX(ERROR_TOKEN)
28
+ #define vFINAL_TOKEN INT2FIX(FINAL_TOKEN)
29
+
30
+ /* -----------------------------------------------------------------------
31
+ File Local Variables
32
+ ----------------------------------------------------------------------- */
33
+
34
+ static VALUE RaccBug;
35
+ static VALUE CparseParams;
36
+
37
+ static ID id_yydebug;
38
+ static ID id_nexttoken;
39
+ static ID id_onerror;
40
+ static ID id_noreduce;
41
+ static ID id_errstatus;
42
+
43
+ static ID id_d_shift;
44
+ static ID id_d_reduce;
45
+ static ID id_d_accept;
46
+ static ID id_d_read_token;
47
+ static ID id_d_next_state;
48
+ static ID id_d_e_pop;
49
+
50
+ /* -----------------------------------------------------------------------
51
+ Utils
52
+ ----------------------------------------------------------------------- */
53
+
54
+ /* For backward compatibility */
55
+ #ifndef ID2SYM
56
+ # define ID2SYM(i) ULONG2NUM(i)
57
+ #endif
58
+ #ifndef SYM2ID
59
+ # define SYM2ID(v) ((ID)NUM2ULONG(v))
60
+ #endif
61
+ #ifndef SYMBOL_P
62
+ # define SYMBOL_P(v) FIXNUM_P(v)
63
+ #endif
64
+ #ifndef LONG2NUM
65
+ # define LONG2NUM(i) INT2NUM(i)
66
+ #endif
67
+
68
+ static ID value_to_id _((VALUE v));
69
+ static inline long num_to_long _((VALUE n));
70
+
71
+ static ID
72
+ value_to_id(VALUE v)
73
+ {
74
+ if (! SYMBOL_P(v)) {
75
+ rb_raise(rb_eTypeError, "not symbol");
76
+ }
77
+ return SYM2ID(v);
78
+ }
79
+
80
+ static inline long
81
+ num_to_long(VALUE n)
82
+ {
83
+ return NUM2LONG(n);
84
+ }
85
+
86
+ #define AREF(s, idx) \
87
+ ((0 <= idx && idx < RARRAY_LEN(s)) ? RARRAY_PTR(s)[idx] : Qnil)
88
+
89
+ /* -----------------------------------------------------------------------
90
+ Parser Stack Interfaces
91
+ ----------------------------------------------------------------------- */
92
+
93
+ static VALUE get_stack_tail _((VALUE stack, long len));
94
+ static void cut_stack_tail _((VALUE stack, long len));
95
+
96
+ static VALUE
97
+ get_stack_tail(VALUE stack, long len)
98
+ {
99
+ if (len < 0) return Qnil; /* system error */
100
+ if (len > RARRAY_LEN(stack)) len = RARRAY_LEN(stack);
101
+ return rb_ary_new4(len, RARRAY_PTR(stack) + RARRAY_LEN(stack) - len);
102
+ }
103
+
104
+ static void
105
+ cut_stack_tail(VALUE stack, long len)
106
+ {
107
+ while (len > 0) {
108
+ rb_ary_pop(stack);
109
+ len--;
110
+ }
111
+ }
112
+
113
+ #define STACK_INIT_LEN 64
114
+ #define NEW_STACK() rb_ary_new2(STACK_INIT_LEN)
115
+ #define PUSH(s, i) rb_ary_store(s, RARRAY_LEN(s), i)
116
+ #define POP(s) rb_ary_pop(s)
117
+ #define LAST_I(s) \
118
+ ((RARRAY_LEN(s) > 0) ? RARRAY_PTR(s)[RARRAY_LEN(s) - 1] : Qnil)
119
+ #define GET_TAIL(s, len) get_stack_tail(s, len)
120
+ #define CUT_TAIL(s, len) cut_stack_tail(s, len)
121
+
122
+ /* -----------------------------------------------------------------------
123
+ struct cparse_params
124
+ ----------------------------------------------------------------------- */
125
+
126
+ struct cparse_params {
127
+ VALUE value_v; /* VALUE version of this struct */
128
+
129
+ VALUE parser; /* parser object */
130
+
131
+ int lex_is_iterator;
132
+ VALUE lexer; /* scanner object */
133
+ ID lexmid; /* name of scanner method (must be an iterator) */
134
+
135
+ /* State transition tables (immutable)
136
+ Data structure is from Dragon Book 4.9 */
137
+ /* action table */
138
+ VALUE action_table;
139
+ VALUE action_check;
140
+ VALUE action_default;
141
+ VALUE action_pointer;
142
+ /* goto table */
143
+ VALUE goto_table;
144
+ VALUE goto_check;
145
+ VALUE goto_default;
146
+ VALUE goto_pointer;
147
+
148
+ long nt_base; /* NonTerminal BASE index */
149
+ VALUE reduce_table; /* reduce data table */
150
+ VALUE token_table; /* token conversion table */
151
+
152
+ /* parser stacks and parameters */
153
+ VALUE state;
154
+ long curstate;
155
+ VALUE vstack;
156
+ VALUE tstack;
157
+ VALUE t;
158
+ long shift_n;
159
+ long reduce_n;
160
+ long ruleno;
161
+
162
+ long errstatus; /* nonzero in error recovering mode */
163
+ long nerr; /* number of error */
164
+
165
+ int use_result_var;
166
+
167
+ VALUE retval; /* return value of parser routine */
168
+ long fin; /* parse result status */
169
+ #define CP_FIN_ACCEPT 1
170
+ #define CP_FIN_EOT 2
171
+ #define CP_FIN_CANTPOP 3
172
+
173
+ int debug; /* user level debug */
174
+ int sys_debug; /* system level debug */
175
+
176
+ long i; /* table index */
177
+ };
178
+
179
+ /* -----------------------------------------------------------------------
180
+ Parser Main Routines
181
+ ----------------------------------------------------------------------- */
182
+
183
+ static VALUE racc_cparse _((VALUE parser, VALUE arg, VALUE sysdebug));
184
+ static VALUE racc_yyparse _((VALUE parser, VALUE lexer, VALUE lexmid,
185
+ VALUE arg, VALUE sysdebug));
186
+
187
+ static void call_lexer _((struct cparse_params *v));
188
+ static VALUE lexer_i _((VALUE block_args, VALUE data, VALUE self));
189
+
190
+ static VALUE assert_array _((VALUE a));
191
+ static long assert_integer _((VALUE n));
192
+ static VALUE assert_hash _((VALUE h));
193
+ static VALUE initialize_params _((VALUE vparams, VALUE parser, VALUE arg,
194
+ VALUE lexer, VALUE lexmid));
195
+ static void cparse_params_mark _((void *ptr));
196
+
197
+ static void parse_main _((struct cparse_params *v,
198
+ VALUE tok, VALUE val, int resume));
199
+ static void extract_user_token _((struct cparse_params *v,
200
+ VALUE block_args, VALUE *tok, VALUE *val));
201
+ static void shift _((struct cparse_params* v, long act, VALUE tok, VALUE val));
202
+ static int reduce _((struct cparse_params* v, long act));
203
+ static VALUE reduce0 _((VALUE block_args, VALUE data, VALUE self));
204
+
205
+ #ifdef DEBUG
206
+ # define D_puts(msg) if (v->sys_debug) puts(msg)
207
+ # define D_printf(fmt,arg) if (v->sys_debug) printf(fmt,arg)
208
+ #else
209
+ # define D_puts(msg)
210
+ # define D_printf(fmt,arg)
211
+ #endif
212
+
213
+ static VALUE
214
+ racc_cparse(VALUE parser, VALUE arg, VALUE sysdebug)
215
+ {
216
+ volatile VALUE vparams;
217
+ struct cparse_params *v;
218
+
219
+ vparams = Data_Make_Struct(CparseParams, struct cparse_params,
220
+ cparse_params_mark, -1, v);
221
+ D_puts("starting cparse");
222
+ v->sys_debug = RTEST(sysdebug);
223
+ vparams = initialize_params(vparams, parser, arg, Qnil, Qnil);
224
+ v->lex_is_iterator = Qfalse;
225
+ parse_main(v, Qnil, Qnil, 0);
226
+
227
+ return v->retval;
228
+ }
229
+
230
+ static VALUE
231
+ racc_yyparse(VALUE parser, VALUE lexer, VALUE lexmid, VALUE arg, VALUE sysdebug)
232
+ {
233
+ volatile VALUE vparams;
234
+ struct cparse_params *v;
235
+
236
+ vparams = Data_Make_Struct(CparseParams, struct cparse_params,
237
+ cparse_params_mark, -1, v);
238
+ v->sys_debug = RTEST(sysdebug);
239
+ D_puts("start C yyparse");
240
+ vparams = initialize_params(vparams, parser, arg, lexer, lexmid);
241
+ v->lex_is_iterator = Qtrue;
242
+ D_puts("params initialized");
243
+ parse_main(v, Qnil, Qnil, 0);
244
+ call_lexer(v);
245
+ if (!v->fin) {
246
+ rb_raise(rb_eArgError, "%s() is finished before EndOfToken",
247
+ rb_id2name(v->lexmid));
248
+ }
249
+
250
+ return v->retval;
251
+ }
252
+
253
+ #ifdef HAVE_RB_BLOCK_CALL
254
+ static void
255
+ call_lexer(struct cparse_params *v)
256
+ {
257
+ rb_block_call(v->lexer, v->lexmid, 0, NULL, lexer_i, v->value_v);
258
+ }
259
+ #else
260
+ static VALUE
261
+ lexer_iter(VALUE data)
262
+ {
263
+ struct cparse_params *v;
264
+
265
+ Data_Get_Struct(data, struct cparse_params, v);
266
+ rb_funcall(v->lexer, v->lexmid, 0);
267
+ return Qnil;
268
+ }
269
+
270
+ static void
271
+ call_lexer(struct cparse_params *v)
272
+ {
273
+ rb_iterate(lexer_iter, v->value_v, lexer_i, v->value_v);
274
+ }
275
+ #endif
276
+
277
+ static VALUE
278
+ lexer_i(VALUE block_args, VALUE data, VALUE self)
279
+ {
280
+ struct cparse_params *v;
281
+ VALUE tok, val;
282
+
283
+ Data_Get_Struct(data, struct cparse_params, v);
284
+ if (v->fin)
285
+ rb_raise(rb_eArgError, "extra token after EndOfToken");
286
+ extract_user_token(v, block_args, &tok, &val);
287
+ parse_main(v, tok, val, 1);
288
+ if (v->fin && v->fin != CP_FIN_ACCEPT)
289
+ rb_iter_break();
290
+ return Qnil;
291
+ }
292
+
293
+ static VALUE
294
+ assert_array(VALUE a)
295
+ {
296
+ Check_Type(a, T_ARRAY);
297
+ return a;
298
+ }
299
+
300
+ static VALUE
301
+ assert_hash(VALUE h)
302
+ {
303
+ Check_Type(h, T_HASH);
304
+ return h;
305
+ }
306
+
307
+ static long
308
+ assert_integer(VALUE n)
309
+ {
310
+ return NUM2LONG(n);
311
+ }
312
+
313
+ static VALUE
314
+ initialize_params(VALUE vparams, VALUE parser, VALUE arg, VALUE lexer, VALUE lexmid)
315
+ {
316
+ struct cparse_params *v;
317
+
318
+ Data_Get_Struct(vparams, struct cparse_params, v);
319
+ v->value_v = vparams;
320
+ v->parser = parser;
321
+ v->lexer = lexer;
322
+ if (! NIL_P(lexmid))
323
+ v->lexmid = value_to_id(lexmid);
324
+
325
+ v->debug = RTEST(rb_ivar_get(parser, id_yydebug));
326
+
327
+ Check_Type(arg, T_ARRAY);
328
+ if (!(13 <= RARRAY_LEN(arg) && RARRAY_LEN(arg) <= 14))
329
+ rb_raise(RaccBug, "[Racc Bug] wrong arg.size %ld", RARRAY_LEN(arg));
330
+ v->action_table = assert_array (RARRAY_PTR(arg)[ 0]);
331
+ v->action_check = assert_array (RARRAY_PTR(arg)[ 1]);
332
+ v->action_default = assert_array (RARRAY_PTR(arg)[ 2]);
333
+ v->action_pointer = assert_array (RARRAY_PTR(arg)[ 3]);
334
+ v->goto_table = assert_array (RARRAY_PTR(arg)[ 4]);
335
+ v->goto_check = assert_array (RARRAY_PTR(arg)[ 5]);
336
+ v->goto_default = assert_array (RARRAY_PTR(arg)[ 6]);
337
+ v->goto_pointer = assert_array (RARRAY_PTR(arg)[ 7]);
338
+ v->nt_base = assert_integer(RARRAY_PTR(arg)[ 8]);
339
+ v->reduce_table = assert_array (RARRAY_PTR(arg)[ 9]);
340
+ v->token_table = assert_hash (RARRAY_PTR(arg)[10]);
341
+ v->shift_n = assert_integer(RARRAY_PTR(arg)[11]);
342
+ v->reduce_n = assert_integer(RARRAY_PTR(arg)[12]);
343
+ if (RARRAY_LEN(arg) > 13) {
344
+ v->use_result_var = RTEST(RARRAY_PTR(arg)[13]);
345
+ }
346
+ else {
347
+ v->use_result_var = Qtrue;
348
+ }
349
+
350
+ v->tstack = v->debug ? NEW_STACK() : Qnil;
351
+ v->vstack = NEW_STACK();
352
+ v->state = NEW_STACK();
353
+ v->curstate = 0;
354
+ PUSH(v->state, INT2FIX(0));
355
+ v->t = INT2FIX(FINAL_TOKEN + 1); /* must not init to FINAL_TOKEN */
356
+ v->nerr = 0;
357
+ v->errstatus = 0;
358
+ rb_ivar_set(parser, id_errstatus, LONG2NUM(v->errstatus));
359
+
360
+ v->retval = Qnil;
361
+ v->fin = 0;
362
+
363
+ v->lex_is_iterator = Qfalse;
364
+
365
+ rb_iv_set(parser, "@vstack", v->vstack);
366
+ if (v->debug) {
367
+ rb_iv_set(parser, "@tstack", v->tstack);
368
+ }
369
+ else {
370
+ rb_iv_set(parser, "@tstack", Qnil);
371
+ }
372
+
373
+ return vparams;
374
+ }
375
+
376
+ static void
377
+ cparse_params_mark(void *ptr)
378
+ {
379
+ struct cparse_params *v = (struct cparse_params*)ptr;
380
+
381
+ rb_gc_mark(v->value_v);
382
+ rb_gc_mark(v->parser);
383
+ rb_gc_mark(v->lexer);
384
+ rb_gc_mark(v->action_table);
385
+ rb_gc_mark(v->action_check);
386
+ rb_gc_mark(v->action_default);
387
+ rb_gc_mark(v->action_pointer);
388
+ rb_gc_mark(v->goto_table);
389
+ rb_gc_mark(v->goto_check);
390
+ rb_gc_mark(v->goto_default);
391
+ rb_gc_mark(v->goto_pointer);
392
+ rb_gc_mark(v->reduce_table);
393
+ rb_gc_mark(v->token_table);
394
+ rb_gc_mark(v->state);
395
+ rb_gc_mark(v->vstack);
396
+ rb_gc_mark(v->tstack);
397
+ rb_gc_mark(v->t);
398
+ rb_gc_mark(v->retval);
399
+ }
400
+
401
+ static void
402
+ extract_user_token(struct cparse_params *v, VALUE block_args,
403
+ VALUE *tok, VALUE *val)
404
+ {
405
+ if (NIL_P(block_args)) {
406
+ /* EOF */
407
+ *tok = Qfalse;
408
+ *val = rb_str_new("$", 1);
409
+ return;
410
+ }
411
+
412
+ if (TYPE(block_args) != T_ARRAY) {
413
+ rb_raise(rb_eTypeError,
414
+ "%s() %s %s (must be Array[2])",
415
+ v->lex_is_iterator ? rb_id2name(v->lexmid) : "next_token",
416
+ v->lex_is_iterator ? "yielded" : "returned",
417
+ rb_class2name(CLASS_OF(block_args)));
418
+ }
419
+ if (RARRAY_LEN(block_args) != 2) {
420
+ rb_raise(rb_eArgError,
421
+ "%s() %s wrong size of array (%ld for 2)",
422
+ v->lex_is_iterator ? rb_id2name(v->lexmid) : "next_token",
423
+ v->lex_is_iterator ? "yielded" : "returned",
424
+ RARRAY_LEN(block_args));
425
+ }
426
+ *tok = AREF(block_args, 0);
427
+ *val = AREF(block_args, 1);
428
+ }
429
+
430
+ #define SHIFT(v,act,tok,val) shift(v,act,tok,val)
431
+ #define REDUCE(v,act) do {\
432
+ switch (reduce(v,act)) { \
433
+ case 0: /* normal */ \
434
+ break; \
435
+ case 1: /* yyerror */ \
436
+ goto user_yyerror; \
437
+ case 2: /* yyaccept */ \
438
+ D_puts("u accept"); \
439
+ goto accept; \
440
+ default: \
441
+ break; \
442
+ } \
443
+ } while (0)
444
+
445
+ static void
446
+ parse_main(struct cparse_params *v, VALUE tok, VALUE val, int resume)
447
+ {
448
+ long i; /* table index */
449
+ long act; /* action type */
450
+ VALUE act_value; /* action type, VALUE version */
451
+ int read_next = 1; /* true if we need to read next token */
452
+ VALUE tmp;
453
+
454
+ if (resume)
455
+ goto resume;
456
+
457
+ while (1) {
458
+ D_puts("");
459
+ D_puts("---- enter new loop ----");
460
+ D_puts("");
461
+
462
+ D_printf("(act) k1=%ld\n", v->curstate);
463
+ tmp = AREF(v->action_pointer, v->curstate);
464
+ if (NIL_P(tmp)) goto notfound;
465
+ D_puts("(act) pointer[k1] ok");
466
+ i = NUM2LONG(tmp);
467
+
468
+ D_printf("read_next=%d\n", read_next);
469
+ if (read_next && (v->t != vFINAL_TOKEN)) {
470
+ if (v->lex_is_iterator) {
471
+ D_puts("resuming...");
472
+ if (v->fin) rb_raise(rb_eArgError, "token given after EOF");
473
+ v->i = i; /* save i */
474
+ return;
475
+ resume:
476
+ D_puts("resumed");
477
+ i = v->i; /* load i */
478
+ }
479
+ else {
480
+ D_puts("next_token");
481
+ tmp = rb_funcall(v->parser, id_nexttoken, 0);
482
+ extract_user_token(v, tmp, &tok, &val);
483
+ }
484
+ /* convert token */
485
+ v->t = rb_hash_aref(v->token_table, tok);
486
+ if (NIL_P(v->t)) {
487
+ v->t = vERROR_TOKEN;
488
+ }
489
+ D_printf("(act) t(k2)=%ld\n", NUM2LONG(v->t));
490
+ if (v->debug) {
491
+ rb_funcall(v->parser, id_d_read_token,
492
+ 3, v->t, tok, val);
493
+ }
494
+ }
495
+ read_next = 0;
496
+
497
+ i += NUM2LONG(v->t);
498
+ D_printf("(act) i=%ld\n", i);
499
+ if (i < 0) goto notfound;
500
+
501
+ act_value = AREF(v->action_table, i);
502
+ if (NIL_P(act_value)) goto notfound;
503
+ act = NUM2LONG(act_value);
504
+ D_printf("(act) table[i]=%ld\n", act);
505
+
506
+ tmp = AREF(v->action_check, i);
507
+ if (NIL_P(tmp)) goto notfound;
508
+ if (NUM2LONG(tmp) != v->curstate) goto notfound;
509
+ D_printf("(act) check[i]=%ld\n", NUM2LONG(tmp));
510
+
511
+ D_puts("(act) found");
512
+ act_fixed:
513
+ D_printf("act=%ld\n", act);
514
+ goto handle_act;
515
+
516
+ notfound:
517
+ D_puts("(act) not found: use default");
518
+ act_value = AREF(v->action_default, v->curstate);
519
+ act = NUM2LONG(act_value);
520
+ goto act_fixed;
521
+
522
+
523
+ handle_act:
524
+ if (act > 0 && act < v->shift_n) {
525
+ D_puts("shift");
526
+ if (v->errstatus > 0) {
527
+ v->errstatus--;
528
+ rb_ivar_set(v->parser, id_errstatus, LONG2NUM(v->errstatus));
529
+ }
530
+ SHIFT(v, act, v->t, val);
531
+ read_next = 1;
532
+ }
533
+ else if (act < 0 && act > -(v->reduce_n)) {
534
+ D_puts("reduce");
535
+ REDUCE(v, act);
536
+ }
537
+ else if (act == -(v->reduce_n)) {
538
+ goto error;
539
+ error_recovered:
540
+ ; /* goto label requires stmt */
541
+ }
542
+ else if (act == v->shift_n) {
543
+ D_puts("accept");
544
+ goto accept;
545
+ }
546
+ else {
547
+ rb_raise(RaccBug, "[Racc Bug] unknown act value %ld", act);
548
+ }
549
+
550
+ if (v->debug) {
551
+ rb_funcall(v->parser, id_d_next_state,
552
+ 2, LONG2NUM(v->curstate), v->state);
553
+ }
554
+ }
555
+ /* not reach */
556
+
557
+
558
+ accept:
559
+ if (v->debug) rb_funcall(v->parser, id_d_accept, 0);
560
+ v->retval = RARRAY_PTR(v->vstack)[0];
561
+ v->fin = CP_FIN_ACCEPT;
562
+ return;
563
+
564
+
565
+ error:
566
+ D_printf("error detected, status=%ld\n", v->errstatus);
567
+ if (v->errstatus == 0) {
568
+ v->nerr++;
569
+ rb_funcall(v->parser, id_onerror,
570
+ 3, v->t, val, v->vstack);
571
+ }
572
+ user_yyerror:
573
+ if (v->errstatus == 3) {
574
+ if (v->t == vFINAL_TOKEN) {
575
+ v->retval = Qfalse;
576
+ v->fin = CP_FIN_EOT;
577
+ return;
578
+ }
579
+ read_next = 1;
580
+ }
581
+ v->errstatus = 3;
582
+ rb_ivar_set(v->parser, id_errstatus, LONG2NUM(v->errstatus));
583
+
584
+ /* check if we can shift/reduce error token */
585
+ D_printf("(err) k1=%ld\n", v->curstate);
586
+ D_printf("(err) k2=%d (error)\n", ERROR_TOKEN);
587
+ while (1) {
588
+ tmp = AREF(v->action_pointer, v->curstate);
589
+ if (NIL_P(tmp)) goto error_pop;
590
+ D_puts("(err) pointer[k1] ok");
591
+
592
+ i = NUM2LONG(tmp) + ERROR_TOKEN;
593
+ D_printf("(err) i=%ld\n", i);
594
+ if (i < 0) goto error_pop;
595
+
596
+ act_value = AREF(v->action_table, i);
597
+ if (NIL_P(act_value)) {
598
+ D_puts("(err) table[i] == nil");
599
+ goto error_pop;
600
+ }
601
+ act = NUM2LONG(act_value);
602
+ D_printf("(err) table[i]=%ld\n", act);
603
+
604
+ tmp = AREF(v->action_check, i);
605
+ if (NIL_P(tmp)) {
606
+ D_puts("(err) check[i] == nil");
607
+ goto error_pop;
608
+ }
609
+ if (NUM2LONG(tmp) != v->curstate) {
610
+ D_puts("(err) check[i] != k1");
611
+ goto error_pop;
612
+ }
613
+
614
+ D_puts("(err) found: can handle error token");
615
+ break;
616
+
617
+ error_pop:
618
+ D_puts("(err) act not found: can't handle error token; pop");
619
+
620
+ if (RARRAY_LEN(v->state) <= 1) {
621
+ v->retval = Qnil;
622
+ v->fin = CP_FIN_CANTPOP;
623
+ return;
624
+ }
625
+ POP(v->state);
626
+ POP(v->vstack);
627
+ v->curstate = num_to_long(LAST_I(v->state));
628
+ if (v->debug) {
629
+ POP(v->tstack);
630
+ rb_funcall(v->parser, id_d_e_pop,
631
+ 3, v->state, v->tstack, v->vstack);
632
+ }
633
+ }
634
+
635
+ /* shift/reduce error token */
636
+ if (act > 0 && act < v->shift_n) {
637
+ D_puts("e shift");
638
+ SHIFT(v, act, ERROR_TOKEN, val);
639
+ }
640
+ else if (act < 0 && act > -(v->reduce_n)) {
641
+ D_puts("e reduce");
642
+ REDUCE(v, act);
643
+ }
644
+ else if (act == v->shift_n) {
645
+ D_puts("e accept");
646
+ goto accept;
647
+ }
648
+ else {
649
+ rb_raise(RaccBug, "[Racc Bug] unknown act value %ld", act);
650
+ }
651
+ goto error_recovered;
652
+ }
653
+
654
+ static void
655
+ shift(struct cparse_params *v, long act, VALUE tok, VALUE val)
656
+ {
657
+ PUSH(v->vstack, val);
658
+ if (v->debug) {
659
+ PUSH(v->tstack, tok);
660
+ rb_funcall(v->parser, id_d_shift,
661
+ 3, tok, v->tstack, v->vstack);
662
+ }
663
+ v->curstate = act;
664
+ PUSH(v->state, LONG2NUM(v->curstate));
665
+ }
666
+
667
+ static int
668
+ reduce(struct cparse_params *v, long act)
669
+ {
670
+ VALUE code;
671
+ v->ruleno = -act * 3;
672
+ code = rb_catch("racc_jump", reduce0, v->value_v);
673
+ v->errstatus = num_to_long(rb_ivar_get(v->parser, id_errstatus));
674
+ return NUM2INT(code);
675
+ }
676
+
677
+ static VALUE
678
+ reduce0(VALUE val, VALUE data, VALUE self)
679
+ {
680
+ struct cparse_params *v;
681
+ VALUE reduce_to, reduce_len, method_id;
682
+ long len;
683
+ ID mid;
684
+ VALUE tmp, tmp_t = Qundef, tmp_v = Qundef;
685
+ long i, k1, k2;
686
+ VALUE goto_state;
687
+
688
+ Data_Get_Struct(data, struct cparse_params, v);
689
+ reduce_len = RARRAY_PTR(v->reduce_table)[v->ruleno];
690
+ reduce_to = RARRAY_PTR(v->reduce_table)[v->ruleno+1];
691
+ method_id = RARRAY_PTR(v->reduce_table)[v->ruleno+2];
692
+ len = NUM2LONG(reduce_len);
693
+ mid = value_to_id(method_id);
694
+
695
+ /* call action */
696
+ if (len == 0) {
697
+ tmp = Qnil;
698
+ if (mid != id_noreduce)
699
+ tmp_v = rb_ary_new();
700
+ if (v->debug)
701
+ tmp_t = rb_ary_new();
702
+ }
703
+ else {
704
+ if (mid != id_noreduce) {
705
+ tmp_v = GET_TAIL(v->vstack, len);
706
+ tmp = RARRAY_PTR(tmp_v)[0];
707
+ }
708
+ else {
709
+ tmp = RARRAY_PTR(v->vstack)[ RARRAY_LEN(v->vstack) - len ];
710
+ }
711
+ CUT_TAIL(v->vstack, len);
712
+ if (v->debug) {
713
+ tmp_t = GET_TAIL(v->tstack, len);
714
+ CUT_TAIL(v->tstack, len);
715
+ }
716
+ CUT_TAIL(v->state, len);
717
+ }
718
+ if (mid != id_noreduce) {
719
+ if (v->use_result_var) {
720
+ tmp = rb_funcall(v->parser, mid,
721
+ 3, tmp_v, v->vstack, tmp);
722
+ }
723
+ else {
724
+ tmp = rb_funcall(v->parser, mid,
725
+ 2, tmp_v, v->vstack);
726
+ }
727
+ }
728
+
729
+ /* then push result */
730
+ PUSH(v->vstack, tmp);
731
+ if (v->debug) {
732
+ PUSH(v->tstack, reduce_to);
733
+ rb_funcall(v->parser, id_d_reduce,
734
+ 4, tmp_t, reduce_to, v->tstack, v->vstack);
735
+ }
736
+
737
+ /* calculate transition state */
738
+ if (RARRAY_LEN(v->state) == 0)
739
+ rb_raise(RaccBug, "state stack unexpectedly empty");
740
+ k2 = num_to_long(LAST_I(v->state));
741
+ k1 = num_to_long(reduce_to) - v->nt_base;
742
+ D_printf("(goto) k1=%ld\n", k1);
743
+ D_printf("(goto) k2=%ld\n", k2);
744
+
745
+ tmp = AREF(v->goto_pointer, k1);
746
+ if (NIL_P(tmp)) goto notfound;
747
+
748
+ i = NUM2LONG(tmp) + k2;
749
+ D_printf("(goto) i=%ld\n", i);
750
+ if (i < 0) goto notfound;
751
+
752
+ goto_state = AREF(v->goto_table, i);
753
+ if (NIL_P(goto_state)) {
754
+ D_puts("(goto) table[i] == nil");
755
+ goto notfound;
756
+ }
757
+ D_printf("(goto) table[i]=%ld (goto_state)\n", NUM2LONG(goto_state));
758
+
759
+ tmp = AREF(v->goto_check, i);
760
+ if (NIL_P(tmp)) {
761
+ D_puts("(goto) check[i] == nil");
762
+ goto notfound;
763
+ }
764
+ if (tmp != LONG2NUM(k1)) {
765
+ D_puts("(goto) check[i] != table[i]");
766
+ goto notfound;
767
+ }
768
+ D_printf("(goto) check[i]=%ld\n", NUM2LONG(tmp));
769
+
770
+ D_puts("(goto) found");
771
+ transit:
772
+ PUSH(v->state, goto_state);
773
+ v->curstate = NUM2LONG(goto_state);
774
+ return INT2FIX(0);
775
+
776
+ notfound:
777
+ D_puts("(goto) not found: use default");
778
+ /* overwrite `goto-state' by default value */
779
+ goto_state = AREF(v->goto_default, k1);
780
+ goto transit;
781
+ }
782
+
783
+ /* -----------------------------------------------------------------------
784
+ Ruby Interface
785
+ ----------------------------------------------------------------------- */
786
+
787
+ void
788
+ Init_cparse(void)
789
+ {
790
+ VALUE Racc, Parser;
791
+ ID id_racc = rb_intern("Racc");
792
+
793
+ if (rb_const_defined(rb_cObject, id_racc)) {
794
+ Racc = rb_const_get(rb_cObject, id_racc);
795
+ Parser = rb_const_get_at(Racc, rb_intern("Parser"));
796
+ }
797
+ else {
798
+ Racc = rb_define_module("Racc");
799
+ Parser = rb_define_class_under(Racc, "Parser", rb_cObject);
800
+ }
801
+ rb_define_private_method(Parser, "_racc_do_parse_c", racc_cparse, 2);
802
+ rb_define_private_method(Parser, "_racc_yyparse_c", racc_yyparse, 4);
803
+ rb_define_const(Parser, "Racc_Runtime_Core_Version_C",
804
+ rb_str_new2(RACC_VERSION));
805
+ rb_define_const(Parser, "Racc_Runtime_Core_Id_C",
806
+ rb_str_new2("$originalId: cparse.c,v 1.8 2006/07/06 11:39:46 aamine Exp $"));
807
+
808
+ CparseParams = rb_define_class_under(Racc, "CparseParams", rb_cObject);
809
+
810
+ RaccBug = rb_eRuntimeError;
811
+
812
+ id_yydebug = rb_intern("@yydebug");
813
+ id_nexttoken = rb_intern("next_token");
814
+ id_onerror = rb_intern("on_error");
815
+ id_noreduce = rb_intern("_reduce_none");
816
+ id_errstatus = rb_intern("@racc_error_status");
817
+
818
+ id_d_shift = rb_intern("racc_shift");
819
+ id_d_reduce = rb_intern("racc_reduce");
820
+ id_d_accept = rb_intern("racc_accept");
821
+ id_d_read_token = rb_intern("racc_read_token");
822
+ id_d_next_state = rb_intern("racc_next_state");
823
+ id_d_e_pop = rb_intern("racc_e_pop");
824
+ }