slash.py 8.3 KB

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