Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Jsonnet support #2239

Merged
merged 9 commits into from Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions pygments/lexers/_mapping.py
Expand Up @@ -237,6 +237,7 @@
'JsonBareObjectLexer': ('pygments.lexers.data', 'JSONBareObject', (), (), ()),
'JsonLdLexer': ('pygments.lexers.data', 'JSON-LD', ('jsonld', 'json-ld'), ('*.jsonld',), ('application/ld+json',)),
'JsonLexer': ('pygments.lexers.data', 'JSON', ('json', 'json-object'), ('*.json', 'Pipfile.lock'), ('application/json', 'application/json-object')),
'JsonnetLexer': ('pygments.lexers.jsonnet', 'Jsonnet', ('jsonnet',), ('*.jsonnet', '*.libsonnet'), ()),
'JspLexer': ('pygments.lexers.templates', 'Java Server Page', ('jsp',), ('*.jsp',), ('application/x-jsp',)),
'JuliaConsoleLexer': ('pygments.lexers.julia', 'Julia console', ('jlcon', 'julia-repl'), (), ()),
'JuliaLexer': ('pygments.lexers.julia', 'Julia', ('julia', 'jl'), ('*.jl',), ('text/x-julia', 'application/x-julia')),
Expand Down
160 changes: 160 additions & 0 deletions pygments/lexers/jsonnet.py
@@ -0,0 +1,160 @@
from pygments.lexer import RegexLexer
from pygments.token import (
Comment,
Keyword,
Name,
Number,
Operator,
Punctuation,
String,
Whitespace,
)

__all__ = ['JsonnetLexer']

jsonnet_token_chars = r'[_A-Za-z0-9]'
jeanas marked this conversation as resolved.
Show resolved Hide resolved
jsonnet_token = r'[_A-Za-z]' + jsonnet_token_chars + '*'
jeanas marked this conversation as resolved.
Show resolved Hide resolved
jsonnet_function_token = jsonnet_token + r'(?=\()'


comments = [
(r'//.*\n', Comment.Single),
(r'#.*\n', Comment.Single),
abentley marked this conversation as resolved.
Show resolved Hide resolved
(r'/\*\*([^/]|/(?!\*))*\*/', String.Doc),
(r'/\*([^/]|/(?!\*))*\*/', Comment),
]
jeanas marked this conversation as resolved.
Show resolved Hide resolved


whitespace = (r'[\n ]+', Whitespace)
jeanas marked this conversation as resolved.
Show resolved Hide resolved


keywords = '|'.join([
'assert', 'else', 'error', 'false', 'for', 'if', 'import', 'importstr',
'in', 'null', 'tailstrict', 'then', 'self', 'super', 'true',
])
jeanas marked this conversation as resolved.
Show resolved Hide resolved


rvalues = comments + [
(r"@'.*'", String),
(r'@".*"', String),
(r"'", String, 'singlestring'),
(r'"', String, 'doublestring'),
(r'(?s:\|\|\|.*\|\|\|)', String),
(r'[+-]?[0-9]+(.[0-9])?', Number.Float),
# Omit : despite spec because it appears to be used as a field separator
(r'[!$~+\-&|^=<>*/%]', Operator),
(r'[{]', Punctuation, 'object'),
(r'\[', Punctuation, 'array'),
(r'local', Keyword, ('local_name')),
jeanas marked this conversation as resolved.
Show resolved Hide resolved
(r'assert', Keyword, 'assert'),
jeanas marked this conversation as resolved.
Show resolved Hide resolved
(fr'({keywords})(?!{jsonnet_token_chars})', Keyword),
jeanas marked this conversation as resolved.
Show resolved Hide resolved
whitespace,
(r'function(?=\()', Keyword, 'function_params'),
(r'std\.' + jsonnet_function_token, Name.Builtin, 'function_args'),
(jsonnet_function_token, Name.Function, 'function_args'),
(jsonnet_token, Name.Variable),
(r'[\.()]', Punctuation),
]


def string_rules(quote_mark):
return [
(r"[^{}\\]".format(quote_mark), String),
(r"\\.", String.Escape),
(quote_mark, String, '#pop'),
]


def quoted_field_name(quote_mark):
return [
(r'([^{quote}\\]|\\.)*{quote}'.format(quote=quote_mark),
Name.Variable, 'field_separator')
]


class JsonnetLexer(RegexLexer):
name = 'Jsonnet'
aliases = ['jsonnet']
filenames = ['*.jsonnet', '*.libsonnet']
jeanas marked this conversation as resolved.
Show resolved Hide resolved
tokens = {
'root': rvalues,
'singlestring': string_rules("'"),
'doublestring': string_rules('"'),
'array': [
(r',', Punctuation),
(r'\]', Punctuation, '#pop'),
] + rvalues,
'local_name': [
(jsonnet_function_token, Name.Function, 'function_params'),
(jsonnet_token, Name.Variable),
whitespace,
('(?==)', Whitespace, ('#pop', 'local_value')),
],
'local_value': [
(r'=', Operator),
(r';', Punctuation, '#pop'),
] + rvalues,
'assert': [
(r':', Punctuation),
(r';', Punctuation, '#pop'),
] + rvalues,
'function_params': [
(jsonnet_token, Name.Variable),
(r'\(', Punctuation),
(r'\)', Punctuation, '#pop'),
(r',', Punctuation),
whitespace,
(r'=', Operator, 'function_param_default'),
],
'function_args': [
(r'\(', Punctuation),
(r'\)', Punctuation, '#pop'),
(r',', Punctuation),
whitespace,
] + rvalues,
'object': [
whitespace,
(r'local', Keyword, 'object_local_name'),
('assert', Keyword, 'object_assert'),
jeanas marked this conversation as resolved.
Show resolved Hide resolved
(r'\[', Operator, 'field_name_expr'),
(fr'(?={jsonnet_token})', Name.Variable, 'field_name'),
jeanas marked this conversation as resolved.
Show resolved Hide resolved
(r'}', Punctuation, '#pop'),
(r'"', Name.Variable, 'double_field_name'),
(r"'", Name.Variable, 'single_field_name'),
] + comments,
'field_name': [
(jsonnet_function_token, Name.Function,
('field_separator', 'function_params')
),
(jsonnet_token, Name.Variable, 'field_separator'),
],
'double_field_name': quoted_field_name('"'),
'single_field_name': quoted_field_name("'"),
'field_name_expr': [
(r'\]', Operator, 'field_separator'),
] + rvalues,
'function_param_default': [
(r'(?=[,\)])', Whitespace, '#pop')
] + rvalues,
'field_separator': [
whitespace,
(r'\+?::?:?', Punctuation, ('#pop', '#pop', 'field_value')),
] + comments,
'field_value': [
(r',', Punctuation, '#pop'),
(r'}', Punctuation, '#pop:2'),
] + rvalues,
'object_assert': [
(r':', Punctuation),
(r',', Punctuation, '#pop'),
] + rvalues,
'object_local_name': [
(jsonnet_token, Name.Variable, ('#pop', 'object_local_value')),
whitespace,
],
'object_local_value': [
(r'=', Operator),
(r',', Punctuation, '#pop'),
(r'}', Punctuation, '#pop:2'),
] + rvalues,
}
74 changes: 74 additions & 0 deletions tests/examplefiles/jsonnet/example.jsonnet
@@ -0,0 +1,74 @@
/* Multiline /
comment */
local x = 100;
/* Multiline
comment */

/**
* Docs
**/


local i = import 'foo';
local foo = 'bar';
local lambda = function(foo, bar) ['baz', 'qux'];
local named(foo, bar=10, baz=20) = ['baz', 'qux'];
local in1troduction = 'introduction';
assert 5 > 3: "interesting";
//comment
{
local foo = "bar",
spam: 'eggs',
}

{local foo = "bar"}


{
// this is a comment
# python-style comment
assert 1 == 1: 'huh?',
spam: 'eggs // /* #',
spam2(foo, bar):: 2,
spa: 'eggs',
foo: 'bar',
block: |||
Hello
Block
|||,
baz: 'qux' + 'fuzz',
'm:oo': 'cow',
"b:oo": ('cow'),
goo: ['bar', 'ba\nz'],
spam3: 2.25,
spam4::: -2.25,
spam5::: +2.25,
spam6//funky
: 'hello' //moo
, //moo
['spa\'m']: 'eggs',
["spa\"m2"]: "eggs",
raw1: @'hello\',
raw2: @"hello\",
spam7: foo,
spam8: lambda(1, 2),
spam9: self.spam2(3, 4),
intro: in1troduction,
spam_10: 10,
spam_11(y=10):: 11,
spam12: std.type('null'),
spam13+: 27,
spam14: $.spam13,
spam15: ~5,
spam16: !false,
spam17: 0 - 5,
spam18: 5 & 3,
spam19: 5 | 3,
spam20: 5 ^ 3,
spam21: 5 == 3,
spam22: 5 < 3,
spam23: 5 > 3,
spam24: 5 * 3,
spam25: 5 / 3,
spam26: 5 % 3,
}