Source

htsql-charset-option / src / htsql / tr / syntax.py

Full commit
  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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#
# Copyright (c) 2006-2010, Prometheus Research, LLC
# Authors: Clark C. Evans <cce@clarkevans.com>,
#          Kirill Simonov <xi@resolvent.net>
#


"""
:mod:`htsql.tr.syntax`
======================

This module defines syntax nodes for the HTSQL grammar.
"""


from ..mark import Mark
from ..util import maybe, listof
import re


class Syntax(object):
    """
    Represents a syntax node.

    `mark` (:class:`htsql.mark.Mark`)
        The location of the node in the original query.
    """

    # The pattern to %-escape unsafe characters.
    escape_pattern = r"[\x00-\x1F%\x7F]"
    escape_regexp = re.compile(escape_pattern)
    escape_replace = (lambda m: "%%%02X" % ord(m.group()))

    def __init__(self, mark):
        assert isinstance(mark, Mark)
        self.mark = mark

    def __str__(self):
        """
        Returns an HTSQL expression that would produce the same syntax node.
        """
        # Override when subclassing.
        raise NotImplementedError()

    def __repr__(self):
        return "<%s %s>" % (self.__class__.__name__, self)


class QuerySyntax(Syntax):
    """
    Represents an HTSQL query.

    An HTSQL query consists of a segment expression.

    `segment` (:class:`SegmentSyntax` or ``None``)
        The segment expression.
    """

    def __init__(self, segment, mark):
        assert isinstance(segment, maybe(SegmentSyntax))

        super(QuerySyntax, self).__init__(mark)
        self.segment = segment

    def __str__(self):
        # Generate an HTSQL query corresponding to the node.
        # Note that since the format identifier could split the segment
        # expression between the selector and the filter, we have to
        # serialize parts of the segment expression separately.
        chunks = []
        chunks.append('/')
        if self.segment is not None:
            chunks.append(str(self.segment))
        return ''.join(chunks)


class SegmentSyntax(Syntax):
    """
    Represents a segment expression.

    A segment expression consists of the base expression, the selector,
    and the filter::

        /base{selector}?filter

    `base` (:class:`Syntax` or ``None``)
        The base expression.

    `selector` (:class:`SelectorSyntax` or ``None``)
        The selector.

    `filter` (:class:`Syntax` or ``None``)
        The filter expression.
    """

    def __init__(self, base, selector, filter, mark):
        assert isinstance(base, maybe(Syntax))
        assert isinstance(selector, maybe(SelectorSyntax))
        assert isinstance(filter, maybe(Syntax))

        super(SegmentSyntax, self).__init__(mark)
        self.base = base
        self.selector = selector
        self.filter = filter

    def __str__(self):
        # Generate an HTSQL expression of the form:
        #   base{selector}?filter
        chunks = []
        if self.base is not None:
            chunks.append(str(self.base))
        if self.selector is not None:
            chunks.append(str(self.selector))
        if self.filter is not None:
            chunks.append('?')
            chunks.append(str(self.filter))
        return ''.join(chunks)


class SelectorSyntax(Syntax):
    """
    Represents a selector expression.

    A selector is a sequence of elements::

        {element, ...}

    `elements` (a list of :class:`Syntax`)
        The list of selector elements.
    """

    def __init__(self, elements, mark):
        assert isinstance(elements, listof(Syntax))

        super(SelectorSyntax, self).__init__(mark)
        self.elements = elements

    def __str__(self):
        # Generate an expression of the form:
        #   {element,...}
        return '{%s}' % ','.join(str(element)
                                 for element in self.elements)


class SieveSyntax(Syntax):
    """
    Represents a sieve expression.

    A sieve expression has the same shape as the segment expression.
    It consists of the base, the selector and the filter::

        base{selector}?filter

    `base` (:class:`Syntax`)
        The sieve base expression.

    `selector` (:class:`SelectorSyntax` or ``None``)
        The sieve selector.

    `filter` (:class:`Syntax` or ``None``)
        The sieve filter expression.
    """

    def __init__(self, base, selector, filter, mark):
        assert isinstance(base, Syntax)
        assert isinstance(selector, maybe(SelectorSyntax))
        assert isinstance(filter, maybe(Syntax))
        assert selector is not None or filter is not None

        super(SieveSyntax, self).__init__(mark)
        self.base = base
        self.selector = selector
        self.filter = filter

    def __str__(self):
        # Generate an expression of the form:
        #   base{selector}?filter
        chunks = []
        chunks.append(str(self.base))
        if self.selector is not None:
            chunks.append(str(self.selector))
        if self.filter is not None:
            chunks.append('?')
            chunks.append(str(self.filter))
        return ''.join(chunks)


class OperatorSyntax(Syntax):
    """
    Represents an operator expression.

    An operator expression has one of the following forms::

        left <symbol> right
        left <symbol>
        <symbol> right

    `symbol` (a string)
        The operator.

    `left` (:class:`Syntax` or ``None``)
        The left argument.

    `right` (:class:`Syntax` or ``None``)
        The right argument.
    """

    def __init__(self, symbol, left, right, mark):
        assert isinstance(symbol, str)
        assert isinstance(left, maybe(Syntax))
        assert isinstance(right, maybe(Syntax))
        assert left is not None or right is not None
        super(OperatorSyntax, self).__init__(mark)
        self.symbol = symbol
        self.left = left
        self.right = right

    def __str__(self):
        # Generate an expression of the form:
        #   left<symbol>right
        chunks = []
        if self.left is not None:
            chunks.append(str(self.left))
        chunks.append(self.symbol)
        if self.right is not None:
            chunks.append(str(self.right))
        return ''.join(chunks)


class FunctionOperatorSyntax(Syntax):
    """
    Represents a binary function call in the operator form.

    This expression has the form::

        left identifier right

    and is equivalent to the expression::

        identifier(left, right)

    `identifier` (:class:`IdentifierSyntax`)
        The function name.

    `left` (:class:`Syntax`)
        The first argument.

    `right` (:class:`Syntax`)
        The second argument.
    """

    def __init__(self, identifier, left, right, mark):
        assert isinstance(identifier, IdentifierSyntax)
        assert isinstance(left, Syntax)
        assert isinstance(right, Syntax)

        super(FunctionOperatorSyntax, self).__init__(mark)
        self.identifier = identifier
        self.left = left
        self.right = right

    def __str__(self):
        # Generate an expression of the form:
        #   left identifier right
        chunks = []
        if self.left is not None:
            chunks.append(str(self.left))
            chunks.append(' ')
        chunks.append(str(self.identifier))
        if self.right is not None:
            chunks.append(' ')
            chunks.append(str(self.right))
        return ''.join(chunks)


class FunctionCallSyntax(Syntax):
    """
    Represents a function or a method call.

    This expression has one of the forms::

        identifier(arguments)
        base.identifier(arguments)

    `base` (:class:`Syntax` or ``None``)
        The method base.

    `identifier` (:class:`IdentifierSyntax`)
        The function name.

    `arguments` (a list of :class:`Syntax`)
        The list of arguments.
    """

    def __init__(self, base, identifier, arguments, mark):
        assert isinstance(base, maybe(Syntax))
        assert isinstance(identifier, IdentifierSyntax)
        assert isinstance(arguments, listof(Syntax))

        super(FunctionCallSyntax, self).__init__(mark)
        self.base = base
        self.identifier = identifier
        self.arguments = arguments

    def __str__(self):
        # Generate an expression of the form:
        #   base.identifier(arguments)
        chunks = []
        if self.base is not None:
            chunks.append(str(self.base))
            chunks.append('.')
        chunks.append(str(self.identifier))
        chunks.append('(%s)' % ','.join(str(argument)
                                        for argument in self.arguments))
        return ''.join(chunks)


class GroupSyntax(Syntax):
    """
    Represents an expression in parentheses.

    `expression` (:class:`Syntax`)
        The expression.
    """
    # We keep the parentheses in the syntax tree to ease the reverse
    # translation from the syntax tree to an HTSQL query.

    def __init__(self, expression, mark):
        assert isinstance(expression, Syntax)

        super(GroupSyntax, self).__init__(mark)
        self.expression = expression

    def __str__(self):
        return '(%s)' % self.expression


class SpecifierSyntax(Syntax):
    """
    Represents a specifier expression.

    A specifier expression has one of the forms::

        base.identifier
        base.*

    `base` (:class:`Syntax`)
        The specifier base.

    `identifier` (:class:`IdentifierSyntax` or :class:`WildcardSyntax`)
        The specifier identifier.
    """

    def __init__(self, base, identifier, mark):
        assert isinstance(base, Syntax)
        assert isinstance(identifier, (IdentifierSyntax, WildcardSyntax))

        super(SpecifierSyntax, self).__init__(mark)
        self.base = base
        self.identifier = identifier

    def __str__(self):
        return '%s.%s' % (self.base, self.identifier)


class IdentifierSyntax(Syntax):
    """
    Represents an identifier.

    `value` (a string)
        The identifier.
    """

    def __init__(self, value, mark):
        assert isinstance(value, str)
        super(IdentifierSyntax, self).__init__(mark)

        self.value = value

    def __str__(self):
        return self.value


class WildcardSyntax(Syntax):
    """
    Represents a wildcard.
    """

    def __str__(self):
        return '*'


class LiteralSyntax(Syntax):
    """
    Represents a literal expression.

    This is an abstract class with subclasses :class:`StringSyntax` and
    :class:`NumberSyntax`.

    `value` (a string)
        The value.
    """

    def __init__(self, value, mark):
        assert isinstance(value, str)
        super(LiteralSyntax, self).__init__(mark)
        self.value = value


class StringSyntax(LiteralSyntax):
    """
    Represents a string literal.
    """

    def __str__(self):
        # Quote and %-encode the value.
        value = '\'%s\'' % self.value.replace('\'', '\'\'')
        value = self.escape_regexp.sub(self.escape_replace, value)
        return value


class NumberSyntax(LiteralSyntax):
    """
    Represents a number literal.
    """

    def __str__(self):
        return self.value