ed-precompiled_racc 1.8.1

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