algebra.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.algebra
  4. ~~~~~~~~~~~~~~~~~~~~~~~
  5. Lexers for computer algebra systems.
  6. :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. import re
  10. from pygments.lexer import RegexLexer, bygroups, words
  11. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  12. Number, Punctuation
  13. __all__ = ['GAPLexer', 'MathematicaLexer', 'MuPADLexer', 'BCLexer']
  14. class GAPLexer(RegexLexer):
  15. """
  16. For `GAP <http://www.gap-system.org>`_ source code.
  17. .. versionadded:: 2.0
  18. """
  19. name = 'GAP'
  20. aliases = ['gap']
  21. filenames = ['*.g', '*.gd', '*.gi', '*.gap']
  22. tokens = {
  23. 'root': [
  24. (r'#.*$', Comment.Single),
  25. (r'"(?:[^"\\]|\\.)*"', String),
  26. (r'\(|\)|\[|\]|\{|\}', Punctuation),
  27. (r'''(?x)\b(?:
  28. if|then|elif|else|fi|
  29. for|while|do|od|
  30. repeat|until|
  31. break|continue|
  32. function|local|return|end|
  33. rec|
  34. quit|QUIT|
  35. IsBound|Unbind|
  36. TryNextMethod|
  37. Info|Assert
  38. )\b''', Keyword),
  39. (r'''(?x)\b(?:
  40. true|false|fail|infinity
  41. )\b''',
  42. Name.Constant),
  43. (r'''(?x)\b(?:
  44. (Declare|Install)([A-Z][A-Za-z]+)|
  45. BindGlobal|BIND_GLOBAL
  46. )\b''',
  47. Name.Builtin),
  48. (r'\.|,|:=|;|=|\+|-|\*|/|\^|>|<', Operator),
  49. (r'''(?x)\b(?:
  50. and|or|not|mod|in
  51. )\b''',
  52. Operator.Word),
  53. (r'''(?x)
  54. (?:\w+|`[^`]*`)
  55. (?:::\w+|`[^`]*`)*''', Name.Variable),
  56. (r'[0-9]+(?:\.[0-9]*)?(?:e[0-9]+)?', Number),
  57. (r'\.[0-9]+(?:e[0-9]+)?', Number),
  58. (r'.', Text)
  59. ],
  60. }
  61. class MathematicaLexer(RegexLexer):
  62. """
  63. Lexer for `Mathematica <http://www.wolfram.com/mathematica/>`_ source code.
  64. .. versionadded:: 2.0
  65. """
  66. name = 'Mathematica'
  67. aliases = ['mathematica', 'mma', 'nb']
  68. filenames = ['*.nb', '*.cdf', '*.nbp', '*.ma']
  69. mimetypes = ['application/mathematica',
  70. 'application/vnd.wolfram.mathematica',
  71. 'application/vnd.wolfram.mathematica.package',
  72. 'application/vnd.wolfram.cdf']
  73. # http://reference.wolfram.com/mathematica/guide/Syntax.html
  74. operators = (
  75. ";;", "=", "=.", "!=" "==", ":=", "->", ":>", "/.", "+", "-", "*", "/",
  76. "^", "&&", "||", "!", "<>", "|", "/;", "?", "@", "//", "/@", "@@",
  77. "@@@", "~~", "===", "&", "<", ">", "<=", ">=",
  78. )
  79. punctuation = (",", ";", "(", ")", "[", "]", "{", "}")
  80. def _multi_escape(entries):
  81. return '(%s)' % ('|'.join(re.escape(entry) for entry in entries))
  82. tokens = {
  83. 'root': [
  84. (r'(?s)\(\*.*?\*\)', Comment),
  85. (r'([a-zA-Z]+[A-Za-z0-9]*`)', Name.Namespace),
  86. (r'([A-Za-z0-9]*_+[A-Za-z0-9]*)', Name.Variable),
  87. (r'#\d*', Name.Variable),
  88. (r'([a-zA-Z]+[a-zA-Z0-9]*)', Name),
  89. (r'-?\d+\.\d*', Number.Float),
  90. (r'-?\d*\.\d+', Number.Float),
  91. (r'-?\d+', Number.Integer),
  92. (words(operators), Operator),
  93. (words(punctuation), Punctuation),
  94. (r'".*?"', String),
  95. (r'\s+', Text.Whitespace),
  96. ],
  97. }
  98. class MuPADLexer(RegexLexer):
  99. """
  100. A `MuPAD <http://www.mupad.com>`_ lexer.
  101. Contributed by Christopher Creutzig <christopher@creutzig.de>.
  102. .. versionadded:: 0.8
  103. """
  104. name = 'MuPAD'
  105. aliases = ['mupad']
  106. filenames = ['*.mu']
  107. tokens = {
  108. 'root': [
  109. (r'//.*?$', Comment.Single),
  110. (r'/\*', Comment.Multiline, 'comment'),
  111. (r'"(?:[^"\\]|\\.)*"', String),
  112. (r'\(|\)|\[|\]|\{|\}', Punctuation),
  113. (r'''(?x)\b(?:
  114. next|break|end|
  115. axiom|end_axiom|category|end_category|domain|end_domain|inherits|
  116. if|%if|then|elif|else|end_if|
  117. case|of|do|otherwise|end_case|
  118. while|end_while|
  119. repeat|until|end_repeat|
  120. for|from|to|downto|step|end_for|
  121. proc|local|option|save|begin|end_proc|
  122. delete|frame
  123. )\b''', Keyword),
  124. (r'''(?x)\b(?:
  125. DOM_ARRAY|DOM_BOOL|DOM_COMPLEX|DOM_DOMAIN|DOM_EXEC|DOM_EXPR|
  126. DOM_FAIL|DOM_FLOAT|DOM_FRAME|DOM_FUNC_ENV|DOM_HFARRAY|DOM_IDENT|
  127. DOM_INT|DOM_INTERVAL|DOM_LIST|DOM_NIL|DOM_NULL|DOM_POLY|DOM_PROC|
  128. DOM_PROC_ENV|DOM_RAT|DOM_SET|DOM_STRING|DOM_TABLE|DOM_VAR
  129. )\b''', Name.Class),
  130. (r'''(?x)\b(?:
  131. PI|EULER|E|CATALAN|
  132. NIL|FAIL|undefined|infinity|
  133. TRUE|FALSE|UNKNOWN
  134. )\b''',
  135. Name.Constant),
  136. (r'\b(?:dom|procname)\b', Name.Builtin.Pseudo),
  137. (r'\.|,|:|;|=|\+|-|\*|/|\^|@|>|<|\$|\||!|\'|%|~=', Operator),
  138. (r'''(?x)\b(?:
  139. and|or|not|xor|
  140. assuming|
  141. div|mod|
  142. union|minus|intersect|in|subset
  143. )\b''',
  144. Operator.Word),
  145. (r'\b(?:I|RDN_INF|RD_NINF|RD_NAN)\b', Number),
  146. # (r'\b(?:adt|linalg|newDomain|hold)\b', Name.Builtin),
  147. (r'''(?x)
  148. ((?:[a-zA-Z_#][\w#]*|`[^`]*`)
  149. (?:::[a-zA-Z_#][\w#]*|`[^`]*`)*)(\s*)([(])''',
  150. bygroups(Name.Function, Text, Punctuation)),
  151. (r'''(?x)
  152. (?:[a-zA-Z_#][\w#]*|`[^`]*`)
  153. (?:::[a-zA-Z_#][\w#]*|`[^`]*`)*''', Name.Variable),
  154. (r'[0-9]+(?:\.[0-9]*)?(?:e[0-9]+)?', Number),
  155. (r'\.[0-9]+(?:e[0-9]+)?', Number),
  156. (r'.', Text)
  157. ],
  158. 'comment': [
  159. (r'[^*/]', Comment.Multiline),
  160. (r'/\*', Comment.Multiline, '#push'),
  161. (r'\*/', Comment.Multiline, '#pop'),
  162. (r'[*/]', Comment.Multiline)
  163. ],
  164. }
  165. class BCLexer(RegexLexer):
  166. """
  167. A `BC <https://www.gnu.org/software/bc/>`_ lexer.
  168. .. versionadded:: 2.1
  169. """
  170. name = 'BC'
  171. aliases = ['bc']
  172. filenames = ['*.bc']
  173. tokens = {
  174. 'root': [
  175. (r'/\*', Comment.Multiline, 'comment'),
  176. (r'"(?:[^"\\]|\\.)*"', String),
  177. (r'[{}();,]', Punctuation),
  178. (words(('if', 'else', 'while', 'for', 'break', 'continue',
  179. 'halt', 'return', 'define', 'auto', 'print', 'read',
  180. 'length', 'scale', 'sqrt', 'limits', 'quit',
  181. 'warranty'), suffix=r'\b'), Keyword),
  182. (r'\+\+|--|\|\||&&|'
  183. r'([-<>+*%\^/!=])=?', Operator),
  184. # bc doesn't support exponential
  185. (r'[0-9]+(\.[0-9]*)?', Number),
  186. (r'\.[0-9]+', Number),
  187. (r'.', Text)
  188. ],
  189. 'comment': [
  190. (r'[^*/]+', Comment.Multiline),
  191. (r'\*/', Comment.Multiline, '#pop'),
  192. (r'[*/]', Comment.Multiline)
  193. ],
  194. }