algebra.py 7.5 KB

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