racc 1.4.9-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. data/.gemtest +0 -0
  2. data/COPYING +515 -0
  3. data/ChangeLog +846 -0
  4. data/DEPENDS +4 -0
  5. data/Manifest.txt +105 -0
  6. data/README.ja.rdoc +96 -0
  7. data/README.rdoc +86 -0
  8. data/Rakefile +51 -0
  9. data/TODO +5 -0
  10. data/bin/racc +308 -0
  11. data/bin/racc2y +195 -0
  12. data/bin/y2racc +339 -0
  13. data/ext/racc/MANIFEST +4 -0
  14. data/ext/racc/cparse.c +824 -0
  15. data/ext/racc/depend +1 -0
  16. data/ext/racc/extconf.rb +5 -0
  17. data/fastcache/extconf.rb +2 -0
  18. data/fastcache/fastcache.c +185 -0
  19. data/lib/racc.rb +6 -0
  20. data/lib/racc/compat.rb +40 -0
  21. data/lib/racc/debugflags.rb +59 -0
  22. data/lib/racc/exception.rb +15 -0
  23. data/lib/racc/grammar.rb +1115 -0
  24. data/lib/racc/grammarfileparser.rb +559 -0
  25. data/lib/racc/info.rb +16 -0
  26. data/lib/racc/iset.rb +91 -0
  27. data/lib/racc/logfilegenerator.rb +211 -0
  28. data/lib/racc/parser-text.rb +479 -0
  29. data/lib/racc/parser.rb +474 -0
  30. data/lib/racc/parserfilegenerator.rb +512 -0
  31. data/lib/racc/pre-setup +13 -0
  32. data/lib/racc/sourcetext.rb +34 -0
  33. data/lib/racc/state.rb +971 -0
  34. data/lib/racc/statetransitiontable.rb +316 -0
  35. data/lib/racc/static.rb +5 -0
  36. data/misc/dist.sh +31 -0
  37. data/rdoc/en/NEWS.en.rdoc +282 -0
  38. data/rdoc/en/command.en.html +78 -0
  39. data/rdoc/en/debug.en.rdoc +20 -0
  40. data/rdoc/en/grammar.en.rdoc +230 -0
  41. data/rdoc/en/index.en.html +10 -0
  42. data/rdoc/en/parser.en.rdoc +74 -0
  43. data/rdoc/en/usage.en.rdoc +83 -0
  44. data/rdoc/ja/NEWS.ja.rdoc +307 -0
  45. data/rdoc/ja/command.ja.html +94 -0
  46. data/rdoc/ja/debug.ja.rdoc +36 -0
  47. data/rdoc/ja/grammar.ja.rdoc +348 -0
  48. data/rdoc/ja/index.ja.html +10 -0
  49. data/rdoc/ja/parser.ja.rdoc +125 -0
  50. data/rdoc/ja/usage.ja.html +414 -0
  51. data/sample/array.y +67 -0
  52. data/sample/array2.y +59 -0
  53. data/sample/calc-ja.y +66 -0
  54. data/sample/calc.y +65 -0
  55. data/sample/conflict.y +15 -0
  56. data/sample/hash.y +60 -0
  57. data/sample/lalr.y +17 -0
  58. data/sample/lists.y +57 -0
  59. data/sample/syntax.y +46 -0
  60. data/sample/yyerr.y +46 -0
  61. data/setup.rb +1587 -0
  62. data/tasks/doc.rb +12 -0
  63. data/tasks/email.rb +55 -0
  64. data/test/assets/chk.y +126 -0
  65. data/test/assets/conf.y +16 -0
  66. data/test/assets/digraph.y +29 -0
  67. data/test/assets/echk.y +118 -0
  68. data/test/assets/err.y +60 -0
  69. data/test/assets/expect.y +7 -0
  70. data/test/assets/firstline.y +4 -0
  71. data/test/assets/ichk.y +102 -0
  72. data/test/assets/intp.y +546 -0
  73. data/test/assets/mailp.y +437 -0
  74. data/test/assets/newsyn.y +25 -0
  75. data/test/assets/noend.y +4 -0
  76. data/test/assets/nonass.y +41 -0
  77. data/test/assets/normal.y +27 -0
  78. data/test/assets/norule.y +4 -0
  79. data/test/assets/nullbug1.y +25 -0
  80. data/test/assets/nullbug2.y +15 -0
  81. data/test/assets/opt.y +123 -0
  82. data/test/assets/percent.y +35 -0
  83. data/test/assets/recv.y +97 -0
  84. data/test/assets/rrconf.y +14 -0
  85. data/test/assets/scan.y +72 -0
  86. data/test/assets/syntax.y +50 -0
  87. data/test/assets/unterm.y +5 -0
  88. data/test/assets/useless.y +12 -0
  89. data/test/assets/yyerr.y +46 -0
  90. data/test/bench.y +36 -0
  91. data/test/helper.rb +91 -0
  92. data/test/infini.y +8 -0
  93. data/test/scandata/brace +7 -0
  94. data/test/scandata/gvar +1 -0
  95. data/test/scandata/normal +4 -0
  96. data/test/scandata/percent +18 -0
  97. data/test/scandata/slash +10 -0
  98. data/test/src.intp +34 -0
  99. data/test/start.y +20 -0
  100. data/test/test_chk_y.rb +51 -0
  101. data/test/test_grammar_file_parser.rb +15 -0
  102. data/test/test_racc_command.rb +155 -0
  103. data/test/test_scan_y.rb +51 -0
  104. data/test/testscanner.rb +51 -0
  105. data/web/racc.en.rhtml +42 -0
  106. data/web/racc.ja.rhtml +51 -0
  107. metadata +233 -0
@@ -0,0 +1,512 @@
1
+ #
2
+ # $Id: 75aee0ac3cd6429e28eac511d78a717614123a89 $
3
+ #
4
+ # Copyright (c) 1999-2006 Minero Aoki
5
+ #
6
+ # This program is free software.
7
+ # You can distribute/modify this program under the terms of
8
+ # the GNU LGPL, Lesser General Public License version 2.1.
9
+ # For details of the GNU LGPL, see the file "COPYING".
10
+ #
11
+
12
+ require 'enumerator'
13
+ require 'racc/compat'
14
+ require 'racc/sourcetext'
15
+ require 'racc/parser-text'
16
+ require 'rbconfig'
17
+
18
+ module Racc
19
+
20
+ class ParserFileGenerator
21
+
22
+ class Params
23
+ def self.bool_attr(name)
24
+ module_eval(<<-End)
25
+ def #{name}?
26
+ @#{name}
27
+ end
28
+
29
+ def #{name}=(b)
30
+ @#{name} = b
31
+ end
32
+ End
33
+ end
34
+
35
+ attr_accessor :filename
36
+ attr_accessor :classname
37
+ attr_accessor :superclass
38
+ bool_attr :omit_action_call
39
+ bool_attr :result_var
40
+ attr_accessor :header
41
+ attr_accessor :inner
42
+ attr_accessor :footer
43
+
44
+ bool_attr :debug_parser
45
+ bool_attr :convert_line
46
+ bool_attr :convert_line_all
47
+ bool_attr :embed_runtime
48
+ bool_attr :make_executable
49
+ attr_accessor :interpreter
50
+
51
+ def initialize
52
+ # Parameters derived from parser
53
+ self.filename = nil
54
+ self.classname = nil
55
+ self.superclass = 'Racc::Parser'
56
+ self.omit_action_call = true
57
+ self.result_var = true
58
+ self.header = []
59
+ self.inner = []
60
+ self.footer = []
61
+
62
+ # Parameters derived from command line options
63
+ self.debug_parser = false
64
+ self.convert_line = true
65
+ self.convert_line_all = false
66
+ self.embed_runtime = false
67
+ self.make_executable = false
68
+ self.interpreter = nil
69
+ end
70
+ end
71
+
72
+ def initialize(states, params)
73
+ @states = states
74
+ @grammar = states.grammar
75
+ @params = params
76
+ end
77
+
78
+ def generate_parser
79
+ string_io = StringIO.new
80
+
81
+ init_line_conversion_system
82
+ @f = string_io
83
+ parser_file
84
+
85
+ string_io.rewind
86
+ string_io.read
87
+ end
88
+
89
+ def generate_parser_file(destpath)
90
+ init_line_conversion_system
91
+ File.open(destpath, 'w') {|f|
92
+ @f = f
93
+ parser_file
94
+ }
95
+ File.chmod 0755, destpath if @params.make_executable?
96
+ end
97
+
98
+ private
99
+
100
+ def parser_file
101
+ shebang @params.interpreter if @params.make_executable?
102
+ notice
103
+ line
104
+ if @params.embed_runtime?
105
+ embed_library runtime_source()
106
+ else
107
+ require 'racc/parser.rb'
108
+ end
109
+ header
110
+ parser_class(@params.classname, @params.superclass) {
111
+ inner
112
+ state_transition_table
113
+ }
114
+ footer
115
+ end
116
+
117
+ c = ::RbConfig::CONFIG
118
+ RUBY_PATH = "#{c['bindir']}/#{c['ruby_install_name']}#{c['EXEEXT']}"
119
+
120
+ def shebang(path)
121
+ line '#!' + (path == 'ruby' ? RUBY_PATH : path)
122
+ end
123
+
124
+ def notice
125
+ line %q[#]
126
+ line %q[# DO NOT MODIFY!!!!]
127
+ line %Q[# This file is automatically generated by Racc #{Racc::Version}]
128
+ line %Q[# from Racc grammer file "#{@params.filename}".]
129
+ line %q[#]
130
+ end
131
+
132
+ def runtime_source
133
+ SourceText.new(::Racc::PARSER_TEXT, 'racc/parser.rb', 1)
134
+ end
135
+
136
+ def embed_library(src)
137
+ line %[###### #{src.filename} begin]
138
+ line %[unless $".index '#{src.filename}']
139
+ line %[$".push '#{src.filename}']
140
+ put src, @params.convert_line?
141
+ line %[end]
142
+ line %[###### #{src.filename} end]
143
+ end
144
+
145
+ def require(feature)
146
+ line "require '#{feature}'"
147
+ end
148
+
149
+ def parser_class(classname, superclass)
150
+ mods = classname.split('::')
151
+ classid = mods.pop
152
+ mods.each do |mod|
153
+ indent; line "module #{mod}"
154
+ cref_push mod
155
+ end
156
+ indent; line "class #{classid} < #{superclass}"
157
+ cref_push classid
158
+ yield
159
+ cref_pop
160
+ indent; line "end \# class #{classid}"
161
+ mods.reverse_each do |mod|
162
+ indent; line "end \# module #{mod}"
163
+ cref_pop
164
+ end
165
+ end
166
+
167
+ def header
168
+ @params.header.each do |src|
169
+ line
170
+ put src, @params.convert_line_all?
171
+ end
172
+ end
173
+
174
+ def inner
175
+ @params.inner.each do |src|
176
+ line
177
+ put src, @params.convert_line?
178
+ end
179
+ end
180
+
181
+ def footer
182
+ @params.footer.each do |src|
183
+ line
184
+ put src, @params.convert_line_all?
185
+ end
186
+ end
187
+
188
+ # Low Level Routines
189
+
190
+ def put(src, convert_line = false)
191
+ if convert_line
192
+ replace_location(src) {
193
+ @f.puts src.text
194
+ }
195
+ else
196
+ @f.puts src.text
197
+ end
198
+ end
199
+
200
+ def line(str = '')
201
+ @f.puts str
202
+ end
203
+
204
+ def init_line_conversion_system
205
+ @cref = []
206
+ @used_separator = {}
207
+ end
208
+
209
+ def cref_push(name)
210
+ @cref.push name
211
+ end
212
+
213
+ def cref_pop
214
+ @cref.pop
215
+ end
216
+
217
+ def indent
218
+ @f.print ' ' * @cref.size
219
+ end
220
+
221
+ def toplevel?
222
+ @cref.empty?
223
+ end
224
+
225
+ def replace_location(src)
226
+ sep = make_separator(src)
227
+ @f.print 'self.class.' if toplevel?
228
+ @f.puts "module_eval(<<'#{sep}', '#{src.filename}', #{src.lineno})"
229
+ yield
230
+ @f.puts sep
231
+ end
232
+
233
+ def make_separator(src)
234
+ sep = unique_separator(src.filename)
235
+ sep *= 2 while src.text.index(sep)
236
+ sep
237
+ end
238
+
239
+ def unique_separator(id)
240
+ sep = "...end #{id}/module_eval..."
241
+ while @used_separator.key?(sep)
242
+ sep.concat sprintf('%02x', rand(255))
243
+ end
244
+ @used_separator[sep] = true
245
+ sep
246
+ end
247
+
248
+ #
249
+ # State Transition Table Serialization
250
+ #
251
+
252
+ public
253
+
254
+ def put_state_transition_table(f)
255
+ @f = f
256
+ state_transition_table
257
+ end
258
+
259
+ private
260
+
261
+ def state_transition_table
262
+ table = @states.state_transition_table
263
+ table.use_result_var = @params.result_var?
264
+ table.debug_parser = @params.debug_parser?
265
+
266
+ line "##### State transition tables begin ###"
267
+ line
268
+ integer_list 'racc_action_table', table.action_table
269
+ line
270
+ integer_list 'racc_action_check', table.action_check
271
+ line
272
+ integer_list 'racc_action_pointer', table.action_pointer
273
+ line
274
+ integer_list 'racc_action_default', table.action_default
275
+ line
276
+ integer_list 'racc_goto_table', table.goto_table
277
+ line
278
+ integer_list 'racc_goto_check', table.goto_check
279
+ line
280
+ integer_list 'racc_goto_pointer', table.goto_pointer
281
+ line
282
+ integer_list 'racc_goto_default', table.goto_default
283
+ line
284
+ i_i_sym_list 'racc_reduce_table', table.reduce_table
285
+ line
286
+ line "racc_reduce_n = #{table.reduce_n}"
287
+ line
288
+ line "racc_shift_n = #{table.shift_n}"
289
+ line
290
+ sym_int_hash 'racc_token_table', table.token_table
291
+ line
292
+ line "racc_nt_base = #{table.nt_base}"
293
+ line
294
+ line "racc_use_result_var = #{table.use_result_var}"
295
+ line
296
+ @f.print(unindent_auto(<<-End))
297
+ Racc_arg = [
298
+ racc_action_table,
299
+ racc_action_check,
300
+ racc_action_default,
301
+ racc_action_pointer,
302
+ racc_goto_table,
303
+ racc_goto_check,
304
+ racc_goto_default,
305
+ racc_goto_pointer,
306
+ racc_nt_base,
307
+ racc_reduce_table,
308
+ racc_token_table,
309
+ racc_shift_n,
310
+ racc_reduce_n,
311
+ racc_use_result_var ]
312
+ End
313
+ line
314
+ string_list 'Racc_token_to_s_table', table.token_to_s_table
315
+ line
316
+ line "Racc_debug_parser = #{table.debug_parser}"
317
+ line
318
+ line '##### State transition tables end #####'
319
+ actions
320
+ end
321
+
322
+ def integer_list(name, table)
323
+ if table.size > 2000
324
+ serialize_integer_list_compressed name, table
325
+ else
326
+ serialize_integer_list_std name, table
327
+ end
328
+ end
329
+
330
+ def serialize_integer_list_compressed(name, table)
331
+ # TODO: this can be made a LOT more clean with a simple split/map
332
+ sep = "\n"
333
+ nsep = ",\n"
334
+ buf = ''
335
+ com = ''
336
+ ncom = ','
337
+ co = com
338
+ @f.print 'clist = ['
339
+ table.each do |i|
340
+ buf << co << i.to_s; co = ncom
341
+ if buf.size > 66
342
+ @f.print sep; sep = nsep
343
+ @f.print "'", buf, "'"
344
+ buf = ''
345
+ co = com
346
+ end
347
+ end
348
+ unless buf.empty?
349
+ @f.print sep
350
+ @f.print "'", buf, "'"
351
+ end
352
+ line ' ]'
353
+
354
+ @f.print(<<-End)
355
+ #{name} = arr = ::Array.new(#{table.size}, nil)
356
+ idx = 0
357
+ clist.each do |str|
358
+ str.split(',', -1).each do |i|
359
+ arr[idx] = i.to_i unless i.empty?
360
+ idx += 1
361
+ end
362
+ end
363
+ End
364
+ end
365
+
366
+ def serialize_integer_list_std(name, table)
367
+ sep = ''
368
+ line "#{name} = ["
369
+ table.each_slice(10) do |ns|
370
+ @f.print sep; sep = ",\n"
371
+ @f.print ns.map {|n| sprintf('%6s', n ? n.to_s : 'nil') }.join(',')
372
+ end
373
+ line ' ]'
374
+ end
375
+
376
+ def i_i_sym_list(name, table)
377
+ sep = ''
378
+ line "#{name} = ["
379
+ table.each_slice(3) do |len, target, mid|
380
+ @f.print sep; sep = ",\n"
381
+ @f.printf ' %d, %d, %s', len, target, mid.inspect
382
+ end
383
+ line " ]"
384
+ end
385
+
386
+ def sym_int_hash(name, h)
387
+ sep = "\n"
388
+ @f.print "#{name} = {"
389
+ h.to_a.sort_by {|sym, i| i }.each do |sym, i|
390
+ @f.print sep; sep = ",\n"
391
+ @f.printf " %s => %d", sym.serialize, i
392
+ end
393
+ line " }"
394
+ end
395
+
396
+ def string_list(name, list)
397
+ sep = " "
398
+ line "#{name} = ["
399
+ list.each do |s|
400
+ @f.print sep; sep = ",\n "
401
+ @f.print s.dump
402
+ end
403
+ line ' ]'
404
+ end
405
+
406
+ def actions
407
+ @grammar.each do |rule|
408
+ unless rule.action.source?
409
+ raise "racc: fatal: cannot generate parser file when any action is a Proc"
410
+ end
411
+ end
412
+
413
+ if @params.result_var?
414
+ decl = ', result'
415
+ retval = "\n result"
416
+ default_body = ''
417
+ else
418
+ decl = ''
419
+ retval = ''
420
+ default_body = 'val[0]'
421
+ end
422
+ @grammar.each do |rule|
423
+ line
424
+ if rule.action.empty? and @params.omit_action_call?
425
+ line "# reduce #{rule.ident} omitted"
426
+ else
427
+ src0 = rule.action.source || SourceText.new(default_body, __FILE__, 0)
428
+ if @params.convert_line?
429
+ src = remove_blank_lines(src0)
430
+ delim = make_delimiter(src.text)
431
+ @f.printf unindent_auto(<<-End),
432
+ module_eval(<<'%s', '%s', %d)
433
+ def _reduce_%d(val, _values%s)
434
+ %s%s
435
+ end
436
+ %s
437
+ End
438
+ delim, src.filename, src.lineno - 1,
439
+ rule.ident, decl,
440
+ src.text, retval,
441
+ delim
442
+ else
443
+ src = remove_blank_lines(src0)
444
+ @f.printf unindent_auto(<<-End),
445
+ def _reduce_%d(val, _values%s)
446
+ %s%s
447
+ end
448
+ End
449
+ rule.ident, decl,
450
+ src.text, retval
451
+ end
452
+ end
453
+ end
454
+ line
455
+ @f.printf unindent_auto(<<-'End'), decl
456
+ def _reduce_none(val, _values%s)
457
+ val[0]
458
+ end
459
+ End
460
+ line
461
+ end
462
+
463
+ def remove_blank_lines(src)
464
+ body = src.text.dup
465
+ line = src.lineno
466
+ while body.slice!(/\A[ \t\f]*(?:\n|\r\n|\r)/)
467
+ line += 1
468
+ end
469
+ SourceText.new(body, src.filename, line)
470
+ end
471
+
472
+ def make_delimiter(body)
473
+ delim = '.,.,'
474
+ while body.index(delim)
475
+ delim *= 2
476
+ end
477
+ delim
478
+ end
479
+
480
+ def unindent_auto(str)
481
+ lines = str.to_a
482
+ n = minimum_indent(lines)
483
+ lines.map {|line| detab(line).sub(indent_re(n), '').rstrip + "\n" }.join('')
484
+ end
485
+
486
+ def minimum_indent(lines)
487
+ lines.map {|line| n_indent(line) }.min
488
+ end
489
+
490
+ def n_indent(line)
491
+ line.slice(/\A\s+/).size
492
+ end
493
+
494
+ RE_CACHE = {}
495
+
496
+ def indent_re(n)
497
+ RE_CACHE[n] ||= /\A {#{n}}/
498
+ end
499
+
500
+ def detab(str, ts = 8)
501
+ add = 0
502
+ len = nil
503
+ str.gsub(/\t/) {
504
+ len = ts - ($`.size + add) % ts
505
+ add += len - 1
506
+ ' ' * len
507
+ }
508
+ end
509
+
510
+ end
511
+
512
+ end