File: jsx_parser.py

package info (click to toggle)
python-esprima 4.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 64,528 kB
  • sloc: javascript: 42,618; python: 5,728; sh: 12; makefile: 3
file content (584 lines) | stat: -rw-r--r-- 19,998 bytes parent folder | download | duplicates (34)
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# -*- coding: utf-8 -*-
# Copyright JS Foundation and other contributors, https://js.foundation/
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#   * Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from __future__ import absolute_import, unicode_literals

from .compat import uchr
from .character import Character
from . import jsx_nodes as JSXNode
from .jsx_syntax import JSXSyntax
from . import nodes as Node
from .parser import Marker, Parser
from .token import Token, TokenName
from .xhtml_entities import XHTMLEntities


class MetaJSXElement(object):
    def __init__(self, node=None, opening=None, closing=None, children=None):
        self.node = node
        self.opening = opening
        self.closing = closing
        self.children = children


class JSXToken(object):
    Identifier = 100
    Text = 101


class RawJSXToken(object):
    def __init__(self, type=None, value=None, lineNumber=None, lineStart=None, start=None, end=None):
        self.type = type
        self.value = value
        self.lineNumber = lineNumber
        self.lineStart = lineStart
        self.start = start
        self.end = end


TokenName[JSXToken.Identifier] = "JSXIdentifier"
TokenName[JSXToken.Text] = "JSXText"


# Fully qualified element name, e.g. <svg:path> returns "svg:path"
def getQualifiedElementName(elementName):
    typ = elementName.type
    if typ is JSXSyntax.JSXIdentifier:
        id = elementName
        qualifiedName = id.name
    elif typ is JSXSyntax.JSXNamespacedName:
        ns = elementName
        qualifiedName = getQualifiedElementName(ns.namespace) + ':' + getQualifiedElementName(ns.name)
    elif typ is JSXSyntax.JSXMemberExpression:
        expr = elementName
        qualifiedName = getQualifiedElementName(expr.object) + '.' + getQualifiedElementName(expr.property)

    return qualifiedName


class JSXParser(Parser):
    def __init__(self, code, options, delegate):
        super(JSXParser, self).__init__(code, options, delegate)

    def parsePrimaryExpression(self):
        return self.parseJSXRoot() if self.match('<') else super(JSXParser, self).parsePrimaryExpression()

    def startJSX(self):
        # Unwind the scanner before the lookahead token.
        self.scanner.index = self.startMarker.index
        self.scanner.lineNumber = self.startMarker.line
        self.scanner.lineStart = self.startMarker.index - self.startMarker.column

    def finishJSX(self):
        # Prime the next lookahead.
        self.nextToken()

    def reenterJSX(self):
        self.startJSX()
        self.expectJSX('}')

        # Pop the closing '}' added from the lookahead.
        if self.config.tokens:
            self.tokens.pop()

    def createJSXNode(self):
        self.collectComments()
        return Marker(
            index=self.scanner.index,
            line=self.scanner.lineNumber,
            column=self.scanner.index - self.scanner.lineStart
        )

    def createJSXChildNode(self):
        return Marker(
            index=self.scanner.index,
            line=self.scanner.lineNumber,
            column=self.scanner.index - self.scanner.lineStart
        )

    def scanXHTMLEntity(self, quote):
        result = '&'

        valid = True
        terminated = False
        numeric = False
        hex = False

        while not self.scanner.eof() and valid and not terminated:
            ch = self.scanner.source[self.scanner.index]
            if ch == quote:
                break

            terminated = (ch == ';')
            result += ch
            self.scanner.index += 1
            if not terminated:
                length = len(result)
                if length == 2:
                    # e.g. '&#123;'
                    numeric = (ch == '#')
                elif length == 3:
                    if numeric:
                        # e.g. '&#x41;'
                        hex = ch == 'x'
                        valid = hex or Character.isDecimalDigit(ch)
                        numeric = numeric and not hex
                else:
                    valid = valid and not (numeric and not Character.isDecimalDigit(ch))
                    valid = valid and not (hex and not Character.isHexDigit(ch))

        if valid and terminated and len(result) > 2:
            # e.g. '&#x41;' becomes just '#x41'
            st = result[1:-1]
            if numeric and len(st) > 1:
                result = uchr(int(st[1:], 10))
            elif hex and len(st) > 2:
                result = uchr(int(st[2:], 16))
            elif not numeric and not hex and st in XHTMLEntities:
                result = XHTMLEntities[st]

        return result

    # Scan the next JSX token. This replaces Scanner#lex when in JSX mode.

    def lexJSX(self):
        ch = self.scanner.source[self.scanner.index]

        # < > / : = { }
        if ch in ('<', '>', '/', ':', '=', '{', '}'):
            value = self.scanner.source[self.scanner.index]
            self.scanner.index += 1
            return RawJSXToken(
                type=Token.Punctuator,
                value=value,
                lineNumber=self.scanner.lineNumber,
                lineStart=self.scanner.lineStart,
                start=self.scanner.index - 1,
                end=self.scanner.index
            )

        # " '
        if ch in ('\'', '"'):
            start = self.scanner.index
            quote = self.scanner.source[self.scanner.index]
            self.scanner.index += 1
            str = ''
            while not self.scanner.eof():
                ch = self.scanner.source[self.scanner.index]
                self.scanner.index += 1
                if ch == quote:
                    break
                elif ch == '&':
                    str += self.scanXHTMLEntity(quote)
                else:
                    str += ch

            return RawJSXToken(
                type=Token.StringLiteral,
                value=str,
                lineNumber=self.scanner.lineNumber,
                lineStart=self.scanner.lineStart,
                start=start,
                end=self.scanner.index
            )

        # ... or .
        if ch == '.':
            start = self.scanner.index
            if self.scanner.source[start + 1:start + 3] == '..':
                value = '...'
                self.scanner.index += 3
            else:
                value = '.'
                self.scanner.index += 1
            return RawJSXToken(
                type=Token.Punctuator,
                value=value,
                lineNumber=self.scanner.lineNumber,
                lineStart=self.scanner.lineStart,
                start=start,
                end=self.scanner.index
            )

        # `
        if ch == '`':
            # Only placeholder, since it will be rescanned as a real assignment expression.
            return RawJSXToken(
                type=Token.Template,
                value='',
                lineNumber=self.scanner.lineNumber,
                lineStart=self.scanner.lineStart,
                start=self.scanner.index,
                end=self.scanner.index
            )

        # Identifer can not contain backslash (char code 92).
        if Character.isIdentifierStart(ch) and ch != '\\':
            start = self.scanner.index
            self.scanner.index += 1
            while not self.scanner.eof():
                ch = self.scanner.source[self.scanner.index]
                if Character.isIdentifierPart(ch) and ch != '\\':
                    self.scanner.index += 1
                elif ch == '-':
                    # Hyphen (char code 45) can be part of an identifier.
                    self.scanner.index += 1
                else:
                    break

            id = self.scanner.source[start:self.scanner.index]
            return RawJSXToken(
                type=JSXToken.Identifier,
                value=id,
                lineNumber=self.scanner.lineNumber,
                lineStart=self.scanner.lineStart,
                start=start,
                end=self.scanner.index
            )

        return self.scanner.lex()

    def nextJSXToken(self):
        self.collectComments()

        self.startMarker.index = self.scanner.index
        self.startMarker.line = self.scanner.lineNumber
        self.startMarker.column = self.scanner.index - self.scanner.lineStart
        token = self.lexJSX()
        self.lastMarker.index = self.scanner.index
        self.lastMarker.line = self.scanner.lineNumber
        self.lastMarker.column = self.scanner.index - self.scanner.lineStart

        if self.config.tokens:
            self.tokens.append(self.convertToken(token))

        return token

    def nextJSXText(self):
        self.startMarker.index = self.scanner.index
        self.startMarker.line = self.scanner.lineNumber
        self.startMarker.column = self.scanner.index - self.scanner.lineStart

        start = self.scanner.index

        text = ''
        while not self.scanner.eof():
            ch = self.scanner.source[self.scanner.index]
            if ch in ('{', '<'):
                break

            self.scanner.index += 1
            text += ch
            if Character.isLineTerminator(ch):
                self.scanner.lineNumber += 1
                if ch == '\r' and self.scanner.source[self.scanner.index] == '\n':
                    self.scanner.index += 1

                self.scanner.lineStart = self.scanner.index

        self.lastMarker.index = self.scanner.index
        self.lastMarker.line = self.scanner.lineNumber
        self.lastMarker.column = self.scanner.index - self.scanner.lineStart

        token = RawJSXToken(
            type=JSXToken.Text,
            value=text,
            lineNumber=self.scanner.lineNumber,
            lineStart=self.scanner.lineStart,
            start=start,
            end=self.scanner.index
        )

        if text and self.config.tokens:
            self.tokens.append(self.convertToken(token))

        return token

    def peekJSXToken(self):
        state = self.scanner.saveState()
        self.scanner.scanComments()
        next = self.lexJSX()
        self.scanner.restoreState(state)

        return next

    # Expect the next JSX token to match the specified punctuator.
    # If not, an exception will be thrown.

    def expectJSX(self, value):
        token = self.nextJSXToken()
        if token.type is not Token.Punctuator or token.value != value:
            self.throwUnexpectedToken(token)

    # Return True if the next JSX token matches the specified punctuator.

    def matchJSX(self, *value):
        next = self.peekJSXToken()
        return next.type is Token.Punctuator and next.value in value

    def parseJSXIdentifier(self):
        node = self.createJSXNode()
        token = self.nextJSXToken()
        if token.type is not JSXToken.Identifier:
            self.throwUnexpectedToken(token)

        return self.finalize(node, JSXNode.JSXIdentifier(token.value))

    def parseJSXElementName(self):
        node = self.createJSXNode()
        elementName = self.parseJSXIdentifier()

        if self.matchJSX(':'):
            namespace = elementName
            self.expectJSX(':')
            name = self.parseJSXIdentifier()
            elementName = self.finalize(node, JSXNode.JSXNamespacedName(namespace, name))
        elif self.matchJSX('.'):
            while self.matchJSX('.'):
                object = elementName
                self.expectJSX('.')
                property = self.parseJSXIdentifier()
                elementName = self.finalize(node, JSXNode.JSXMemberExpression(object, property))

        return elementName

    def parseJSXAttributeName(self):
        node = self.createJSXNode()

        identifier = self.parseJSXIdentifier()
        if self.matchJSX(':'):
            namespace = identifier
            self.expectJSX(':')
            name = self.parseJSXIdentifier()
            attributeName = self.finalize(node, JSXNode.JSXNamespacedName(namespace, name))
        else:
            attributeName = identifier

        return attributeName

    def parseJSXStringLiteralAttribute(self):
        node = self.createJSXNode()
        token = self.nextJSXToken()
        if token.type is not Token.StringLiteral:
            self.throwUnexpectedToken(token)

        raw = self.getTokenRaw(token)
        return self.finalize(node, Node.Literal(token.value, raw))

    def parseJSXExpressionAttribute(self):
        node = self.createJSXNode()

        self.expectJSX('{')
        self.finishJSX()

        if self.match('}'):
            self.tolerateError('JSX attributes must only be assigned a non-empty expression')

        expression = self.parseAssignmentExpression()
        self.reenterJSX()

        return self.finalize(node, JSXNode.JSXExpressionContainer(expression))

    def parseJSXAttributeValue(self):
        if self.matchJSX('{'):
            return self.parseJSXExpressionAttribute()
        if self.matchJSX('<'):
            return self.parseJSXElement()

        return self.parseJSXStringLiteralAttribute()

    def parseJSXNameValueAttribute(self):
        node = self.createJSXNode()
        name = self.parseJSXAttributeName()
        value = None
        if self.matchJSX('='):
            self.expectJSX('=')
            value = self.parseJSXAttributeValue()

        return self.finalize(node, JSXNode.JSXAttribute(name, value))

    def parseJSXSpreadAttribute(self):
        node = self.createJSXNode()
        self.expectJSX('{')
        self.expectJSX('...')

        self.finishJSX()
        argument = self.parseAssignmentExpression()
        self.reenterJSX()

        return self.finalize(node, JSXNode.JSXSpreadAttribute(argument))

    def parseJSXAttributes(self):
        attributes = []

        while not self.matchJSX('/', '>'):
            attribute = self.parseJSXSpreadAttribute() if self.matchJSX('{') else self.parseJSXNameValueAttribute()
            attributes.append(attribute)

        return attributes

    def parseJSXOpeningElement(self):
        node = self.createJSXNode()

        self.expectJSX('<')
        name = self.parseJSXElementName()
        attributes = self.parseJSXAttributes()
        selfClosing = self.matchJSX('/')
        if selfClosing:
            self.expectJSX('/')

        self.expectJSX('>')

        return self.finalize(node, JSXNode.JSXOpeningElement(name, selfClosing, attributes))

    def parseJSXBoundaryElement(self):
        node = self.createJSXNode()

        self.expectJSX('<')
        if self.matchJSX('/'):
            self.expectJSX('/')
            elementName = self.parseJSXElementName()
            self.expectJSX('>')
            return self.finalize(node, JSXNode.JSXClosingElement(elementName))

        name = self.parseJSXElementName()
        attributes = self.parseJSXAttributes()
        selfClosing = self.matchJSX('/')
        if selfClosing:
            self.expectJSX('/')

        self.expectJSX('>')

        return self.finalize(node, JSXNode.JSXOpeningElement(name, selfClosing, attributes))

    def parseJSXEmptyExpression(self):
        node = self.createJSXChildNode()
        self.collectComments()
        self.lastMarker.index = self.scanner.index
        self.lastMarker.line = self.scanner.lineNumber
        self.lastMarker.column = self.scanner.index - self.scanner.lineStart
        return self.finalize(node, JSXNode.JSXEmptyExpression())

    def parseJSXExpressionContainer(self):
        node = self.createJSXNode()
        self.expectJSX('{')

        if self.matchJSX('}'):
            expression = self.parseJSXEmptyExpression()
            self.expectJSX('}')
        else:
            self.finishJSX()
            expression = self.parseAssignmentExpression()
            self.reenterJSX()

        return self.finalize(node, JSXNode.JSXExpressionContainer(expression))

    def parseJSXChildren(self):
        children = []

        while not self.scanner.eof():
            node = self.createJSXChildNode()
            token = self.nextJSXText()
            if token.start < token.end:
                raw = self.getTokenRaw(token)
                child = self.finalize(node, JSXNode.JSXText(token.value, raw))
                children.append(child)

            if self.scanner.source[self.scanner.index] == '{':
                container = self.parseJSXExpressionContainer()
                children.append(container)
            else:
                break

        return children

    def parseComplexJSXElement(self, el):
        stack = []

        while not self.scanner.eof():
            el.children.extend(self.parseJSXChildren())
            node = self.createJSXChildNode()
            element = self.parseJSXBoundaryElement()
            if element.type is JSXSyntax.JSXOpeningElement:
                opening = element
                if opening.selfClosing:
                    child = self.finalize(node, JSXNode.JSXElement(opening, [], None))
                    el.children.append(child)
                else:
                    stack.append(el)
                    el = MetaJSXElement(
                        node=node,
                        opening=opening,
                        closing=None,
                        children=[],
                    )

            if element.type is JSXSyntax.JSXClosingElement:
                el.closing = element
                open = getQualifiedElementName(el.opening.name)
                close = getQualifiedElementName(el.closing.name)
                if open != close:
                    self.tolerateError('Expected corresponding JSX closing tag for %0', open)

                if stack:
                    child = self.finalize(el.node, JSXNode.JSXElement(el.opening, el.children, el.closing))
                    el = stack[-1]
                    el.children.append(child)
                    stack.pop()
                else:
                    break

        return el

    def parseJSXElement(self):
        node = self.createJSXNode()

        opening = self.parseJSXOpeningElement()
        children = []
        closing = None

        if not opening.selfClosing:
            el = self.parseComplexJSXElement(MetaJSXElement(
                node=node,
                opening=opening,
                closing=closing,
                children=children
            ))
            children = el.children
            closing = el.closing

        return self.finalize(node, JSXNode.JSXElement(opening, children, closing))

    def parseJSXRoot(self):
        # Pop the opening '<' added from the lookahead.
        if self.config.tokens:
            self.tokens.pop()

        self.startJSX()
        element = self.parseJSXElement()
        self.finishJSX()

        return element

    def isStartOfExpression(self):
        return super(JSXParser, self).isStartOfExpression() or self.match('<')