racc 1.4.9 → 1.4.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  #
2
- # $Id: ad1fffef443194fdfa1052d2eee6850552f94313 $
2
+ # $Id: 3c520ba1f2996b86abc21eeb54768934a7be9d0c $
3
3
  #
4
4
  # Copyright (c) 1999-2006 Minero Aoki
5
5
  #
@@ -11,8 +11,10 @@
11
11
  # without restriction.
12
12
  #
13
13
 
14
+ require 'racc/info'
15
+
14
16
  unless defined?(NotImplementedError)
15
- NotImplementedError = NotImplementError
17
+ NotImplementedError = NotImplementError # :nodoc:
16
18
  end
17
19
 
18
20
  module Racc
@@ -22,19 +24,173 @@ unless defined?(::ParseError)
22
24
  ParseError = Racc::ParseError
23
25
  end
24
26
 
27
+ # Racc is a LALR(1) parser generator.
28
+ # It is written in Ruby itself, and generates Ruby programs.
29
+ #
30
+ # == Command-line Reference
31
+ #
32
+ # racc [-o<var>filename</var>] [--output-file=<var>filename</var>]
33
+ # [-e<var>rubypath</var>] [--embedded=<var>rubypath</var>]
34
+ # [-v] [--verbose]
35
+ # [-O<var>filename</var>] [--log-file=<var>filename</var>]
36
+ # [-g] [--debug]
37
+ # [-E] [--embedded]
38
+ # [-l] [--no-line-convert]
39
+ # [-c] [--line-convert-all]
40
+ # [-a] [--no-omit-actions]
41
+ # [-C] [--check-only]
42
+ # [-S] [--output-status]
43
+ # [--version] [--copyright] [--help] <var>grammarfile</var>
44
+ #
45
+ # [+filename+]
46
+ # Racc grammar file. Any extention is permitted.
47
+ # [-o+outfile+, --output-file=+outfile+]
48
+ # A filename for output. default is <+filename+>.tab.rb
49
+ # [-O+filename+, --log-file=+filename+]
50
+ # Place logging output in file +filename+.
51
+ # Default log file name is <+filename+>.output.
52
+ # [-e+rubypath+, --executable=+rubypath+]
53
+ # output executable file(mode 755). where +path+ is the Ruby interpreter.
54
+ # [-v, --verbose]
55
+ # verbose mode. create +filename+.output file, like yacc's y.output file.
56
+ # [-g, --debug]
57
+ # add debug code to parser class. To display debuggin information,
58
+ # use this '-g' option and set @yydebug true in parser class.
59
+ # [-E, --embedded]
60
+ # Output parser which doesn't need runtime files (racc/parser.rb).
61
+ # [-C, --check-only]
62
+ # Check syntax of racc grammer file and quit.
63
+ # [-S, --output-status]
64
+ # Print messages time to time while compiling.
65
+ # [-l, --no-line-convert]
66
+ # turns off line number converting.
67
+ # [-c, --line-convert-all]
68
+ # Convert line number of actions, inner, header and footer.
69
+ # [-a, --no-omit-actions]
70
+ # Call all actions, even if an action is empty.
71
+ # [--version]
72
+ # print Racc version and quit.
73
+ # [--copyright]
74
+ # Print copyright and quit.
75
+ # [--help]
76
+ # Print usage and quit.
77
+ #
78
+ # == Generating Parser Using Racc
79
+ #
80
+ # To compile Racc grammar file, simply type:
81
+ #
82
+ # $ racc parse.y
83
+ #
84
+ # This creates Ruby script file "parse.tab.y". The -o option can change the output filename.
85
+ #
86
+ # == Writing A Racc Grammar File
87
+ #
88
+ # If you want your own parser, you have to write a grammar file.
89
+ # A grammar file contains the name of your parser class, grammar for the parser,
90
+ # user code, and anything else.
91
+ # When writing a grammar file, yacc's knowledge is helpful.
92
+ # If you have not used yacc before, Racc is not too difficult.
93
+ #
94
+ # Here's an example Racc grammar file.
95
+ #
96
+ # class Calcparser
97
+ # rule
98
+ # target: exp { print val[0] }
99
+ #
100
+ # exp: exp '+' exp
101
+ # | exp '*' exp
102
+ # | '(' exp ')'
103
+ # | NUMBER
104
+ # end
105
+ #
106
+ # Racc grammar files resemble yacc files.
107
+ # But (of course), this is Ruby code.
108
+ # yacc's $$ is the 'result', $0, $1... is
109
+ # an array called 'val', and $-1, $-2... is an array called '_values'.
110
+ #
111
+ # See the {Grammar File Reference}[rdoc-ref:lib/racc/rdoc/grammar.en.rdoc] for
112
+ # more information on grammar files.
113
+ #
114
+ # == Parser
115
+ #
116
+ # Then you must prepare the parse entry method. There are two types of
117
+ # parse methods in Racc, Racc::Parser#do_parse and Racc::Parser#yyparse
118
+ #
119
+ # Racc::Parser#do_parse is simple.
120
+ #
121
+ # It's yyparse() of yacc, and Racc::Parser#next_token is yylex().
122
+ # This method must returns an array like [TOKENSYMBOL, ITS_VALUE].
123
+ # EOF is [false, false].
124
+ # (TOKENSYMBOL is a Ruby symbol (taken from String#intern) by default.
125
+ # If you want to change this, see the grammar reference.
126
+ #
127
+ # Racc::Parser#yyparse is little complicated, but useful.
128
+ # It does not use Racc::Parser#next_token, instead it gets tokens from any iterator.
129
+ #
130
+ # For example, <code>yyparse(obj, :scan)</code> causes
131
+ # calling +obj#scan+, and you can return tokens by yielding them from +obj#scan+.
132
+ #
133
+ # == Debugging
134
+ #
135
+ # When debugging, "-v" or/and the "-g" option is helpful.
136
+ #
137
+ # "-v" creates verbose log file (.output).
138
+ # "-g" creates a "Verbose Parser".
139
+ # Verbose Parser prints the internal status when parsing.
140
+ # But it's _not_ automatic.
141
+ # You must use -g option and set +@yydebug+ to +true+ in order to get output.
142
+ # -g option only creates the verbose parser.
143
+ #
144
+ # === Racc reported syntax error.
145
+ #
146
+ # Isn't there too many "end"?
147
+ # grammar of racc file is changed in v0.10.
148
+ #
149
+ # Racc does not use '%' mark, while yacc uses huge number of '%' marks..
150
+ #
151
+ # === Racc reported "XXXX conflicts".
152
+ #
153
+ # Try "racc -v xxxx.y".
154
+ # It causes producing racc's internal log file, xxxx.output.
155
+ #
156
+ # === Generated parsers does not work correctly
157
+ #
158
+ # Try "racc -g xxxx.y".
159
+ # This command let racc generate "debugging parser".
160
+ # Then set @yydebug=true in your parser.
161
+ # It produces a working log of your parser.
162
+ #
163
+ # == Re-distributing Racc runtime
164
+ #
165
+ # A parser, which is created by Racc, requires the Racc runtime module;
166
+ # racc/parser.rb.
167
+ #
168
+ # Ruby 1.8.x comes with Racc runtime module,
169
+ # you need NOT distribute Racc runtime files.
170
+ #
171
+ # If you want to include the Racc runtime module with your parser.
172
+ # This can be done by using '-E' option:
173
+ #
174
+ # $ racc -E -omyparser.rb myparser.y
175
+ #
176
+ # This command creates myparser.rb which `includes' Racc runtime.
177
+ # Only you must do is to distribute your parser file (myparser.rb).
178
+ #
179
+ # Note: parser.rb is LGPL, but your parser is not.
180
+ # Your own parser is completely yours.
25
181
  module Racc
26
182
 
27
183
  unless defined?(Racc_No_Extentions)
28
- Racc_No_Extentions = false
184
+ Racc_No_Extentions = false # :nodoc:
29
185
  end
30
186
 
31
187
  class Parser
32
188
 
33
- Racc_Runtime_Version = '1.4.6'
34
- Racc_Runtime_Revision = '$Id: ad1fffef443194fdfa1052d2eee6850552f94313 $'
189
+ Racc_Runtime_Version = ::Racc::VERSION
190
+ Racc_Runtime_Revision = '$Id: 3c520ba1f2996b86abc21eeb54768934a7be9d0c $'
35
191
 
36
- Racc_Runtime_Core_Version_R = '1.4.6'
37
- Racc_Runtime_Core_Revision_R = '$Id: ad1fffef443194fdfa1052d2eee6850552f94313 $'.split[1]
192
+ Racc_Runtime_Core_Version_R = ::Racc::VERSION
193
+ Racc_Runtime_Core_Revision_R = '$Id: 3c520ba1f2996b86abc21eeb54768934a7be9d0c $'.split[1]
38
194
  begin
39
195
  require 'racc/cparse'
40
196
  # Racc_Runtime_Core_Version_C = (defined in extention)
@@ -46,11 +202,11 @@ module Racc
46
202
  raise LoadError, 'selecting ruby version of racc runtime core'
47
203
  end
48
204
 
49
- Racc_Main_Parsing_Routine = :_racc_do_parse_c
50
- Racc_YY_Parse_Method = :_racc_yyparse_c
51
- Racc_Runtime_Core_Version = Racc_Runtime_Core_Version_C
52
- Racc_Runtime_Core_Revision = Racc_Runtime_Core_Revision_C
53
- Racc_Runtime_Type = 'c'
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:
54
210
  rescue LoadError
55
211
  Racc_Main_Parsing_Routine = :_racc_do_parse_rb
56
212
  Racc_YY_Parse_Method = :_racc_yyparse_rb
@@ -59,12 +215,10 @@ module Racc
59
215
  Racc_Runtime_Type = 'ruby'
60
216
  end
61
217
 
62
- def Parser.racc_runtime_type
218
+ def Parser.racc_runtime_type # :nodoc:
63
219
  Racc_Runtime_Type
64
220
  end
65
221
 
66
- private
67
-
68
222
  def _racc_setup
69
223
  @yydebug = false unless self.class::Racc_debug_parser
70
224
  @yydebug = false unless defined?(@yydebug)
@@ -91,14 +245,33 @@ module Racc
91
245
  @racc_error_status = 0
92
246
  end
93
247
 
94
- ###
95
- ### do_parse
96
- ###
97
-
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
98
263
  def do_parse
99
264
  __send__(Racc_Main_Parsing_Routine, _racc_setup(), false)
100
265
  end
101
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.
102
275
  def next_token
103
276
  raise NotImplementedError, "#{self.class}\#next_token is not defined"
104
277
  end
@@ -142,10 +315,11 @@ module Racc
142
315
  }
143
316
  end
144
317
 
145
- ###
146
- ### yyparse
147
- ###
148
-
318
+ # Another entry point for the parser.
319
+ # If you use this method, you must implement RECEIVER#METHOD_ID method.
320
+ #
321
+ # RECEIVER#METHOD_ID is a method to get next token.
322
+ # It must 'yield' the token, which format is [TOKEN-SYMBOL, VALUE].
149
323
  def yyparse(recv, mid)
150
324
  __send__(Racc_YY_Parse_Method, recv, mid, _racc_setup(), true)
151
325
  end
@@ -340,27 +514,43 @@ module Racc
340
514
  goto_default[k1]
341
515
  end
342
516
 
517
+ # This method is called when a parse error is found.
518
+ #
519
+ # ERROR_TOKEN_ID is an internal ID of token which caused error.
520
+ # You can get string representation of this ID by calling
521
+ # #token_to_str.
522
+ #
523
+ # ERROR_VALUE is a value of error token.
524
+ #
525
+ # value_stack is a stack of symbol values.
526
+ # DO NOT MODIFY this object.
527
+ #
528
+ # This method raises ParseError by default.
529
+ #
530
+ # If this method returns, parsers enter "error recovering mode".
343
531
  def on_error(t, val, vstack)
344
532
  raise ParseError, sprintf("\nparse error on value %s (%s)",
345
533
  val.inspect, token_to_str(t) || '?')
346
534
  end
347
535
 
536
+ # Enter error recovering mode.
537
+ # This method does not call #on_error.
348
538
  def yyerror
349
539
  throw :racc_jump, 1
350
540
  end
351
541
 
542
+ # Exit parser.
543
+ # Return value is Symbol_Value_Stack[0].
352
544
  def yyaccept
353
545
  throw :racc_jump, 2
354
546
  end
355
547
 
548
+ # Leave error recovering mode.
356
549
  def yyerrok
357
550
  @racc_error_status = 0
358
551
  end
359
552
 
360
- #
361
- # for debugging output
362
- #
363
-
553
+ # For debugging output
364
554
  def racc_read_token(t, tok, val)
365
555
  @racc_debug_out.print 'read '
366
556
  @racc_debug_out.print tok.inspect, '(', racc_token2str(t), ') '
@@ -383,7 +573,6 @@ module Racc
383
573
  toks.each {|t| out.print ' ', racc_token2str(t) }
384
574
  end
385
575
  out.puts " --> #{racc_token2str(sim)}"
386
-
387
576
  racc_print_stacks tstack, vstack
388
577
  @racc_debug_out.puts
389
578
  end
@@ -427,6 +616,7 @@ module Racc
427
616
  raise "[Racc Bug] can't convert token #{tok} to string"
428
617
  end
429
618
 
619
+ # Convert internal ID of token symbol to the string.
430
620
  def token_to_str(t)
431
621
  self.class::Racc_token_to_s_table[t]
432
622
  end
@@ -81,7 +81,7 @@ module Racc
81
81
  def ruby arg
82
82
  Dir.chdir(TEST_DIR) do
83
83
  Tempfile.open 'test' do |io|
84
- cmd = "#{ENV['_']} -I #{INC} #{arg} 2>#{io.path}"
84
+ cmd = "#{ENV['_'] || Gem.ruby} -I #{INC} #{arg} 2>#{io.path}"
85
85
  result = system(cmd)
86
86
  assert(result, io.read)
87
87
  end
@@ -14,11 +14,10 @@ module Racc
14
14
  def test_compile_chk_y
15
15
  generator = Racc::ParserFileGenerator.new(@states, @result.params.dup)
16
16
 
17
- fork {
18
- eval(generator.generate_parser)
17
+ # it generates valid ruby
18
+ assert Module.new {
19
+ self.instance_eval(generator.generate_parser, __FILE__, __LINE__)
19
20
  }
20
- Process.wait
21
- assert_equal 0, $?.exitstatus
22
21
 
23
22
  grammar = @states.grammar
24
23
 
@@ -35,9 +34,10 @@ module Racc
35
34
 
36
35
  generator = Racc::ParserFileGenerator.new(@states, @result.params.dup)
37
36
 
38
- fork { eval(generator.generate_parser) }
39
- assert_equal 0, $?.exitstatus
40
- Process.wait
37
+ # it generates valid ruby
38
+ assert Module.new {
39
+ self.instance_eval(generator.generate_parser, __FILE__, __LINE__)
40
+ }
41
41
 
42
42
  grammar = @states.grammar
43
43
 
@@ -14,11 +14,10 @@ module Racc
14
14
  def test_compile
15
15
  generator = Racc::ParserFileGenerator.new(@states, @result.params.dup)
16
16
 
17
- fork {
18
- eval(generator.generate_parser)
17
+ # it generates valid ruby
18
+ assert Module.new {
19
+ self.class_eval(generator.generate_parser)
19
20
  }
20
- Process.wait
21
- assert_equal 0, $?.exitstatus
22
21
 
23
22
  grammar = @states.grammar
24
23
 
@@ -35,9 +34,10 @@ module Racc
35
34
 
36
35
  generator = Racc::ParserFileGenerator.new(@states, @result.params.dup)
37
36
 
38
- fork { eval(generator.generate_parser) }
39
- assert_equal 0, $?.exitstatus
40
- Process.wait
37
+ # it generates valid ruby
38
+ assert Module.new {
39
+ self.class_eval(generator.generate_parser)
40
+ }
41
41
 
42
42
  grammar = @states.grammar
43
43
 
metadata CHANGED
@@ -1,67 +1,64 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: racc
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.9
5
- prerelease:
4
+ version: 1.4.10
6
5
  platform: ruby
7
6
  authors:
8
7
  - Aaron Patterson
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-08-07 00:00:00.000000000 Z
11
+ date: 2013-10-16 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rdoc
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
- version: '3.10'
19
+ version: '4.0'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
- version: '3.10'
26
+ version: '4.0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake-compiler
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: 0.4.1
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: 0.4.1
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: hoe
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ~>
45
+ - - "~>"
52
46
  - !ruby/object:Gem::Version
53
- version: '3.0'
47
+ version: '3.6'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ~>
52
+ - - "~>"
60
53
  - !ruby/object:Gem::Version
61
- version: '3.0'
62
- description: ! "Racc is a LALR(1) parser generator.\n It is written in Ruby itself,
63
- and generates Ruby program.\n\n NOTE: Ruby 1.8.x comes with Racc runtime module.
64
- \ You\n can run your parsers generated by racc 1.4.x out of the\n box."
54
+ version: '3.6'
55
+ description: |-
56
+ Racc is a LALR(1) parser generator.
57
+ It is written in Ruby itself, and generates Ruby program.
58
+
59
+ NOTE: Ruby 1.8.x comes with Racc runtime module. You
60
+ can run your parsers generated by racc 1.4.x out of the
61
+ box.
65
62
  email:
66
63
  - aaron@tenderlovemaking.com
67
64
  executables:
@@ -75,9 +72,7 @@ extra_rdoc_files:
75
72
  - README.ja.rdoc
76
73
  - README.rdoc
77
74
  - rdoc/en/NEWS.en.rdoc
78
- - rdoc/en/debug.en.rdoc
79
75
  - rdoc/en/grammar.en.rdoc
80
- - rdoc/en/parser.en.rdoc
81
76
  - rdoc/ja/NEWS.ja.rdoc
82
77
  - rdoc/ja/debug.ja.rdoc
83
78
  - rdoc/ja/grammar.ja.rdoc
@@ -119,12 +114,7 @@ files:
119
114
  - lib/racc/static.rb
120
115
  - misc/dist.sh
121
116
  - rdoc/en/NEWS.en.rdoc
122
- - rdoc/en/command.en.html
123
- - rdoc/en/debug.en.rdoc
124
117
  - rdoc/en/grammar.en.rdoc
125
- - rdoc/en/index.en.html
126
- - rdoc/en/parser.en.rdoc
127
- - rdoc/en/usage.en.html
128
118
  - rdoc/ja/NEWS.ja.rdoc
129
119
  - rdoc/ja/command.ja.html
130
120
  - rdoc/ja/debug.ja.rdoc
@@ -188,32 +178,31 @@ files:
188
178
  - test/testscanner.rb
189
179
  - web/racc.en.rhtml
190
180
  - web/racc.ja.rhtml
191
- - .gemtest
181
+ - ".gemtest"
192
182
  homepage: http://i.loveruby.net/en/projects/racc/
193
183
  licenses: []
184
+ metadata: {}
194
185
  post_install_message:
195
186
  rdoc_options:
196
- - --main
187
+ - "--main"
197
188
  - README.rdoc
198
189
  require_paths:
199
190
  - lib
200
191
  required_ruby_version: !ruby/object:Gem::Requirement
201
- none: false
202
192
  requirements:
203
- - - ! '>='
193
+ - - ">="
204
194
  - !ruby/object:Gem::Version
205
195
  version: '0'
206
196
  required_rubygems_version: !ruby/object:Gem::Requirement
207
- none: false
208
197
  requirements:
209
- - - ! '>='
198
+ - - ">="
210
199
  - !ruby/object:Gem::Version
211
200
  version: '0'
212
201
  requirements: []
213
202
  rubyforge_project: racc
214
- rubygems_version: 1.8.23
203
+ rubygems_version: 2.0.2
215
204
  signing_key:
216
- specification_version: 3
205
+ specification_version: 4
217
206
  summary: Racc is a LALR(1) parser generator
218
207
  test_files:
219
208
  - test/test_chk_y.rb