summaryrefslogtreecommitdiff
path: root/pygments
diff options
context:
space:
mode:
authorMax Horn <max@quendi.de>2022-08-19 22:19:14 +0200
committerGitHub <noreply@github.com>2022-08-19 22:19:14 +0200
commitf4976545a11bb126cf201b5942f543359a03cc58 (patch)
tree385e265bc62499f1fe447cc460d6eef0793d6532 /pygments
parentadb90dc65f2f211675af5be27d2a7efdf96c6f44 (diff)
downloadpygments-git-f4976545a11bb126cf201b5942f543359a03cc58.tar.gz
Add GAP console session mode (#2211)
* Add GAP console session mode This is also appropriate for GAP .tst files. Add `analyse_text` methods for `ScilabLexer` and `GAPConsoleLexer` to distinguish Scilab and GAP .tst files * Use explicit name for 'keepends' argument to splitlines
Diffstat (limited to 'pygments')
-rw-r--r--pygments/lexers/_mapping.py1
-rw-r--r--pygments/lexers/algebra.py63
-rw-r--r--pygments/lexers/julia.py2
-rw-r--r--pygments/lexers/make.py2
-rw-r--r--pygments/lexers/matlab.py12
5 files changed, 75 insertions, 5 deletions
diff --git a/pygments/lexers/_mapping.py b/pygments/lexers/_mapping.py
index 577d6645..b87396f9 100644
--- a/pygments/lexers/_mapping.py
+++ b/pygments/lexers/_mapping.py
@@ -168,6 +168,7 @@ LEXERS = {
'FoxProLexer': ('pygments.lexers.foxpro', 'FoxPro', ('foxpro', 'vfp', 'clipper', 'xbase'), ('*.PRG', '*.prg'), ()),
'FreeFemLexer': ('pygments.lexers.freefem', 'Freefem', ('freefem',), ('*.edp',), ('text/x-freefem',)),
'FutharkLexer': ('pygments.lexers.futhark', 'Futhark', ('futhark',), ('*.fut',), ('text/x-futhark',)),
+ 'GAPConsoleLexer': ('pygments.lexers.algebra', 'GAP session', ('gap-console', 'gap-repl'), ('*.tst',), ()),
'GAPLexer': ('pygments.lexers.algebra', 'GAP', ('gap',), ('*.g', '*.gd', '*.gi', '*.gap'), ()),
'GDScriptLexer': ('pygments.lexers.gdscript', 'GDScript', ('gdscript', 'gd'), ('*.gd',), ('text/x-gdscript', 'application/x-gdscript')),
'GLShaderLexer': ('pygments.lexers.graphics', 'GLSL', ('glsl',), ('*.vert', '*.frag', '*.geo'), ('text/x-glslsrc',)),
diff --git a/pygments/lexers/algebra.py b/pygments/lexers/algebra.py
index 59072708..058906d6 100644
--- a/pygments/lexers/algebra.py
+++ b/pygments/lexers/algebra.py
@@ -10,11 +10,11 @@
import re
-from pygments.lexer import RegexLexer, bygroups, words
+from pygments.lexer import Lexer, RegexLexer, bygroups, do_insertions, words
from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
- Number, Punctuation, Whitespace
+ Number, Punctuation, Generic, Whitespace
-__all__ = ['GAPLexer', 'MathematicaLexer', 'MuPADLexer', 'BCLexer']
+__all__ = ['GAPLexer', 'GAPConsoleLexer', 'MathematicaLexer', 'MuPADLexer', 'BCLexer']
class GAPLexer(RegexLexer):
@@ -88,6 +88,63 @@ class GAPLexer(RegexLexer):
return min(score, 1.0)
+class GAPConsoleLexer(Lexer):
+ """
+ For GAP console sessions. Modeled after JuliaConsoleLexer.
+
+ .. versionadded:: 2.14
+ """
+ name = 'GAP session'
+ aliases = ['gap-console', 'gap-repl']
+ filenames = ['*.tst']
+
+ def get_tokens_unprocessed(self, text):
+ gaplexer = GAPLexer(**self.options)
+ start = 0
+ curcode = ''
+ insertions = []
+ output = False
+ error = False
+
+ for line in text.splitlines(keepends=True):
+ if line.startswith('gap> ') or line.startswith('brk> '):
+ insertions.append((len(curcode), [(0, Generic.Prompt, line[:5])]))
+ curcode += line[5:]
+ output = False
+ error = False
+ elif not output and line.startswith('> '):
+ insertions.append((len(curcode), [(0, Generic.Prompt, line[:2])]))
+ curcode += line[2:]
+ else:
+ if curcode:
+ yield from do_insertions(
+ insertions, gaplexer.get_tokens_unprocessed(curcode))
+ curcode = ''
+ insertions = []
+ if line.startswith('Error, ') or error:
+ yield start, Generic.Error, line
+ error = True
+ else:
+ yield start, Generic.Output, line
+ output = True
+ start += len(line)
+
+ if curcode:
+ yield from do_insertions(
+ insertions, gaplexer.get_tokens_unprocessed(curcode))
+
+ # the following is needed to distinguish Scilab and GAP .tst files
+ def analyse_text(text):
+ # GAP prompts are a dead give away, although hypothetical;y a
+ # file in another language could be trying to compare a variable
+ # "gap" as in "gap> 0.1". But that this should happen at the
+ # start of a line seems unlikely...
+ if re.search(r"^gap> ", text):
+ return 0.9
+ else:
+ return 0.0
+
+
class MathematicaLexer(RegexLexer):
"""
Lexer for Mathematica source code.
diff --git a/pygments/lexers/julia.py b/pygments/lexers/julia.py
index 97d9163a..5ac94357 100644
--- a/pygments/lexers/julia.py
+++ b/pygments/lexers/julia.py
@@ -252,7 +252,7 @@ class JuliaConsoleLexer(Lexer):
output = False
error = False
- for line in text.splitlines(True):
+ for line in text.splitlines(keepends=True):
if line.startswith('julia>'):
insertions.append((len(curcode), [(0, Generic.Prompt, line[:6])]))
curcode += line[6:]
diff --git a/pygments/lexers/make.py b/pygments/lexers/make.py
index d9027f3c..e573d519 100644
--- a/pygments/lexers/make.py
+++ b/pygments/lexers/make.py
@@ -44,7 +44,7 @@ class MakefileLexer(Lexer):
def get_tokens_unprocessed(self, text):
ins = []
- lines = text.splitlines(True)
+ lines = text.splitlines(keepends=True)
done = ''
lex = BaseMakefileLexer(**self.options)
backslashflag = False
diff --git a/pygments/lexers/matlab.py b/pygments/lexers/matlab.py
index b70058c5..058661ee 100644
--- a/pygments/lexers/matlab.py
+++ b/pygments/lexers/matlab.py
@@ -3294,3 +3294,15 @@ class ScilabLexer(RegexLexer):
(r'(\s*)([a-zA-Z_]\w*)', bygroups(Text, Name.Function), '#pop'),
],
}
+
+ # the following is needed to distinguish Scilab and GAP .tst files
+ def analyse_text(text):
+ score = 0.0
+
+ # Scilab comments (don't appear in e.g. GAP code)
+ if re.search(r"^\s*//", text):
+ score += 0.1
+ if re.search(r"^\s*/\*", text):
+ score += 0.1
+
+ return min(score, 1.0)