|
248 | 248 | describe "#translated_code" do |
249 | 249 | it "translates '$$' to '(yyval)' with member" do |
250 | 250 | code = grammar.rules.find {|r| r.lhs.id.s_value == "rule1" } |
251 | | - expect(code.translated_code).to eq(" (yyval.rule1) = 0; ") |
| 251 | + expect(code.translated_code(grammar)).to eq(" (yyval.rule1) = 0; ") |
252 | 252 | end |
253 | 253 |
|
254 | 254 | it "translates '@$' to '(yyloc)'" do |
255 | 255 | code = grammar.rules.find {|r| r.lhs.id.s_value == "rule2" } |
256 | | - expect(code.translated_code).to eq(" (yyloc) = 0; ") |
| 256 | + expect(code.translated_code(grammar)).to eq(" (yyloc) = 0; ") |
257 | 257 | end |
258 | 258 |
|
259 | 259 | it "translates '$n' to '(yyvsp)' with index and member" do |
260 | 260 | code = grammar.rules.find {|r| r.lhs.id.s_value == "rule3" } |
261 | | - expect(code.translated_code).to eq(" (yyvsp[-2].expr) + (yyvsp[0].expr); ") |
| 261 | + expect(code.translated_code(grammar)).to eq(" (yyvsp[-2].expr) + (yyvsp[0].expr); ") |
262 | 262 | end |
263 | 263 |
|
264 | 264 | it "translates '@n' to '(yylsp)' with index" do |
265 | 265 | code = grammar.rules.find {|r| r.lhs.id.s_value == "rule4" } |
266 | | - expect(code.translated_code).to eq(" (yylsp[-2]) + (yylsp[0]); (yylsp[-3]); ") |
| 266 | + expect(code.translated_code(grammar)).to eq(" (yylsp[-2]) + (yylsp[0]); (yylsp[-3]); ") |
267 | 267 | end |
268 | 268 |
|
269 | 269 | it "respects explicit tag in a rule" do |
270 | 270 | code = grammar.rules.find {|r| r.lhs.id.s_value == "rule5" } |
271 | | - expect(code.translated_code).to eq(" (yyvsp[-2].expr) + (yyvsp[0].integer); ") |
| 271 | + expect(code.translated_code(grammar)).to eq(" (yyvsp[-2].expr) + (yyvsp[0].integer); ") |
272 | 272 | end |
273 | 273 |
|
274 | 274 | context "midrule action exists" do |
275 | 275 | it "uses index on the original rule (-1)" do |
276 | 276 | # midrule action in rule6 |
277 | 277 | code = grammar.rules.find {|r| r.lhs.id.s_value == "$@1" } |
278 | | - expect(code.translated_code).to eq(" (yyval.integer) = (yyvsp[-1].expr); (yyloc) = (yylsp[-1]); ") |
| 278 | + expect(code.translated_code(grammar)).to eq(" (yyval.integer) = (yyvsp[-1].expr); (yyloc) = (yylsp[-1]); ") |
279 | 279 |
|
280 | 280 | code = grammar.rules.find {|r| r.lhs.id.s_value == "rule6" } |
281 | | - expect(code.translated_code).to eq(" (yyvsp[-3].expr) + (yyvsp[0].integer); ") |
| 281 | + expect(code.translated_code(grammar)).to eq(" (yyvsp[-3].expr) + (yyvsp[0].integer); ") |
282 | 282 | end |
283 | 283 |
|
284 | 284 | it "uses an explicit tag for type casting" do |
285 | 285 | # midrule action in rule13 |
286 | 286 | code = grammar.rules.find {|r| r.lhs.id.s_value == "@5" } |
287 | | - expect(code.translated_code).to eq(" (yyval.integer) = (yyvsp[-1].expr); (yyloc) = (yylsp[-1]); ") |
| 287 | + expect(code.translated_code(grammar)).to eq(" (yyval.integer) = (yyvsp[-1].expr); (yyloc) = (yylsp[-1]); ") |
288 | 288 |
|
289 | 289 | code = grammar.rules.find {|r| r.lhs.id.s_value == "rule13" } |
290 | | - expect(code.translated_code).to eq(" (yyvsp[-3].expr) + (yyvsp[-1].integer); ") |
| 290 | + expect(code.translated_code(grammar)).to eq(" (yyvsp[-3].expr) + (yyvsp[-1].integer); ") |
291 | 291 | end |
292 | 292 | end |
293 | 293 |
|
|
296 | 296 | # midrule action in rule7 |
297 | 297 | # rule7 has tag |
298 | 298 | code = grammar.rules.find {|r| r.lhs.id.s_value == "@2" } |
299 | | - expect { code.translated_code }.to raise_error("Tag is not specified for '$$' in '@2 -> ε'") |
| 299 | + expect { code.translated_code(grammar) }.to raise_error("Tag is not specified for '$$' in '@2 -> ε'") |
300 | 300 |
|
301 | 301 | code = grammar.rules.find {|r| r.lhs.id.s_value == "rule7" } |
302 | | - expect { code.translated_code }.to raise_error("Tag is not specified for '$2' in 'rule7 -> expr @2 '+' expr'") |
| 302 | + expect { code.translated_code(grammar) }.to raise_error("Tag is not specified for '$2' in 'rule7 -> expr @2 '+' expr'") |
303 | 303 |
|
304 | 304 | # midrule action in rule8 |
305 | 305 | # rule8 has no tag |
306 | 306 | code = grammar.rules.find {|r| r.lhs.id.s_value == "@3" } |
307 | | - expect { code.translated_code }.to raise_error("Tag is not specified for '$$' in '@3 -> ε'") |
| 307 | + expect { code.translated_code(grammar) }.to raise_error("Tag is not specified for '$$' in '@3 -> ε'") |
308 | 308 |
|
309 | 309 | code = grammar.rules.find {|r| r.lhs.id.s_value == "rule8" } |
310 | | - expect { code.translated_code }.to raise_error("Tag is not specified for '$2' in 'rule8 -> expr @3 '+' expr'") |
| 310 | + expect { code.translated_code(grammar) }.to raise_error("Tag is not specified for '$2' in 'rule8 -> expr @3 '+' expr'") |
311 | 311 | end |
312 | 312 | end |
313 | 313 |
|
314 | 314 | context "$: is used" do |
315 | 315 | it "translates '$:$' to '-yylen' and '$:n' to index from the last of array" do |
316 | 316 | code = grammar.rules.find {|r| r.lhs.id.s_value == "rule9" } |
317 | | - expect(code.translated_code).to eq(" (-2 - 1); (-1 - 1); (0 - 1); ") |
| 317 | + expect(code.translated_code(grammar)).to eq(" (-2 - 1); (-1 - 1); (0 - 1); ") |
318 | 318 |
|
319 | 319 | code = grammar.rules.find {|r| r.lhs.id.s_value == "rule10" } |
320 | | - expect { code.translated_code }.to raise_error("$:$ is not supported") |
| 320 | + expect { code.translated_code(grammar) }.to raise_error("$:$ is not supported") |
321 | 321 |
|
322 | 322 | # midrule action in rule11 |
323 | 323 | code = grammar.rules.find {|r| r.lhs.id.s_value == "@4" } |
324 | | - expect(code.translated_code).to eq(" (0 - 1); ") |
| 324 | + expect(code.translated_code(grammar)).to eq(" (0 - 1); ") |
325 | 325 |
|
326 | 326 | code = grammar.rules.find {|r| r.lhs.id.s_value == "rule11" } |
327 | | - expect(code.translated_code).to eq(" (-3 - 1); (-2 - 1); (-1 - 1); (0 - 1); ") |
| 327 | + expect(code.translated_code(grammar)).to eq(" (-3 - 1); (-2 - 1); (-1 - 1); (0 - 1); ") |
328 | 328 |
|
329 | 329 | code = grammar.rules.find {|r| r.lhs.id.s_value == "rule12" } |
330 | | - expect(code.translated_code).to eq(" (-2 - 1); (-1 - 1); (0 - 1); ") |
| 330 | + expect(code.translated_code(grammar)).to eq(" (-2 - 1); (-1 - 1); (0 - 1); ") |
331 | 331 | end |
332 | 332 | end |
333 | 333 | end |
|
0 commit comments