diff options
| author | Christopher Trudeau <ctrudeau@arsensa.com> | 2021-05-29 16:03:44 -0400 |
|---|---|---|
| committer | Christopher Trudeau <ctrudeau@arsensa.com> | 2021-05-29 16:03:44 -0400 |
| commit | 18a7fdb2ce1695e471071f7d635cb98de6080ee8 (patch) | |
| tree | aeafaf9ee5ecaefaa7e866ad6756ca69d693bbac | |
| parent | e8803c5e756bf2f78d913d546348bfd2987c918d (diff) | |
| download | pygments-git-18a7fdb2ce1695e471071f7d635cb98de6080ee8.tar.gz | |
Add JavaScript Node.js Console Lexer
| -rw-r--r-- | pygments/lexers/_mapping.py | 1 | ||||
| -rw-r--r-- | pygments/lexers/javascript.py | 70 | ||||
| -rw-r--r-- | tests/examplefiles/nodecon/nodecod_test.nodecon | 20 | ||||
| -rw-r--r-- | tests/examplefiles/nodecon/nodecod_test.nodecon.output | 112 |
4 files changed, 199 insertions, 4 deletions
diff --git a/pygments/lexers/_mapping.py b/pygments/lexers/_mapping.py index aab8bde4..67c8df89 100644 --- a/pygments/lexers/_mapping.py +++ b/pygments/lexers/_mapping.py @@ -320,6 +320,7 @@ LEXERS = { 'NimrodLexer': ('pygments.lexers.nimrod', 'Nimrod', ('nimrod', 'nim'), ('*.nim', '*.nimrod'), ('text/x-nim',)), 'NitLexer': ('pygments.lexers.nit', 'Nit', ('nit',), ('*.nit',), ()), 'NixLexer': ('pygments.lexers.nix', 'Nix', ('nixos', 'nix'), ('*.nix',), ('text/x-nix',)), + 'NodeConsoleLexer': ('pygments.lexers.javascript', 'JavaScript Node.js console session', ('nodecon',), (), ('text/x-nodecon',)), 'NotmuchLexer': ('pygments.lexers.textfmts', 'Notmuch', ('notmuch',), (), ()), 'NuSMVLexer': ('pygments.lexers.smv', 'NuSMV', ('nusmv',), ('*.smv',), ()), 'NumPyLexer': ('pygments.lexers.python', 'NumPy', ('numpy',), (), ()), diff --git a/pygments/lexers/javascript.py b/pygments/lexers/javascript.py index f04d42d4..1ea1f553 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, using, \ - this, words, combined +from pygments.lexer import RegexLexer, Lexer, include, bygroups, default, \ + using, this, words, combined, do_insertions 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', 'KalLexer', 'LiveScriptLexer', 'DartLexer', 'TypeScriptLexer', 'LassoLexer', 'ObjectiveJLexer', - 'CoffeeScriptLexer', 'MaskLexer', 'EarlGreyLexer', 'JuttleLexer'] + '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): """ @@ -1534,3 +1536,63 @@ class JuttleLexer(RegexLexer): ] } + + +class NodeConsoleLexer(Lexer): + """ + For parsing JavaScript within an interactive Node.js shell, such as: + + .. sourcecode:: nodejs + + > let a = 3 + undefined + > a + 3 + > let b = '4' + undefined + > b + '4' + > b == a + false + """ + name = 'JavaScript Node.js console session' + aliases = ['nodejs', 'nodecon'] + mimetypes = ['application/javascript', 'application/x-javascript', + 'text/x-javascript', 'text/javascript'] + + 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)) diff --git a/tests/examplefiles/nodecon/nodecod_test.nodecon b/tests/examplefiles/nodecon/nodecod_test.nodecon new file mode 100644 index 00000000..1442b6f5 --- /dev/null +++ b/tests/examplefiles/nodecon/nodecod_test.nodecon @@ -0,0 +1,20 @@ +// Node.js Console Session +> let a = 3 +undefined +> a +3 +> let b = '4' +undefined +> b +'4' +> b == a +false +> b === a +false +> if(a) { +... console.log(a) +... } +3 +undefined +> c +Uncaught ReferenceError: c is not defined diff --git a/tests/examplefiles/nodecon/nodecod_test.nodecon.output b/tests/examplefiles/nodecon/nodecod_test.nodecon.output new file mode 100644 index 00000000..a812407a --- /dev/null +++ b/tests/examplefiles/nodecon/nodecod_test.nodecon.output @@ -0,0 +1,112 @@ +'' Text +'// Node.js Console Session\n' Comment.Single + +'> ' Generic.Prompt +'let' Keyword.Declaration +' ' Text +'a' Name.Other +' ' Text +'=' Operator +' ' Text +'3' Literal.Number.Float +'\n' Text + +'undefined' Keyword.Constant +'\n' Text + +'> ' Generic.Prompt +'a' Name.Other +'\n' Text + +'3' Literal.Number.Float +'\n' Text + +'> ' Generic.Prompt +'let' Keyword.Declaration +' ' Text +'b' Name.Other +' ' Text +'=' Operator +' ' Text +"'4'" Literal.String.Single +'\n' Text + +'undefined' Keyword.Constant +'\n' Text + +'> ' Generic.Prompt +'b' Name.Other +'\n' Text + +"'4'" Literal.String.Single +'\n' Text + +'> ' Generic.Prompt +'b' Name.Other +' ' Text +'==' Operator +' ' Text +'a' Name.Other +'\n' Text + +'false' Keyword.Constant +'\n' Text + +'> ' Generic.Prompt +'b' Name.Other +' ' Text +'===' Operator +' ' Text +'a' Name.Other +'\n' Text + +'false' Keyword.Constant +'\n' Text + +'> ' Generic.Prompt +'if' Keyword +'(' Punctuation +'a' Name.Other +')' Punctuation +' ' Text +'{' Punctuation +'\n' Text + +'...' Generic.Prompt +' ' Text +'console' Name.Other +'.' Punctuation +'log' Name.Other +'(' Punctuation +'a' Name.Other +')' Punctuation +'\n' Text + +'...' Generic.Prompt +' ' Text +'}' Punctuation +'\n' Text + +'3' Literal.Number.Float +'\n' Text + +'undefined' Keyword.Constant +'\n' Text + +'> ' Generic.Prompt +'c' Name.Other +'\n' Text + +'Uncaught' Name.Other +' ' Text +'ReferenceError' Name.Other +':' Operator +' ' Text +'c' Name.Other +' ' Text +'is' Name.Other +' ' Text +'not' Name.Other +' ' Text +'defined' Name.Other +'\n' Text |
