monte.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. """
  2. pygments.lexers.monte
  3. ~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for the Monte programming language.
  5. :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.token import Comment, Error, Keyword, Name, Number, Operator, \
  9. Punctuation, String, Whitespace
  10. from pygments.lexer import RegexLexer, include, words
  11. __all__ = ['MonteLexer']
  12. # `var` handled separately
  13. # `interface` handled separately
  14. _declarations = ['bind', 'def', 'fn', 'object']
  15. _methods = ['method', 'to']
  16. _keywords = [
  17. 'as', 'break', 'catch', 'continue', 'else', 'escape', 'exit', 'exports',
  18. 'extends', 'finally', 'for', 'guards', 'if', 'implements', 'import',
  19. 'in', 'match', 'meta', 'pass', 'return', 'switch', 'try', 'via', 'when',
  20. 'while',
  21. ]
  22. _operators = [
  23. # Unary
  24. '~', '!',
  25. # Binary
  26. '+', '-', '*', '/', '%', '**', '&', '|', '^', '<<', '>>',
  27. # Binary augmented
  28. '+=', '-=', '*=', '/=', '%=', '**=', '&=', '|=', '^=', '<<=', '>>=',
  29. # Comparison
  30. '==', '!=', '<', '<=', '>', '>=', '<=>',
  31. # Patterns and assignment
  32. ':=', '?', '=~', '!~', '=>',
  33. # Calls and sends
  34. '.', '<-', '->',
  35. ]
  36. _escape_pattern = (
  37. r'(?:\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|'
  38. r'\\["\'\\bftnr])')
  39. # _char = _escape_chars + [('.', String.Char)]
  40. _identifier = r'[_a-zA-Z]\w*'
  41. _constants = [
  42. # Void constants
  43. 'null',
  44. # Bool constants
  45. 'false', 'true',
  46. # Double constants
  47. 'Infinity', 'NaN',
  48. # Special objects
  49. 'M', 'Ref', 'throw', 'traceln',
  50. ]
  51. _guards = [
  52. 'Any', 'Binding', 'Bool', 'Bytes', 'Char', 'DeepFrozen', 'Double',
  53. 'Empty', 'Int', 'List', 'Map', 'Near', 'NullOk', 'Same', 'Selfless',
  54. 'Set', 'Str', 'SubrangeGuard', 'Transparent', 'Void',
  55. ]
  56. _safeScope = [
  57. '_accumulateList', '_accumulateMap', '_auditedBy', '_bind',
  58. '_booleanFlow', '_comparer', '_equalizer', '_iterForever', '_loop',
  59. '_makeBytes', '_makeDouble', '_makeFinalSlot', '_makeInt', '_makeList',
  60. '_makeMap', '_makeMessageDesc', '_makeOrderedSpace', '_makeParamDesc',
  61. '_makeProtocolDesc', '_makeSourceSpan', '_makeString', '_makeVarSlot',
  62. '_makeVerbFacet', '_mapExtract', '_matchSame', '_quasiMatcher',
  63. '_slotToBinding', '_splitList', '_suchThat', '_switchFailed',
  64. '_validateFor', 'b__quasiParser', 'eval', 'import', 'm__quasiParser',
  65. 'makeBrandPair', 'makeLazySlot', 'safeScope', 'simple__quasiParser',
  66. ]
  67. class MonteLexer(RegexLexer):
  68. """
  69. Lexer for the Monte programming language.
  70. .. versionadded:: 2.2
  71. """
  72. name = 'Monte'
  73. url = 'https://monte.readthedocs.io/'
  74. aliases = ['monte']
  75. filenames = ['*.mt']
  76. tokens = {
  77. 'root': [
  78. # Comments
  79. (r'#[^\n]*\n', Comment),
  80. # Docstrings
  81. # Apologies for the non-greedy matcher here.
  82. (r'/\*\*.*?\*/', String.Doc),
  83. # `var` declarations
  84. (r'\bvar\b', Keyword.Declaration, 'var'),
  85. # `interface` declarations
  86. (r'\binterface\b', Keyword.Declaration, 'interface'),
  87. # method declarations
  88. (words(_methods, prefix='\\b', suffix='\\b'),
  89. Keyword, 'method'),
  90. # All other declarations
  91. (words(_declarations, prefix='\\b', suffix='\\b'),
  92. Keyword.Declaration),
  93. # Keywords
  94. (words(_keywords, prefix='\\b', suffix='\\b'), Keyword),
  95. # Literals
  96. ('[+-]?0x[_0-9a-fA-F]+', Number.Hex),
  97. (r'[+-]?[_0-9]+\.[_0-9]*([eE][+-]?[_0-9]+)?', Number.Float),
  98. ('[+-]?[_0-9]+', Number.Integer),
  99. ("'", String.Double, 'char'),
  100. ('"', String.Double, 'string'),
  101. # Quasiliterals
  102. ('`', String.Backtick, 'ql'),
  103. # Operators
  104. (words(_operators), Operator),
  105. # Verb operators
  106. (_identifier + '=', Operator.Word),
  107. # Safe scope constants
  108. (words(_constants, prefix='\\b', suffix='\\b'),
  109. Keyword.Pseudo),
  110. # Safe scope guards
  111. (words(_guards, prefix='\\b', suffix='\\b'), Keyword.Type),
  112. # All other safe scope names
  113. (words(_safeScope, prefix='\\b', suffix='\\b'),
  114. Name.Builtin),
  115. # Identifiers
  116. (_identifier, Name),
  117. # Punctuation
  118. (r'\(|\)|\{|\}|\[|\]|:|,', Punctuation),
  119. # Whitespace
  120. (' +', Whitespace),
  121. # Definite lexer errors
  122. ('=', Error),
  123. ],
  124. 'char': [
  125. # It is definitely an error to have a char of width == 0.
  126. ("'", Error, 'root'),
  127. (_escape_pattern, String.Escape, 'charEnd'),
  128. ('.', String.Char, 'charEnd'),
  129. ],
  130. 'charEnd': [
  131. ("'", String.Char, '#pop:2'),
  132. # It is definitely an error to have a char of width > 1.
  133. ('.', Error),
  134. ],
  135. # The state of things coming into an interface.
  136. 'interface': [
  137. (' +', Whitespace),
  138. (_identifier, Name.Class, '#pop'),
  139. include('root'),
  140. ],
  141. # The state of things coming into a method.
  142. 'method': [
  143. (' +', Whitespace),
  144. (_identifier, Name.Function, '#pop'),
  145. include('root'),
  146. ],
  147. 'string': [
  148. ('"', String.Double, 'root'),
  149. (_escape_pattern, String.Escape),
  150. (r'\n', String.Double),
  151. ('.', String.Double),
  152. ],
  153. 'ql': [
  154. ('`', String.Backtick, 'root'),
  155. (r'\$' + _escape_pattern, String.Escape),
  156. (r'\$\$', String.Escape),
  157. (r'@@', String.Escape),
  158. (r'\$\{', String.Interpol, 'qlNest'),
  159. (r'@\{', String.Interpol, 'qlNest'),
  160. (r'\$' + _identifier, Name),
  161. ('@' + _identifier, Name),
  162. ('.', String.Backtick),
  163. ],
  164. 'qlNest': [
  165. (r'\}', String.Interpol, '#pop'),
  166. include('root'),
  167. ],
  168. # The state of things immediately following `var`.
  169. 'var': [
  170. (' +', Whitespace),
  171. (_identifier, Name.Variable, '#pop'),
  172. include('root'),
  173. ],
  174. }