class Mustermann::AST::Transformer::ArrayTransform
Inserts with_look_ahead nodes wherever appropriate @!visibility private
Public Instance Methods
create_lookahead(elements, *args)
click to toggle source
turn look ahead buffer into look ahead node @!visibility private
# File lib/mustermann/ast/transformer.rb, line 142 def create_lookahead(elements, *args) return elements unless elements.size > 1 [Node[:with_look_ahead].new(elements, *args, start: elements.first.start, stop: elements.last.stop)] end
expect_lookahead?(element)
click to toggle source
can the current element deal with a look-ahead? @!visibility private
# File lib/mustermann/ast/transformer.rb, line 166 def expect_lookahead?(element) return element.class == Node[:capture] unless element.is_a? Node[:group] element.payload.all? { |e| expect_lookahead?(e) } end
list_for(element)
click to toggle source
helper method for deciding where to put an element for now @!visibility private
# File lib/mustermann/ast/transformer.rb, line 173 def list_for(element) expect_lookahead?(element) ? lookahead_buffer : payload end
lookahead?(element, in_lookahead = false)
click to toggle source
can the given element be used in a look-ahead? @!visibility private
# File lib/mustermann/ast/transformer.rb, line 149 def lookahead?(element, in_lookahead = false) case element when Node[:char] then in_lookahead when Node[:group] then lookahead_payload?(element.payload, in_lookahead) when Node[:optional] then lookahead?(element.payload, true) or expect_lookahead?(element.payload) end end
lookahead_buffer()
click to toggle source
buffer for potential look ahead @!visibility private
# File lib/mustermann/ast/transformer.rb, line 116 def lookahead_buffer @lookahead_buffer ||= [] end
lookahead_payload?(payload, in_lookahead)
click to toggle source
does the list of elements look look-ahead-ish to you? @!visibility private
# File lib/mustermann/ast/transformer.rb, line 159 def lookahead_payload?(payload, in_lookahead) return unless payload[0..-2].all? { |e| lookahead?(e, in_lookahead) } expect_lookahead?(payload.last) or lookahead?(payload.last, in_lookahead) end
payload()
click to toggle source
the new array @!visibility private
# File lib/mustermann/ast/transformer.rb, line 110 def payload @payload ||= [] end
track(element)
click to toggle source
handle a single element from the array @!visibility private
# File lib/mustermann/ast/transformer.rb, line 129 def track(element) return list_for(element) << element if lookahead_buffer.empty? return lookahead_buffer << element if lookahead? element lookahead = lookahead_buffer.dup lookahead = create_lookahead(lookahead, false) if element.is_a? Node[:separator] lookahead_buffer.clear payload.concat(lookahead) << element end
translate()
click to toggle source
transform the array @!visibility private
# File lib/mustermann/ast/transformer.rb, line 122 def translate each { |e| track t(e) } payload.concat create_lookahead(lookahead_buffer, true) end