summaryrefslogtreecommitdiff
path: root/pygments/lexers/javascript.py
diff options
context:
space:
mode:
authorMatthäus G. Chajdas <dev@anteru.net>2021-08-08 15:30:43 +0200
committerMatthäus G. Chajdas <dev@anteru.net>2021-08-08 15:30:43 +0200
commitb5bf0f1fc79f0e6aec5e47dbdc0e31e147cecfde (patch)
tree7f72ed25d0026b070b80d99f8636370b3e22ac6e /pygments/lexers/javascript.py
parent1f2ab0496366932e4cbf450875bddf12907d0930 (diff)
parentfbdcfa8066f98d896c2a746aa4fc6d5869f7e8d2 (diff)
downloadpygments-git-b5bf0f1fc79f0e6aec5e47dbdc0e31e147cecfde.tar.gz
Merge branch 'master' of https://github.com/cltrudeau/pygments into cltrudeau-master
Diffstat (limited to 'pygments/lexers/javascript.py')
-rw-r--r--pygments/lexers/javascript.py75
1 files changed, 69 insertions, 6 deletions
diff --git a/pygments/lexers/javascript.py b/pygments/lexers/javascript.py
index 485fc4cf..7ddd1148 100644
--- a/pygments/lexers/javascript.py
+++ b/pygments/lexers/javascript.py
@@ -10,16 +10,17 @@
import re
-from pygments.lexer import RegexLexer, include, bygroups, default, inherit, using, \
- this, words, combined
+from pygments.lexer import bygroups, combined, default, do_insertions, include, \
+ inherit, Lexer, RegexLexer, this, using, words
from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
- Number, Punctuation, Other
+ Number, Punctuation, Other, Generic
from pygments.util import get_bool_opt
import pygments.unistring as uni
-__all__ = ['JavascriptLexer', 'TypeScriptLexer', 'KalLexer', 'LiveScriptLexer',
- 'DartLexer', 'LassoLexer', 'ObjectiveJLexer', 'CoffeeScriptLexer',
- 'MaskLexer', 'EarlGreyLexer', 'JuttleLexer']
+__all__ = ['JavascriptLexer', 'KalLexer', 'LiveScriptLexer', 'DartLexer',
+ 'TypeScriptLexer', 'LassoLexer', 'ObjectiveJLexer',
+ 'CoffeeScriptLexer', 'MaskLexer', 'EarlGreyLexer', 'JuttleLexer',
+ 'NodeConsoleLexer']
JS_IDENT_START = ('(?:[$_' + uni.combine('Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nl') +
']|\\\\u[a-fA-F0-9]{4})')
@@ -28,6 +29,7 @@ JS_IDENT_PART = ('(?:[$' + uni.combine('Lu', 'Ll', 'Lt', 'Lm', 'Lo', 'Nl',
'\u200c\u200d]|\\\\u[a-fA-F0-9]{4})')
JS_IDENT = JS_IDENT_START + '(?:' + JS_IDENT_PART + ')*'
+line_re = re.compile('.*?\n')
class JavascriptLexer(RegexLexer):
"""
@@ -1489,3 +1491,64 @@ class JuttleLexer(RegexLexer):
]
}
+
+
+class NodeConsoleLexer(Lexer):
+ """
+ For parsing within an interactive Node.js REPL, such as:
+
+ .. sourcecode:: nodejsrepl
+
+ > let a = 3
+ undefined
+ > a
+ 3
+ > let b = '4'
+ undefined
+ > b
+ '4'
+ > b == a
+ false
+
+ .. versionadded: 2.10
+ """
+ name = 'Node.js REPL console session'
+ aliases = ['nodejsrepl', ]
+ mimetypes = ['text/x-nodejsrepl', ]
+
+ def get_tokens_unprocessed(self, text):
+ jslexer = JavascriptLexer(**self.options)
+
+ curcode = ''
+ insertions = []
+
+ for match in line_re.finditer(text):
+ line = match.group()
+ if line.startswith('> '):
+ insertions.append((len(curcode),
+ [(0, Generic.Prompt, line[:2])]))
+
+ curcode += line[2:]
+ elif line.startswith('...'):
+ # node does a nested ... thing depending on depth
+ code = line.lstrip('.')
+ lead = len(line) - len(code)
+
+ insertions.append((len(curcode),
+ [(0, Generic.Prompt, line[:lead])]))
+
+ curcode += code
+ else:
+ if curcode:
+ yield from do_insertions(insertions,
+ jslexer.get_tokens_unprocessed(curcode))
+
+ curcode = ''
+ insertions = []
+
+ yield from do_insertions([],
+ jslexer.get_tokens_unprocessed(line))
+
+ if curcode:
+ yield from do_insertions(insertions,
+ jslexer.get_tokens_unprocessed(curcode))