Skip to content

Commit

Permalink
Merge pull request #230 from yui-knk/merge_into_prepare
Browse files Browse the repository at this point in the history
Control grammar preprocess logic in `#prepare`
  • Loading branch information
yui-knk authored Nov 14, 2023
2 parents 0308be0 + c34be03 commit 0c81b8b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 59 deletions.
112 changes: 57 additions & 55 deletions lib/lrama/grammar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ def prepare
fill_symbol_printer
fill_symbol_error_token
@symbols.sort_by!(&:number)
compute_nullable
compute_first_set
end

# TODO: More validation methods
Expand All @@ -167,6 +169,61 @@ def validate!
validate_rule_lhs_is_nterm!
end

def find_symbol_by_s_value(s_value)
@symbols.find do |sym|
sym.id.s_value == s_value
end
end

def find_symbol_by_s_value!(s_value)
find_symbol_by_s_value(s_value) || (raise "Symbol not found: #{s_value}")
end

def find_symbol_by_id(id)
@symbols.find do |sym|
sym.id == id || sym.alias_name == id.s_value
end
end

def find_symbol_by_id!(id)
find_symbol_by_id(id) || (raise "Symbol not found: #{id}")
end

def find_symbol_by_number!(number)
sym = @symbols[number]

raise "Symbol not found: #{number}" unless sym
raise "[BUG] Symbol number mismatch. #{number}, #{sym}" if sym.number != number

sym
end

def find_rules_by_symbol!(sym)
find_rules_by_symbol(sym) || (raise "Rules for #{sym} not found")
end

def find_rules_by_symbol(sym)
@sym_to_rules[sym.number]
end

def terms_count
terms.count
end

def terms
@terms ||= @symbols.select(&:term?)
end

def nterms_count
nterms.count
end

def nterms
@nterms ||= @symbols.select(&:nterm?)
end

private

def compute_nullable
@rules.each do |rule|
case
Expand Down Expand Up @@ -251,61 +308,6 @@ def compute_first_set
end
end

def find_symbol_by_s_value(s_value)
@symbols.find do |sym|
sym.id.s_value == s_value
end
end

def find_symbol_by_s_value!(s_value)
find_symbol_by_s_value(s_value) || (raise "Symbol not found: #{s_value}")
end

def find_symbol_by_id(id)
@symbols.find do |sym|
sym.id == id || sym.alias_name == id.s_value
end
end

def find_symbol_by_id!(id)
find_symbol_by_id(id) || (raise "Symbol not found: #{id}")
end

def find_symbol_by_number!(number)
sym = @symbols[number]

raise "Symbol not found: #{number}" unless sym
raise "[BUG] Symbol number mismatch. #{number}, #{sym}" if sym.number != number

sym
end

def find_rules_by_symbol!(sym)
find_rules_by_symbol(sym) || (raise "Rules for #{sym} not found")
end

def find_rules_by_symbol(sym)
@sym_to_rules[sym.number]
end

def terms_count
terms.count
end

def terms
@terms ||= @symbols.select(&:term?)
end

def nterms_count
nterms.count
end

def nterms
@nterms ||= @symbols.select(&:nterm?)
end

private

def setup_rules
@rule_builders.each do |builder|
builder.setup_rules
Expand Down
2 changes: 0 additions & 2 deletions lib/lrama/parser.rb

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

2 changes: 0 additions & 2 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,6 @@ def parse
reset_precs
do_parse
@grammar.prepare
@grammar.compute_nullable
@grammar.compute_first_set
@grammar.validate!
@grammar
end
Expand Down

0 comments on commit 0c81b8b

Please sign in to comment.