Method: Function#parse

Defined in:
lib/yodel/models/core/functions/function.rb

#parse(tokens) ⇒ Object



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
# File 'lib/yodel/models/core/functions/function.rb', line 48

def parse(tokens)
  params = false
  chain  = false
  hash   = false
  instructions = []

  until tokens.empty?
    token = tokens.shift
    case token[0]
    when CALL_TOKEN
      chain = true
    when START_PARAMS_TOKEN
      params = true
      instructions += parse(tokens)
    when END_PARAMS_TOKEN
      tokens.unshift(END_PARAMS_TOKEN) unless params
      break
    when ENTRY_FLAG
      hash = true
      instructions << ['entry'] + parse(tokens)
    when START_HASH_TOKEN
      tokens.unshift(ENTRY_FLAG)
      instructions << ['hash'] + parse(tokens)
    when END_HASH_TOKEN
      tokens.unshift(END_HASH_TOKEN) if hash
      break
    when HASH_DELIM_TOKEN
      instructions += parse(tokens)
    when PARAM_DELIM_TOKEN
      if params || hash
        tokens.unshift(ENTRY_FLAG) if hash
        instructions += parse(tokens)
      else
        tokens.unshift(PARAM_DELIM_TOKEN)
        break
      end
    when DOUBLE_QUOTE_TOKEN, SINGLE_QUOTE_TOKEN
      instructions << ['string', token[1...-1]]
    else
      if tokens.first == START_PARAMS_TOKEN
        instructions << [token] + parse(tokens)
      elsif token.to_i.to_s == token
        instructions << ['int', token]
      else
        instructions << ['field', token]
      end
    end
  end

  if chain
    [['chain'] + instructions]
  else
    instructions
  end
end