3 pygments.lexers.javascript |
3 pygments.lexers.javascript |
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
5 |
5 |
6 Lexers for JavaScript and related languages. |
6 Lexers for JavaScript and related languages. |
7 |
7 |
8 :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS. |
8 :copyright: Copyright 2006-2020 by the Pygments team, see AUTHORS. |
9 :license: BSD, see LICENSE for details. |
9 :license: BSD, see LICENSE for details. |
10 """ |
10 """ |
11 |
11 |
12 import re |
12 import re |
13 |
13 |
24 |
24 |
25 JS_IDENT_START = ('(?:[$_' + uni.combine('Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nl') + |
25 JS_IDENT_START = ('(?:[$_' + uni.combine('Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nl') + |
26 ']|\\\\u[a-fA-F0-9]{4})') |
26 ']|\\\\u[a-fA-F0-9]{4})') |
27 JS_IDENT_PART = ('(?:[$' + uni.combine('Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nl', |
27 JS_IDENT_PART = ('(?:[$' + uni.combine('Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nl', |
28 'Mn', 'Mc', 'Nd', 'Pc') + |
28 'Mn', 'Mc', 'Nd', 'Pc') + |
29 u'\u200c\u200d]|\\\\u[a-fA-F0-9]{4})') |
29 '\u200c\u200d]|\\\\u[a-fA-F0-9]{4})') |
30 JS_IDENT = JS_IDENT_START + '(?:' + JS_IDENT_PART + ')*' |
30 JS_IDENT = JS_IDENT_START + '(?:' + JS_IDENT_PART + ')*' |
31 |
31 |
32 |
32 |
33 class JavascriptLexer(RegexLexer): |
33 class JavascriptLexer(RegexLexer): |
34 """ |
34 """ |
62 ], |
62 ], |
63 'root': [ |
63 'root': [ |
64 (r'\A#! ?/.*?\n', Comment.Hashbang), # recognized by node.js |
64 (r'\A#! ?/.*?\n', Comment.Hashbang), # recognized by node.js |
65 (r'^(?=\s|/|<!--)', Text, 'slashstartsregex'), |
65 (r'^(?=\s|/|<!--)', Text, 'slashstartsregex'), |
66 include('commentsandwhitespace'), |
66 include('commentsandwhitespace'), |
67 (r'(\.\d+|[0-9]+\.[0-9]*)([eE][-+]?[0-9]+)?', Number.Float), |
67 |
68 (r'0[bB][01]+', Number.Bin), |
68 # Numeric literals |
69 (r'0[oO][0-7]+', Number.Oct), |
69 (r'0[bB][01]+n?', Number.Bin), |
70 (r'0[xX][0-9a-fA-F]+', Number.Hex), |
70 (r'0[oO]?[0-7]+n?', Number.Oct), # Browsers support "0o7" and "07" notations |
71 (r'[0-9]+', Number.Integer), |
71 (r'0[xX][0-9a-fA-F]+n?', Number.Hex), |
|
72 (r'[0-9]+n', Number.Integer), # Javascript BigInt requires an "n" postfix |
|
73 # Javascript doesn't have actual integer literals, so every other |
|
74 # numeric literal is handled by the regex below (including "normal") |
|
75 # integers |
|
76 (r'(\.[0-9]+|[0-9]+\.[0-9]*|[0-9]+)([eE][-+]?[0-9]+)?', Number.Float), |
|
77 |
72 (r'\.\.\.|=>', Punctuation), |
78 (r'\.\.\.|=>', Punctuation), |
73 (r'\+\+|--|~|&&|\?|:|\|\||\\(?=\n)|' |
79 (r'\+\+|--|~|&&|\?|:|\|\||\\(?=\n)|' |
74 r'(<<|>>>?|==?|!=?|[-<>+*%&|^/])=?', Operator, 'slashstartsregex'), |
80 r'(<<|>>>?|==?|!=?|[-<>+*%&|^/])=?', Operator, 'slashstartsregex'), |
75 (r'[{(\[;,]', Punctuation, 'slashstartsregex'), |
81 (r'[{(\[;,]', Punctuation, 'slashstartsregex'), |
76 (r'[})\].]', Punctuation), |
82 (r'[})\].]', Punctuation), |
261 r'([gim]+\b|\B)', String.Regex, '#pop'), |
267 r'([gim]+\b|\B)', String.Regex, '#pop'), |
262 (r'/', Operator, '#pop'), |
268 (r'/', Operator, '#pop'), |
263 default('#pop'), |
269 default('#pop'), |
264 ], |
270 ], |
265 'root': [ |
271 'root': [ |
266 (r'^(?=\s|/)', Text, 'slashstartsregex'), |
272 (r'\A(?=\s|/)', Text, 'slashstartsregex'), |
267 include('commentsandwhitespace'), |
273 include('commentsandwhitespace'), |
268 (r'(?:\([^()]+\))?[ ]*[~-]{1,2}>|' |
274 (r'(?:\([^()]+\))?[ ]*[~-]{1,2}>|' |
269 r'(?:\(?[^()\n]+\)?)?[ ]*<[~-]{1,2}', Name.Function), |
275 r'(?:\(?[^()\n]+\)?)?[ ]*<[~-]{1,2}', Name.Function), |
270 (r'\+\+|&&|(?<![.$])\b(?:and|x?or|is|isnt|not)\b|\?|:|=|' |
276 (r'\+\+|&&|(?<![.$])\b(?:and|x?or|is|isnt|not)\b|\?|:|=|' |
271 r'\|\||\\(?=\n)|(<<|>>>?|==?|!=?|' |
277 r'\|\||\\(?=\n)|(<<|>>>?|==?|!=?|' |
359 (r'\b(import|export)\b', Keyword, 'import_decl'), |
365 (r'\b(import|export)\b', Keyword, 'import_decl'), |
360 (r'\b(library|source|part of|part)\b', Keyword), |
366 (r'\b(library|source|part of|part)\b', Keyword), |
361 (r'[^\S\n]+', Text), |
367 (r'[^\S\n]+', Text), |
362 (r'//.*?\n', Comment.Single), |
368 (r'//.*?\n', Comment.Single), |
363 (r'/\*.*?\*/', Comment.Multiline), |
369 (r'/\*.*?\*/', Comment.Multiline), |
364 (r'\b(class)\b(\s+)', |
370 (r'\b(class|extension|mixin)\b(\s+)', |
365 bygroups(Keyword.Declaration, Text), 'class'), |
371 bygroups(Keyword.Declaration, Text), 'class'), |
366 (r'\b(assert|break|case|catch|continue|default|do|else|finally|for|' |
372 (r'\b(as|assert|break|case|catch|const|continue|default|do|else|finally|' |
367 r'if|in|is|new|return|super|switch|this|throw|try|while)\b', |
373 r'for|if|in|is|new|rethrow|return|super|switch|this|throw|try|while)\b', |
368 Keyword), |
374 Keyword), |
369 (r'\b(abstract|async|await|const|extends|factory|final|get|' |
375 (r'\b(abstract|async|await|const|covariant|extends|external|factory|final|' |
370 r'implements|native|operator|set|static|sync|typedef|var|with|' |
376 r'get|implements|late|native|on|operator|required|set|static|sync|typedef|' |
371 r'yield)\b', Keyword.Declaration), |
377 r'var|with|yield)\b', Keyword.Declaration), |
372 (r'\b(bool|double|dynamic|int|num|Object|String|void)\b', Keyword.Type), |
378 (r'\b(bool|double|dynamic|int|num|Function|Never|Null|Object|String|void)\b', |
|
379 Keyword.Type), |
373 (r'\b(false|null|true)\b', Keyword.Constant), |
380 (r'\b(false|null|true)\b', Keyword.Constant), |
374 (r'[~!%^&*+=|?:<>/-]|as\b', Operator), |
381 (r'[~!%^&*+=|?:<>/-]|as\b', Operator), |
375 (r'@[a-zA-Z_$]\w*', Name.Decorator), |
382 (r'@[a-zA-Z_$]\w*', Name.Decorator), |
376 (r'[a-zA-Z_$]\w*:', Name.Label), |
383 (r'[a-zA-Z_$]\w*:', Name.Label), |
377 (r'[a-zA-Z_$]\w*', Name), |
384 (r'[a-zA-Z_$]\w*', Name), |
387 (r'[a-zA-Z_$]\w*', Name.Class, '#pop') |
394 (r'[a-zA-Z_$]\w*', Name.Class, '#pop') |
388 ], |
395 ], |
389 'import_decl': [ |
396 'import_decl': [ |
390 include('string_literal'), |
397 include('string_literal'), |
391 (r'\s+', Text), |
398 (r'\s+', Text), |
392 (r'\b(as|show|hide)\b', Keyword), |
399 (r'\b(as|deferred|show|hide)\b', Keyword), |
393 (r'[a-zA-Z_$]\w*', Name), |
400 (r'[a-zA-Z_$]\w*', Name), |
394 (r'\,', Punctuation), |
401 (r'\,', Punctuation), |
395 (r'\;', Punctuation, '#pop') |
402 (r'\;', Punctuation, '#pop') |
396 ], |
403 ], |
397 'string_literal': [ |
404 'string_literal': [ |
1063 (r'/', Operator, '#pop'), |
1070 (r'/', Operator, '#pop'), |
1064 default('#pop'), |
1071 default('#pop'), |
1065 ], |
1072 ], |
1066 'root': [ |
1073 'root': [ |
1067 include('commentsandwhitespace'), |
1074 include('commentsandwhitespace'), |
1068 (r'^(?=\s|/)', Text, 'slashstartsregex'), |
1075 (r'\A(?=\s|/)', Text, 'slashstartsregex'), |
1069 (_operator_re, Operator, 'slashstartsregex'), |
1076 (_operator_re, Operator, 'slashstartsregex'), |
1070 (r'(?:\([^()]*\))?\s*[=-]>', Name.Function, 'slashstartsregex'), |
1077 (r'(?:\([^()]*\))?\s*[=-]>', Name.Function, 'slashstartsregex'), |
1071 (r'[{(\[;,]', Punctuation, 'slashstartsregex'), |
1078 (r'[{(\[;,]', Punctuation, 'slashstartsregex'), |
1072 (r'[})\].]', Punctuation), |
1079 (r'[})\].]', Punctuation), |
1073 (r'(?<![.$])(for|own|in|of|while|until|' |
1080 (r'(?<![.$])(for|own|in|of|while|until|' |