algebra.py 9.7 KB

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