algebra.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. """
  2. pygments.lexers.algebra
  3. ~~~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for computer algebra systems.
  5. :copyright: Copyright 2006-2023 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. .. versionadded:: 2.0
  18. """
  19. name = 'GAP'
  20. url = 'http://www.gap-system.org'
  21. aliases = ['gap']
  22. filenames = ['*.g', '*.gd', '*.gi', '*.gap']
  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. .. versionadded:: 2.14
  81. """
  82. name = 'GAP session'
  83. aliases = ['gap-console', 'gap-repl']
  84. filenames = ['*.tst']
  85. def get_tokens_unprocessed(self, text):
  86. gaplexer = GAPLexer(**self.options)
  87. start = 0
  88. curcode = ''
  89. insertions = []
  90. output = False
  91. error = False
  92. for line in text.splitlines(keepends=True):
  93. if line.startswith('gap> ') or line.startswith('brk> '):
  94. insertions.append((len(curcode), [(0, Generic.Prompt, line[:5])]))
  95. curcode += line[5:]
  96. output = False
  97. error = False
  98. elif not output and line.startswith('> '):
  99. insertions.append((len(curcode), [(0, Generic.Prompt, line[:2])]))
  100. curcode += line[2:]
  101. else:
  102. if curcode:
  103. yield from do_insertions(
  104. insertions, gaplexer.get_tokens_unprocessed(curcode))
  105. curcode = ''
  106. insertions = []
  107. if line.startswith('Error, ') or error:
  108. yield start, Generic.Error, line
  109. error = True
  110. else:
  111. yield start, Generic.Output, line
  112. output = True
  113. start += len(line)
  114. if curcode:
  115. yield from do_insertions(
  116. insertions, gaplexer.get_tokens_unprocessed(curcode))
  117. # the following is needed to distinguish Scilab and GAP .tst files
  118. def analyse_text(text):
  119. # GAP prompts are a dead give away, although hypothetical;y a
  120. # file in another language could be trying to compare a variable
  121. # "gap" as in "gap> 0.1". But that this should happen at the
  122. # start of a line seems unlikely...
  123. if re.search(r"^gap> ", text):
  124. return 0.9
  125. else:
  126. return 0.0
  127. class MathematicaLexer(RegexLexer):
  128. """
  129. Lexer for Mathematica source code.
  130. .. versionadded:: 2.0
  131. """
  132. name = 'Mathematica'
  133. url = 'http://www.wolfram.com/mathematica/'
  134. aliases = ['mathematica', 'mma', 'nb']
  135. filenames = ['*.nb', '*.cdf', '*.nbp', '*.ma']
  136. mimetypes = ['application/mathematica',
  137. 'application/vnd.wolfram.mathematica',
  138. 'application/vnd.wolfram.mathematica.package',
  139. 'application/vnd.wolfram.cdf']
  140. # http://reference.wolfram.com/mathematica/guide/Syntax.html
  141. operators = (
  142. ";;", "=", "=.", "!=" "==", ":=", "->", ":>", "/.", "+", "-", "*", "/",
  143. "^", "&&", "||", "!", "<>", "|", "/;", "?", "@", "//", "/@", "@@",
  144. "@@@", "~~", "===", "&", "<", ">", "<=", ">=",
  145. )
  146. punctuation = (",", ";", "(", ")", "[", "]", "{", "}")
  147. def _multi_escape(entries):
  148. return '(%s)' % ('|'.join(re.escape(entry) for entry in entries))
  149. tokens = {
  150. 'root': [
  151. (r'(?s)\(\*.*?\*\)', Comment),
  152. (r'([a-zA-Z]+[A-Za-z0-9]*`)', Name.Namespace),
  153. (r'([A-Za-z0-9]*_+[A-Za-z0-9]*)', Name.Variable),
  154. (r'#\d*', Name.Variable),
  155. (r'([a-zA-Z]+[a-zA-Z0-9]*)', Name),
  156. (r'-?\d+\.\d*', Number.Float),
  157. (r'-?\d*\.\d+', Number.Float),
  158. (r'-?\d+', Number.Integer),
  159. (words(operators), Operator),
  160. (words(punctuation), Punctuation),
  161. (r'".*?"', String),
  162. (r'\s+', Text.Whitespace),
  163. ],
  164. }
  165. class MuPADLexer(RegexLexer):
  166. """
  167. A MuPAD lexer.
  168. Contributed by Christopher Creutzig <christopher@creutzig.de>.
  169. .. versionadded:: 0.8
  170. """
  171. name = 'MuPAD'
  172. url = 'http://www.mupad.com'
  173. aliases = ['mupad']
  174. filenames = ['*.mu']
  175. tokens = {
  176. 'root': [
  177. (r'//.*?$', Comment.Single),
  178. (r'/\*', Comment.Multiline, 'comment'),
  179. (r'"(?:[^"\\]|\\.)*"', String),
  180. (r'\(|\)|\[|\]|\{|\}', Punctuation),
  181. (r'''(?x)\b(?:
  182. next|break|end|
  183. axiom|end_axiom|category|end_category|domain|end_domain|inherits|
  184. if|%if|then|elif|else|end_if|
  185. case|of|do|otherwise|end_case|
  186. while|end_while|
  187. repeat|until|end_repeat|
  188. for|from|to|downto|step|end_for|
  189. proc|local|option|save|begin|end_proc|
  190. delete|frame
  191. )\b''', Keyword),
  192. (r'''(?x)\b(?:
  193. DOM_ARRAY|DOM_BOOL|DOM_COMPLEX|DOM_DOMAIN|DOM_EXEC|DOM_EXPR|
  194. DOM_FAIL|DOM_FLOAT|DOM_FRAME|DOM_FUNC_ENV|DOM_HFARRAY|DOM_IDENT|
  195. DOM_INT|DOM_INTERVAL|DOM_LIST|DOM_NIL|DOM_NULL|DOM_POLY|DOM_PROC|
  196. DOM_PROC_ENV|DOM_RAT|DOM_SET|DOM_STRING|DOM_TABLE|DOM_VAR
  197. )\b''', Name.Class),
  198. (r'''(?x)\b(?:
  199. PI|EULER|E|CATALAN|
  200. NIL|FAIL|undefined|infinity|
  201. TRUE|FALSE|UNKNOWN
  202. )\b''',
  203. Name.Constant),
  204. (r'\b(?:dom|procname)\b', Name.Builtin.Pseudo),
  205. (r'\.|,|:|;|=|\+|-|\*|/|\^|@|>|<|\$|\||!|\'|%|~=', Operator),
  206. (r'''(?x)\b(?:
  207. and|or|not|xor|
  208. assuming|
  209. div|mod|
  210. union|minus|intersect|in|subset
  211. )\b''',
  212. Operator.Word),
  213. (r'\b(?:I|RDN_INF|RD_NINF|RD_NAN)\b', Number),
  214. # (r'\b(?:adt|linalg|newDomain|hold)\b', Name.Builtin),
  215. (r'''(?x)
  216. ((?:[a-zA-Z_#][\w#]*|`[^`]*`)
  217. (?:::[a-zA-Z_#][\w#]*|`[^`]*`)*)(\s*)([(])''',
  218. bygroups(Name.Function, Text, Punctuation)),
  219. (r'''(?x)
  220. (?:[a-zA-Z_#][\w#]*|`[^`]*`)
  221. (?:::[a-zA-Z_#][\w#]*|`[^`]*`)*''', Name.Variable),
  222. (r'[0-9]+(?:\.[0-9]*)?(?:e[0-9]+)?', Number),
  223. (r'\.[0-9]+(?:e[0-9]+)?', Number),
  224. (r'\s+', Whitespace),
  225. (r'.', Text)
  226. ],
  227. 'comment': [
  228. (r'[^/*]+', Comment.Multiline),
  229. (r'/\*', Comment.Multiline, '#push'),
  230. (r'\*/', Comment.Multiline, '#pop'),
  231. (r'[*/]', Comment.Multiline)
  232. ],
  233. }
  234. class BCLexer(RegexLexer):
  235. """
  236. A BC lexer.
  237. .. versionadded:: 2.1
  238. """
  239. name = 'BC'
  240. url = 'https://www.gnu.org/software/bc/'
  241. aliases = ['bc']
  242. filenames = ['*.bc']
  243. tokens = {
  244. 'root': [
  245. (r'/\*', Comment.Multiline, 'comment'),
  246. (r'"(?:[^"\\]|\\.)*"', String),
  247. (r'[{}();,]', Punctuation),
  248. (words(('if', 'else', 'while', 'for', 'break', 'continue',
  249. 'halt', 'return', 'define', 'auto', 'print', 'read',
  250. 'length', 'scale', 'sqrt', 'limits', 'quit',
  251. 'warranty'), suffix=r'\b'), Keyword),
  252. (r'\+\+|--|\|\||&&|'
  253. r'([-<>+*%\^/!=])=?', Operator),
  254. # bc doesn't support exponential
  255. (r'[0-9]+(\.[0-9]*)?', Number),
  256. (r'\.[0-9]+', Number),
  257. (r'.', Text)
  258. ],
  259. 'comment': [
  260. (r'[^*/]+', Comment.Multiline),
  261. (r'\*/', Comment.Multiline, '#pop'),
  262. (r'[*/]', Comment.Multiline)
  263. ],
  264. }