monte.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. """
  2. pygments.lexers.monte
  3. ~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for the Monte programming language.
  5. :copyright: Copyright 2006-2021 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 <https://monte.readthedocs.io/>`_ programming language.
  70. .. versionadded:: 2.2
  71. """
  72. name = 'Monte'
  73. aliases = ['monte']
  74. filenames = ['*.mt']
  75. tokens = {
  76. 'root': [
  77. # Comments
  78. (r'#[^\n]*\n', Comment),
  79. # Docstrings
  80. # Apologies for the non-greedy matcher here.
  81. (r'/\*\*.*?\*/', String.Doc),
  82. # `var` declarations
  83. (r'\bvar\b', Keyword.Declaration, 'var'),
  84. # `interface` declarations
  85. (r'\binterface\b', Keyword.Declaration, 'interface'),
  86. # method declarations
  87. (words(_methods, prefix='\\b', suffix='\\b'),
  88. Keyword, 'method'),
  89. # All other declarations
  90. (words(_declarations, prefix='\\b', suffix='\\b'),
  91. Keyword.Declaration),
  92. # Keywords
  93. (words(_keywords, prefix='\\b', suffix='\\b'), Keyword),
  94. # Literals
  95. ('[+-]?0x[_0-9a-fA-F]+', Number.Hex),
  96. (r'[+-]?[_0-9]+\.[_0-9]*([eE][+-]?[_0-9]+)?', Number.Float),
  97. ('[+-]?[_0-9]+', Number.Integer),
  98. ("'", String.Double, 'char'),
  99. ('"', String.Double, 'string'),
  100. # Quasiliterals
  101. ('`', String.Backtick, 'ql'),
  102. # Operators
  103. (words(_operators), Operator),
  104. # Verb operators
  105. (_identifier + '=', Operator.Word),
  106. # Safe scope constants
  107. (words(_constants, prefix='\\b', suffix='\\b'),
  108. Keyword.Pseudo),
  109. # Safe scope guards
  110. (words(_guards, prefix='\\b', suffix='\\b'), Keyword.Type),
  111. # All other safe scope names
  112. (words(_safeScope, prefix='\\b', suffix='\\b'),
  113. Name.Builtin),
  114. # Identifiers
  115. (_identifier, Name),
  116. # Punctuation
  117. (r'\(|\)|\{|\}|\[|\]|:|,', Punctuation),
  118. # Whitespace
  119. (' +', Whitespace),
  120. # Definite lexer errors
  121. ('=', Error),
  122. ],
  123. 'char': [
  124. # It is definitely an error to have a char of width == 0.
  125. ("'", Error, 'root'),
  126. (_escape_pattern, String.Escape, 'charEnd'),
  127. ('.', String.Char, 'charEnd'),
  128. ],
  129. 'charEnd': [
  130. ("'", String.Char, '#pop:2'),
  131. # It is definitely an error to have a char of width > 1.
  132. ('.', Error),
  133. ],
  134. # The state of things coming into an interface.
  135. 'interface': [
  136. (' +', Whitespace),
  137. (_identifier, Name.Class, '#pop'),
  138. include('root'),
  139. ],
  140. # The state of things coming into a method.
  141. 'method': [
  142. (' +', Whitespace),
  143. (_identifier, Name.Function, '#pop'),
  144. include('root'),
  145. ],
  146. 'string': [
  147. ('"', String.Double, 'root'),
  148. (_escape_pattern, String.Escape),
  149. (r'\n', String.Double),
  150. ('.', String.Double),
  151. ],
  152. 'ql': [
  153. ('`', String.Backtick, 'root'),
  154. (r'\$' + _escape_pattern, String.Escape),
  155. (r'\$\$', String.Escape),
  156. (r'@@', String.Escape),
  157. (r'\$\{', String.Interpol, 'qlNest'),
  158. (r'@\{', String.Interpol, 'qlNest'),
  159. (r'\$' + _identifier, Name),
  160. ('@' + _identifier, Name),
  161. ('.', String.Backtick),
  162. ],
  163. 'qlNest': [
  164. (r'\}', String.Interpol, '#pop'),
  165. include('root'),
  166. ],
  167. # The state of things immediately following `var`.
  168. 'var': [
  169. (' +', Whitespace),
  170. (_identifier, Name.Variable, '#pop'),
  171. include('root'),
  172. ],
  173. }