maple.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. """
  2. pygments.lexers.maple
  3. ~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for Maple.
  5. :copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import words, bygroups, ExtendedRegexLexer
  9. from pygments.token import Comment, Name, String, Whitespace, Operator, Punctuation, Number, Keyword
  10. __all__ = ['MapleLexer']
  11. class MapleLexer(ExtendedRegexLexer):
  12. """
  13. Lexer for Maple.
  14. """
  15. name = 'Maple'
  16. aliases = ['maple']
  17. filenames = ['*.mpl', '*.mi', '*.mm']
  18. mimetypes = ['text/x-maple']
  19. url = 'https://www.maplesoft.com/products/Maple/'
  20. version_added = '2.19'
  21. keywords = ('and',
  22. 'assuming',
  23. 'break',
  24. 'by',
  25. 'catch',
  26. 'description',
  27. 'do',
  28. 'done',
  29. 'elif',
  30. 'else',
  31. 'end',
  32. 'error',
  33. 'export',
  34. 'fi',
  35. 'finally',
  36. 'for',
  37. 'from',
  38. 'global',
  39. 'if',
  40. 'implies',
  41. 'in',
  42. 'intersect',
  43. 'local',
  44. 'minus',
  45. 'mod',
  46. 'module',
  47. 'next',
  48. 'not',
  49. 'od',
  50. 'option',
  51. 'options',
  52. 'or',
  53. 'proc',
  54. 'quit',
  55. 'read',
  56. 'return',
  57. 'save',
  58. 'stop',
  59. 'subset',
  60. 'then',
  61. 'to',
  62. 'try',
  63. 'union',
  64. 'use',
  65. 'uses',
  66. 'while',
  67. 'xor')
  68. builtins = ('abs',
  69. 'add',
  70. 'addressof',
  71. 'anames',
  72. 'and',
  73. 'andmap',
  74. 'andseq',
  75. 'appendto',
  76. 'Array',
  77. 'array',
  78. 'ArrayOptions',
  79. 'assemble',
  80. 'ASSERT',
  81. 'assign',
  82. 'assigned',
  83. 'attributes',
  84. 'cat',
  85. 'ceil',
  86. 'coeff',
  87. 'coeffs',
  88. 'conjugate',
  89. 'convert',
  90. 'CopySign',
  91. 'DEBUG',
  92. 'debugopts',
  93. 'Default0',
  94. 'DefaultOverflow',
  95. 'DefaultUnderflow',
  96. 'degree',
  97. 'denom',
  98. 'diff',
  99. 'disassemble',
  100. 'divide',
  101. 'done',
  102. 'entries',
  103. 'EqualEntries',
  104. 'eval',
  105. 'evalb',
  106. 'evalf',
  107. 'evalhf',
  108. 'evalindets',
  109. 'evaln',
  110. 'expand',
  111. 'exports',
  112. 'factorial',
  113. 'floor',
  114. 'frac',
  115. 'frem',
  116. 'FromInert',
  117. 'frontend',
  118. 'gc',
  119. 'genpoly',
  120. 'has',
  121. 'hastype',
  122. 'hfarray',
  123. 'icontent',
  124. 'igcd',
  125. 'ilcm',
  126. 'ilog10',
  127. 'Im',
  128. 'implies',
  129. 'indets',
  130. 'indices',
  131. 'intersect',
  132. 'iolib',
  133. 'iquo',
  134. 'irem',
  135. 'iroot',
  136. 'iroot',
  137. 'isqrt',
  138. 'kernelopts',
  139. 'lcoeff',
  140. 'ldegree',
  141. 'length',
  142. 'lexorder',
  143. 'lhs',
  144. 'lowerbound',
  145. 'lprint',
  146. 'macro',
  147. 'map',
  148. 'max',
  149. 'maxnorm',
  150. 'member',
  151. 'membertype',
  152. 'min',
  153. 'minus',
  154. 'mod',
  155. 'modp',
  156. 'modp1',
  157. 'modp2',
  158. 'mods',
  159. 'mul',
  160. 'NextAfter',
  161. 'nops',
  162. 'normal',
  163. 'not',
  164. 'numboccur',
  165. 'numelems',
  166. 'numer',
  167. 'NumericClass',
  168. 'NumericEvent',
  169. 'NumericEventHandler',
  170. 'NumericStatus',
  171. 'op',
  172. 'or',
  173. 'order',
  174. 'OrderedNE',
  175. 'ormap',
  176. 'orseq',
  177. 'parse',
  178. 'piecewise',
  179. 'pointto',
  180. 'print',
  181. 'quit',
  182. 'Re',
  183. 'readlib',
  184. 'Record',
  185. 'remove',
  186. 'rhs',
  187. 'round',
  188. 'rtable',
  189. 'rtable_elems',
  190. 'rtable_eval',
  191. 'rtable_indfns',
  192. 'rtable_num_elems',
  193. 'rtable_options',
  194. 'rtable_redim',
  195. 'rtable_scanblock',
  196. 'rtable_set_indfn',
  197. 'rtable_split_unit',
  198. 'savelib',
  199. 'Scale10',
  200. 'Scale2',
  201. 'SDMPolynom',
  202. 'searchtext',
  203. 'SearchText',
  204. 'select',
  205. 'selectremove',
  206. 'seq',
  207. 'series',
  208. 'setattribute',
  209. 'SFloatExponent',
  210. 'SFloatMantissa',
  211. 'sign',
  212. 'sort',
  213. 'ssystem',
  214. 'stop',
  215. 'String',
  216. 'subs',
  217. 'subset',
  218. 'subsindets',
  219. 'subsop',
  220. 'substring',
  221. 'system',
  222. 'table',
  223. 'taylor',
  224. 'tcoeff',
  225. 'time',
  226. 'timelimit',
  227. 'ToInert',
  228. 'traperror',
  229. 'trunc',
  230. 'type',
  231. 'typematch',
  232. 'unames',
  233. 'unassign',
  234. 'union',
  235. 'Unordered',
  236. 'upperbound',
  237. 'userinfo',
  238. 'writeto',
  239. 'xor',
  240. 'xormap',
  241. 'xorseq')
  242. def delayed_callback(self, match, ctx):
  243. yield match.start(1), Punctuation, match.group(1) # quote
  244. ctx.pos = match.start(2)
  245. orig_end = ctx.end
  246. ctx.end = match.end(2)
  247. yield from self.get_tokens_unprocessed(context=ctx)
  248. yield match.end(2), Punctuation, match.group(1) # quote
  249. ctx.pos = match.end()
  250. ctx.end = orig_end
  251. tokens = {
  252. 'root': [
  253. (r'#.*\n', Comment.Single),
  254. (r'\(\*', Comment.Multiline, 'comment'),
  255. (r'"(\\.|.|\s)*?"', String),
  256. (r"('+)((.|\n)*?)\1", delayed_callback),
  257. (r'`(\\`|.)*?`', Name),
  258. (words(keywords, prefix=r'\b', suffix=r'\b'), Keyword),
  259. (words(builtins, prefix=r'\b', suffix=r'\b'), Name.Builtin),
  260. (r'[a-zA-Z_][a-zA-Z0-9_]*', Name),
  261. (r'(:=|\*\*|@@|<=|>=|<>|->|::|\.\.|&\+|[\+\-\*\.\^\$/@&,:=<>%~])', Operator),
  262. (r'[;^!@$\(\)\[\]{}|_\\#?]+', Punctuation),
  263. (r'(\d+)(\.\.)', bygroups(Number.Integer, Punctuation)),
  264. (r'(\d*\.\d+|\d+\.\d*)([eE][+-]?\d+)?', Number.Float),
  265. (r'\d+', Number.Integer),
  266. (r'\s+', Whitespace),
  267. ],
  268. 'comment': [
  269. (r'.*\(\*', Comment.Multiline, '#push'),
  270. (r'.*\*\)', Comment.Multiline, '#pop'),
  271. (r'.*\n', Comment.Multiline),
  272. ]
  273. }
  274. def analyse_text(text):
  275. if ':=' in text:
  276. return 0.1