Skip to content

Commit

Permalink
Cache the lazily instantiated node elements.
Browse files Browse the repository at this point in the history
  • Loading branch information
jgarber committed Aug 2, 2009
1 parent 70dfe22 commit 6cab58a
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/treetop/runtime/syntax_node.rb
Expand Up @@ -8,7 +8,7 @@ def initialize(input, interval, elements = nil)
@input = input
@interval = interval
if @elements = elements
@elements.delete(true)
@comprehensive_elements = @elements unless @elements.delete(true)
@elements.each do |element|
element.parent = self
end
Expand All @@ -17,15 +17,15 @@ def initialize(input, interval, elements = nil)

def elements
return @elements if terminal?
# fill in any gaps in the sequence (lazy instantiation)
interval.each do |index|
# fill in any gaps in the sequence (lazy instantiation) if needed
@comprehensive_elements ||= interval.inject(@elements) do |elements, index|
unless @elements.any? {|element| element.interval.include?(index) }
node = SyntaxNode.new(input, index...(index + 1))
node.parent = self
@elements << node
elements << node
end
elements
end
@elements
end

def terminal?
Expand Down

0 comments on commit 6cab58a

Please sign in to comment.