Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JavaScript Node.js Console Lexer #1825

Merged
merged 2 commits into from Aug 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions pygments/lexers/_mapping.py
Expand Up @@ -320,6 +320,7 @@
'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', 'Node.js REPL console session', ('nodejsrepl',), (), ('text/x-nodejsrepl',)),
'NotmuchLexer': ('pygments.lexers.textfmts', 'Notmuch', ('notmuch',), (), ()),
'NuSMVLexer': ('pygments.lexers.smv', 'NuSMV', ('nusmv',), ('*.smv',), ()),
'NumPyLexer': ('pygments.lexers.python', 'NumPy', ('numpy',), (), ()),
Expand Down
71 changes: 67 additions & 4 deletions pygments/lexers/javascript.py
Expand Up @@ -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})')
Expand All @@ -28,6 +29,7 @@
'\u200c\u200d]|\\\\u[a-fA-F0-9]{4})')
JS_IDENT = JS_IDENT_START + '(?:' + JS_IDENT_PART + ')*'

line_re = re.compile('.*?\n')

class JavascriptLexer(RegexLexer):
"""
Expand Down Expand Up @@ -1534,3 +1536,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
"""
cltrudeau marked this conversation as resolved.
Show resolved Hide resolved
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('.')
Anteru marked this conversation as resolved.
Show resolved Hide resolved
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))
20 changes: 20 additions & 0 deletions tests/examplefiles/nodejsrepl/nodejsrepl_test.nodejsrepl
@@ -0,0 +1,20 @@
// Node.js REPL 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
112 changes: 112 additions & 0 deletions tests/examplefiles/nodejsrepl/nodejsrepl_test.nodejsrepl.output

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.