summaryrefslogtreecommitdiffstats
path: root/third_party/rust/jsparagus/js_parser/lexer.py
blob: 2d8ed530ed4e428308c70c79c99aa549b45fe27f (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
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
"""Vague approximation of an ECMAScript lexer.

A parser has two levels: the *lexer* scans bytes to produce tokens. The
*parser* consumes tokens and produces ASTs.

In a traditional design, the parser drives the process. It *pulls* one token at
a time from the lexer. However, for a parser that can accept arbitrary slabs of
data, scan them, then keep going, it makes more sense for the user to feed
those slabs to the lexer, which then *pushes* tokens to the parser. So that's
what we do.

Usage:

    from js_parser.lexer import JSLexer
    from js_parser.parser import JSParser

    lexer = JSLexer(JSParser())
    lexer.write(some_source_text)
    lexer.write(some_more_source_text)
    ast = lexer.close()
"""

import re
import jsparagus.lexer


def _get_punctuators():
    punctuators = '''
        &&= ||= ??=
        { ( ) [ ] . ... ; , < > <= >= == != === !== + - * % ** ++ --
        << >> >>> & | ^ ! ~ && || ? : = += -= *= %=
        **= ><<= >>= >>>= &= |= ^= =>
    '''.split()

    return '|'.join(
        re.escape(token)
        for token in sorted(punctuators, key=len, reverse=True))


TOKEN_RE = re.compile(r'''(?x)
  (?:
      # WhiteSpace
      [\ \t\v\r\n\u00a0\u2028\u2029\ufeff]
      # SingleLineComment
    | // [^\r\n\u2028\u2029]* (?= [\r\n\u2028\u2029] | \Z )
      # MultiLineComment
    | /\*  (?: [^*] | \*+[^/] )*  \*+/
  )*
  (
      # Incomplete MultiLineComment
      /\*  (?: [^*] | \*+[^/] )*  \**
    | # Incomplete SingleLineComment
      // [^\r\n\u2028\u2029]*
    | # IdentifierName
      (?: [$_A-Za-z]     | \\ u [0-9A-Fa-f]{4} | \\ u \{ [0-9A-Fa-f]+ \})
      (?: [$_0-9A-Za-z]  | \\ u [0-9A-Fa-f]{4} | \\ u \{ [0-9A-Fa-f]+ \})*
    | # NumericLiteral
      [0-9][0-9A-Za-z]*(?:\.[0-9A-Za-z]*)?
    | \.[0-9][0-9A-Za-z]*
    | # Punctuator
      <INSERT_PUNCTUATORS>
    | # The slash special case
      /
    | # The curly brace special case
      }
    | # StringLiteral
      '
        # SingleStringCharacters
        (?:
            # SourceCharacter but not one of ' or \\ or LineTerminator
            # but also allow LINE SEPARATOR or PARAGRAPH SEPARATOR
            [^'\\\r\n]
          | \\ [^0-9xu\r\n\u2028\u2029]  # CharacterEscapeSequence
          | \\ x [0-9A-Fa-f]{2}          # HexEscapeSequence
          | \\ u [0-9A-Fa-f]{4}          # UnicodeEscapeSequence
          | \\ u \{ [0-9A-Fa-f]+ \}
          | \\\r\n?                      # LineContinuation
          | \\[\n\u2028\u2029]
        )*
      '
    | "
        # DoubleStringCharacters
        (?:
            # SourceCharacter but not one of " or \\ or LineTerminator
            # but also allow LINE SEPARATOR or PARAGRAPH SEPARATOR
            [^"\\\r\n]
          | \\ [^0-9xu\r\n\u2028\u2029]  # CharacterEscapeSequence
          | \\ x [0-9A-Fa-f]{2}          # HexEscapeSequence
          | \\ u [0-9A-Fa-f]{4}          # UnicodeEscapeSequence
          | \\ u \{ [0-9A-Fa-f]+ \}
          | \\\r\n?                      # LineContinuation
          | \\[\n\u2028\u2029]
        )*
      "
    | # Template
      ` (?: [^`\\$] | \\. )* (?: \${ | ` )
    | # illegal character or end of input (this branch matches no characters)
  )
'''.replace("<INSERT_PUNCTUATORS>", _get_punctuators()))

DIV_RE = re.compile(r'(/=?)')

REGEXP_RE = re.compile(r'''(?x)
(
    /
    (?:
        # RegularExpressionFirstChar - implemented using
        #     RegularExpressionChars on the theory that we have already
        #     ruled out the possibility of a comment.
        # RegularExpressionChars
        (?:
            # RegularExpressionNonTerminator but not one of \\ or / or [
            [^/\\\[\r\n\u2028\u2029]
          | # RegularExpressionBackslashSequence
            \\ [^\r\n\u2028\u2029]
          | # RegularExpressionClass
            \[
                # RegularExpressionClassChars
                (?:
                    # RegularExpressionNonTerminator but not one of ] or \\
                    [^]\\\r\n\u2028\u2029]
                  | # RegularExpressionBackslashSequence
                    \\ [^\r\n\u2028\u2029]
                )*
            \]
        )+
    )
    /
    (?: \w* )
)
''')

# Words that never match Identifier. (`await` and `yield` nonetheless
# conditionally match IdentifierReference, BindingIdentifier, and
# LabelIdentifier.)
#
# Technically the term for these is "reserved word", not "keyword", but
# whatever.
ECMASCRIPT_FULL_KEYWORDS = [
    'await',
    'break',
    'case',
    'catch',
    'class',
    'const',
    'continue',
    'debugger',
    'default',
    'delete',
    'do',
    'else',
    'enum',
    'export',
    'extends',
    'finally',
    'for',
    'function',
    'if',
    'import',
    'in',
    'instanceof',
    'new',
    'null',
    'return',
    'super',
    'switch',
    'this',
    'throw',
    'true',
    'false',
    'try',
    'typeof',
    'var',
    'void',
    'while',
    'with',
    'yield',
]

ECMASCRIPT_CONDITIONAL_KEYWORDS = [
    # Words that are identifiers except in strict mode
    'let',  # this one is also banned at the beginning of an ExpressionStatement
    'static',
    'implements',
    'interface',
    'package',
    'private',
    'protected',
    'public',

    # Words that are always allowed as identifiers, but are also keywords in
    # other contexts.
    'as',
    'async',
    'from',
    'get',
    'of',
    'set',
    'target',
]

# Technically this set includes a reserved word that isn't currently being used
# as a keyword in the grammar: `enum`.
ALL_KEYWORDS = set(ECMASCRIPT_FULL_KEYWORDS + ECMASCRIPT_CONDITIONAL_KEYWORDS)


class JSLexer(jsparagus.lexer.FlatStringLexer):
    """Vague approximation of an ECMAScript lexer. """
    def __init__(self, parser, filename=None):
        super().__init__(parser, filename)

    def _match(self, closing):
        match = TOKEN_RE.match(self.src, self.point)
        assert match is not None

        if match.end() == len(self.src) and not closing:
            # The current token runs right up against the end of the current
            # chunk of source and thus might continue in the next chunk. Do not
            # move self.point.
            return None

        token = match.group(1)
        if token == '':
            # Whitespace followed by end of input or illegal character.
            if match.end() == len(self.src):
                # End of input. Success!
                assert closing
                self.point = match.end()
                return None
            else:
                c = self.src[match.end()]
                self.throw("unexpected character: {!r}".format(c))

        c = token[0]
        t = None
        if c.isdigit() or c == '.' and token != '.':
            t = 'NumericLiteral'
        elif c.isalpha() or c in '$_':
            if token in ALL_KEYWORDS:  # TODO support strict mode
                if token == 'null':
                    t = 'NullLiteral'
                elif token in ('true', 'false'):
                    t = 'BooleanLiteral'
                else:
                    t = token
            else:
                t = 'Name'
        elif c == '/':
            if token.startswith(('/*', '//')):
                # Incomplete comment. (In non-closing mode, this is handled
                # above, immediately after the match.)
                assert match.end() == len(self.src)
                assert closing
                self.point = len(self.src)
                self.throw("incomplete comment at end of source")

            # We choose RegExp vs. division based on what the parser can
            # accept, a literal implementation of the spec.
            #
            # To make this correct in combination with end-of-line ASI, make
            # the parser rewind the lexer one token and ask for it again in
            # that case, so that the lexer asks the can-accept question again.
            point = match.start(1)
            if self.parser.can_accept_terminal(self, 'RegularExpressionLiteral'):
                match = REGEXP_RE.match(self.src, point)
                if match is None:
                    if closing:
                        self.throw("unterminated regexp literal")
                    else:
                        return None
                token = 'RegularExpressionLiteral'
            else:
                match = DIV_RE.match(self.src, point)
                token = match.group(1)

            if not closing and match.end() == len(self.src):
                # At the end of a chunk, `/a*b/` could be the start of
                # `/a*b/g`, and `/` could be the start of `/=`.
                return None

            t = token
        elif c == '`':
            if token.endswith('`'):
                t = 'NoSubstitutionTemplate'
            else:
                t = 'TemplateHead'
        elif c == '"' or c == "'":
            t = 'StringLiteral'
        elif c == '}':
            # TODO: TemplateTail
            t = token
        elif c in '{()[];,~?:.<>=!+-*%&|^':
            t = token
        else:
            assert False

        self._current_match = match
        self.previous_token_end = self.point
        self.current_token_start = match.start(1)
        self.point = match.end()
        return t

    def take(self):
        return self._current_match.group(1)

    def saw_line_terminator(self):
        """True if there's a LineTerminator before the current token."""
        i = self.previous_token_end
        j = self.current_token_start
        ws_between = self.src[i:j]
        return any(c in ws_between for c in '\r\n\u2028\u2029')

    def can_close(self):
        match = TOKEN_RE.match(self.src)
        return match.group(1) == '' and self.parser.can_close()