summaryrefslogtreecommitdiffstats
path: root/third_party/python/json-e/jsone/interpreter.py
blob: eb38a9c85bce911e94a67e3b99a624bd1f7c0e9e (plain)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from __future__ import absolute_import, print_function, unicode_literals

from .prattparser import PrattParser, infix, prefix
from .shared import TemplateError, InterpreterError, string
import operator
import json

OPERATORS = {
    '-': operator.sub,
    '*': operator.mul,
    '/': operator.truediv,
    '**': operator.pow,
    '==': operator.eq,
    '!=': operator.ne,
    '<=': operator.le,
    '<': operator.lt,
    '>': operator.gt,
    '>=': operator.ge,
    '&&': lambda a, b: bool(a and b),
    '||': lambda a, b: bool(a or b),
}


def infixExpectationError(operator, expected):
    return InterpreterError('infix: {} expects {} {} {}'.
                            format(operator, expected, operator, expected))


class ExpressionEvaluator(PrattParser):

    ignore = '\\s+'
    patterns = {
        'number': '[0-9]+(?:\\.[0-9]+)?',
        'identifier': '[a-zA-Z_][a-zA-Z_0-9]*',
        'string': '\'[^\']*\'|"[^"]*"',
        # avoid matching these as prefixes of identifiers e.g., `insinutations`
        'true': 'true(?![a-zA-Z_0-9])',
        'false': 'false(?![a-zA-Z_0-9])',
        'in': 'in(?![a-zA-Z_0-9])',
        'null': 'null(?![a-zA-Z_0-9])',
    }
    tokens = [
        '**', '+', '-', '*', '/', '[', ']', '.', '(', ')', '{', '}', ':', ',',
        '>=', '<=', '<', '>', '==', '!=', '!', '&&', '||', 'true', 'false', 'in',
        'null', 'number', 'identifier', 'string',
    ]
    precedence = [
        ['||'],
        ['&&'],
        ['in'],
        ['==', '!='],
        ['>=', '<=', '<', '>'],
        ['+', '-'],
        ['*', '/'],
        ['**-right-associative'],
        ['**'],
        ['[', '.'],
        ['('],
        ['unary'],
    ]

    def __init__(self, context):
        super(ExpressionEvaluator, self).__init__()
        self.context = context

    def parse(self, expression):
        if not isinstance(expression, string):
            raise TemplateError('expression to be evaluated must be a string')
        return super(ExpressionEvaluator, self).parse(expression)

    @prefix('number')
    def number(self, token, pc):
        v = token.value
        return float(v) if '.' in v else int(v)

    @prefix("!")
    def bang(self, token, pc):
        return not pc.parse('unary')

    @prefix("-")
    def uminus(self, token, pc):
        v = pc.parse('unary')
        if not isNumber(v):
            raise InterpreterError('{} expects {}'.format('unary -', 'number'))
        return -v

    @prefix("+")
    def uplus(self, token, pc):
        v = pc.parse('unary')
        if not isNumber(v):
            raise InterpreterError('{} expects {}'.format('unary +', 'number'))
        return v

    @prefix("identifier")
    def identifier(self, token, pc):
        try:
            return self.context[token.value]
        except KeyError:
            raise InterpreterError(
                'unknown context value {}'.format(token.value))

    @prefix("null")
    def null(self, token, pc):
        return None

    @prefix("[")
    def array_bracket(self, token, pc):
        return parseList(pc, ',', ']')

    @prefix("(")
    def grouping_paren(self, token, pc):
        rv = pc.parse()
        pc.require(')')
        return rv

    @prefix("{")
    def object_brace(self, token, pc):
        return parseObject(pc)

    @prefix("string")
    def string(self, token, pc):
        return parseString(token.value)

    @prefix("true")
    def true(self, token, pc):
        return True

    @prefix("false")
    def false(self, token, ps):
        return False

    @infix("+")
    def plus(self, left, token, pc):
        if not isinstance(left, (string, int, float)) or isinstance(left, bool):
            raise infixExpectationError('+', 'number/string')
        right = pc.parse(token.kind)
        if not isinstance(right, (string, int, float)) or isinstance(right, bool):
            raise infixExpectationError('+', 'number/string')
        if type(right) != type(left) and \
                (isinstance(left, string) or isinstance(right, string)):
            raise infixExpectationError('+', 'numbers/strings')
        return left + right

    @infix('-', '*', '/', '**')
    def arith(self, left, token, pc):
        op = token.kind
        if not isNumber(left):
            raise infixExpectationError(op, 'number')
        right = pc.parse({'**': '**-right-associative'}.get(op))
        if not isNumber(right):
            raise infixExpectationError(op, 'number')
        return OPERATORS[op](left, right)

    @infix("[")
    def index_slice(self, left, token, pc):
        a = None
        b = None
        is_interval = False
        if pc.attempt(':'):
            a = 0
            is_interval = True
        else:
            a = pc.parse()
            if pc.attempt(':'):
                is_interval = True

        if is_interval and not pc.attempt(']'):
            b = pc.parse()
            pc.require(']')

        if not is_interval:
            pc.require(']')

        return accessProperty(left, a, b, is_interval)

    @infix(".")
    def property_dot(self, left, token, pc):
        if not isinstance(left, dict):
            raise infixExpectationError('.', 'object')
        k = pc.require('identifier').value
        try:
            return left[k]
        except KeyError:
            raise TemplateError(
                '{} not found in {}'.format(k, json.dumps(left)))

    @infix("(")
    def function_call(self, left, token, pc):
        if not callable(left):
            raise TemplateError('function call', 'callable')
        args = parseList(pc, ',', ')')
        return left(*args)

    @infix('==', '!=', '||', '&&')
    def equality_and_logic(self, left, token, pc):
        op = token.kind
        right = pc.parse(op)
        return OPERATORS[op](left, right)

    @infix('<=', '<', '>', '>=')
    def inequality(self, left, token, pc):
        op = token.kind
        right = pc.parse(op)
        if type(left) != type(right) or \
                not (isinstance(left, (int, float, string)) and not isinstance(left, bool)):
            raise infixExpectationError(op, 'numbers/strings')
        return OPERATORS[op](left, right)

    @infix("in")
    def contains(self, left, token, pc):
        right = pc.parse(token.kind)
        if isinstance(right, dict):
            if not isinstance(left, string):
                raise infixExpectationError('in-object', 'string on left side')
        elif isinstance(right, string):
            if not isinstance(left, string):
                raise infixExpectationError('in-string', 'string on left side')
        elif not isinstance(right, list):
            raise infixExpectationError(
                'in', 'Array, string, or object on right side')
        try:
            return left in right
        except TypeError:
            raise infixExpectationError('in', 'scalar value, collection')


def isNumber(v):
    return isinstance(v, (int, float)) and not isinstance(v, bool)


def parseString(v):
    return v[1:-1]


def parseList(pc, separator, terminator):
    rv = []
    if not pc.attempt(terminator):
        while True:
            rv.append(pc.parse())
            if not pc.attempt(separator):
                break
        pc.require(terminator)
    return rv


def parseObject(pc):
    rv = {}
    if not pc.attempt('}'):
        while True:
            k = pc.require('identifier', 'string')
            if k.kind == 'string':
                k = parseString(k.value)
            else:
                k = k.value
            pc.require(':')
            v = pc.parse()
            rv[k] = v
            if not pc.attempt(','):
                break
        pc.require('}')
    return rv


def accessProperty(value, a, b, is_interval):
    if isinstance(value, (list, string)):
        if is_interval:
            if b is None:
                b = len(value)
            try:
                return value[a:b]
            except TypeError:
                raise infixExpectationError('[..]', 'integer')
        else:
            try:
                return value[a]
            except IndexError:
                raise TemplateError('index out of bounds')
            except TypeError:
                raise infixExpectationError('[..]', 'integer')

    if not isinstance(value, dict):
        raise infixExpectationError('[..]', 'object, array, or string')
    if not isinstance(a, string):
        raise infixExpectationError('[..]', 'string index')

    try:
        return value[a]
    except KeyError:
        return None