maxima.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. """
  2. pygments.lexers.maxima
  3. ~~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for the computer algebra system Maxima.
  5. Derived from pygments/lexers/algebra.py.
  6. :copyright: Copyright 2006-2024 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__ = ['MaximaLexer']
  14. class MaximaLexer(RegexLexer):
  15. """
  16. A Maxima lexer.
  17. Derived from pygments.lexers.MuPADLexer.
  18. """
  19. name = 'Maxima'
  20. url = 'http://maxima.sourceforge.net'
  21. aliases = ['maxima', 'macsyma']
  22. filenames = ['*.mac', '*.max']
  23. version_added = '2.11'
  24. keywords = ('if', 'then', 'else', 'elseif',
  25. 'do', 'while', 'repeat', 'until',
  26. 'for', 'from', 'to', 'downto', 'step', 'thru')
  27. constants = ('%pi', '%e', '%phi', '%gamma', '%i',
  28. 'und', 'ind', 'infinity', 'inf', 'minf',
  29. 'true', 'false', 'unknown', 'done')
  30. operators = (r'.', r':', r'=', r'#',
  31. r'+', r'-', r'*', r'/', r'^',
  32. r'@', r'>', r'<', r'|', r'!', r"'")
  33. operator_words = ('and', 'or', 'not')
  34. tokens = {
  35. 'root': [
  36. (r'/\*', Comment.Multiline, 'comment'),
  37. (r'"(?:[^"\\]|\\.)*"', String),
  38. (r'\(|\)|\[|\]|\{|\}', Punctuation),
  39. (r'[,;$]', Punctuation),
  40. (words (constants), Name.Constant),
  41. (words (keywords), Keyword),
  42. (words (operators), Operator),
  43. (words (operator_words), Operator.Word),
  44. (r'''(?x)
  45. ((?:[a-zA-Z_#][\w#]*|`[^`]*`)
  46. (?:::[a-zA-Z_#][\w#]*|`[^`]*`)*)(\s*)([(])''',
  47. bygroups(Name.Function, Text.Whitespace, Punctuation)),
  48. (r'''(?x)
  49. (?:[a-zA-Z_#%][\w#%]*|`[^`]*`)
  50. (?:::[a-zA-Z_#%][\w#%]*|`[^`]*`)*''', Name.Variable),
  51. (r'[-+]?(\d*\.\d+([bdefls][-+]?\d+)?|\d+(\.\d*)?[bdefls][-+]?\d+)', Number.Float),
  52. (r'[-+]?\d+', Number.Integer),
  53. (r'\s+', Text.Whitespace),
  54. (r'.', Text)
  55. ],
  56. 'comment': [
  57. (r'[^*/]+', Comment.Multiline),
  58. (r'/\*', Comment.Multiline, '#push'),
  59. (r'\*/', Comment.Multiline, '#pop'),
  60. (r'[*/]', Comment.Multiline)
  61. ]
  62. }
  63. def analyse_text (text):
  64. strength = 0.0
  65. # Input expression terminator.
  66. if re.search (r'\$\s*$', text, re.MULTILINE):
  67. strength += 0.05
  68. # Function definition operator.
  69. if ':=' in text:
  70. strength += 0.02
  71. return strength