monte.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. # -*- coding: utf-8 -*-
  2. """
  3. pygments.lexers.monte
  4. ~~~~~~~~~~~~~~~~~~~~~
  5. Lexer for the Monte programming language.
  6. :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. from pygments.token import Comment, Error, Keyword, Name, Number, Operator, \
  10. Punctuation, String, Whitespace
  11. from pygments.lexer import RegexLexer, include, words
  12. __all__ = ['MonteLexer']
  13. # `var` handled separately
  14. # `interface` handled separately
  15. _declarations = ['bind', 'def', 'fn', 'object']
  16. _methods = ['method', 'to']
  17. _keywords = [
  18. 'as', 'break', 'catch', 'continue', 'else', 'escape', 'exit', 'exports',
  19. 'extends', 'finally', 'for', 'guards', 'if', 'implements', 'import',
  20. 'in', 'match', 'meta', 'pass', 'return', 'switch', 'try', 'via', 'when',
  21. 'while',
  22. ]
  23. _operators = [
  24. # Unary
  25. '~', '!',
  26. # Binary
  27. '+', '-', '*', '/', '%', '**', '&', '|', '^', '<<', '>>',
  28. # Binary augmented
  29. '+=', '-=', '*=', '/=', '%=', '**=', '&=', '|=', '^=', '<<=', '>>=',
  30. # Comparison
  31. '==', '!=', '<', '<=', '>', '>=', '<=>',
  32. # Patterns and assignment
  33. ':=', '?', '=~', '!~', '=>',
  34. # Calls and sends
  35. '.', '<-', '->',
  36. ]
  37. _escape_pattern = (
  38. r'(?:\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|'
  39. r'\\["\'\\bftnr])')
  40. # _char = _escape_chars + [('.', String.Char)]
  41. _identifier = r'[_a-zA-Z]\w*'
  42. _constants = [
  43. # Void constants
  44. 'null',
  45. # Bool constants
  46. 'false', 'true',
  47. # Double constants
  48. 'Infinity', 'NaN',
  49. # Special objects
  50. 'M', 'Ref', 'throw', 'traceln',
  51. ]
  52. _guards = [
  53. 'Any', 'Binding', 'Bool', 'Bytes', 'Char', 'DeepFrozen', 'Double',
  54. 'Empty', 'Int', 'List', 'Map', 'Near', 'NullOk', 'Same', 'Selfless',
  55. 'Set', 'Str', 'SubrangeGuard', 'Transparent', 'Void',
  56. ]
  57. _safeScope = [
  58. '_accumulateList', '_accumulateMap', '_auditedBy', '_bind',
  59. '_booleanFlow', '_comparer', '_equalizer', '_iterForever', '_loop',
  60. '_makeBytes', '_makeDouble', '_makeFinalSlot', '_makeInt', '_makeList',
  61. '_makeMap', '_makeMessageDesc', '_makeOrderedSpace', '_makeParamDesc',
  62. '_makeProtocolDesc', '_makeSourceSpan', '_makeString', '_makeVarSlot',
  63. '_makeVerbFacet', '_mapExtract', '_matchSame', '_quasiMatcher',
  64. '_slotToBinding', '_splitList', '_suchThat', '_switchFailed',
  65. '_validateFor', 'b__quasiParser', 'eval', 'import', 'm__quasiParser',
  66. 'makeBrandPair', 'makeLazySlot', 'safeScope', 'simple__quasiParser',
  67. ]
  68. class MonteLexer(RegexLexer):
  69. """
  70. Lexer for the `Monte <https://monte.readthedocs.io/>`_ programming language.
  71. .. versionadded:: 2.2
  72. """
  73. name = 'Monte'
  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. }