1"""
2 pygments.lexers.savi
3 ~~~~~~~~~~~~~~~~~~~~
4
5 Lexer for Savi.
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 RegexLexer, bygroups, include
12from pygments.token import Whitespace, Keyword, Name, String, Number, \
13 Operator, Punctuation, Comment, Generic, Error
14
15__all__ = ['SaviLexer']
16
17
18# The canonical version of this file can be found in the following repository,
19# where it is kept in sync with any language changes, as well as the other
20# pygments-like lexers that are maintained for use with other tools:
21# - https://github.com/savi-lang/savi/blob/main/tooling/pygments/lexers/savi.py
22#
23# If you're changing this file in the pygments repository, please ensure that
24# any changes you make are also propagated to the official Savi repository,
25# in order to avoid accidental clobbering of your changes later when an update
26# from the Savi repository flows forward into the pygments repository.
27#
28# If you're changing this file in the Savi repository, please ensure that
29# any changes you make are also reflected in the other pygments-like lexers
30# (rouge, vscode, etc) so that all of the lexers can be kept cleanly in sync.
31
32class SaviLexer(RegexLexer):
33 """
34 For Savi source code.
35
36 .. versionadded: 2.10
37 """
38
39 name = 'Savi'
40 url = 'https://github.com/savi-lang/savi'
41 aliases = ['savi']
42 filenames = ['*.savi']
43 version_added = ''
44
45 tokens = {
46 "root": [
47 # Line Comment
48 (r'//.*?$', Comment.Single),
49
50 # Doc Comment
51 (r'::.*?$', Comment.Single),
52
53 # Capability Operator
54 (r'(\')(\w+)(?=[^\'])', bygroups(Operator, Name)),
55
56 # Double-Quote String
57 (r'\w?"', String.Double, "string.double"),
58
59 # Single-Char String
60 (r"'", String.Char, "string.char"),
61
62 # Type Name
63 (r'(_?[A-Z]\w*)', Name.Class),
64
65 # Nested Type Name
66 (r'(\.)(\s*)(_?[A-Z]\w*)', bygroups(Punctuation, Whitespace, Name.Class)),
67
68 # Declare
69 (r'^([ \t]*)(:\w+)',
70 bygroups(Whitespace, Name.Tag),
71 "decl"),
72
73 # Error-Raising Calls/Names
74 (r'((\w+|\+|\-|\*)\!)', Generic.Deleted),
75
76 # Numeric Values
77 (r'\b\d([\d_]*(\.[\d_]+)?)\b', Number),
78
79 # Hex Numeric Values
80 (r'\b0x([0-9a-fA-F_]+)\b', Number.Hex),
81
82 # Binary Numeric Values
83 (r'\b0b([01_]+)\b', Number.Bin),
84
85 # Function Call (with braces)
86 (r'\w+(?=\()', Name.Function),
87
88 # Function Call (with receiver)
89 (r'(\.)(\s*)(\w+)', bygroups(Punctuation, Whitespace, Name.Function)),
90
91 # Function Call (with self receiver)
92 (r'(@)(\w+)', bygroups(Punctuation, Name.Function)),
93
94 # Parenthesis
95 (r'\(', Punctuation, "root"),
96 (r'\)', Punctuation, "#pop"),
97
98 # Brace
99 (r'\{', Punctuation, "root"),
100 (r'\}', Punctuation, "#pop"),
101
102 # Bracket
103 (r'\[', Punctuation, "root"),
104 (r'(\])(\!)', bygroups(Punctuation, Generic.Deleted), "#pop"),
105 (r'\]', Punctuation, "#pop"),
106
107 # Punctuation
108 (r'[,;:\.@]', Punctuation),
109
110 # Piping Operators
111 (r'(\|\>)', Operator),
112
113 # Branching Operators
114 (r'(\&\&|\|\||\?\?|\&\?|\|\?|\.\?)', Operator),
115
116 # Comparison Operators
117 (r'(\<\=\>|\=\~|\=\=|\<\=|\>\=|\<|\>)', Operator),
118
119 # Arithmetic Operators
120 (r'(\+|\-|\/|\*|\%)', Operator),
121
122 # Assignment Operators
123 (r'(\=)', Operator),
124
125 # Other Operators
126 (r'(\!|\<\<|\<|\&|\|)', Operator),
127
128 # Identifiers
129 (r'\b\w+\b', Name),
130
131 # Whitespace
132 (r'[ \t\r]+\n*|\n+', Whitespace),
133 ],
134
135 # Declare (nested rules)
136 "decl": [
137 (r'\b[a-z_]\w*\b(?!\!)', Keyword.Declaration),
138 (r':', Punctuation, "#pop"),
139 (r'\n', Whitespace, "#pop"),
140 include("root"),
141 ],
142
143 # Double-Quote String (nested rules)
144 "string.double": [
145 (r'\\\(', String.Interpol, "string.interpolation"),
146 (r'\\u[0-9a-fA-F]{4}', String.Escape),
147 (r'\\x[0-9a-fA-F]{2}', String.Escape),
148 (r'\\[bfnrt\\\']', String.Escape),
149 (r'\\"', String.Escape),
150 (r'"', String.Double, "#pop"),
151 (r'[^\\"]+', String.Double),
152 (r'.', Error),
153 ],
154
155 # Single-Char String (nested rules)
156 "string.char": [
157 (r'\\u[0-9a-fA-F]{4}', String.Escape),
158 (r'\\x[0-9a-fA-F]{2}', String.Escape),
159 (r'\\[bfnrt\\\']', String.Escape),
160 (r"\\'", String.Escape),
161 (r"'", String.Char, "#pop"),
162 (r"[^\\']+", String.Char),
163 (r'.', Error),
164 ],
165
166 # Interpolation inside String (nested rules)
167 "string.interpolation": [
168 (r"\)", String.Interpol, "#pop"),
169 include("root"),
170 ]
171 }