Skip to content

Commit

Permalink
Lazy initialization of PrinterCode
Browse files Browse the repository at this point in the history
PrinterCode needs tag for code translation.
It's possible to pass tag as argument by lazy initialization.
  • Loading branch information
yui-knk committed Nov 5, 2023
1 parent 74735f1 commit 0eb8095
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 25 deletions.
8 changes: 4 additions & 4 deletions lib/lrama/grammar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ def add_percent_code(id:, code:)
@percent_codes << PercentCode.new(id, code)
end

def add_printer(ident_or_tags:, code:, lineno:)
@printers << Printer.new(ident_or_tags: ident_or_tags, code: code, lineno: lineno)
def add_printer(ident_or_tags:, token_code:, lineno:)
@printers << Printer.new(ident_or_tags: ident_or_tags, token_code: token_code, lineno: lineno)
end

def add_error_token(ident_or_tags:, code:, lineno:)
@error_tokens << ErrorToken.new(ident_or_tags: ident_or_tags, code: code, lineno: lineno)
def add_error_token(ident_or_tags:, token_code:, lineno:)
@error_tokens << ErrorToken.new(ident_or_tags: ident_or_tags, token_code: token_code, lineno: lineno)
end

def add_term(id:, alias_name: nil, tag: nil, token_id: nil, replace: false)
Expand Down
5 changes: 4 additions & 1 deletion lib/lrama/grammar/code/printer_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module Lrama
class Grammar
class Code
class PrinterCode < Code
attr_accessor :tag
def initialize(type: nil, token_code: nil, tag: nil)
super(type: type, token_code: token_code)
@tag = tag
end

private

Expand Down
5 changes: 2 additions & 3 deletions lib/lrama/grammar/error_token.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module Lrama
class Grammar
class ErrorToken < Struct.new(:ident_or_tags, :code, :lineno, keyword_init: true)
class ErrorToken < Struct.new(:ident_or_tags, :token_code, :lineno, keyword_init: true)
def translated_code(tag)
code.tag = tag
code.translated_code
Code::PrinterCode.new(type: :error_token, token_code: token_code, tag: tag).translated_code
end
end
end
Expand Down
5 changes: 2 additions & 3 deletions lib/lrama/grammar/printer.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module Lrama
class Grammar
class Printer < Struct.new(:ident_or_tags, :code, :lineno, keyword_init: true)
class Printer < Struct.new(:ident_or_tags, :token_code, :lineno, keyword_init: true)
def translated_code(tag)
code.tag = tag
code.translated_code
Code::PrinterCode.new(type: :printer, token_code: token_code, tag: tag).translated_code
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/lrama/parser.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ rule
{
@grammar.add_printer(
ident_or_tags: val[6],
code: Grammar::Code::PrinterCode.new(type: :printer, token_code: val[3]),
token_code: val[3],
lineno: val[3].line
)
}
Expand All @@ -124,7 +124,7 @@ rule
{
@grammar.add_error_token(
ident_or_tags: val[6],
code: Grammar::Code::PrinterCode.new(type: :error_token, token_code: val[3]),
token_code: val[3],
lineno: val[3].line
)
}
Expand Down
12 changes: 4 additions & 8 deletions spec/lrama/grammar/code_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,22 @@
let(:tag) { token_class::Tag.new(s_value: '<val>') }

it "translats '$$' to '((*yyvaluep).val)'" do
code = described_class.new(type: :printer, token_code: user_code_dollar_dollar)
code.tag = tag
code = described_class.new(type: :printer, token_code: user_code_dollar_dollar, tag: tag)
expect(code.translated_code).to eq("print(((*yyvaluep).val));")
end

it "translats '@$' to '(*yylocationp)'" do
code = described_class.new(type: :printer, token_code: user_code_at_dollar)
code.tag = tag
code = described_class.new(type: :printer, token_code: user_code_at_dollar, tag: tag)
expect(code.translated_code).to eq("print((*yylocationp));")
end

it "raises error for '$n'" do
code = described_class.new(type: :printer, token_code: user_code_dollar_n)
code.tag = tag
code = described_class.new(type: :printer, token_code: user_code_dollar_n, tag: tag)
expect { code.translated_code }.to raise_error("$n can not be used in printer.")
end

it "raises error for '@n'" do
code = described_class.new(type: :printer, token_code: user_code_at_n)
code.tag = tag
code = described_class.new(type: :printer, token_code: user_code_at_n, tag: tag)
expect { code.translated_code }.to raise_error("@n can not be used in printer.")
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/lrama/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@
expect(grammar.printers).to eq([
Printer.new(
ident_or_tags: [T::Tag.new(s_value: "<int>")],
code: Code::PrinterCode.new(type: :printer, token_code: T::UserCode.new(s_value: "\n print_int();\n")),
token_code: T::UserCode.new(s_value: "\n print_int();\n"),
lineno: 15
),
Printer.new(
ident_or_tags: [T::Ident.new(s_value: "tNUMBER"), T::Ident.new(s_value: "tSTRING")],
code: Code::PrinterCode.new(type: :printer, token_code: T::UserCode.new(s_value: "\n print_token();\n")),
token_code: T::UserCode.new(s_value: "\n print_token();\n"),
lineno: 18
),
])
Expand Down

0 comments on commit 0eb8095

Please sign in to comment.