slash.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. """
  2. pygments.lexers.slash
  3. ~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for the `Slash <https://github.com/arturadib/Slash-A>`_ programming
  5. language.
  6. :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. from pygments.lexer import ExtendedRegexLexer, bygroups, DelegatingLexer
  10. from pygments.token import Name, Number, String, Comment, Punctuation, \
  11. Other, Keyword, Operator, Whitespace
  12. __all__ = ['SlashLexer']
  13. class SlashLanguageLexer(ExtendedRegexLexer):
  14. _nkw = r'(?=[^a-zA-Z_0-9])'
  15. def move_state(new_state):
  16. return ("#pop", new_state)
  17. def right_angle_bracket(lexer, match, ctx):
  18. if len(ctx.stack) > 1 and ctx.stack[-2] == "string":
  19. ctx.stack.pop()
  20. yield match.start(), String.Interpol, '}'
  21. ctx.pos = match.end()
  22. pass
  23. tokens = {
  24. "root": [
  25. (r"<%=", Comment.Preproc, move_state("slash")),
  26. (r"<%!!", Comment.Preproc, move_state("slash")),
  27. (r"<%#.*?%>", Comment.Multiline),
  28. (r"<%", Comment.Preproc, move_state("slash")),
  29. (r".|\n", Other),
  30. ],
  31. "string": [
  32. (r"\\", String.Escape, move_state("string_e")),
  33. (r"\"", String, move_state("slash")),
  34. (r"#\{", String.Interpol, "slash"),
  35. (r'.|\n', String),
  36. ],
  37. "string_e": [
  38. (r'n', String.Escape, move_state("string")),
  39. (r't', String.Escape, move_state("string")),
  40. (r'r', String.Escape, move_state("string")),
  41. (r'e', String.Escape, move_state("string")),
  42. (r'x[a-fA-F0-9]{2}', String.Escape, move_state("string")),
  43. (r'.', String.Escape, move_state("string")),
  44. ],
  45. "regexp": [
  46. (r'}[a-z]*', String.Regex, move_state("slash")),
  47. (r'\\(.|\n)', String.Regex),
  48. (r'{', String.Regex, "regexp_r"),
  49. (r'.|\n', String.Regex),
  50. ],
  51. "regexp_r": [
  52. (r'}[a-z]*', String.Regex, "#pop"),
  53. (r'\\(.|\n)', String.Regex),
  54. (r'{', String.Regex, "regexp_r"),
  55. ],
  56. "slash": [
  57. (r"%>", Comment.Preproc, move_state("root")),
  58. (r"\"", String, move_state("string")),
  59. (r"'[a-zA-Z0-9_]+", String),
  60. (r'%r{', String.Regex, move_state("regexp")),
  61. (r'/\*.*?\*/', Comment.Multiline),
  62. (r"(#|//).*?\n", Comment.Single),
  63. (r'-?[0-9]+e[+-]?[0-9]+', Number.Float),
  64. (r'-?[0-9]+\.[0-9]+(e[+-]?[0-9]+)?', Number.Float),
  65. (r'-?[0-9]+', Number.Integer),
  66. (r'nil'+_nkw, Name.Builtin),
  67. (r'true'+_nkw, Name.Builtin),
  68. (r'false'+_nkw, Name.Builtin),
  69. (r'self'+_nkw, Name.Builtin),
  70. (r'(class)(\s+)([A-Z][a-zA-Z0-9_\']*)',
  71. bygroups(Keyword, Whitespace, Name.Class)),
  72. (r'class'+_nkw, Keyword),
  73. (r'extends'+_nkw, Keyword),
  74. (r'(def)(\s+)(self)(\s*)(\.)(\s*)([a-z_][a-zA-Z0-9_\']*=?|<<|>>|==|<=>|<=|<|>=|>|\+|-(self)?|~(self)?|\*|/|%|^|&&|&|\||\[\]=?)',
  75. bygroups(Keyword, Whitespace, Name.Builtin, Whitespace, Punctuation, Whitespace, Name.Function)),
  76. (r'(def)(\s+)([a-z_][a-zA-Z0-9_\']*=?|<<|>>|==|<=>|<=|<|>=|>|\+|-(self)?|~(self)?|\*|/|%|^|&&|&|\||\[\]=?)',
  77. bygroups(Keyword, Whitespace, Name.Function)),
  78. (r'def'+_nkw, Keyword),
  79. (r'if'+_nkw, Keyword),
  80. (r'elsif'+_nkw, Keyword),
  81. (r'else'+_nkw, Keyword),
  82. (r'unless'+_nkw, Keyword),
  83. (r'for'+_nkw, Keyword),
  84. (r'in'+_nkw, Keyword),
  85. (r'while'+_nkw, Keyword),
  86. (r'until'+_nkw, Keyword),
  87. (r'and'+_nkw, Keyword),
  88. (r'or'+_nkw, Keyword),
  89. (r'not'+_nkw, Keyword),
  90. (r'lambda'+_nkw, Keyword),
  91. (r'try'+_nkw, Keyword),
  92. (r'catch'+_nkw, Keyword),
  93. (r'return'+_nkw, Keyword),
  94. (r'next'+_nkw, Keyword),
  95. (r'last'+_nkw, Keyword),
  96. (r'throw'+_nkw, Keyword),
  97. (r'use'+_nkw, Keyword),
  98. (r'switch'+_nkw, Keyword),
  99. (r'\\', Keyword),
  100. (r'λ', Keyword),
  101. (r'__FILE__'+_nkw, Name.Builtin.Pseudo),
  102. (r'__LINE__'+_nkw, Name.Builtin.Pseudo),
  103. (r'[A-Z][a-zA-Z0-9_\']*'+_nkw, Name.Constant),
  104. (r'[a-z_][a-zA-Z0-9_\']*'+_nkw, Name),
  105. (r'@[a-z_][a-zA-Z0-9_\']*'+_nkw, Name.Variable.Instance),
  106. (r'@@[a-z_][a-zA-Z0-9_\']*'+_nkw, Name.Variable.Class),
  107. (r'\(', Punctuation),
  108. (r'\)', Punctuation),
  109. (r'\[', Punctuation),
  110. (r'\]', Punctuation),
  111. (r'\{', Punctuation),
  112. (r'\}', right_angle_bracket),
  113. (r';', Punctuation),
  114. (r',', Punctuation),
  115. (r'<<=', Operator),
  116. (r'>>=', Operator),
  117. (r'<<', Operator),
  118. (r'>>', Operator),
  119. (r'==', Operator),
  120. (r'!=', Operator),
  121. (r'=>', Operator),
  122. (r'=', Operator),
  123. (r'<=>', Operator),
  124. (r'<=', Operator),
  125. (r'>=', Operator),
  126. (r'<', Operator),
  127. (r'>', Operator),
  128. (r'\+\+', Operator),
  129. (r'\+=', Operator),
  130. (r'-=', Operator),
  131. (r'\*\*=', Operator),
  132. (r'\*=', Operator),
  133. (r'\*\*', Operator),
  134. (r'\*', Operator),
  135. (r'/=', Operator),
  136. (r'\+', Operator),
  137. (r'-', Operator),
  138. (r'/', Operator),
  139. (r'%=', Operator),
  140. (r'%', Operator),
  141. (r'^=', Operator),
  142. (r'&&=', Operator),
  143. (r'&=', Operator),
  144. (r'&&', Operator),
  145. (r'&', Operator),
  146. (r'\|\|=', Operator),
  147. (r'\|=', Operator),
  148. (r'\|\|', Operator),
  149. (r'\|', Operator),
  150. (r'!', Operator),
  151. (r'\.\.\.', Operator),
  152. (r'\.\.', Operator),
  153. (r'\.', Operator),
  154. (r'::', Operator),
  155. (r':', Operator),
  156. (r'(\s|\n)+', Whitespace),
  157. (r'[a-z_][a-zA-Z0-9_\']*', Name.Variable),
  158. ],
  159. }
  160. class SlashLexer(DelegatingLexer):
  161. """
  162. Lexer for the Slash programming language.
  163. .. versionadded:: 2.4
  164. """
  165. name = 'Slash'
  166. aliases = ['slash']
  167. filenames = ['*.sla']
  168. def __init__(self, **options):
  169. from pygments.lexers.web import HtmlLexer
  170. super().__init__(HtmlLexer, SlashLanguageLexer, **options)