Permalink
Browse files

Added Rule#[]

  • Loading branch information...
1 parent 07407e0 commit 1c00634afc5bcd4a1c047c4ba8a01ab2b4174d0d @mjackson committed Nov 4, 2010
Showing with 19 additions and 8 deletions.
  1. +19 −8 lib/citrus.rb
View
@@ -490,20 +490,28 @@ def self.new(obj)
end
@unique_id = 0
+ @rules = {}
- # Generates a new rule id.
- def self.new_id
- @unique_id += 1
+ def self.<<(rule) # :nodoc:
+ rule.id = @unique_id += 1
+ @rules[rule.id] = rule
end
- # The grammar this rule belongs to.
- attr_accessor :grammar
+ # Returns the Rule object with the given +id+.
+ def self.[](id)
+ @rules[id]
+ end
- # An integer id that is unique to this rule.
- def id
- @id ||= Rule.new_id
+ def initialize(*args) # :nodoc:
+ Rule << self
end
+ # An integer id that is unique to this rule.
+ attr_accessor :id
+
+ # The grammar this rule belongs to.
+ attr_accessor :grammar
+
# Sets the name of this rule.
def name=(name)
@name = name.to_sym
@@ -576,6 +584,7 @@ module Proxy
include Rule
def initialize(rule_name='<proxy>')
+ super
self.rule_name = rule_name
end
@@ -665,6 +674,7 @@ class Terminal
include Rule
def initialize(rule=/^/)
+ super
@rule = rule
end
@@ -728,6 +738,7 @@ module Nonterminal
include Rule
def initialize(rules=[])
+ super
@rules = rules.map {|r| Rule.new(r) }
end

0 comments on commit 1c00634

Please sign in to comment.