1"""
2 pygments.lexers.jsonnet
3 ~~~~~~~~~~~~~~~~~~~~~~~
4
5 Lexer for Jsonnet data templating language.
6
7 :copyright: Copyright 2006-present 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 (r'[!$~+\-&|^=<>*/%:]', Operator),
61 (r'\{', Punctuation, 'object'),
62 (r'\[', Punctuation, 'array'),
63 (r'local\b', Keyword, ('local_name')),
64 (r'assert\b', Keyword, 'assert'),
65 (words([
66 'assert', 'else', 'error', 'false', 'for', 'if', 'import',
67 'importstr', 'in', 'null', 'tailstrict', 'then', 'self',
68 'super', 'true',
69 ], suffix=r'\b'), Keyword),
70 (r'\s+', Whitespace),
71 (r'function(?=\()', Keyword, 'function_params'),
72 (r'std\.' + jsonnet_function_token, Name.Builtin, 'function_args'),
73 (jsonnet_function_token, Name.Function, 'function_args'),
74 (jsonnet_token, Name.Variable),
75 (r'[\.()]', Punctuation),
76 ],
77 'singlestring': string_rules("'"),
78 'doublestring': string_rules('"'),
79 'array': [
80 (r',', Punctuation),
81 (r'\]', Punctuation, '#pop'),
82 include('root'),
83 ],
84 'local_name': [
85 (jsonnet_function_token, Name.Function, 'function_params'),
86 (jsonnet_token, Name.Variable),
87 (r'\s+', Whitespace),
88 ('(?==)', Whitespace, ('#pop', 'local_value')),
89 ],
90 'local_value': [
91 (r'=', Operator),
92 (r';', Punctuation, '#pop'),
93 include('root'),
94 ],
95 'assert': [
96 (r':', Punctuation),
97 (r';', Punctuation, '#pop'),
98 include('root'),
99 ],
100 'function_params': [
101 (jsonnet_token, Name.Variable),
102 (r'\(', Punctuation),
103 (r'\)', Punctuation, '#pop'),
104 (r',', Punctuation),
105 (r'\s+', Whitespace),
106 (r'=', Operator, 'function_param_default'),
107 ],
108 'function_args': [
109 (r'\(', Punctuation),
110 (r'\)', Punctuation, '#pop'),
111 (r',', Punctuation),
112 (r'\s+', Whitespace),
113 include('root'),
114 ],
115 'object': [
116 (r'\s+', Whitespace),
117 (r'local\b', Keyword, 'object_local_name'),
118 (r'assert\b', Keyword, 'object_assert'),
119 (r'\[', Operator, 'field_name_expr'),
120 (fr'(?={jsonnet_token})', Text, 'field_name'),
121 (r'\}', Punctuation, '#pop'),
122 (r'"', Name.Variable, 'double_field_name'),
123 (r"'", Name.Variable, 'single_field_name'),
124 include('_comments'),
125 ],
126 'field_name': [
127 (jsonnet_function_token, Name.Function,
128 ('field_separator', 'function_params')
129 ),
130 (jsonnet_token, Name.Variable, 'field_separator'),
131 ],
132 'double_field_name': quoted_field_name('"'),
133 'single_field_name': quoted_field_name("'"),
134 'field_name_expr': [
135 (r'\]', Operator, 'field_separator'),
136 include('root'),
137 ],
138 'function_param_default': [
139 (r'(?=[,\)])', Whitespace, '#pop'),
140 include('root'),
141 ],
142 'field_separator': [
143 (r'\s+', Whitespace),
144 (r'\+?::?:?', Punctuation, ('#pop', '#pop', 'field_value')),
145 include('_comments'),
146 ],
147 'field_value': [
148 (r',', Punctuation, '#pop'),
149 (r'\}', Punctuation, '#pop:2'),
150 include('root'),
151 ],
152 'object_assert': [
153 (r':', Punctuation),
154 (r',', Punctuation, '#pop'),
155 include('root'),
156 ],
157 'object_local_name': [
158 (jsonnet_token, Name.Variable, ('#pop', 'object_local_value')),
159 (r'\s+', Whitespace),
160 ],
161 'object_local_value': [
162 (r'=', Operator),
163 (r',', Punctuation, '#pop'),
164 (r'\}', Punctuation, '#pop:2'),
165 include('root'),
166 ],
167 }