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