1"""
2 pygments.lexers.jsonnet
3 ~~~~~~~~~~~~~~~~~~~~~~~
4
5 Lexer for Jsonnet data templating 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 include, RegexLexer, words
12from pygments.token import Comment, Keyword, Name, Number, Operator, \
13 Punctuation, String, Text, Whitespace
14
15__all__ = ['JsonnetLexer']
16
17jsonnet_token = r'[^\W\d]\w*'
18jsonnet_function_token = jsonnet_token + r'(?=\()'
19
20
21def string_rules(quote_mark):
22 return [
23 (rf"[^{quote_mark}\\]", String),
24 (r"\\.", String.Escape),
25 (quote_mark, String, '#pop'),
26 ]
27
28
29def quoted_field_name(quote_mark):
30 return [
31 (rf'([^{quote_mark}\\]|\\.)*{quote_mark}',
32 Name.Variable, 'field_separator')
33 ]
34
35
36class JsonnetLexer(RegexLexer):
37 """Lexer for Jsonnet source code."""
38
39 name = 'Jsonnet'
40 aliases = ['jsonnet']
41 filenames = ['*.jsonnet', '*.libsonnet']
42 url = "https://jsonnet.org"
43 version_added = ''
44 tokens = {
45 # Not used by itself
46 '_comments': [
47 (r'(//|#).*\n', Comment.Single),
48 (r'/\*\*([^/]|/(?!\*))*\*/', String.Doc),
49 (r'/\*([^/]|/(?!\*))*\*/', Comment),
50 ],
51 'root': [
52 include('_comments'),
53 (r"@'.*'", String),
54 (r'@".*"', String),
55 (r"'", String, 'singlestring'),
56 (r'"', String, 'doublestring'),
57 (r'\|\|\|(.|\n)*\|\|\|', String),
58 # Jsonnet has no integers, only an IEEE754 64-bit float
59 (r'[+-]?[0-9]+(.[0-9])?', Number.Float),
60 # Omit : despite spec because it appears to be used as a field
61 # separator
62 (r'[!$~+\-&|^=<>*/%]', Operator),
63 (r'\{', Punctuation, 'object'),
64 (r'\[', Punctuation, 'array'),
65 (r'local\b', Keyword, ('local_name')),
66 (r'assert\b', Keyword, 'assert'),
67 (words([
68 'assert', 'else', 'error', 'false', 'for', 'if', 'import',
69 'importstr', 'in', 'null', 'tailstrict', 'then', 'self',
70 'super', 'true',
71 ], suffix=r'\b'), Keyword),
72 (r'\s+', Whitespace),
73 (r'function(?=\()', Keyword, 'function_params'),
74 (r'std\.' + jsonnet_function_token, Name.Builtin, 'function_args'),
75 (jsonnet_function_token, Name.Function, 'function_args'),
76 (jsonnet_token, Name.Variable),
77 (r'[\.()]', Punctuation),
78 ],
79 'singlestring': string_rules("'"),
80 'doublestring': string_rules('"'),
81 'array': [
82 (r',', Punctuation),
83 (r'\]', Punctuation, '#pop'),
84 include('root'),
85 ],
86 'local_name': [
87 (jsonnet_function_token, Name.Function, 'function_params'),
88 (jsonnet_token, Name.Variable),
89 (r'\s+', Whitespace),
90 ('(?==)', Whitespace, ('#pop', 'local_value')),
91 ],
92 'local_value': [
93 (r'=', Operator),
94 (r';', Punctuation, '#pop'),
95 include('root'),
96 ],
97 'assert': [
98 (r':', Punctuation),
99 (r';', Punctuation, '#pop'),
100 include('root'),
101 ],
102 'function_params': [
103 (jsonnet_token, Name.Variable),
104 (r'\(', Punctuation),
105 (r'\)', Punctuation, '#pop'),
106 (r',', Punctuation),
107 (r'\s+', Whitespace),
108 (r'=', Operator, 'function_param_default'),
109 ],
110 'function_args': [
111 (r'\(', Punctuation),
112 (r'\)', Punctuation, '#pop'),
113 (r',', Punctuation),
114 (r'\s+', Whitespace),
115 include('root'),
116 ],
117 'object': [
118 (r'\s+', Whitespace),
119 (r'local\b', Keyword, 'object_local_name'),
120 (r'assert\b', Keyword, 'object_assert'),
121 (r'\[', Operator, 'field_name_expr'),
122 (fr'(?={jsonnet_token})', Text, 'field_name'),
123 (r'\}', Punctuation, '#pop'),
124 (r'"', Name.Variable, 'double_field_name'),
125 (r"'", Name.Variable, 'single_field_name'),
126 include('_comments'),
127 ],
128 'field_name': [
129 (jsonnet_function_token, Name.Function,
130 ('field_separator', 'function_params')
131 ),
132 (jsonnet_token, Name.Variable, 'field_separator'),
133 ],
134 'double_field_name': quoted_field_name('"'),
135 'single_field_name': quoted_field_name("'"),
136 'field_name_expr': [
137 (r'\]', Operator, 'field_separator'),
138 include('root'),
139 ],
140 'function_param_default': [
141 (r'(?=[,\)])', Whitespace, '#pop'),
142 include('root'),
143 ],
144 'field_separator': [
145 (r'\s+', Whitespace),
146 (r'\+?::?:?', Punctuation, ('#pop', '#pop', 'field_value')),
147 include('_comments'),
148 ],
149 'field_value': [
150 (r',', Punctuation, '#pop'),
151 (r'\}', Punctuation, '#pop:2'),
152 include('root'),
153 ],
154 'object_assert': [
155 (r':', Punctuation),
156 (r',', Punctuation, '#pop'),
157 include('root'),
158 ],
159 'object_local_name': [
160 (jsonnet_token, Name.Variable, ('#pop', 'object_local_value')),
161 (r'\s+', Whitespace),
162 ],
163 'object_local_value': [
164 (r'=', Operator),
165 (r',', Punctuation, '#pop'),
166 (r'\}', Punctuation, '#pop:2'),
167 include('root'),
168 ],
169 }