Skip to content

Commit

Permalink
Move lhs_tag from RuleBuilder to InstantiateRule
Browse files Browse the repository at this point in the history
This tag specifies not type of lhs of the rule
but type of generated rule lhs. Therefore change it to be
managed by InstantiateRule.
  • Loading branch information
yui-knk committed Dec 26, 2023
1 parent ce902a6 commit b50ac2b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 33 deletions.
12 changes: 6 additions & 6 deletions lib/lrama/grammar/rule_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
module Lrama
class Grammar
class RuleBuilder
attr_accessor :lhs, :lhs_tag, :line
attr_reader :rhs, :user_code, :precedence_sym
attr_accessor :lhs, :line
attr_reader :lhs_tag, :rhs, :user_code, :precedence_sym

def initialize(rule_counter, midrule_action_counter, position_in_original_rule_rhs = nil, skip_preprocess_references: false)
def initialize(rule_counter, midrule_action_counter, position_in_original_rule_rhs = nil, lhs_tag: nil, skip_preprocess_references: false)
@rule_counter = rule_counter
@midrule_action_counter = midrule_action_counter
@position_in_original_rule_rhs = position_in_original_rule_rhs
@skip_preprocess_references = skip_preprocess_references

@lhs = nil
@lhs_tag = lhs_tag
@rhs = []
@lhs_tag = nil
@user_code = nil
@precedence_sym = nil
@line = nil
Expand Down Expand Up @@ -103,12 +103,12 @@ def process_rhs(parameterizing_resolver)
@replaced_rhs << token
when Lrama::Lexer::Token::InstantiateRule
if parameterizing_resolver.defined?(token.rule_name)
parameterizing = parameterizing_resolver.build_rules(token, @rule_counter, @lhs_tag, line)
parameterizing = parameterizing_resolver.build_rules(token, @rule_counter, token.lhs_tag, line)
@parameterizing_rules = @parameterizing_rules + parameterizing.map(&:rules).flatten
@replaced_rhs = @replaced_rhs + parameterizing.map(&:token).flatten.uniq
else
# TODO: Delete when the standard library will defined as a grammar file.
parameterizing = ParameterizingRules::Builder.new(token, @rule_counter, @lhs_tag, user_code, precedence_sym, line)
parameterizing = ParameterizingRules::Builder.new(token, @rule_counter, token.lhs_tag, user_code, precedence_sym, line)
@parameterizing_rules = @parameterizing_rules + parameterizing.build
@replaced_rhs << parameterizing.build_token
end
Expand Down
5 changes: 3 additions & 2 deletions lib/lrama/lexer/token/instantiate_rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ module Lrama
class Lexer
class Token
class InstantiateRule < Token
attr_accessor :args
attr_accessor :args, :lhs_tag

def initialize(s_value:, alias_name: nil, location: nil, args: [])
def initialize(s_value:, alias_name: nil, location: nil, args: [], lhs_tag: nil)
super s_value: s_value, alias_name: alias_name, location: location
@args = args
@lhs_tag = lhs_tag
end

def rule_name
Expand Down
34 changes: 16 additions & 18 deletions lib/lrama/parser.rb

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

6 changes: 2 additions & 4 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -408,19 +408,17 @@ rule
}
| rhs symbol parameterizing_suffix tag_opt
{
token = Lrama::Lexer::Token::InstantiateRule.new(s_value: val[2], location: @lexer.location, args: [val[1]])
token = Lrama::Lexer::Token::InstantiateRule.new(s_value: val[2], location: @lexer.location, args: [val[1]], lhs_tag: val[3])
builder = val[0]
builder.add_rhs(token)
builder.lhs_tag = val[3]
builder.line = val[1].first_line
result = builder
}
| rhs IDENTIFIER "(" parameterizing_args ")" tag_opt
{
token = Lrama::Lexer::Token::InstantiateRule.new(s_value: val[1].s_value, location: @lexer.location, args: val[3])
token = Lrama::Lexer::Token::InstantiateRule.new(s_value: val[1].s_value, location: @lexer.location, args: val[3], lhs_tag: val[5])
builder = val[0]
builder.add_rhs(token)
builder.lhs_tag = val[5]
builder.line = val[1].first_line
result = builder
}
Expand Down
4 changes: 2 additions & 2 deletions sig/lrama/grammar/rule_builder.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module Lrama
class Grammar
class RuleBuilder
attr_accessor lhs: Lexer::Token
attr_accessor lhs_tag: untyped
attr_accessor line: Integer?
attr_reader lhs_tag: Lexer::Token::Tag?
attr_reader rhs: Array[Lexer::Token]
attr_reader user_code: Lexer::Token::UserCode?
attr_reader precedence_sym: Lexer::Token?
Expand All @@ -19,7 +19,7 @@ module Lrama
@parameterizing_rules: Array[Rule]
@midrule_action_rules: Array[Rule]

def initialize: (Counter rule_counter, Counter midrule_action_counter, ?Integer position_in_original_rule_rhs, ?skip_preprocess_references: bool) -> void
def initialize: (Counter rule_counter, Counter midrule_action_counter, ?Integer position_in_original_rule_rhs, ?lhs_tag: Lexer::Token::Tag?, ?skip_preprocess_references: bool) -> void
def add_rhs: (Lexer::Token rhs) -> void
def user_code=: (Lexer::Token::UserCode user_code) -> void
def precedence_sym=: (Lexer::Token user_code) -> void
Expand Down
3 changes: 2 additions & 1 deletion sig/lrama/lexer/token/instantiate_rule.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ module Lrama
class Token
class InstantiateRule < Token
attr_accessor args: Array[Lexer::Token]
attr_accessor lhs_tag: Lexer::Token::Tag?

def initialize: (s_value: String, ?alias_name: String, ?location: Location, ?args: Array[Lexer::Token]) -> void
def initialize: (s_value: String, ?alias_name: String, ?location: Location, ?args: Array[Lexer::Token], ?lhs_tag: Lexer::Token::Tag?) -> void
def rule_name: () -> String
end
end
Expand Down

0 comments on commit b50ac2b

Please sign in to comment.