File: factory.py

package info (click to toggle)
python-yaql 2.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,048 kB
  • sloc: python: 7,765; sh: 25; makefile: 19
file content (247 lines) | stat: -rw-r--r-- 9,062 bytes parent folder | download | duplicates (4)
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
#    Copyright (c) 2015 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 collections
import re
import uuid

from ply import lex
from ply import yacc

from yaql.language import exceptions
from yaql.language import expressions
from yaql.language import lexer
from yaql.language import parser
from yaql.language import utils


OperatorType = collections.namedtuple('OperatorType', [
    'PREFIX_UNARY', 'SUFFIX_UNARY',
    'BINARY_LEFT_ASSOCIATIVE', 'BINARY_RIGHT_ASSOCIATIVE',
    'NAME_VALUE_PAIR'
])(
    PREFIX_UNARY='PREFIX_UNARY',
    SUFFIX_UNARY='SUFFIX_UNARY',
    BINARY_LEFT_ASSOCIATIVE='BINARY_LEFT_ASSOCIATIVE',
    BINARY_RIGHT_ASSOCIATIVE='BINARY_RIGHT_ASSOCIATIVE',
    NAME_VALUE_PAIR='NAME_VALUE_PAIR'
)


class YaqlOperators(object):
    def __init__(self, operators, name_value_op=None):
        self.operators = operators
        self.name_value_op = name_value_op


class YaqlEngine(object):
    def __init__(self, ply_lexer, ply_parser, options, factory):
        self._lexer = ply_lexer
        self._parser = ply_parser
        self._options = utils.FrozenDict(options or {})
        self._factory = factory

    @property
    def lexer(self):
        return self._lexer

    @property
    def parser(self):
        return self._parser

    @property
    def options(self):
        return self._options

    @property
    def factory(self):
        return self._factory

    def __call__(self, expression, options=None):
        if options:
            return self.copy(options)(expression)

        return expressions.Statement(
            self.parser.parse(expression, lexer=self.lexer), self)

    def copy(self, options):
        opt = dict(self._options)
        opt.update(options)
        return YaqlEngine(self._lexer, self._parser, opt, self._factory)


class YaqlFactory(object):
    def __init__(self, keyword_operator='=>', allow_delegates=False):
        self._keyword_operator = keyword_operator
        self._allow_delegates = allow_delegates
        self.operators = self._standard_operators()
        if keyword_operator:
            self.operators.insert(0, (keyword_operator,
                                      OperatorType.NAME_VALUE_PAIR))

    @property
    def keyword_operator(self):
        return self._keyword_operator

    @property
    def allow_delegates(self):
        return self._allow_delegates

    # noinspection PyMethodMayBeStatic
    def _standard_operators(self):
        return [
            ('.', OperatorType.BINARY_LEFT_ASSOCIATIVE),
            ('?.', OperatorType.BINARY_LEFT_ASSOCIATIVE),
            (),
            ('[]', OperatorType.BINARY_LEFT_ASSOCIATIVE),
            ('{}', OperatorType.BINARY_LEFT_ASSOCIATIVE),
            (),
            ('+', OperatorType.PREFIX_UNARY),
            ('-', OperatorType.PREFIX_UNARY),
            (),
            ('=~', OperatorType.BINARY_LEFT_ASSOCIATIVE),
            ('!~', OperatorType.BINARY_LEFT_ASSOCIATIVE),
            (),
            ('*', OperatorType.BINARY_LEFT_ASSOCIATIVE),
            ('/', OperatorType.BINARY_LEFT_ASSOCIATIVE),
            ('mod', OperatorType.BINARY_LEFT_ASSOCIATIVE),
            (),
            ('+', OperatorType.BINARY_LEFT_ASSOCIATIVE),
            ('-', OperatorType.BINARY_LEFT_ASSOCIATIVE),
            (),
            ('>', OperatorType.BINARY_LEFT_ASSOCIATIVE),
            ('<', OperatorType.BINARY_LEFT_ASSOCIATIVE),
            ('>=', OperatorType.BINARY_LEFT_ASSOCIATIVE),
            ('<=', OperatorType.BINARY_LEFT_ASSOCIATIVE),
            ('!=', OperatorType.BINARY_LEFT_ASSOCIATIVE, 'not_equal'),
            ('=', OperatorType.BINARY_LEFT_ASSOCIATIVE, 'equal'),
            ('in', OperatorType.BINARY_LEFT_ASSOCIATIVE),
            (),
            ('not', OperatorType.PREFIX_UNARY),
            (),
            ('and', OperatorType.BINARY_LEFT_ASSOCIATIVE),
            (),
            ('or', OperatorType.BINARY_LEFT_ASSOCIATIVE),
            (),
            ('->', OperatorType.BINARY_RIGHT_ASSOCIATIVE),
        ]

    def insert_operator(self, existing_operator, existing_operator_binary,
                        new_operator, new_operator_type, create_group,
                        new_operator_alias=None):
        binary_types = (OperatorType.BINARY_RIGHT_ASSOCIATIVE,
                        OperatorType.BINARY_LEFT_ASSOCIATIVE)
        unary_types = (OperatorType.PREFIX_UNARY, OperatorType.SUFFIX_UNARY)
        position = 0
        if existing_operator is not None:
            position = -1
            for i, t in enumerate(self.operators):
                if len(t) < 2 or t[0] != existing_operator:
                    continue
                if existing_operator_binary and t[1] not in binary_types:
                    continue
                if not existing_operator_binary and t[1] not in unary_types:
                    continue
                position = i
                break
            if position < 0:
                raise ValueError('Operator {0} is not found'.format(
                    existing_operator))
            while position < len(self.operators) and len(
                    self.operators[position]) > 1:
                position += 1
        if create_group:
            if position == len(self.operators):
                self.operators.append(())
                position += 1
            else:
                while position < len(self.operators) and len(
                        self.operators[position]) < 2:
                    position += 1
                self.operators.insert(position, ())
        self.operators.insert(
            position, (new_operator, new_operator_type, new_operator_alias))

    @staticmethod
    def _name_generator():
        value = 1
        while True:
            t = value
            chars = []
            while t:
                chars.append(chr(ord('A') + t % 26))
                t //= 26
            yield ''.join(chars)
            value += 1

    def _build_operator_table(self, name_generator):
        operators = {}
        name_value_op = None
        precedence = 1
        for record in self.operators:
            if not record:
                precedence += 1
                continue
            up, bp, name, alias = operators.get(record[0], (0, 0, '', None))
            if record[1] == OperatorType.NAME_VALUE_PAIR:
                if name_value_op is not None:
                    raise exceptions.InvalidOperatorTableException(record[0])
                name_value_op = record[0]
                continue
            if record[1] == OperatorType.PREFIX_UNARY:
                if up:
                    raise exceptions.InvalidOperatorTableException(record[0])
                up = precedence
            elif record[1] == OperatorType.SUFFIX_UNARY:
                if up:
                    raise exceptions.InvalidOperatorTableException(record[0])
                up = -precedence
            elif record[1] == OperatorType.BINARY_LEFT_ASSOCIATIVE:
                if bp:
                    raise exceptions.InvalidOperatorTableException(record[0])
                bp = precedence
            elif record[1] == OperatorType.BINARY_RIGHT_ASSOCIATIVE:
                if bp:
                    raise exceptions.InvalidOperatorTableException(record[0])
                bp = -precedence
            if record[0] == '[]':
                name = 'INDEXER'
            elif record[0] == '{}':
                name = 'MAP'
            else:
                name = name or 'OP_' + next(name_generator)
            operators[record[0]] = (
                up, bp, name, record[2] if len(record) > 2 else None)
        return YaqlOperators(operators, name_value_op)

    # noinspection PyMethodMayBeStatic
    def _create_lexer(self, operators):
        return lexer.Lexer(operators)

    # noinspection PyMethodMayBeStatic
    def _create_parser(self, lexer_rules, operators):
        return parser.Parser(lexer_rules, operators, self)

    def create(self, options=None):
        names = self._name_generator()
        operators = self._build_operator_table(names)
        lexer_rules = self._create_lexer(operators)
        ply_lexer = lex.lex(object=lexer_rules,
                            reflags=re.UNICODE | re.VERBOSE)
        ply_parser = yacc.yacc(
            module=self._create_parser(lexer_rules, operators),
            debug=False if not options else options.get('yaql.debug', False),
            tabmodule='m' + uuid.uuid4().hex, write_tables=False)

        return YaqlEngine(ply_lexer, ply_parser, options, self)