algebra.py 9.7 KB

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