File: parser.py

package info (click to toggle)
python-yaql 0.2.3-2%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 216 kB
  • ctags: 213
  • sloc: python: 986; makefile: 31; sh: 15
file content (214 lines) | stat: -rw-r--r-- 4,406 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#    Copyright (c) 2013 Mirantis, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import types
import ply.yacc as yacc
import expressions
import exceptions
import lexer
import tempfile


tokens = lexer.tokens


def p_value_to_const(p):
    """
    value : STRING
          | QUOTED_STRING
          | NUMBER
          | TRUE
          | FALSE
          | NULL
    """
    p[0] = expressions.Constant(p[1])


def p_value_to_dollar(p):
    """
    value : DOLLAR
    """
    p[0] = expressions.GetContextValue(expressions.Constant(p[1]))


def p_value_to_symbol(p):
    """
    symbol : SYMBOL
    """
    symbol_parts = p[1].split(':')
    sns = symbol_parts[0]
    name = symbol_parts[1]
    expanding = expressions.UnaryOperator(':', expressions.Constant(sns))

    p[0] = expressions.Function('#validate', None, expanding,
                                expressions.Constant(name))


def p_attribution(p):
    """
    value : value '.' STRING
          | value '.' QUOTED_STRING
    """
    p[0] = expressions.Att(p[1], expressions.Constant(p[3]))


def p_attribution_to_symbol(p):
    """
    value : value '.' symbol
    """
    p[0] = expressions.Att(p[1], p[3])


def p_val_to_function(p):
    """
    value : func
          | symbol
    """
    p[0] = p[1]


def p_method_no_args(p):
    """
    func : value '.' FUNC ')'
    """
    p[0] = expressions.Function(p[3], p[1])


def p_arg_definition(p):
    """
    arg : value
    """
    p[0] = p[1]


def p_method_w_args(p):
    """
    func : value '.' FUNC arg ')'
    """
    if isinstance(p[4], types.ListType):
        arg = p[4]
    else:
        arg = [p[4]]
    p[0] = expressions.Function(p[3], p[1], *arg)


def p_function_no_args(p):
    """
    func : FUNC ')'
    """
    p[0] = expressions.Function(p[1], None)


def p_function_w_args(p):
    """
    func : FUNC arg ')'
    """
    if isinstance(p[2], types.ListType):
        arg = p[2]
    else:
        arg = [p[2]]
    p[0] = expressions.Function(p[1], None, *arg)


def p_arg_list(p):
    """
    arg : arg ',' arg
    """
    val_list = []
    if isinstance(p[1], types.ListType):
        val_list += p[1]
    else:
        val_list.append(p[1])
    if isinstance(p[3], types.ListType):
        val_list += p[3]
    else:
        val_list.append(p[3])

    p[0] = val_list


def p_val_with_binary_op(p):
    """
    value : value '+' value
          | value '-' value
          | value '*' value
          | value '/' value
          | value '>' value
          | value '<' value
          | value '=' value
          | value NE value
          | value LE value
          | value GE value
          | value OR value
          | value AND value
          | value IS value
          | value IN value
    """
    p[0] = expressions.BinaryOperator(p[2], p[1], p[3])


def p_val_with_unary_op(p):
    """
    value : NOT value
    """
    p[0] = expressions.UnaryOperator(p[1], p[2])


def p_val_in_parenthesis(p):
    """
    value : '(' value ')'
    """
    p[0] = expressions.Wrap(p[2])


def p_val_w_filter(p):
    """
    value : value FILTER value ']'
    """
    p[0] = expressions.Filter(p[1], p[3])


def p_val_tuple(p):
    """
    value : value TUPLE value
    """
    p[0] = expressions.Tuple.create_tuple(p[1], p[3])


def p_error(p):
    if p:
        raise exceptions.YaqlGrammarException(p.value, p.lexpos)
    else:
        raise exceptions.YaqlGrammarException(None, None)


precedence = (
    ('left', 'TUPLE'),
    ('left', 'OR'),
    ('left', 'AND'),
    ('left', 'NOT'),
    ('nonassoc', '>', '<', '=', 'NE', 'LE', 'GE', 'IS', 'IN'),
    ('left', '+', '-'),
    ('left', '*', '/'),
    ('left', ','),
    ('left', 'FILTER', ']'),
    ('left', '.'),
    ('left', 'SYMBOL')
)

parser = yacc.yacc(debug=False, outputdir=tempfile.gettempdir(), tabmodule='parser_table')


def parse(expression):
    return parser.parse(expression)