hoozuki 0.2.0 → 1.0.0

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.
@@ -1,127 +1,939 @@
1
1
  # frozen_string_literal: true
2
+ #
3
+ # DO NOT MODIFY!!!!
4
+ # This file is automatically generated by Racc 1.8.1
5
+ # from Racc grammar file "parser.y".
6
+ #
7
+
8
+ ###### racc/parser.rb begin
9
+ unless $".find {|p| p.end_with?('/racc/parser.rb')}
10
+ $".push "#{__dir__}/racc/parser.rb"
11
+ self.class.module_eval(<<'...end racc/parser.rb/module_eval...', 'racc/parser.rb', 1)
12
+ #--
13
+ # Copyright (c) 1999-2006 Minero Aoki
14
+ #
15
+ # This program is free software.
16
+ # You can distribute/modify this program under the same terms of ruby.
17
+ #
18
+ # As a special exception, when this code is copied by Racc
19
+ # into a Racc output file, you may use that output file
20
+ # without restriction.
21
+ #++
22
+
23
+ unless $".find {|p| p.end_with?('/racc/info.rb')}
24
+ $".push "#{__dir__}/racc/info.rb"
25
+
26
+ module Racc
27
+ VERSION = '1.8.1'
28
+ Version = VERSION
29
+ Copyright = 'Copyright (c) 1999-2006 Minero Aoki'
30
+ end
31
+
32
+ end
33
+
34
+
35
+ module Racc
36
+ class ParseError < StandardError; end
37
+ end
38
+ unless defined?(::ParseError)
39
+ ParseError = Racc::ParseError # :nodoc:
40
+ end
41
+
42
+ # Racc is an LALR(1) parser generator.
43
+ # It is written in Ruby itself, and generates Ruby programs.
44
+ #
45
+ # == Command-line Reference
46
+ #
47
+ # racc [-o<var>filename</var>] [--output-file=<var>filename</var>]
48
+ # [-e<var>rubypath</var>] [--executable=<var>rubypath</var>]
49
+ # [-v] [--verbose]
50
+ # [-O<var>filename</var>] [--log-file=<var>filename</var>]
51
+ # [-g] [--debug]
52
+ # [-E] [--embedded]
53
+ # [-l] [--no-line-convert]
54
+ # [-c] [--line-convert-all]
55
+ # [-a] [--no-omit-actions]
56
+ # [-C] [--check-only]
57
+ # [-S] [--output-status]
58
+ # [--version] [--copyright] [--help] <var>grammarfile</var>
59
+ #
60
+ # [+grammarfile+]
61
+ # Racc grammar file. Any extension is permitted.
62
+ # [-o+outfile+, --output-file=+outfile+]
63
+ # A filename for output. default is <+filename+>.tab.rb
64
+ # [-O+filename+, --log-file=+filename+]
65
+ # Place logging output in file +filename+.
66
+ # Default log file name is <+filename+>.output.
67
+ # [-e+rubypath+, --executable=+rubypath+]
68
+ # output executable file(mode 755). where +path+ is the Ruby interpreter.
69
+ # [-v, --verbose]
70
+ # verbose mode. create +filename+.output file, like yacc's y.output file.
71
+ # [-g, --debug]
72
+ # add debug code to parser class. To display debugging information,
73
+ # use this '-g' option and set @yydebug true in parser class.
74
+ # [-E, --embedded]
75
+ # Output parser which doesn't need runtime files (racc/parser.rb).
76
+ # [-F, --frozen]
77
+ # Output parser which declares frozen_string_literals: true
78
+ # [-C, --check-only]
79
+ # Check syntax of racc grammar file and quit.
80
+ # [-S, --output-status]
81
+ # Print messages time to time while compiling.
82
+ # [-l, --no-line-convert]
83
+ # turns off line number converting.
84
+ # [-c, --line-convert-all]
85
+ # Convert line number of actions, inner, header and footer.
86
+ # [-a, --no-omit-actions]
87
+ # Call all actions, even if an action is empty.
88
+ # [--version]
89
+ # print Racc version and quit.
90
+ # [--copyright]
91
+ # Print copyright and quit.
92
+ # [--help]
93
+ # Print usage and quit.
94
+ #
95
+ # == Generating Parser Using Racc
96
+ #
97
+ # To compile Racc grammar file, simply type:
98
+ #
99
+ # $ racc parse.y
100
+ #
101
+ # This creates Ruby script file "parse.tab.y". The -o option can change the output filename.
102
+ #
103
+ # == Writing A Racc Grammar File
104
+ #
105
+ # If you want your own parser, you have to write a grammar file.
106
+ # A grammar file contains the name of your parser class, grammar for the parser,
107
+ # user code, and anything else.
108
+ # When writing a grammar file, yacc's knowledge is helpful.
109
+ # If you have not used yacc before, Racc is not too difficult.
110
+ #
111
+ # Here's an example Racc grammar file.
112
+ #
113
+ # class Calcparser
114
+ # rule
115
+ # target: exp { print val[0] }
116
+ #
117
+ # exp: exp '+' exp
118
+ # | exp '*' exp
119
+ # | '(' exp ')'
120
+ # | NUMBER
121
+ # end
122
+ #
123
+ # Racc grammar files resemble yacc files.
124
+ # But (of course), this is Ruby code.
125
+ # yacc's $$ is the 'result', $0, $1... is
126
+ # an array called 'val', and $-1, $-2... is an array called '_values'.
127
+ #
128
+ # See the {Grammar File Reference}[rdoc-ref:lib/racc/rdoc/grammar.en.rdoc] for
129
+ # more information on grammar files.
130
+ #
131
+ # == Parser
132
+ #
133
+ # Then you must prepare the parse entry method. There are two types of
134
+ # parse methods in Racc, Racc::Parser#do_parse and Racc::Parser#yyparse
135
+ #
136
+ # Racc::Parser#do_parse is simple.
137
+ #
138
+ # It's yyparse() of yacc, and Racc::Parser#next_token is yylex().
139
+ # This method must returns an array like [TOKENSYMBOL, ITS_VALUE].
140
+ # EOF is [false, false].
141
+ # (TOKENSYMBOL is a Ruby symbol (taken from String#intern) by default.
142
+ # If you want to change this, see the grammar reference.
143
+ #
144
+ # Racc::Parser#yyparse is little complicated, but useful.
145
+ # It does not use Racc::Parser#next_token, instead it gets tokens from any iterator.
146
+ #
147
+ # For example, <code>yyparse(obj, :scan)</code> causes
148
+ # calling +obj#scan+, and you can return tokens by yielding them from +obj#scan+.
149
+ #
150
+ # == Debugging
151
+ #
152
+ # When debugging, "-v" or/and the "-g" option is helpful.
153
+ #
154
+ # "-v" creates verbose log file (.output).
155
+ # "-g" creates a "Verbose Parser".
156
+ # Verbose Parser prints the internal status when parsing.
157
+ # But it's _not_ automatic.
158
+ # You must use -g option and set +@yydebug+ to +true+ in order to get output.
159
+ # -g option only creates the verbose parser.
160
+ #
161
+ # === Racc reported syntax error.
162
+ #
163
+ # Isn't there too many "end"?
164
+ # grammar of racc file is changed in v0.10.
165
+ #
166
+ # Racc does not use '%' mark, while yacc uses huge number of '%' marks..
167
+ #
168
+ # === Racc reported "XXXX conflicts".
169
+ #
170
+ # Try "racc -v xxxx.y".
171
+ # It causes producing racc's internal log file, xxxx.output.
172
+ #
173
+ # === Generated parsers does not work correctly
174
+ #
175
+ # Try "racc -g xxxx.y".
176
+ # This command let racc generate "debugging parser".
177
+ # Then set @yydebug=true in your parser.
178
+ # It produces a working log of your parser.
179
+ #
180
+ # == Re-distributing Racc runtime
181
+ #
182
+ # A parser, which is created by Racc, requires the Racc runtime module;
183
+ # racc/parser.rb.
184
+ #
185
+ # Ruby 1.8.x comes with Racc runtime module,
186
+ # you need NOT distribute Racc runtime files.
187
+ #
188
+ # If you want to include the Racc runtime module with your parser.
189
+ # This can be done by using '-E' option:
190
+ #
191
+ # $ racc -E -omyparser.rb myparser.y
192
+ #
193
+ # This command creates myparser.rb which `includes' Racc runtime.
194
+ # Only you must do is to distribute your parser file (myparser.rb).
195
+ #
196
+ # Note: parser.rb is ruby license, but your parser is not.
197
+ # Your own parser is completely yours.
198
+ module Racc
199
+
200
+ unless defined?(Racc_No_Extensions)
201
+ Racc_No_Extensions = false # :nodoc:
202
+ end
2
203
 
3
- class Hoozuki
4
204
  class Parser
5
- def initialize(pattern)
6
- @pattern = pattern
7
- @offset = 0
205
+
206
+ Racc_Runtime_Version = ::Racc::VERSION
207
+ Racc_Runtime_Core_Version_R = ::Racc::VERSION
208
+
209
+ begin
210
+ if Object.const_defined?(:RUBY_ENGINE) and RUBY_ENGINE == 'jruby'
211
+ require 'jruby'
212
+ require 'racc/cparse-jruby.jar'
213
+ com.headius.racc.Cparse.new.load(JRuby.runtime, false)
214
+ else
215
+ require 'racc/cparse'
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
8
238
  end
9
239
 
10
- class << self
11
- def parse(pattern)
12
- new(pattern).parse
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
13
246
  end
247
+ arg = self.class::Racc_arg
248
+ arg[13] = true if arg.size < 14
249
+ arg
14
250
  end
15
251
 
16
- def parse
17
- ast = parse_choice
252
+ def _racc_init_sysvars
253
+ @racc_state = [0]
254
+ @racc_tstack = []
255
+ @racc_vstack = []
18
256
 
19
- raise 'Unexpected end of pattern' unless eol?
257
+ @racc_t = nil
258
+ @racc_val = nil
20
259
 
21
- ast
260
+ @racc_read_next = true
261
+
262
+ @racc_user_yyerror = false
263
+ @racc_error_status = 0
22
264
  end
23
265
 
24
- private
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
25
286
 
26
- def current
27
- @pattern[@offset]
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"
28
297
  end
29
298
 
30
- def eol?
31
- @pattern.size <= @offset
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
+ }
32
336
  end
33
337
 
34
- def next_char
35
- @offset += 1
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)
36
346
  end
347
+ RUBY
37
348
 
38
- def parse_choice
39
- children = []
40
- children << parse_concatenation
349
+ def _racc_yyparse_rb(recv, mid, arg, c_debug)
350
+ action_table, action_check, action_default, action_pointer,
351
+ _, _, _, _,
352
+ _, _, token_table, * = arg
41
353
 
42
- while current == '|'
43
- next_char
44
- children << parse_concatenation
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}"
45
492
  end
46
493
 
47
- return children.first if children.size == 1
48
- return Hoozuki::Node::Epsilon.new if children.empty?
494
+ racc_next_state(@racc_state[-1], @racc_state) if @yydebug
495
+
496
+ nil
497
+ end
49
498
 
50
- Hoozuki::Node::Choice.new(children)
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]
51
539
  end
52
540
 
53
- def parse_concatenation
54
- children = []
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
55
559
 
56
- children << parse_repetition until stop_parsing_concatenation?
560
+ # Enter error recovering mode.
561
+ # This method does not call #on_error.
562
+ def yyerror
563
+ throw :racc_jump, 1
564
+ end
57
565
 
58
- return children.first if children.size == 1
59
- return Hoozuki::Node::Epsilon.new if children.empty?
566
+ # Exit parser.
567
+ # Return value is +Symbol_Value_Stack[0]+.
568
+ def yyaccept
569
+ throw :racc_jump, 2
570
+ end
60
571
 
61
- Hoozuki::Node::Concatenation.new(children)
572
+ # Leave error recovering mode.
573
+ def yyerrok
574
+ @racc_error_status = 0
62
575
  end
63
576
 
64
- def stop_parsing_concatenation?
65
- eol? || current == '|' || current == ')'
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
66
583
  end
67
584
 
68
- def parse_repetition
69
- child = parse_group
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
70
590
 
71
- quantifier = nil
72
- case current
73
- when '*'
74
- quantifier = :zero_or_more
75
- when '+'
76
- quantifier = :one_or_more
77
- when '?'
78
- quantifier = :optional
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) }
79
598
  end
80
- return child if quantifier.nil?
599
+ out.puts " --> #{racc_token2str(sim)}"
600
+ racc_print_stacks tstack, vstack
601
+ @racc_debug_out.puts
602
+ end
81
603
 
82
- next_char
604
+ def racc_accept
605
+ @racc_debug_out.puts 'accept'
606
+ @racc_debug_out.puts
607
+ end
83
608
 
84
- Hoozuki::Node::Repetition.new(child, quantifier)
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
85
614
  end
86
615
 
87
- def parse_group
88
- return parse_literal if current != '('
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
89
621
 
90
- next_char
91
- child = parse_choice
92
- raise 'Expected closing parenthesis' unless current == ')'
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
93
630
 
94
- next_char
95
- child
631
+ def racc_print_states(s)
632
+ out = @racc_debug_out
633
+ out.print ' ['
634
+ s.each {|st| out.print ' ', st }
635
+ out.puts ' ]'
96
636
  end
97
637
 
98
- def parse_literal
99
- raise 'Unexpected end of pattern' if eol?
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
100
642
 
101
- if current == '\\'
102
- next_char
103
- raise 'Unexpected end of pattern' if eol?
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
104
647
 
105
- value = current
106
- case value
107
- when '(', ')', '|', '*', '+', '?', '\\'
108
- next_char
109
- else
110
- raise "Invalid escape sequence: \\#{value}"
111
- end
648
+ end
112
649
 
113
- return Hoozuki::Node::Literal.new(value)
114
- end
650
+ end
651
+
652
+ ...end racc/parser.rb/module_eval...
653
+ end
654
+ ###### racc/parser.rb end
655
+
656
+ require_relative 'node'
657
+
658
+ module Hoozuki
659
+ class Parser < Racc::Parser
660
+
661
+ module_eval(<<'...end parser.y/module_eval...', 'parser.y', 47)
662
+ def initialize
663
+ @yydebug = true if ENV['DEBUG']
664
+ end
665
+
666
+ def parse(pattern)
667
+ @pattern = pattern
668
+ @offset = 0
669
+ @tokens = []
670
+ tokenize
671
+ do_parse
672
+ end
673
+
674
+ private
675
+
676
+ ESCAPABLE_CHARS = ['(', ')', '|', '*', '+', '?', '\\'].freeze
677
+ SPECIAL_TOKENS = {
678
+ '(' => :LPAREN,
679
+ ')' => :RPAREN,
680
+ '|' => :PIPE,
681
+ '*' => :STAR,
682
+ '+' => :PLUS,
683
+ '?' => :QUESTION
684
+ }.freeze
685
+
686
+ def tokenize
687
+ while @offset < @pattern.length
688
+ char = @pattern[@offset]
115
689
 
116
- value = current
117
- case value
118
- when '(', ')', '|', '*', '+', '?', '\\'
119
- raise "Unexpected character: #{value}"
690
+ if char == '\\'
691
+ handle_escape_sequence
692
+ elsif SPECIAL_TOKENS.key?(char)
693
+ handle_special_char(char)
120
694
  else
121
- next_char
695
+ add_token(:CHAR, char)
122
696
  end
123
-
124
- Hoozuki::Node::Literal.new(value)
125
697
  end
698
+
699
+ @tokens << [false, false]
700
+ end
701
+
702
+ def handle_escape_sequence
703
+ @offset += 1
704
+ raise 'Unexpected end of pattern' if @offset >= @pattern.length
705
+
706
+ escaped = @pattern[@offset]
707
+ raise "Invalid escape sequence: \\#{escaped}" unless ESCAPABLE_CHARS.include?(escaped)
708
+
709
+ add_token(:CHAR, escaped)
126
710
  end
711
+
712
+ def handle_special_char(char)
713
+ token_type = SPECIAL_TOKENS[char]
714
+ add_token(token_type, char)
715
+
716
+ insert_epsilon_after_lparen if char == '(' && next_char == '|'
717
+ insert_epsilon_after_pipe if char == '|' && should_insert_epsilon_after_pipe?
718
+ end
719
+
720
+ def should_insert_epsilon_after_pipe?
721
+ next_char.nil? || [')', '|'].include?(next_char)
722
+ end
723
+
724
+ def insert_epsilon_after_lparen
725
+ @tokens << [:EPSILON, nil]
726
+ end
727
+
728
+ def insert_epsilon_after_pipe
729
+ @tokens << [:EPSILON, nil]
730
+ end
731
+
732
+ def add_token(type, value)
733
+ @tokens << [type, value]
734
+ @offset += 1
735
+ end
736
+
737
+ def next_char
738
+ @offset < @pattern.length ? @pattern[@offset] : nil
739
+ end
740
+
741
+ def next_token
742
+ @tokens.shift
743
+ end
744
+ ...end parser.y/module_eval...
745
+ ##### State transition tables begin ###
746
+
747
+ racc_action_table = [
748
+ 5, 5, 10, 11, 7, 7, 9, 9, 5, 19,
749
+ 11, 7, 7, 9, 9, 13, 14, 15, 7, 17,
750
+ 9 ]
751
+
752
+ racc_action_check = [
753
+ 0, 7, 1, 16, 0, 7, 0, 7, 11, 16,
754
+ 2, 3, 11, 3, 11, 6, 6, 6, 18, 10,
755
+ 18 ]
756
+
757
+ racc_action_pointer = [
758
+ -3, 2, 8, 4, nil, nil, 11, -2, nil, nil,
759
+ 19, 5, nil, nil, nil, nil, 1, nil, 11, nil ]
760
+
761
+ racc_action_default = [
762
+ -14, -14, -1, -2, -4, -5, -7, -14, -12, -13,
763
+ -14, -14, -6, -8, -9, -10, -14, 20, -3, -11 ]
764
+
765
+ racc_goto_table = [
766
+ 12, 2, 1, 18, nil, nil, nil, nil, 16, nil,
767
+ nil, nil, nil, nil, nil, 12 ]
768
+
769
+ racc_goto_check = [
770
+ 4, 2, 1, 3, nil, nil, nil, nil, 2, nil,
771
+ nil, nil, nil, nil, nil, 4 ]
772
+
773
+ racc_goto_pointer = [
774
+ nil, 2, 1, -8, -3, nil, nil ]
775
+
776
+ racc_goto_default = [
777
+ nil, nil, nil, 3, 4, 6, 8 ]
778
+
779
+ racc_reduce_table = [
780
+ 0, 0, :racc_error,
781
+ 1, 11, :_reduce_none,
782
+ 1, 12, :_reduce_none,
783
+ 3, 12, :_reduce_3,
784
+ 1, 13, :_reduce_none,
785
+ 1, 13, :_reduce_5,
786
+ 2, 13, :_reduce_6,
787
+ 1, 14, :_reduce_none,
788
+ 2, 14, :_reduce_8,
789
+ 2, 14, :_reduce_9,
790
+ 2, 14, :_reduce_10,
791
+ 3, 15, :_reduce_11,
792
+ 1, 15, :_reduce_none,
793
+ 1, 16, :_reduce_13 ]
794
+
795
+ racc_reduce_n = 14
796
+
797
+ racc_shift_n = 20
798
+
799
+ racc_token_table = {
800
+ false => 0,
801
+ :error => 1,
802
+ :PIPE => 2,
803
+ :EPSILON => 3,
804
+ :STAR => 4,
805
+ :PLUS => 5,
806
+ :QUESTION => 6,
807
+ :LPAREN => 7,
808
+ :RPAREN => 8,
809
+ :CHAR => 9 }
810
+
811
+ racc_nt_base = 10
812
+
813
+ racc_use_result_var = true
814
+
815
+ Racc_arg = [
816
+ racc_action_table,
817
+ racc_action_check,
818
+ racc_action_default,
819
+ racc_action_pointer,
820
+ racc_goto_table,
821
+ racc_goto_check,
822
+ racc_goto_default,
823
+ racc_goto_pointer,
824
+ racc_nt_base,
825
+ racc_reduce_table,
826
+ racc_token_table,
827
+ racc_shift_n,
828
+ racc_reduce_n,
829
+ racc_use_result_var ]
830
+ Ractor.make_shareable(Racc_arg) if defined?(Ractor)
831
+
832
+ Racc_token_to_s_table = [
833
+ "$end",
834
+ "error",
835
+ "PIPE",
836
+ "EPSILON",
837
+ "STAR",
838
+ "PLUS",
839
+ "QUESTION",
840
+ "LPAREN",
841
+ "RPAREN",
842
+ "CHAR",
843
+ "$start",
844
+ "target",
845
+ "choice",
846
+ "concatenation",
847
+ "repetition",
848
+ "group",
849
+ "literal" ]
850
+ Ractor.make_shareable(Racc_token_to_s_table) if defined?(Ractor)
851
+
852
+ Racc_debug_parser = true
853
+
854
+ ##### State transition tables end #####
855
+
856
+ # reduce 0 omitted
857
+
858
+ # reduce 1 omitted
859
+
860
+ # reduce 2 omitted
861
+
862
+ module_eval(<<'.,.,', 'parser.y', 9)
863
+ def _reduce_3(val, _values, result)
864
+ children = val[0].is_a?(Hoozuki::Node::Choice) ? val[0].children.dup : [val[0]]
865
+ children << val[2]
866
+ result = Hoozuki::Node::Choice.new(children)
867
+
868
+ result
869
+ end
870
+ .,.,
871
+
872
+ # reduce 4 omitted
873
+
874
+ module_eval(<<'.,.,', 'parser.y', 16)
875
+ def _reduce_5(val, _values, result)
876
+ result = Hoozuki::Node::Epsilon.new
877
+ result
878
+ end
879
+ .,.,
880
+
881
+ module_eval(<<'.,.,', 'parser.y', 18)
882
+ def _reduce_6(val, _values, result)
883
+ if val[0].is_a?(Hoozuki::Node::Epsilon)
884
+ result = val[1]
885
+ else
886
+ children = val[0].is_a?(Hoozuki::Node::Concatenation) ? val[0].children.dup : [val[0]]
887
+ children << val[1]
888
+ result = Hoozuki::Node::Concatenation.new(children)
889
+ end
890
+
891
+ result
892
+ end
893
+ .,.,
894
+
895
+ # reduce 7 omitted
896
+
897
+ module_eval(<<'.,.,', 'parser.y', 29)
898
+ def _reduce_8(val, _values, result)
899
+ result = Hoozuki::Node::Repetition.new(val[0], :zero_or_more)
900
+ result
901
+ end
902
+ .,.,
903
+
904
+ module_eval(<<'.,.,', 'parser.y', 30)
905
+ def _reduce_9(val, _values, result)
906
+ result = Hoozuki::Node::Repetition.new(val[0], :one_or_more)
907
+ result
908
+ end
909
+ .,.,
910
+
911
+ module_eval(<<'.,.,', 'parser.y', 31)
912
+ def _reduce_10(val, _values, result)
913
+ result = Hoozuki::Node::Repetition.new(val[0], :optional)
914
+ result
915
+ end
916
+ .,.,
917
+
918
+ module_eval(<<'.,.,', 'parser.y', 34)
919
+ def _reduce_11(val, _values, result)
920
+ result = val[1]
921
+ result
922
+ end
923
+ .,.,
924
+
925
+ # reduce 12 omitted
926
+
927
+ module_eval(<<'.,.,', 'parser.y', 38)
928
+ def _reduce_13(val, _values, result)
929
+ result = Hoozuki::Node::Literal.new(val[0])
930
+ result
931
+ end
932
+ .,.,
933
+
934
+ def _reduce_none(val, _values, result)
935
+ val[0]
127
936
  end
937
+
938
+ end # class Parser
939
+ end # module Hoozuki