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-2017 by the Pygments team, see AUTHORS. |
8 :copyright: Copyright 2006-2019 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 |
14 from pygments.lexer import RegexLexer, include, bygroups, default, using, \ |
14 from pygments.lexer import RegexLexer, include, bygroups, default, using, \ |
15 this, words, combined |
15 this, words, combined |
16 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ |
16 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ |
17 Number, Punctuation, Other |
17 Number, Punctuation, Other |
18 from pygments.util import get_bool_opt, iteritems |
18 from pygments.util import get_bool_opt |
19 import pygments.unistring as uni |
19 import pygments.unistring as uni |
20 |
20 |
21 __all__ = ['JavascriptLexer', 'KalLexer', 'LiveScriptLexer', 'DartLexer', |
21 __all__ = ['JavascriptLexer', 'KalLexer', 'LiveScriptLexer', 'DartLexer', |
22 'TypeScriptLexer', 'LassoLexer', 'ObjectiveJLexer', |
22 'TypeScriptLexer', 'LassoLexer', 'ObjectiveJLexer', |
23 'CoffeeScriptLexer', 'MaskLexer', 'EarlGreyLexer', 'JuttleLexer'] |
23 'CoffeeScriptLexer', 'MaskLexer', 'EarlGreyLexer', 'JuttleLexer'] |
35 For JavaScript source code. |
35 For JavaScript source code. |
36 """ |
36 """ |
37 |
37 |
38 name = 'JavaScript' |
38 name = 'JavaScript' |
39 aliases = ['js', 'javascript'] |
39 aliases = ['js', 'javascript'] |
40 filenames = ['*.js', '*.jsm'] |
40 filenames = ['*.js', '*.jsm', '*.mjs'] |
41 mimetypes = ['application/javascript', 'application/x-javascript', |
41 mimetypes = ['application/javascript', 'application/x-javascript', |
42 'text/x-javascript', 'text/javascript'] |
42 'text/x-javascript', 'text/javascript'] |
43 |
43 |
44 flags = re.DOTALL | re.UNICODE | re.MULTILINE |
44 flags = re.DOTALL | re.UNICODE | re.MULTILINE |
45 |
45 |
257 'slashstartsregex': [ |
257 'slashstartsregex': [ |
258 include('commentsandwhitespace'), |
258 include('commentsandwhitespace'), |
259 (r'//', String.Regex, ('#pop', 'multilineregex')), |
259 (r'//', String.Regex, ('#pop', 'multilineregex')), |
260 (r'/(?! )(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/' |
260 (r'/(?! )(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/' |
261 r'([gim]+\b|\B)', String.Regex, '#pop'), |
261 r'([gim]+\b|\B)', String.Regex, '#pop'), |
|
262 (r'/', Operator, '#pop'), |
262 default('#pop'), |
263 default('#pop'), |
263 ], |
264 ], |
264 'root': [ |
265 'root': [ |
265 # this next expr leads to infinite loops root -> slashstartsregex |
266 (r'^(?=\s|/)', Text, 'slashstartsregex'), |
266 # (r'^(?=\s|/|<!--)', Text, 'slashstartsregex'), |
|
267 include('commentsandwhitespace'), |
267 include('commentsandwhitespace'), |
268 (r'(?:\([^()]+\))?[ ]*[~-]{1,2}>|' |
268 (r'(?:\([^()]+\))?[ ]*[~-]{1,2}>|' |
269 r'(?:\(?[^()\n]+\)?)?[ ]*<[~-]{1,2}', Name.Function), |
269 r'(?:\(?[^()\n]+\)?)?[ ]*<[~-]{1,2}', Name.Function), |
270 (r'\+\+|&&|(?<![.$])\b(?:and|x?or|is|isnt|not)\b|\?|:|=|' |
270 (r'\+\+|&&|(?<![.$])\b(?:and|x?or|is|isnt|not)\b|\?|:|=|' |
271 r'\|\||\\(?=\n)|(<<|>>>?|==?|!=?|' |
271 r'\|\||\\(?=\n)|(<<|>>>?|==?|!=?|' |
370 r'implements|native|operator|set|static|sync|typedef|var|with|' |
370 r'implements|native|operator|set|static|sync|typedef|var|with|' |
371 r'yield)\b', Keyword.Declaration), |
371 r'yield)\b', Keyword.Declaration), |
372 (r'\b(bool|double|dynamic|int|num|Object|String|void)\b', Keyword.Type), |
372 (r'\b(bool|double|dynamic|int|num|Object|String|void)\b', Keyword.Type), |
373 (r'\b(false|null|true)\b', Keyword.Constant), |
373 (r'\b(false|null|true)\b', Keyword.Constant), |
374 (r'[~!%^&*+=|?:<>/-]|as\b', Operator), |
374 (r'[~!%^&*+=|?:<>/-]|as\b', Operator), |
|
375 (r'@[a-zA-Z_$]\w*', Name.Decorator), |
375 (r'[a-zA-Z_$]\w*:', Name.Label), |
376 (r'[a-zA-Z_$]\w*:', Name.Label), |
376 (r'[a-zA-Z_$]\w*', Name), |
377 (r'[a-zA-Z_$]\w*', Name), |
377 (r'[(){}\[\],.;]', Punctuation), |
378 (r'[(){}\[\],.;]', Punctuation), |
378 (r'0[xX][0-9a-fA-F]+', Number.Hex), |
379 (r'0[xX][0-9a-fA-F]+', Number.Hex), |
379 # DIGIT+ (‘.’ DIGIT*)? EXPONENT? |
380 # DIGIT+ (‘.’ DIGIT*)? EXPONENT? |
476 (r'\+\+|--|~|&&|\?|:|\|\||\\(?=\n)|' |
481 (r'\+\+|--|~|&&|\?|:|\|\||\\(?=\n)|' |
477 r'(<<|>>>?|==?|!=?|[-<>+*%&|^/])=?', Operator, 'slashstartsregex'), |
482 r'(<<|>>>?|==?|!=?|[-<>+*%&|^/])=?', Operator, 'slashstartsregex'), |
478 (r'[{(\[;,]', Punctuation, 'slashstartsregex'), |
483 (r'[{(\[;,]', Punctuation, 'slashstartsregex'), |
479 (r'[})\].]', Punctuation), |
484 (r'[})\].]', Punctuation), |
480 (r'(for|in|while|do|break|return|continue|switch|case|default|if|else|' |
485 (r'(for|in|while|do|break|return|continue|switch|case|default|if|else|' |
481 r'throw|try|catch|finally|new|delete|typeof|instanceof|void|' |
486 r'throw|try|catch|finally|new|delete|typeof|instanceof|void|of|' |
482 r'this)\b', Keyword, 'slashstartsregex'), |
487 r'this)\b', Keyword, 'slashstartsregex'), |
483 (r'(var|let|with|function)\b', Keyword.Declaration, 'slashstartsregex'), |
488 (r'(var|let|with|function)\b', Keyword.Declaration, 'slashstartsregex'), |
484 (r'(abstract|boolean|byte|char|class|const|debugger|double|enum|export|' |
489 (r'(abstract|boolean|byte|char|class|const|debugger|double|enum|export|' |
485 r'extends|final|float|goto|implements|import|int|interface|long|native|' |
490 r'extends|final|float|goto|implements|import|int|interface|long|native|' |
486 r'package|private|protected|public|short|static|super|synchronized|throws|' |
491 r'package|private|protected|public|short|static|super|synchronized|throws|' |
766 |
765 |
767 self._builtins = set() |
766 self._builtins = set() |
768 self._members = set() |
767 self._members = set() |
769 if self.builtinshighlighting: |
768 if self.builtinshighlighting: |
770 from pygments.lexers._lasso_builtins import BUILTINS, MEMBERS |
769 from pygments.lexers._lasso_builtins import BUILTINS, MEMBERS |
771 for key, value in iteritems(BUILTINS): |
770 for key, value in BUILTINS.items(): |
772 self._builtins.update(value) |
771 self._builtins.update(value) |
773 for key, value in iteritems(MEMBERS): |
772 for key, value in MEMBERS.items(): |
774 self._members.update(value) |
773 self._members.update(value) |
775 RegexLexer.__init__(self, **options) |
774 RegexLexer.__init__(self, **options) |
776 |
775 |
777 def get_tokens_unprocessed(self, text): |
776 def get_tokens_unprocessed(self, text): |
778 stack = ['root'] |
777 stack = ['root'] |
1060 (r'/(?! )(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/' |
1058 (r'/(?! )(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/' |
1061 r'([gim]+\b|\B)', String.Regex, '#pop'), |
1059 r'([gim]+\b|\B)', String.Regex, '#pop'), |
1062 # This isn't really guarding against mishighlighting well-formed |
1060 # This isn't really guarding against mishighlighting well-formed |
1063 # code, just the ability to infinite-loop between root and |
1061 # code, just the ability to infinite-loop between root and |
1064 # slashstartsregex. |
1062 # slashstartsregex. |
1065 (r'/', Operator), |
1063 (r'/', Operator, '#pop'), |
1066 default('#pop'), |
1064 default('#pop'), |
1067 ], |
1065 ], |
1068 'root': [ |
1066 'root': [ |
1069 include('commentsandwhitespace'), |
1067 include('commentsandwhitespace'), |
1070 (r'^(?=\s|/)', Text, 'slashstartsregex'), |
1068 (r'^(?=\s|/)', Text, 'slashstartsregex'), |
1457 (r'\d+\.(?!\.)\d*([eE][+-]?[0-9]+)?', Number.Float), |
1455 (r'\d+\.(?!\.)\d*([eE][+-]?[0-9]+)?', Number.Float), |
1458 (r'\d+[eE][+-]?[0-9]+', Number.Float), |
1456 (r'\d+[eE][+-]?[0-9]+', Number.Float), |
1459 (r'8r[0-7]+', Number.Oct), |
1457 (r'8r[0-7]+', Number.Oct), |
1460 (r'2r[01]+', Number.Bin), |
1458 (r'2r[01]+', Number.Bin), |
1461 (r'16r[a-fA-F0-9]+', Number.Hex), |
1459 (r'16r[a-fA-F0-9]+', Number.Hex), |
1462 (r'([3-79]|[12][0-9]|3[0-6])r[a-zA-Z\d]+(\.[a-zA-Z\d]+)?', Number.Radix), |
1460 (r'([3-79]|[12][0-9]|3[0-6])r[a-zA-Z\d]+(\.[a-zA-Z\d]+)?', |
|
1461 Number.Radix), |
1463 (r'\d+', Number.Integer) |
1462 (r'\d+', Number.Integer) |
1464 ], |
1463 ], |
1465 } |
1464 } |
1466 |
1465 |
|
1466 |
1467 class JuttleLexer(RegexLexer): |
1467 class JuttleLexer(RegexLexer): |
1468 """ |
1468 """ |
1469 For `Juttle`_ source code. |
1469 For `Juttle`_ source code. |
1470 |
1470 |
1471 .. _Juttle: https://github.com/juttle/juttle |
1471 .. _Juttle: https://github.com/juttle/juttle |
1472 |
1472 |
|
1473 .. versionadded:: 2.2 |
1473 """ |
1474 """ |
1474 |
1475 |
1475 name = 'Juttle' |
1476 name = 'Juttle' |
1476 aliases = ['juttle', 'juttle'] |
1477 aliases = ['juttle', 'juttle'] |
1477 filenames = ['*.juttle'] |
1478 filenames = ['*.juttle'] |
1502 (r':\d{2}:\d{2}:\d{2}(\.\d*)?:', String.Moment), |
1503 (r':\d{2}:\d{2}:\d{2}(\.\d*)?:', String.Moment), |
1503 (r':(now|beginning|end|forever|yesterday|today|tomorrow|' |
1504 (r':(now|beginning|end|forever|yesterday|today|tomorrow|' |
1504 r'(\d+(\.\d*)?|\.\d+)(ms|[smhdwMy])?):', String.Moment), |
1505 r'(\d+(\.\d*)?|\.\d+)(ms|[smhdwMy])?):', String.Moment), |
1505 (r':\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}(\.\d*)?)?' |
1506 (r':\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}(\.\d*)?)?' |
1506 r'(Z|[+-]\d{2}:\d{2}|[+-]\d{4})?:', String.Moment), |
1507 r'(Z|[+-]\d{2}:\d{2}|[+-]\d{4})?:', String.Moment), |
1507 (r':((\d+(\.\d*)?|\.\d+)[ ]+)?(millisecond|second|minute|hour|day|week|month|year)[s]?' |
1508 (r':((\d+(\.\d*)?|\.\d+)[ ]+)?(millisecond|second|minute|hour|' |
1508 r'(([ ]+and[ ]+(\d+[ ]+)?(millisecond|second|minute|hour|day|week|month|year)[s]?)' |
1509 r'day|week|month|year)[s]?' |
|
1510 r'(([ ]+and[ ]+(\d+[ ]+)?(millisecond|second|minute|hour|' |
|
1511 r'day|week|month|year)[s]?)' |
1509 r'|[ ]+(ago|from[ ]+now))*:', String.Moment), |
1512 r'|[ ]+(ago|from[ ]+now))*:', String.Moment), |
1510 (r'\+\+|--|~|&&|\?|:|\|\||\\(?=\n)|' |
1513 (r'\+\+|--|~|&&|\?|:|\|\||\\(?=\n)|' |
1511 r'(==?|!=?|[-<>+*%&|^/])=?', Operator, 'slashstartsregex'), |
1514 r'(==?|!=?|[-<>+*%&|^/])=?', Operator, 'slashstartsregex'), |
1512 (r'[{(\[;,]', Punctuation, 'slashstartsregex'), |
1515 (r'[{(\[;,]', Punctuation, 'slashstartsregex'), |
1513 (r'[})\].]', Punctuation), |
1516 (r'[})\].]', Punctuation), |
1514 (r'(import|return|continue|if|else)\b', Keyword, 'slashstartsregex'), |
1517 (r'(import|return|continue|if|else)\b', Keyword, 'slashstartsregex'), |
1515 (r'(var|const|function|reducer|sub|input)\b', Keyword.Declaration, 'slashstartsregex'), |
1518 (r'(var|const|function|reducer|sub|input)\b', Keyword.Declaration, |
|
1519 'slashstartsregex'), |
1516 (r'(batch|emit|filter|head|join|keep|pace|pass|put|read|reduce|remove|' |
1520 (r'(batch|emit|filter|head|join|keep|pace|pass|put|read|reduce|remove|' |
1517 r'sequence|skip|sort|split|tail|unbatch|uniq|view|write)\b', Keyword.Reserved), |
1521 r'sequence|skip|sort|split|tail|unbatch|uniq|view|write)\b', |
|
1522 Keyword.Reserved), |
1518 (r'(true|false|null|Infinity)\b', Keyword.Constant), |
1523 (r'(true|false|null|Infinity)\b', Keyword.Constant), |
1519 (r'(Array|Date|Juttle|Math|Number|Object|RegExp|String)\b', Name.Builtin), |
1524 (r'(Array|Date|Juttle|Math|Number|Object|RegExp|String)\b', |
|
1525 Name.Builtin), |
1520 (JS_IDENT, Name.Other), |
1526 (JS_IDENT, Name.Other), |
1521 (r'[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float), |
1527 (r'[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float), |
1522 (r'[0-9]+', Number.Integer), |
1528 (r'[0-9]+', Number.Integer), |
1523 (r'"(\\\\|\\"|[^"])*"', String.Double), |
1529 (r'"(\\\\|\\"|[^"])*"', String.Double), |
1524 (r"'(\\\\|\\'|[^'])*'", String.Single) |
1530 (r"'(\\\\|\\'|[^'])*'", String.Single) |