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