Class: Lrama::Output
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#after_pop_stack_function(len, comment = "") ⇒ Object
-
#after_reduce_function(comment = "") ⇒ Object
-
#after_shift_error_token_function(comment = "") ⇒ Object
-
#after_shift_function(comment = "") ⇒ Object
-
#aux ⇒ Object
-
#b4_cpp_guard__b4_spec_mapped_header_file ⇒ Object
-
#before_reduce_function(comment = "") ⇒ Object
-
#extract_param_name(param) ⇒ Object
-
#initialize(out:, output_file_path:, template_name:, grammar_file_path:, context:, grammar:, header_out: nil, header_file_path: nil, error_recovery: false) ⇒ Output
constructor
A new instance of Output.
-
#int_array_to_string(ary) ⇒ Object
-
#int_type_for(ary) ⇒ Object
-
#lex_param ⇒ Object
-
#lex_param_name ⇒ Object
-
#omit_blanks(param) ⇒ Object
-
#parse_param ⇒ Object
-
#parse_param_name ⇒ Object
-
#parse_param_use(val, loc) ⇒ Object
-
#percent_code(name) ⇒ Object
-
#render ⇒ Object
-
#render_partial(file) ⇒ Object
-
#spec_mapped_header_file ⇒ Object
-
#symbol_actions_for_destructor ⇒ Object
-
#symbol_actions_for_error_token ⇒ Object
-
#symbol_actions_for_printer ⇒ Object
-
#symbol_enum ⇒ Object
-
#table_value_equals(table, value, literal, symbol) ⇒ Object
-
#template_basename ⇒ Object
-
#token_enums ⇒ Object
A part of b4_token_enums.
-
#user_actions ⇒ Object
-
#user_args ⇒ Object
-
#user_formals ⇒ Object
-
#user_initial_action(comment = "") ⇒ Object
-
#yyerror_args ⇒ Object
-
#yylex_formals ⇒ Object
-
#yyrline ⇒ Object
-
#yytname ⇒ Object
-
#yytranslate ⇒ Object
-
#yytranslate_inverted ⇒ Object
enable, enabled?, #report_duration
Constructor Details
#initialize(out:, output_file_path:, template_name:, grammar_file_path:, context:, grammar:, header_out: nil, header_file_path: nil, error_recovery: false) ⇒ Output
Returns a new instance of Output.
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/lrama/output.rb', line 19
def initialize(
out:, output_file_path:, template_name:, grammar_file_path:,
context:, grammar:, header_out: nil, header_file_path: nil, error_recovery: false
)
@out = out
@output_file_path = output_file_path
@template_name = template_name
@grammar_file_path = grammar_file_path
@header_out =
@header_file_path =
@context = context
@grammar = grammar
@error_recovery = error_recovery
@include_header = ? .sub("./", "") : nil
end
|
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
12
13
14
|
# File 'lib/lrama/output.rb', line 12
def context
@context
end
|
#error_recovery ⇒ Object
Returns the value of attribute error_recovery.
12
13
14
|
# File 'lib/lrama/output.rb', line 12
def error_recovery
@error_recovery
end
|
#grammar ⇒ Object
Returns the value of attribute grammar.
12
13
14
|
# File 'lib/lrama/output.rb', line 12
def grammar
@grammar
end
|
#grammar_file_path ⇒ Object
Returns the value of attribute grammar_file_path.
12
13
14
|
# File 'lib/lrama/output.rb', line 12
def grammar_file_path
@grammar_file_path
end
|
Returns the value of attribute include_header.
12
13
14
|
# File 'lib/lrama/output.rb', line 12
def
@include_header
end
|
Class Method Details
.erb(input) ⇒ Object
36
37
38
|
# File 'lib/lrama/output.rb', line 36
def self.erb(input)
ERB.new(input, trim_mode: '-')
end
|
Instance Method Details
#after_pop_stack_function(len, comment = "") ⇒ Object
213
214
215
216
217
218
219
220
221
222
|
# File 'lib/lrama/output.rb', line 213
def after_pop_stack_function(len, = "")
return "" unless @grammar.after_pop_stack
<<-STR
#{}
#line #{@grammar.after_pop_stack.line} "#{@grammar_file_path}"
{#{@grammar.after_pop_stack.s_value}(#{len}#{user_args});}
#line [@oline@] [@ofile@]
STR
end
|
#after_reduce_function(comment = "") ⇒ Object
191
192
193
194
195
196
197
198
199
200
|
# File 'lib/lrama/output.rb', line 191
def after_reduce_function( = "")
return "" unless @grammar.after_reduce
<<-STR
#{}
#line #{@grammar.after_reduce.line} "#{@grammar_file_path}"
{#{@grammar.after_reduce.s_value}(yylen#{user_args});}
#line [@oline@] [@ofile@]
STR
end
|
#after_shift_error_token_function(comment = "") ⇒ Object
202
203
204
205
206
207
208
209
210
211
|
# File 'lib/lrama/output.rb', line 202
def after_shift_error_token_function( = "")
return "" unless @grammar.after_shift_error_token
<<-STR
#{}
#line #{@grammar.after_shift_error_token.line} "#{@grammar_file_path}"
{#{@grammar.after_shift_error_token.s_value}(#{parse_param_name});}
#line [@oline@] [@ofile@]
STR
end
|
#after_shift_function(comment = "") ⇒ Object
169
170
171
172
173
174
175
176
177
178
|
# File 'lib/lrama/output.rb', line 169
def after_shift_function( = "")
return "" unless @grammar.after_shift
<<-STR
#{}
#line #{@grammar.after_shift.line} "#{@grammar_file_path}"
{#{@grammar.after_shift.s_value}(#{parse_param_name});}
#line [@oline@] [@ofile@]
STR
end
|
#aux ⇒ Object
372
373
374
|
# File 'lib/lrama/output.rb', line 372
def aux
@grammar.aux
end
|
388
389
390
391
392
393
394
|
# File 'lib/lrama/output.rb', line 388
def
if @header_file_path
"YY_YY_" + @header_file_path.gsub(/[^a-zA-Z_0-9]+/, "_").upcase + "_INCLUDED"
else
""
end
end
|
#before_reduce_function(comment = "") ⇒ Object
180
181
182
183
184
185
186
187
188
189
|
# File 'lib/lrama/output.rb', line 180
def before_reduce_function( = "")
return "" unless @grammar.before_reduce
<<-STR
#{}
#line #{@grammar.before_reduce.line} "#{@grammar_file_path}"
{#{@grammar.before_reduce.s_value}(yylen#{user_args});}
#line [@oline@] [@ofile@]
STR
end
|
302
303
304
|
# File 'lib/lrama/output.rb', line 302
def (param)
param[/\b([a-zA-Z0-9_]+)(?=\s*\z)/]
end
|
#int_array_to_string(ary) ⇒ Object
376
377
378
379
380
381
382
|
# File 'lib/lrama/output.rb', line 376
def int_array_to_string(ary)
last = ary.count - 1
ary.each_with_index.each_slice(10).map do |slice|
" " + slice.map { |e, i| sprintf("%6d%s", e, (i == last) ? "" : ",") }.join
end.join("\n")
end
|
#int_type_for(ary) ⇒ Object
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/lrama/output.rb', line 110
def int_type_for(ary)
min = ary.min
max = ary.max
case
when (-127 <= min && min <= 127) && (-127 <= max && max <= 127)
"yytype_int8"
when (0 <= min && min <= 255) && (0 <= max && max <= 255)
"yytype_uint8"
when (-32767 <= min && min <= 32767) && (-32767 <= max && max <= 32767)
"yytype_int16"
when (0 <= min && min <= 65535) && (0 <= max && max <= 65535)
"yytype_uint16"
else
"int"
end
end
|
#lex_param ⇒ Object
276
277
278
279
280
281
282
|
# File 'lib/lrama/output.rb', line 276
def lex_param
if @grammar.lex_param
omit_blanks(@grammar.lex_param)
else
""
end
end
|
#lex_param_name ⇒ Object
314
315
316
317
318
319
320
|
# File 'lib/lrama/output.rb', line 314
def lex_param_name
if @grammar.lex_param
(lex_param)
else
""
end
end
|
#omit_blanks(param) ⇒ Object
263
264
265
|
# File 'lib/lrama/output.rb', line 263
def omit_blanks(param)
param.strip
end
|
#parse_param ⇒ Object
268
269
270
271
272
273
274
|
# File 'lib/lrama/output.rb', line 268
def parse_param
if @grammar.parse_param
omit_blanks(@grammar.parse_param)
else
""
end
end
|
#parse_param_name ⇒ Object
306
307
308
309
310
311
312
|
# File 'lib/lrama/output.rb', line 306
def parse_param_name
if @grammar.parse_param
(parse_param)
else
""
end
end
|
#parse_param_use(val, loc) ⇒ Object
323
324
325
326
327
328
329
330
331
332
333
334
|
# File 'lib/lrama/output.rb', line 323
def parse_param_use(val, loc)
str = <<-STR.dup
YY_USE (#{val});
YY_USE (#{loc});
STR
if @grammar.parse_param
str << " YY_USE (#{parse_param_name});"
end
str
end
|
#percent_code(name) ⇒ Object
397
398
399
400
401
402
403
|
# File 'lib/lrama/output.rb', line 397
def percent_code(name)
@grammar.percent_codes.select do |percent_code|
percent_code.name == name
end.map do |percent_code|
percent_code.code
end.join
end
|
#render ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/lrama/output.rb', line 49
def render
report_duration(:render) do
tmp = eval_template(template_file, @output_file_path)
@out << tmp
if @header_file_path
tmp = eval_template(, @header_file_path)
if @header_out
@header_out << tmp
else
File.write(@header_file_path, tmp)
end
end
end
end
|
#render_partial(file) ⇒ Object
45
46
47
|
# File 'lib/lrama/output.rb', line 45
def render_partial(file)
render_template(partial_file(file))
end
|
384
385
386
|
# File 'lib/lrama/output.rb', line 384
def
@header_file_path
end
|
#symbol_actions_for_destructor ⇒ Object
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/lrama/output.rb', line 143
def symbol_actions_for_destructor
@grammar.symbols.map do |sym|
next unless sym.destructor
<<-STR
case #{sym.enum_name}: /* #{sym.} */
#line #{sym.destructor.lineno} "#{@grammar_file_path}"
{#{sym.destructor.translated_code(sym.tag)}}
#line [@oline@] [@ofile@]
break;
STR
end.join
end
|
#symbol_actions_for_error_token ⇒ Object
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
# File 'lib/lrama/output.rb', line 224
def symbol_actions_for_error_token
@grammar.symbols.map do |sym|
next unless sym.error_token
<<-STR
case #{sym.enum_name}: /* #{sym.} */
#line #{sym.error_token.lineno} "#{@grammar_file_path}"
{#{sym.error_token.translated_code(sym.tag)}}
#line [@oline@] [@ofile@]
break;
STR
end.join
end
|
#symbol_actions_for_printer ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/lrama/output.rb', line 128
def symbol_actions_for_printer
@grammar.symbols.map do |sym|
next unless sym.printer
<<-STR
case #{sym.enum_name}: /* #{sym.} */
#line #{sym.printer.lineno} "#{@grammar_file_path}"
{#{sym.printer.translated_code(sym.tag)}}
#line [@oline@] [@ofile@]
break;
STR
end.join
end
|
#symbol_enum ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/lrama/output.rb', line 80
def symbol_enum
last_sym_number = @context.yysymbol_kind_t.last[1]
@context.yysymbol_kind_t.map do |s_value, sym_number, display_name|
s = sprintf("%s = %d%s", s_value, sym_number, (sym_number == last_sym_number) ? "" : ",")
if display_name
sprintf(" %-40s /* %s */\n", s, display_name)
else
sprintf(" %s\n", s)
end
end.join
end
|
#table_value_equals(table, value, literal, symbol) ⇒ Object
349
350
351
352
353
354
355
|
# File 'lib/lrama/output.rb', line 349
def table_value_equals(table, value, literal, symbol)
if literal < table.min || table.max < literal
"0"
else
"((#{value}) == #{symbol})"
end
end
|
#template_basename ⇒ Object
368
369
370
|
# File 'lib/lrama/output.rb', line 368
def template_basename
File.basename(template_file)
end
|
#token_enums ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/lrama/output.rb', line 67
def token_enums
@context.yytokentype.map do |s_value, token_id, display_name|
s = sprintf("%s = %d%s", s_value, token_id, token_id == yymaxutok ? "" : ",")
if display_name
sprintf(" %-30s /* %s */\n", s, display_name)
else
sprintf(" %s\n", s)
end
end.join
end
|
#user_actions ⇒ Object
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
# File 'lib/lrama/output.rb', line 240
def user_actions
action = @context.states.rules.map do |rule|
next unless rule.token_code
code = rule.token_code
spaces = " " * (code.column - 1)
<<-STR
case #{rule.id + 1}: /* #{rule.} */
#line #{code.line} "#{@grammar_file_path}"
#{spaces}{#{rule.translated_code}}
#line [@oline@] [@ofile@]
break;
STR
end.join
action + <<-STR
#line [@oline@] [@ofile@]
STR
end
|
#user_args ⇒ Object
294
295
296
297
298
299
300
|
# File 'lib/lrama/output.rb', line 294
def user_args
if @grammar.parse_param
", #{parse_param_name}"
else
""
end
end
|
285
286
287
288
289
290
291
|
# File 'lib/lrama/output.rb', line 285
def user_formals
if @grammar.parse_param
", #{parse_param}"
else
""
end
end
|
#user_initial_action(comment = "") ⇒ Object
159
160
161
162
163
164
165
166
167
|
# File 'lib/lrama/output.rb', line 159
def user_initial_action( = "")
return "" unless @grammar.initial_action
<<-STR
#{}
#line #{@grammar.initial_action.line} "#{@grammar_file_path}"
{#{@grammar.initial_action.translated_code}}
STR
end
|
#yyerror_args ⇒ Object
358
359
360
361
362
363
364
365
366
|
# File 'lib/lrama/output.rb', line 358
def yyerror_args
ary = ["&yylloc"]
if @grammar.parse_param
ary << parse_param_name
end
"#{ary.join(', ')}"
end
|
337
338
339
340
341
342
343
344
345
346
|
# File 'lib/lrama/output.rb', line 337
def yylex_formals
ary = ["&yylval"]
ary << "&yylloc" if @grammar.locations
if @grammar.lex_param
ary << lex_param_name
end
"(#{ary.join(', ')})"
end
|
#yyrline ⇒ Object
101
102
103
|
# File 'lib/lrama/output.rb', line 101
def yyrline
int_array_to_string(@context.yyrline)
end
|
#yytname ⇒ Object
105
106
107
|
# File 'lib/lrama/output.rb', line 105
def yytname
string_array_to_string(@context.yytname) + " YY_NULLPTR"
end
|
#yytranslate ⇒ Object
93
94
95
|
# File 'lib/lrama/output.rb', line 93
def yytranslate
int_array_to_string(@context.yytranslate)
end
|
#yytranslate_inverted ⇒ Object
97
98
99
|
# File 'lib/lrama/output.rb', line 97
def yytranslate_inverted
int_array_to_string(@context.yytranslate_inverted)
end
|