Class: Platform::IEL::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/introhive_expression_language/iel/parser.rb

Class Method Summary collapse

Class Method Details

.apply_transforms(node) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/introhive_expression_language/iel/parser.rb', line 10

def self.apply_transforms(node)
  if node.kind == :list
    # recursively apply first
    raise EvaluationError.new("Node can't end with trailing quote", node) if node.value[-1].to_s == "'" #trailing quote
    node.value.each { |child| apply_transforms(child) }
    # look for the quote pattern in the nodes
    loop do
      match, index = node.value.each_with_index.find { |child, index| child.kind == :symbol && child.value == "'" }
      break if match.nil?
      if index + 1 < node.value.size # There's a node following the quote
        # Replace the two nodes with a single one
        after_match = node.value.delete_at(index + 1)
        node.value[index] = SexpParser::Node.list([SexpParser::Node.symbol('quote', match.source), after_match], match.source)
      end
    end
  end
end

.parse(expr) ⇒ Object



4
5
6
7
8
# File 'lib/introhive_expression_language/iel/parser.rb', line 4

def self.parse(expr)
  ast = SexpParser.parse(expr)
  apply_transforms(ast)
  ast
end