lib/rly/parse/rule_parser.rb in rly-0.2.0 vs lib/rly/parse/rule_parser.rb in rly-0.2.1
- old
+ new
@@ -6,10 +6,11 @@
class RuleParser < Yacc
def self.lexer_class
return @lexer_class if @lexer_class
@lexer_class = Class.new(Lex) do
+ token :PREC, /\%prec/
token :ID, /[a-zA-Z_][a-zA-Z_0-9]*/
token :LITERAL, /"."|'.'/ do |t|
t.value = t.value[1]
t
end
@@ -23,13 +24,23 @@
def grammar
return @grammar if @grammar
@grammar = Grammar.new(self.class.lexer_class.terminals)
+ @grammar.add_production(:grammar_def, [:grammar]) do |d, g|
+ d.value = g.value
+ end
+ @grammar.add_production(:grammar, [:ID, ':', :rules, :PREC, :ID]) do |g, pname, _, r, _, prec|
+ productions = []
+ r.value.each do |p|
+ productions << [pname.value.to_sym, p, prec.value.to_sym]
+ end
+ g.value = productions
+ end
@grammar.add_production(:grammar, [:ID, ':', :rules]) do |g, pname, _, r|
productions = []
r.value.each do |p|
- productions << [pname.value.to_sym, p]
+ productions << [pname.value.to_sym, p, nil]
end
g.value = productions
end
@grammar.add_production(:rules, [:rule, '|', :rules]) do |rls, r, _, rl|
rls.value = [r.value] + rl.value