slash.py 8.3 KB

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