lrama 0.5.5 → 0.5.7

Sign up to get free protection for your applications and to get access to all the features.
data/lib/lrama/output.rb CHANGED
@@ -7,7 +7,7 @@ module Lrama
7
7
  extend Forwardable
8
8
  include Report::Duration
9
9
 
10
- attr_reader :grammar_file_path, :context, :grammar, :error_recovery
10
+ attr_reader :grammar_file_path, :context, :grammar, :error_recovery, :include_header
11
11
 
12
12
  def_delegators "@context", :yyfinal, :yylast, :yyntokens, :yynnts, :yynrules, :yynstates,
13
13
  :yymaxutok, :yypact_ninf, :yytable_ninf
@@ -28,6 +28,7 @@ module Lrama
28
28
  @context = context
29
29
  @grammar = grammar
30
30
  @error_recovery = error_recovery
31
+ @include_header = header_file_path ? header_file_path.sub("./", "") : nil
31
32
  end
32
33
 
33
34
  if ERB.instance_method(:initialize).parameters.last.first == :key
@@ -40,11 +41,8 @@ module Lrama
40
41
  end
41
42
  end
42
43
 
43
- def eval_template(file, path)
44
- erb = self.class.erb(File.read(file))
45
- erb.filename = file
46
- tmp = erb.result_with_hash(context: @context, output: self)
47
- replace_special_variables(tmp, path)
44
+ def render_partial(file)
45
+ render_template(partial_file(file))
48
46
  end
49
47
 
50
48
  def render
@@ -143,7 +141,7 @@ module Lrama
143
141
  str << <<-STR
144
142
  case #{sym.enum_name}: /* #{sym.comment} */
145
143
  #line #{sym.printer.lineno} "#{@grammar_file_path}"
146
- #{sym.printer.translated_code(sym.tag)}
144
+ {#{sym.printer.translated_code(sym.tag)}}
147
145
  #line [@oline@] [@ofile@]
148
146
  break;
149
147
 
@@ -160,7 +158,7 @@ module Lrama
160
158
  <<-STR
161
159
  #{comment}
162
160
  #line #{@grammar.initial_action.line} "#{@grammar_file_path}"
163
- #{@grammar.initial_action.translated_code}
161
+ {#{@grammar.initial_action.translated_code}}
164
162
  STR
165
163
  end
166
164
 
@@ -173,7 +171,7 @@ module Lrama
173
171
  str << <<-STR
174
172
  case #{sym.enum_name}: /* #{sym.comment} */
175
173
  #line #{sym.error_token.lineno} "#{@grammar_file_path}"
176
- #{sym.error_token.translated_code(sym.tag)}
174
+ {#{sym.error_token.translated_code(sym.tag)}}
177
175
  #line [@oline@] [@ofile@]
178
176
  break;
179
177
 
@@ -190,14 +188,13 @@ module Lrama
190
188
  @context.states.rules.each do |rule|
191
189
  next unless rule.code
192
190
 
193
- rule = rule
194
191
  code = rule.code
195
192
  spaces = " " * (code.column - 1)
196
193
 
197
194
  str << <<-STR
198
195
  case #{rule.id + 1}: /* #{rule.as_comment} */
199
196
  #line #{code.line} "#{@grammar_file_path}"
200
- #{spaces}#{rule.translated_code}
197
+ #{spaces}{#{rule.translated_code}}
201
198
  #line [@oline@] [@ofile@]
202
199
  break;
203
200
 
@@ -212,14 +209,14 @@ module Lrama
212
209
  str
213
210
  end
214
211
 
215
- def omit_braces_and_blanks(param)
216
- param[1..-2].strip
212
+ def omit_blanks(param)
213
+ param.strip
217
214
  end
218
215
 
219
216
  # b4_parse_param
220
217
  def parse_param
221
218
  if @grammar.parse_param
222
- omit_braces_and_blanks(@grammar.parse_param)
219
+ omit_blanks(@grammar.parse_param)
223
220
  else
224
221
  ""
225
222
  end
@@ -227,7 +224,7 @@ module Lrama
227
224
 
228
225
  def lex_param
229
226
  if @grammar.lex_param
230
- omit_braces_and_blanks(@grammar.lex_param)
227
+ omit_blanks(@grammar.lex_param)
231
228
  else
232
229
  ""
233
230
  end
@@ -354,6 +351,17 @@ module Lrama
354
351
 
355
352
  private
356
353
 
354
+ def eval_template(file, path)
355
+ tmp = render_template(file)
356
+ replace_special_variables(tmp, path)
357
+ end
358
+
359
+ def render_template(file)
360
+ erb = self.class.erb(File.read(file))
361
+ erb.filename = file
362
+ erb.result_with_hash(context: @context, output: self)
363
+ end
364
+
357
365
  def template_file
358
366
  File.join(template_dir, @template_name)
359
367
  end
@@ -362,6 +370,10 @@ module Lrama
362
370
  File.join(template_dir, "bison/yacc.h")
363
371
  end
364
372
 
373
+ def partial_file(file)
374
+ File.join(template_dir, file)
375
+ end
376
+
365
377
  def template_dir
366
378
  File.expand_path("../../../template", __FILE__)
367
379
  end