Class: Dentaku::Parser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokens) ⇒ Parser

Returns a new instance of Parser.



7
8
9
10
11
12
# File 'lib/dentaku/parser.rb', line 7

def initialize(tokens)
  @input      = tokens.dup
  @output     = []
  @operations = []
  @arities    = []
end

Instance Attribute Details

#aritiesObject (readonly)

Returns the value of attribute arities.



5
6
7
# File 'lib/dentaku/parser.rb', line 5

def arities
  @arities
end

#inputObject (readonly)

Returns the value of attribute input.



5
6
7
# File 'lib/dentaku/parser.rb', line 5

def input
  @input
end

#operationsObject (readonly)

Returns the value of attribute operations.



5
6
7
# File 'lib/dentaku/parser.rb', line 5

def operations
  @operations
end

#outputObject (readonly)

Returns the value of attribute output.



5
6
7
# File 'lib/dentaku/parser.rb', line 5

def output
  @output
end

Instance Method Details

#consume(count = 2) ⇒ Object



18
19
20
21
# File 'lib/dentaku/parser.rb', line 18

def consume(count=2)
  operator = operations.pop
  output.push operator.new(*get_args(operator.arity || count))
end

#function(token) ⇒ Object



130
131
132
# File 'lib/dentaku/parser.rb', line 130

def function(token)
  Dentaku::AST::Function.get(token.value)
end

#get_args(count) ⇒ Object



14
15
16
# File 'lib/dentaku/parser.rb', line 14

def get_args(count)
  Array.new(count) { output.pop }.reverse
end

#operation(token) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/dentaku/parser.rb', line 109

def operation(token)
  {
    add:      AST::Addition,
    subtract: AST::Subtraction,
    multiply: AST::Multiplication,
    divide:   AST::Division,
    pow:      AST::Exponentiation,
    negate:   AST::Negation,

    lt:       AST::LessThan,
    gt:       AST::GreaterThan,
    le:       AST::LessThanOrEqual,
    ge:       AST::GreaterThanOrEqual,
    ne:       AST::NotEqual,
    eq:       AST::Equal,

    and:      AST::And,
    or:       AST::Or,
  }.fetch(token.value)
end

#parseObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/dentaku/parser.rb', line 23

def parse
  return AST::Nil.new if input.empty?

  while token = input.shift
    case token.category
    when :numeric
      output.push AST::Numeric.new(token)

    when :logical
      output.push AST::Logical.new(token)

    when :string
      output.push AST::String.new(token)

    when :identifier
      output.push AST::Identifier.new(token)

    when :operator, :comparator, :combinator
      op_class = operation(token)

      if op_class.right_associative?
        while operations.last && operations.last < AST::Operation && op_class.precedence < operations.last.precedence
          consume
        end

        operations.push op_class
      else
        while operations.last && operations.last < AST::Operation && op_class.precedence <= operations.last.precedence
          consume
        end

        operations.push op_class
      end

    when :function
      arities.push 0
      operations.push function(token)

    when :grouping
      case token.value
      when :open, :fopen
        if input.first && input.first.value == :close
          input.shift
          consume(0)
        else
          operations.push AST::Grouping
        end

      when :close
        while operations.any? && operations.last != AST::Grouping
          consume
        end

        lparen = operations.pop
        fail "Unbalanced parenthesis" unless lparen == AST::Grouping

        if operations.last && operations.last < AST::Function
          consume(arities.pop.succ)
        end

      when :comma
        arities[-1] += 1
        while operations.any? && operations.last != AST::Grouping
          consume
        end

      else
        fail "Unknown grouping token #{ token.value }"
      end

    else
      fail "Not implemented for tokens of category #{ token.category }"
    end
  end

  while operations.any?
    consume
  end

  unless output.count == 1
    fail "Parse error"
  end

  output.first
end