opal 0.10.0.beta2 → 0.10.0.beta3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +9 -1
  3. data/lib/opal/compiler.rb +15 -9
  4. data/lib/opal/fragment.rb +8 -1
  5. data/lib/opal/nodes/args/restarg.rb +6 -1
  6. data/lib/opal/nodes/base.rb +1 -1
  7. data/lib/opal/nodes/call.rb +4 -0
  8. data/lib/opal/nodes/def.rb +20 -25
  9. data/lib/opal/nodes/hash.rb +89 -17
  10. data/lib/opal/nodes/iter.rb +30 -2
  11. data/lib/opal/nodes/logic.rb +54 -4
  12. data/lib/opal/nodes/node_with_args.rb +72 -0
  13. data/lib/opal/parser.rb +16 -0
  14. data/lib/opal/parser/grammar.rb +2555 -2562
  15. data/lib/opal/parser/grammar.y +28 -20
  16. data/lib/opal/parser/lexer.rb +4 -4
  17. data/lib/opal/regexp_anchors.rb +13 -5
  18. data/lib/opal/source_map.rb +2 -1
  19. data/lib/opal/version.rb +1 -1
  20. data/opal/corelib/array.rb +4 -0
  21. data/opal/corelib/basic_object.rb +3 -1
  22. data/opal/corelib/constants.rb +1 -1
  23. data/opal/corelib/file.rb +196 -4
  24. data/opal/corelib/hash.rb +7 -7
  25. data/opal/corelib/kernel.rb +7 -4
  26. data/opal/corelib/marshal.rb +31 -0
  27. data/opal/corelib/marshal/read_buffer.rb +427 -0
  28. data/opal/corelib/marshal/write_buffer.rb +383 -0
  29. data/opal/corelib/method.rb +8 -0
  30. data/opal/corelib/module.rb +21 -0
  31. data/opal/corelib/number.rb +19 -5
  32. data/opal/corelib/proc.rb +33 -6
  33. data/opal/corelib/range.rb +6 -0
  34. data/opal/corelib/regexp.rb +5 -1
  35. data/opal/corelib/runtime.js +69 -17
  36. data/opal/corelib/string.rb +8 -0
  37. data/opal/corelib/string/inheritance.rb +4 -0
  38. data/opal/corelib/struct.rb +5 -0
  39. data/opal/corelib/unsupported.rb +0 -18
  40. data/opal/opal/full.rb +1 -0
  41. data/spec/filters/bugs/basicobject.rb +0 -2
  42. data/spec/filters/bugs/compiler_opal.rb +5 -0
  43. data/spec/filters/bugs/enumerable.rb +1 -0
  44. data/spec/filters/bugs/enumerator.rb +0 -2
  45. data/spec/filters/bugs/exception.rb +0 -1
  46. data/spec/filters/bugs/kernel.rb +0 -5
  47. data/spec/filters/bugs/language.rb +7 -27
  48. data/spec/filters/bugs/marshal.rb +43 -0
  49. data/spec/filters/bugs/method.rb +0 -56
  50. data/spec/filters/bugs/module.rb +0 -1
  51. data/spec/filters/bugs/proc.rb +0 -46
  52. data/spec/filters/bugs/regexp.rb +1 -0
  53. data/spec/filters/bugs/unboundmethod.rb +0 -13
  54. data/spec/filters/unsupported/bignum.rb +5 -0
  55. data/spec/filters/unsupported/freeze.rb +2 -0
  56. data/spec/filters/unsupported/marshal.rb +46 -0
  57. data/spec/filters/unsupported/symbol.rb +5 -0
  58. data/spec/lib/compiler/call_spec.rb +29 -29
  59. data/spec/lib/compiler_spec.rb +7 -1
  60. data/spec/opal/core/kernel/instance_variables_spec.rb +40 -0
  61. data/spec/opal/core/language/ternary_operator_spec.rb +6 -0
  62. data/spec/opal/core/marshal/dump_spec.rb +53 -0
  63. data/spec/opal/core/marshal/load_spec.rb +7 -0
  64. data/spec/opal/core/source_map_spec.rb +35 -1
  65. data/spec/opal/javascript_api_spec.rb +16 -0
  66. data/spec/opal/stdlib/source_map_spec.rb +8 -0
  67. data/spec/ruby_specs +7 -4
  68. data/spec/support/match_helpers.rb +57 -0
  69. data/spec/support/mspec_rspec_adapter.rb +1 -1
  70. data/stdlib/opal-parser.rb +3 -1
  71. data/stdlib/pathname.rb +105 -7
  72. data/stdlib/racc/parser.rb +551 -138
  73. data/stdlib/source_map/vlq.rb +3 -2
  74. data/tasks/testing.rake +4 -2
  75. metadata +22 -2
@@ -2,6 +2,8 @@
2
2
  #
3
3
  # Original license:
4
4
  #
5
+ # frozen_string_literal: false
6
+ #--
5
7
  # $originalId: parser.rb,v 1.8 2006/07/06 11:42:07 aamine Exp $
6
8
  #
7
9
  # Copyright (c) 1999-2006 Minero Aoki
@@ -12,204 +14,615 @@
12
14
  # As a special exception, when this code is copied by Racc
13
15
  # into a Racc output file, you may use that output file
14
16
  # without restriction.
17
+ #++
18
+
15
19
  module Racc
20
+ class ParseError < StandardError; end
21
+ end
22
+ unless defined?(::ParseError)
23
+ ParseError = Racc::ParseError
24
+ end
25
+
26
+ # Racc is a LALR(1) parser generator.
27
+ # It is written in Ruby itself, and generates Ruby programs.
28
+ #
29
+ # == Command-line Reference
30
+ #
31
+ # racc [-o<var>filename</var>] [--output-file=<var>filename</var>]
32
+ # [-e<var>rubypath</var>] [--embedded=<var>rubypath</var>]
33
+ # [-v] [--verbose]
34
+ # [-O<var>filename</var>] [--log-file=<var>filename</var>]
35
+ # [-g] [--debug]
36
+ # [-E] [--embedded]
37
+ # [-l] [--no-line-convert]
38
+ # [-c] [--line-convert-all]
39
+ # [-a] [--no-omit-actions]
40
+ # [-C] [--check-only]
41
+ # [-S] [--output-status]
42
+ # [--version] [--copyright] [--help] <var>grammarfile</var>
43
+ #
44
+ # [+filename+]
45
+ # Racc grammar file. Any extension is permitted.
46
+ # [-o+outfile+, --output-file=+outfile+]
47
+ # A filename for output. default is <+filename+>.tab.rb
48
+ # [-O+filename+, --log-file=+filename+]
49
+ # Place logging output in file +filename+.
50
+ # Default log file name is <+filename+>.output.
51
+ # [-e+rubypath+, --executable=+rubypath+]
52
+ # output executable file(mode 755). where +path+ is the Ruby interpreter.
53
+ # [-v, --verbose]
54
+ # verbose mode. create +filename+.output file, like yacc's y.output file.
55
+ # [-g, --debug]
56
+ # add debug code to parser class. To display debugging information,
57
+ # use this '-g' option and set @yydebug true in parser class.
58
+ # [-E, --embedded]
59
+ # Output parser which doesn't need runtime files (racc/parser.rb).
60
+ # [-C, --check-only]
61
+ # Check syntax of racc grammar file and quit.
62
+ # [-S, --output-status]
63
+ # Print messages time to time while compiling.
64
+ # [-l, --no-line-convert]
65
+ # turns off line number converting.
66
+ # [-c, --line-convert-all]
67
+ # Convert line number of actions, inner, header and footer.
68
+ # [-a, --no-omit-actions]
69
+ # Call all actions, even if an action is empty.
70
+ # [--version]
71
+ # print Racc version and quit.
72
+ # [--copyright]
73
+ # Print copyright and quit.
74
+ # [--help]
75
+ # Print usage and quit.
76
+ #
77
+ # == Generating Parser Using Racc
78
+ #
79
+ # To compile Racc grammar file, simply type:
80
+ #
81
+ # $ racc parse.y
82
+ #
83
+ # This creates Ruby script file "parse.tab.y". The -o option can change the output filename.
84
+ #
85
+ # == Writing A Racc Grammar File
86
+ #
87
+ # If you want your own parser, you have to write a grammar file.
88
+ # A grammar file contains the name of your parser class, grammar for the parser,
89
+ # user code, and anything else.
90
+ # When writing a grammar file, yacc's knowledge is helpful.
91
+ # If you have not used yacc before, Racc is not too difficult.
92
+ #
93
+ # Here's an example Racc grammar file.
94
+ #
95
+ # class Calcparser
96
+ # rule
97
+ # target: exp { print val[0] }
98
+ #
99
+ # exp: exp '+' exp
100
+ # | exp '*' exp
101
+ # | '(' exp ')'
102
+ # | NUMBER
103
+ # end
104
+ #
105
+ # Racc grammar files resemble yacc files.
106
+ # But (of course), this is Ruby code.
107
+ # yacc's $$ is the 'result', $0, $1... is
108
+ # an array called 'val', and $-1, $-2... is an array called '_values'.
109
+ #
110
+ # See the {Grammar File Reference}[rdoc-ref:lib/racc/rdoc/grammar.en.rdoc] for
111
+ # more information on grammar files.
112
+ #
113
+ # == Parser
114
+ #
115
+ # Then you must prepare the parse entry method. There are two types of
116
+ # parse methods in Racc, Racc::Parser#do_parse and Racc::Parser#yyparse
117
+ #
118
+ # Racc::Parser#do_parse is simple.
119
+ #
120
+ # It's yyparse() of yacc, and Racc::Parser#next_token is yylex().
121
+ # This method must returns an array like [TOKENSYMBOL, ITS_VALUE].
122
+ # EOF is [false, false].
123
+ # (TOKENSYMBOL is a Ruby symbol (taken from String#intern) by default.
124
+ # If you want to change this, see the grammar reference.
125
+ #
126
+ # Racc::Parser#yyparse is little complicated, but useful.
127
+ # It does not use Racc::Parser#next_token, instead it gets tokens from any iterator.
128
+ #
129
+ # For example, <code>yyparse(obj, :scan)</code> causes
130
+ # calling +obj#scan+, and you can return tokens by yielding them from +obj#scan+.
131
+ #
132
+ # == Debugging
133
+ #
134
+ # When debugging, "-v" or/and the "-g" option is helpful.
135
+ #
136
+ # "-v" creates verbose log file (.output).
137
+ # "-g" creates a "Verbose Parser".
138
+ # Verbose Parser prints the internal status when parsing.
139
+ # But it's _not_ automatic.
140
+ # You must use -g option and set +@yydebug+ to +true+ in order to get output.
141
+ # -g option only creates the verbose parser.
142
+ #
143
+ # === Racc reported syntax error.
144
+ #
145
+ # Isn't there too many "end"?
146
+ # grammar of racc file is changed in v0.10.
147
+ #
148
+ # Racc does not use '%' mark, while yacc uses huge number of '%' marks..
149
+ #
150
+ # === Racc reported "XXXX conflicts".
151
+ #
152
+ # Try "racc -v xxxx.y".
153
+ # It causes producing racc's internal log file, xxxx.output.
154
+ #
155
+ # === Generated parsers does not work correctly
156
+ #
157
+ # Try "racc -g xxxx.y".
158
+ # This command let racc generate "debugging parser".
159
+ # Then set @yydebug=true in your parser.
160
+ # It produces a working log of your parser.
161
+ #
162
+ # == Re-distributing Racc runtime
163
+ #
164
+ # A parser, which is created by Racc, requires the Racc runtime module;
165
+ # racc/parser.rb.
166
+ #
167
+ # Ruby 1.8.x comes with Racc runtime module,
168
+ # you need NOT distribute Racc runtime files.
169
+ #
170
+ # If you want to include the Racc runtime module with your parser.
171
+ # This can be done by using '-E' option:
172
+ #
173
+ # $ racc -E -omyparser.rb myparser.y
174
+ #
175
+ # This command creates myparser.rb which `includes' Racc runtime.
176
+ # Only you must do is to distribute your parser file (myparser.rb).
177
+ #
178
+ # Note: parser.rb is LGPL, but your parser is not.
179
+ # Your own parser is completely yours.
180
+ module Racc
181
+
182
+ unless defined?(Racc_No_Extensions)
183
+ Racc_No_Extensions = false # :nodoc:
184
+ end
16
185
 
17
186
  class Parser
18
187
 
188
+ Racc_Runtime_Version = '1.4.6'
189
+ Racc_Runtime_Revision = %w$originalRevision: 1.8 $[1]
190
+
191
+ Racc_Runtime_Core_Version_R = '1.4.6'
192
+ Racc_Runtime_Core_Revision_R = %w$originalRevision: 1.8 $[1]
193
+ # DISABLED: opal has no racc/cparse
194
+ # begin
195
+ # require 'racc/cparse'
196
+ # # Racc_Runtime_Core_Version_C = (defined in extension)
197
+ # Racc_Runtime_Core_Revision_C = Racc_Runtime_Core_Id_C.split[2]
198
+ # unless new.respond_to?(:_racc_do_parse_c, true)
199
+ # raise LoadError, 'old cparse.so'
200
+ # end
201
+ # if Racc_No_Extensions
202
+ # raise LoadError, 'selecting ruby version of racc runtime core'
203
+ # end
204
+ #
205
+ # Racc_Main_Parsing_Routine = :_racc_do_parse_c # :nodoc:
206
+ # Racc_YY_Parse_Method = :_racc_yyparse_c # :nodoc:
207
+ # Racc_Runtime_Core_Version = Racc_Runtime_Core_Version_C # :nodoc:
208
+ # Racc_Runtime_Core_Revision = Racc_Runtime_Core_Revision_C # :nodoc:
209
+ # Racc_Runtime_Type = 'c' # :nodoc:
210
+ # rescue LoadError
211
+ Racc_Main_Parsing_Routine = :_racc_do_parse_rb
212
+ Racc_YY_Parse_Method = :_racc_yyparse_rb
213
+ Racc_Runtime_Core_Version = Racc_Runtime_Core_Version_R
214
+ Racc_Runtime_Core_Revision = Racc_Runtime_Core_Revision_R
215
+ Racc_Runtime_Type = 'ruby'
216
+ # end
217
+
218
+ def Parser.racc_runtime_type # :nodoc:
219
+ Racc_Runtime_Type
220
+ end
221
+
19
222
  def _racc_setup
20
- self.class::Racc_arg
223
+ @yydebug = false unless self.class::Racc_debug_parser
224
+ @yydebug = false unless defined?(@yydebug)
225
+ if @yydebug
226
+ @racc_debug_out = $stderr unless defined?(@racc_debug_out)
227
+ @racc_debug_out ||= $stderr
228
+ end
229
+ arg = self.class::Racc_arg
230
+ arg[13] = true if arg.size < 14
231
+ arg
232
+ end
233
+
234
+ def _racc_init_sysvars
235
+ @racc_state = [0]
236
+ @racc_tstack = []
237
+ @racc_vstack = []
238
+
239
+ @racc_t = nil
240
+ @racc_val = nil
241
+
242
+ @racc_read_next = true
243
+
244
+ @racc_user_yyerror = false
245
+ @racc_error_status = 0
21
246
  end
22
247
 
248
+ # The entry point of the parser. This method is used with #next_token.
249
+ # If Racc wants to get token (and its value), calls next_token.
250
+ #
251
+ # Example:
252
+ # def parse
253
+ # @q = [[1,1],
254
+ # [2,2],
255
+ # [3,3],
256
+ # [false, '$']]
257
+ # do_parse
258
+ # end
259
+ #
260
+ # def next_token
261
+ # @q.shift
262
+ # end
23
263
  def do_parse
24
- _racc_do_parse_rb _racc_setup, false
264
+ __send__(Racc_Main_Parsing_Routine, _racc_setup(), false)
265
+ end
266
+
267
+ # The method to fetch next token.
268
+ # If you use #do_parse method, you must implement #next_token.
269
+ #
270
+ # The format of return value is [TOKEN_SYMBOL, VALUE].
271
+ # +token-symbol+ is represented by Ruby's symbol by default, e.g. :IDENT
272
+ # for 'IDENT'. ";" (String) for ';'.
273
+ #
274
+ # The final symbol (End of file) must be false.
275
+ def next_token
276
+ raise NotImplementedError, "#{self.class}\#next_token is not defined"
25
277
  end
26
278
 
27
279
  def _racc_do_parse_rb(arg, in_debug)
28
- action_table = arg[0]
29
- action_check = arg[1]
30
- action_default = arg[2]
31
- action_pointer = arg[3]
32
-
33
- goto_table = arg[4]
34
- goto_check = arg[5]
35
- goto_default = arg[6]
36
- goto_pointer = arg[7]
37
-
38
- nt_base = arg[8]
39
- reduce_table = arg[9]
40
- token_table = arg[10]
41
- shift_n = arg[11]
42
- reduce_n = arg[12]
43
-
44
- use_result = arg[13]
45
-
46
- # racc sys vars
47
- racc_state = [0]
48
- racc_tstack = []
49
- racc_vstack = []
50
-
51
- racc_t = nil
52
- racc_tok = nil
53
- racc_val = nil
54
- racc_read_next = true
55
-
56
- racc_user_yyerror = false
57
- racc_error_status = 0
58
-
59
- token = nil; act = nil; i = nil; nerr = nil; custate = nil
60
-
61
- while true
62
- i = action_pointer[racc_state[-1]]
63
-
64
- if i
65
- if racc_read_next
66
- if racc_t != 0 # not EOF
67
- token = next_token
68
-
69
- racc_tok = token[0]
70
- racc_val = token[1]
71
-
72
- if racc_tok == false # EOF
73
- racc_t = 0
74
- else
75
- racc_t = token_table[racc_tok]
76
- racc_t = 1 unless racc_t
77
- # racc_t ||= 1
280
+ action_table, action_check, action_default, action_pointer,
281
+ _, _, _, _,
282
+ _, _, token_table, _,
283
+ _, _, * = arg
284
+
285
+ _racc_init_sysvars
286
+ tok = act = i = nil
287
+
288
+ catch(:racc_end_parse) {
289
+ while true
290
+ if i = action_pointer[@racc_state[-1]]
291
+ if @racc_read_next
292
+ if @racc_t != 0 # not EOF
293
+ tok, @racc_val = next_token()
294
+ unless tok # EOF
295
+ @racc_t = 0
296
+ else
297
+ @racc_t = (token_table[tok] or 1) # error token
298
+ end
299
+ racc_read_token(@racc_t, tok, @racc_val) if @yydebug
300
+ @racc_read_next = false
78
301
  end
79
-
80
- racc_read_token(racc_t, racc_tok, racc_val) if @yydebug
81
- racc_read_next = false
82
302
  end
303
+ i += @racc_t
304
+ unless i >= 0 and
305
+ act = action_table[i] and
306
+ action_check[i] == @racc_state[-1]
307
+ act = action_default[@racc_state[-1]]
308
+ end
309
+ else
310
+ act = action_default[@racc_state[-1]]
83
311
  end
84
-
85
- i += racc_t
86
-
87
- if (i < 0) || (act = action_table[i]).nil? || (action_check[i] != racc_state[-1])
88
- act = action_default[racc_state[-1]]
312
+ while act = _racc_evalact(act, arg)
313
+ ;
89
314
  end
90
-
91
- else
92
- act = action_default[racc_state[-1]]
93
315
  end
316
+ }
317
+ end
94
318
 
95
- puts "(act: #{act}, shift_n: #{shift_n}, reduce_n: #{reduce_n})" if @yydebug
96
- if act > 0 && act < shift_n
97
- if racc_error_status > 0
98
- if racc_t != 1
99
- racc_error_status -= 1
100
- end
101
- end
319
+ # Another entry point for the parser.
320
+ # If you use this method, you must implement RECEIVER#METHOD_ID method.
321
+ #
322
+ # RECEIVER#METHOD_ID is a method to get next token.
323
+ # It must 'yield' the token, which format is [TOKEN-SYMBOL, VALUE].
324
+ def yyparse(recv, mid)
325
+ __send__(Racc_YY_Parse_Method, recv, mid, _racc_setup(), true)
326
+ end
102
327
 
103
- racc_vstack.push racc_val
104
- curstate = act
105
- racc_state << act
106
- racc_read_next = true
328
+ def _racc_yyparse_rb(recv, mid, arg, c_debug)
329
+ action_table, action_check, action_default, action_pointer,
330
+ _, _, _, _,
331
+ _, _, token_table, _,
332
+ _, _, * = arg
107
333
 
108
- if @yydebug
109
- racc_tstack.push racc_t
110
- racc_shift racc_t, racc_tstack, racc_vstack
111
- end
334
+ _racc_init_sysvars
335
+ act = nil
336
+ i = nil
112
337
 
113
- elsif act < 0 && act > -reduce_n
114
- reduce_i = act * -3
115
- reduce_len = reduce_table[reduce_i]
116
- reduce_to = reduce_table[reduce_i + 1]
117
- method_id = reduce_table[reduce_i + 2]
338
+ catch(:racc_end_parse) {
339
+ until i = action_pointer[@racc_state[-1]]
340
+ while act = _racc_evalact(action_default[@racc_state[-1]], arg)
341
+ ;
342
+ end
343
+ end
344
+ recv.__send__(mid) do |tok, val|
345
+ unless tok
346
+ @racc_t = 0
347
+ else
348
+ @racc_t = (token_table[tok] or 1) # error token
349
+ end
350
+ @racc_val = val
351
+ @racc_read_next = false
352
+
353
+ i += @racc_t
354
+ unless i >= 0 and
355
+ act = action_table[i] and
356
+ action_check[i] == @racc_state[-1]
357
+ act = action_default[@racc_state[-1]]
358
+ end
359
+ while act = _racc_evalact(act, arg)
360
+ ;
361
+ end
118
362
 
119
- tmp_t = racc_tstack.last reduce_len
120
- tmp_v = racc_vstack.last reduce_len
363
+ while not(i = action_pointer[@racc_state[-1]]) or
364
+ not @racc_read_next or
365
+ @racc_t == 0 # $
366
+ unless i and i += @racc_t and
367
+ i >= 0 and
368
+ act = action_table[i] and
369
+ action_check[i] == @racc_state[-1]
370
+ act = action_default[@racc_state[-1]]
371
+ end
372
+ while act = _racc_evalact(act, arg)
373
+ ;
374
+ end
375
+ end
376
+ end
377
+ }
378
+ end
121
379
 
122
- racc_state.pop reduce_len
123
- racc_vstack.pop reduce_len
124
- racc_tstack.pop reduce_len
380
+ ###
381
+ ### common
382
+ ###
383
+
384
+ def _racc_evalact(act, arg)
385
+ action_table, action_check, _, action_pointer,
386
+ _, _, _, _,
387
+ _, _, _, shift_n, reduce_n,
388
+ _, _, * = arg
389
+
390
+ if act > 0 and act < shift_n
391
+ #
392
+ # shift
393
+ #
394
+ if @racc_error_status > 0
395
+ @racc_error_status -= 1 unless @racc_t == 1 # error token
396
+ end
397
+ @racc_vstack.push @racc_val
398
+ @racc_state.push act
399
+ @racc_read_next = true
400
+ if @yydebug
401
+ @racc_tstack.push @racc_t
402
+ racc_shift @racc_t, @racc_tstack, @racc_vstack
403
+ end
125
404
 
126
- if use_result
127
- reduce_call_result = self.__send__ method_id, tmp_v, nil, tmp_v[0]
128
- racc_vstack.push reduce_call_result
405
+ elsif act < 0 and act > -reduce_n
406
+ #
407
+ # reduce
408
+ #
409
+ code = catch(:racc_jump) {
410
+ @racc_state.push _racc_do_reduce(arg, act)
411
+ false
412
+ }
413
+ if code
414
+ case code
415
+ when 1 # yyerror
416
+ @racc_user_yyerror = true # user_yyerror
417
+ return -reduce_n
418
+ when 2 # yyaccept
419
+ return shift_n
129
420
  else
130
- raise "not using result??"
421
+ raise '[Racc Bug] unknown jump code'
131
422
  end
423
+ end
132
424
 
133
- racc_tstack.push reduce_to
134
-
425
+ elsif act == shift_n
426
+ #
427
+ # accept
428
+ #
429
+ racc_accept if @yydebug
430
+ throw :racc_end_parse, @racc_vstack[0]
431
+
432
+ elsif act == -reduce_n
433
+ #
434
+ # error
435
+ #
436
+ case @racc_error_status
437
+ when 0
438
+ unless arg[21] # user_yyerror
439
+ on_error @racc_t, @racc_val, @racc_vstack
440
+ end
441
+ when 3
442
+ if @racc_t == 0 # is $
443
+ throw :racc_end_parse, nil
444
+ end
445
+ @racc_read_next = true
446
+ end
447
+ @racc_user_yyerror = false
448
+ @racc_error_status = 3
449
+ while true
450
+ if i = action_pointer[@racc_state[-1]]
451
+ i += 1 # error token
452
+ if i >= 0 and
453
+ (act = action_table[i]) and
454
+ action_check[i] == @racc_state[-1]
455
+ break
456
+ end
457
+ end
458
+ throw :racc_end_parse, nil if @racc_state.size <= 1
459
+ @racc_state.pop
460
+ @racc_vstack.pop
135
461
  if @yydebug
136
- racc_reduce tmp_t, reduce_to, racc_tstack, racc_vstack
462
+ @racc_tstack.pop
463
+ racc_e_pop @racc_state, @racc_tstack, @racc_vstack
137
464
  end
465
+ end
466
+ return act
138
467
 
139
- k1 = reduce_to - nt_base
140
-
141
- if (reduce_i = goto_pointer[k1]) != nil
142
- reduce_i += racc_state[-1]
468
+ else
469
+ raise "[Racc Bug] unknown action #{act.inspect}"
470
+ end
143
471
 
144
- if (reduce_i >= 0) && ((curstate = goto_table[reduce_i]) != nil) && (goto_check[reduce_i] == k1)
145
- racc_state.push curstate
146
- else
147
- racc_state.push goto_default[k1]
148
- end
472
+ racc_next_state(@racc_state[-1], @racc_state) if @yydebug
149
473
 
150
- else
151
- racc_state.push goto_default[k1]
152
- end
153
-
154
- elsif act == shift_n
155
- # action
156
- return racc_vstack[0]
474
+ nil
475
+ end
157
476
 
158
- elsif act == -reduce_n
159
- # reduce
160
- raise SyntaxError, "unexpected '#{racc_tok.inspect}'"
477
+ def _racc_do_reduce(arg, act)
478
+ _, _, _, _,
479
+ goto_table, goto_check, goto_default, goto_pointer,
480
+ nt_base, reduce_table, _, _,
481
+ _, use_result, * = arg
482
+ state = @racc_state
483
+ vstack = @racc_vstack
484
+ tstack = @racc_tstack
485
+
486
+ i = act * -3
487
+ len = reduce_table[i]
488
+ reduce_to = reduce_table[i+1]
489
+ method_id = reduce_table[i+2]
490
+ void_array = []
491
+
492
+ tmp_t = tstack[-len, len] if @yydebug
493
+ tmp_v = vstack[-len, len]
494
+ tstack[-len, len] = void_array if @yydebug
495
+ vstack[-len, len] = void_array
496
+ state[-len, len] = void_array
497
+
498
+ # tstack must be updated AFTER method call
499
+ if use_result
500
+ vstack.push __send__(method_id, tmp_v, vstack, tmp_v[0])
501
+ else
502
+ vstack.push __send__(method_id, tmp_v, vstack)
503
+ end
504
+ tstack.push reduce_to
161
505
 
162
- else
163
- raise "Rac: unknown action: #{act}"
164
- end
506
+ racc_reduce(tmp_t, reduce_to, tstack, vstack) if @yydebug
165
507
 
166
- if @yydebug
167
- racc_next_state racc_state[-1], racc_state
508
+ k1 = reduce_to - nt_base
509
+ if i = goto_pointer[k1]
510
+ i += state[-1]
511
+ if i >= 0 and (curstate = goto_table[i]) and goto_check[i] == k1
512
+ return curstate
168
513
  end
169
- # raise "and finished loop"
170
514
  end
515
+ goto_default[k1]
516
+ end
171
517
 
172
- end # _racc_do_parse_rb
518
+ # This method is called when a parse error is found.
519
+ #
520
+ # ERROR_TOKEN_ID is an internal ID of token which caused error.
521
+ # You can get string representation of this ID by calling
522
+ # #token_to_str.
523
+ #
524
+ # ERROR_VALUE is a value of error token.
525
+ #
526
+ # value_stack is a stack of symbol values.
527
+ # DO NOT MODIFY this object.
528
+ #
529
+ # This method raises ParseError by default.
530
+ #
531
+ # If this method returns, parsers enter "error recovering mode".
532
+ def on_error(t, val, vstack)
533
+ raise ParseError, sprintf("\nparse error on value %s (%s)",
534
+ val.inspect, token_to_str(t) || '?')
535
+ end
173
536
 
537
+ # Enter error recovering mode.
538
+ # This method does not call #on_error.
539
+ def yyerror
540
+ throw :racc_jump, 1
541
+ end
542
+
543
+ # Exit parser.
544
+ # Return value is Symbol_Value_Stack[0].
545
+ def yyaccept
546
+ throw :racc_jump, 2
547
+ end
548
+
549
+ # Leave error recovering mode.
550
+ def yyerrok
551
+ @racc_error_status = 0
552
+ end
553
+
554
+ # For debugging output
174
555
  def racc_read_token(t, tok, val)
175
- puts "read #{tok}(#{racc_token2str(t)}) #{val.inspect}"
176
- puts "\n"
556
+ @racc_debug_out.print 'read '
557
+ @racc_debug_out.print tok.inspect, '(', racc_token2str(t), ') '
558
+ @racc_debug_out.puts val.inspect
559
+ @racc_debug_out.puts
177
560
  end
178
561
 
179
562
  def racc_shift(tok, tstack, vstack)
180
- puts "shift #{racc_token2str tok}"
563
+ @racc_debug_out.puts "shift #{racc_token2str tok}"
181
564
  racc_print_stacks tstack, vstack
182
- puts "\n"
565
+ @racc_debug_out.puts
183
566
  end
184
567
 
185
568
  def racc_reduce(toks, sim, tstack, vstack)
186
- puts "reduce #{toks.empty? ? '<none>' : toks.map { |t| racc_token2str(t) }}"
187
- puts " --> #{racc_token2str(sim)}"
569
+ out = @racc_debug_out
570
+ out.print 'reduce '
571
+ if toks.empty?
572
+ out.print ' <none>'
573
+ else
574
+ toks.each {|t| out.print ' ', racc_token2str(t) }
575
+ end
576
+ out.puts " --> #{racc_token2str(sim)}"
577
+
188
578
  racc_print_stacks tstack, vstack
579
+ @racc_debug_out.puts
189
580
  end
190
581
 
191
- def racc_next_state(curstate, state)
192
- puts "goto #{curstate}"
582
+ def racc_accept
583
+ @racc_debug_out.puts 'accept'
584
+ @racc_debug_out.puts
585
+ end
586
+
587
+ def racc_e_pop(state, tstack, vstack)
588
+ @racc_debug_out.puts 'error recovering mode: pop token'
193
589
  racc_print_states state
194
- puts "\n"
590
+ racc_print_stacks tstack, vstack
591
+ @racc_debug_out.puts
195
592
  end
196
593
 
197
- def racc_token2str(tok)
198
- self.class::Racc_token_to_s_table[tok]
594
+ def racc_next_state(curstate, state)
595
+ @racc_debug_out.puts "goto #{curstate}"
596
+ racc_print_states state
597
+ @racc_debug_out.puts
199
598
  end
200
599
 
201
600
  def racc_print_stacks(t, v)
202
- puts ' ['
601
+ out = @racc_debug_out
602
+ out.print ' ['
203
603
  t.each_index do |i|
204
- puts " (#{racc_token2str(t[i])} #{v[i].inspect})"
604
+ out.print ' (', racc_token2str(t[i]), ' ', v[i].inspect, ')'
205
605
  end
206
- puts ' ]'
606
+ out.puts ' ]'
207
607
  end
208
608
 
209
609
  def racc_print_states(s)
210
- puts ' ['
211
- s.each { |st| puts " #{st}" }
212
- puts ' ]'
610
+ out = @racc_debug_out
611
+ out.print ' ['
612
+ s.each {|st| out.print ' ', st }
613
+ out.puts ' ]'
213
614
  end
615
+
616
+ def racc_token2str(tok)
617
+ self.class::Racc_token_to_s_table[tok] or
618
+ raise "[Racc Bug] can't convert token #{tok} to string"
619
+ end
620
+
621
+ # Convert internal ID of token symbol to the string.
622
+ def token_to_str(t)
623
+ self.class::Racc_token_to_s_table[t]
624
+ end
625
+
214
626
  end
627
+
215
628
  end