codekingpro/portable-devtools
114k
1"""2 pygments.lexers.algebra3 ~~~~~~~~~~~~~~~~~~~~~~~4 5 Lexers for computer algebra systems.6 7 :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.8 :license: BSD, see LICENSE for details.9"""10 11import re12 13from pygments.lexer import Lexer, RegexLexer, bygroups, do_insertions, words14from pygments.token import Text, Comment, Operator, Keyword, Name, String, \15 Number, Punctuation, Generic, Whitespace16 17__all__ = ['GAPLexer', 'GAPConsoleLexer', 'MathematicaLexer', 'MuPADLexer',18 'BCLexer']19 20 21class GAPLexer(RegexLexer):22 """23 For GAP source code.24 """25 name = 'GAP'26 url = 'https://www.gap-system.org'27 aliases = ['gap']28 filenames = ['*.g', '*.gd', '*.gi', '*.gap']29 version_added = '2.0'30 31 tokens = {32 'root': [33 (r'#.*$', Comment.Single),34 (r'"(?:[^"\\]|\\.)*"', String),35 (r'\(|\)|\[|\]|\{|\}', Punctuation),36 (r'''(?x)\b(?:37 if|then|elif|else|fi|38 for|while|do|od|39 repeat|until|40 break|continue|41 function|local|return|end|42 rec|43 quit|QUIT|44 IsBound|Unbind|45 TryNextMethod|46 Info|Assert47 )\b''', Keyword),48 (r'''(?x)\b(?:49 true|false|fail|infinity50 )\b''',51 Name.Constant),52 (r'''(?x)\b(?:53 (Declare|Install)([A-Z][A-Za-z]+)|54 BindGlobal|BIND_GLOBAL55 )\b''',56 Name.Builtin),57 (r'\.|,|:=|;|=|\+|-|\*|/|\^|>|<', Operator),58 (r'''(?x)\b(?:59 and|or|not|mod|in60 )\b''',61 Operator.Word),62 (r'''(?x)63 (?:\w+|`[^`]*`)64 (?:::\w+|`[^`]*`)*''', Name.Variable),65 (r'[0-9]+(?:\.[0-9]*)?(?:e[0-9]+)?', Number),66 (r'\.[0-9]+(?:e[0-9]+)?', Number),67 (r'.', Text)68 ],69 }70 71 def analyse_text(text):72 score = 0.073 74 # Declaration part75 if re.search(76 r"(InstallTrueMethod|Declare(Attribute|Category|Filter|Operation" +77 r"|GlobalFunction|Synonym|SynonymAttr|Property))", text78 ):79 score += 0.780 81 # Implementation part82 if re.search(83 r"(DeclareRepresentation|Install(GlobalFunction|Method|" +84 r"ImmediateMethod|OtherMethod)|New(Family|Type)|Objectify)", text85 ):86 score += 0.787 88 return min(score, 1.0)89 90 91class GAPConsoleLexer(Lexer):92 """93 For GAP console sessions. Modeled after JuliaConsoleLexer.94 """95 name = 'GAP session'96 aliases = ['gap-console', 'gap-repl']97 filenames = ['*.tst']98 url = 'https://www.gap-system.org'99 version_added = '2.14'100 101 def get_tokens_unprocessed(self, text):102 gaplexer = GAPLexer(**self.options)103 start = 0104 curcode = ''105 insertions = []106 output = False107 error = False108 109 for line in text.splitlines(keepends=True):110 if line.startswith('gap> ') or line.startswith('brk> '):111 insertions.append((len(curcode), [(0, Generic.Prompt, line[:5])]))112 curcode += line[5:]113 output = False114 error = False115 elif not output and line.startswith('> '):116 insertions.append((len(curcode), [(0, Generic.Prompt, line[:2])]))117 curcode += line[2:]118 else:119 if curcode:120 yield from do_insertions(121 insertions, gaplexer.get_tokens_unprocessed(curcode))122 curcode = ''123 insertions = []124 if line.startswith('Error, ') or error:125 yield start, Generic.Error, line126 error = True127 else:128 yield start, Generic.Output, line129 output = True130 start += len(line)131 132 if curcode:133 yield from do_insertions(134 insertions, gaplexer.get_tokens_unprocessed(curcode))135 136 # the following is needed to distinguish Scilab and GAP .tst files137 def analyse_text(text):138 # GAP prompts are a dead give away, although hypothetical;y a139 # file in another language could be trying to compare a variable140 # "gap" as in "gap> 0.1". But that this should happen at the141 # start of a line seems unlikely...142 if re.search(r"^gap> ", text):143 return 0.9144 else:145 return 0.0146 147 148class MathematicaLexer(RegexLexer):149 """150 Lexer for Mathematica source code.151 """152 name = 'Mathematica'153 url = 'http://www.wolfram.com/mathematica/'154 aliases = ['mathematica', 'mma', 'nb']155 filenames = ['*.nb', '*.cdf', '*.nbp', '*.ma']156 mimetypes = ['application/mathematica',157 'application/vnd.wolfram.mathematica',158 'application/vnd.wolfram.mathematica.package',159 'application/vnd.wolfram.cdf']160 version_added = '2.0'161 162 # http://reference.wolfram.com/mathematica/guide/Syntax.html163 operators = (164 ";;", "=", "=.", "!=" "==", ":=", "->", ":>", "/.", "+", "-", "*", "/",165 "^", "&&", "||", "!", "<>", "|", "/;", "?", "@", "//", "/@", "@@",166 "@@@", "~~", "===", "&", "<", ">", "<=", ">=",167 )168 169 punctuation = (",", ";", "(", ")", "[", "]", "{", "}")170 171 def _multi_escape(entries):172 return '({})'.format('|'.join(re.escape(entry) for entry in entries))173 174 tokens = {175 'root': [176 (r'(?s)\(\*.*?\*\)', Comment),177 178 (r'([a-zA-Z]+[A-Za-z0-9]*`)', Name.Namespace),179 (r'([A-Za-z0-9]*_+[A-Za-z0-9]*)', Name.Variable),180 (r'#\d*', Name.Variable),181 (r'([a-zA-Z]+[a-zA-Z0-9]*)', Name),182 183 (r'-?\d+\.\d*', Number.Float),184 (r'-?\d*\.\d+', Number.Float),185 (r'-?\d+', Number.Integer),186 187 (words(operators), Operator),188 (words(punctuation), Punctuation),189 (r'".*?"', String),190 (r'\s+', Text.Whitespace),191 ],192 }193 194 195class MuPADLexer(RegexLexer):196 """197 A MuPAD lexer.198 Contributed by Christopher Creutzig <christopher@creutzig.de>.199 """200 name = 'MuPAD'201 url = 'http://www.mupad.com'202 aliases = ['mupad']203 filenames = ['*.mu']204 version_added = '0.8'205 206 tokens = {207 'root': [208 (r'//.*?$', Comment.Single),209 (r'/\*', Comment.Multiline, 'comment'),210 (r'"(?:[^"\\]|\\.)*"', String),211 (r'\(|\)|\[|\]|\{|\}', Punctuation),212 (r'''(?x)\b(?:213 next|break|end|214 axiom|end_axiom|category|end_category|domain|end_domain|inherits|215 if|%if|then|elif|else|end_if|216 case|of|do|otherwise|end_case|217 while|end_while|218 repeat|until|end_repeat|219 for|from|to|downto|step|end_for|220 proc|local|option|save|begin|end_proc|221 delete|frame222 )\b''', Keyword),223 (r'''(?x)\b(?:224 DOM_ARRAY|DOM_BOOL|DOM_COMPLEX|DOM_DOMAIN|DOM_EXEC|DOM_EXPR|225 DOM_FAIL|DOM_FLOAT|DOM_FRAME|DOM_FUNC_ENV|DOM_HFARRAY|DOM_IDENT|226 DOM_INT|DOM_INTERVAL|DOM_LIST|DOM_NIL|DOM_NULL|DOM_POLY|DOM_PROC|227 DOM_PROC_ENV|DOM_RAT|DOM_SET|DOM_STRING|DOM_TABLE|DOM_VAR228 )\b''', Name.Class),229 (r'''(?x)\b(?:230 PI|EULER|E|CATALAN|231 NIL|FAIL|undefined|infinity|232 TRUE|FALSE|UNKNOWN233 )\b''',234 Name.Constant),235 (r'\b(?:dom|procname)\b', Name.Builtin.Pseudo),236 (r'\.|,|:|;|=|\+|-|\*|/|\^|@|>|<|\$|\||!|\'|%|~=', Operator),237 (r'''(?x)\b(?:238 and|or|not|xor|239 assuming|240 div|mod|241 union|minus|intersect|in|subset242 )\b''',243 Operator.Word),244 (r'\b(?:I|RDN_INF|RD_NINF|RD_NAN)\b', Number),245 # (r'\b(?:adt|linalg|newDomain|hold)\b', Name.Builtin),246 (r'''(?x)247 ((?:[a-zA-Z_#][\w#]*|`[^`]*`)248 (?:::[a-zA-Z_#][\w#]*|`[^`]*`)*)(\s*)([(])''',249 bygroups(Name.Function, Text, Punctuation)),250 (r'''(?x)251 (?:[a-zA-Z_#][\w#]*|`[^`]*`)252 (?:::[a-zA-Z_#][\w#]*|`[^`]*`)*''', Name.Variable),253 (r'[0-9]+(?:\.[0-9]*)?(?:e[0-9]+)?', Number),254 (r'\.[0-9]+(?:e[0-9]+)?', Number),255 (r'\s+', Whitespace),256 (r'.', Text)257 ],258 'comment': [259 (r'[^/*]+', Comment.Multiline),260 (r'/\*', Comment.Multiline, '#push'),261 (r'\*/', Comment.Multiline, '#pop'),262 (r'[*/]', Comment.Multiline)263 ],264 }265 266 267class BCLexer(RegexLexer):268 """269 A BC lexer.270 """271 name = 'BC'272 url = 'https://www.gnu.org/software/bc/'273 aliases = ['bc']274 filenames = ['*.bc']275 version_added = '2.1'276 277 tokens = {278 'root': [279 (r'/\*', Comment.Multiline, 'comment'),280 (r'"(?:[^"\\]|\\.)*"', String),281 (r'[{}();,]', Punctuation),282 (words(('if', 'else', 'while', 'for', 'break', 'continue',283 'halt', 'return', 'define', 'auto', 'print', 'read',284 'length', 'scale', 'sqrt', 'limits', 'quit',285 'warranty'), suffix=r'\b'), Keyword),286 (r'\+\+|--|\|\||&&|'287 r'([-<>+*%\^/!=])=?', Operator),288 # bc doesn't support exponential289 (r'[0-9]+(\.[0-9]*)?', Number),290 (r'\.[0-9]+', Number),291 (r'.', Text)292 ],293 'comment': [294 (r'[^*/]+', Comment.Multiline),295 (r'\*/', Comment.Multiline, '#pop'),296 (r'[*/]', Comment.Multiline)297 ],298 }299 