Skip to content

Commit

Permalink
Added support for Jexl (#2764)
Browse files Browse the repository at this point in the history
  • Loading branch information
czosel committed Mar 21, 2021
1 parent 9633564 commit 7e51b99
Show file tree
Hide file tree
Showing 12 changed files with 298 additions and 1 deletion.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions components.json
Expand Up @@ -605,6 +605,10 @@
"title": "Java stack trace",
"owner": "RunDevelopment"
},
"jexl": {
"title": "Jexl",
"owner": "czosel"
},
"jolie": {
"title": "Jolie",
"require": "clike",
Expand Down
14 changes: 14 additions & 0 deletions components/prism-jexl.js
@@ -0,0 +1,14 @@
Prism.languages.jexl = {
'string': /(["'])(?:\\[\s\S]|(?!\1)[^\\])*\1/,
'transform': {
pattern: /(\|\s*)[a-zA-Zа-яА-Я_\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u00FF$][a-zA-Zа-яА-Я0-9_\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u00FF$]*/,
alias: "function",
lookbehind: true
},
'function': /[a-zA-Zа-яА-Я_\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u00FF$][a-zA-Zа-яА-Я0-9_\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u00FF$]*\s*(?=\()/,
'number': /\b\d+(?:\.\d+)?\b|\B\.\d+\b/,
'operator': /[<>!]=?|-|\+|&&|==|\|\|?|\/\/?|[?:*^%]/,
'boolean': /\b(?:true|false)\b/,
'keyword': /\bin\b/,
'punctuation': /[{}[\](),.]/,
};
1 change: 1 addition & 0 deletions components/prism-jexl.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions examples/prism-jexl.html
@@ -0,0 +1,8 @@
<h2>Full example</h2>
<pre><code>
[
fun(1,2),
{ obj: "1" }.obj,
ctx|transform
]|join(", ")
</code></pre>
13 changes: 13 additions & 0 deletions tests/languages/jexl/boolean_feature.test
@@ -0,0 +1,13 @@
true
false

----------------------------------------------------

[
["boolean", "true"],
["boolean", "false"]
]

----------------------------------------------------

Checks for booleans.
96 changes: 96 additions & 0 deletions tests/languages/jexl/function_feature.test
@@ -0,0 +1,96 @@
foo()
foo ()
foo
()
foo . bar ()
foo_bar()
foo_bar ( )
f42()
_()
$()
ä()
Ý()
foo({ x: 1 })
foo({ y: fun() })
foo(bar())

----------------------------------------------------

[
["function", "foo"],
["punctuation", "("],
["punctuation", ")"],

["function", "foo "],
["punctuation", "("],
["punctuation", ")"],

["function", "foo\r\n"],
["punctuation", "("],
["punctuation", ")"],

"\r\nfoo ",
["punctuation", "."],
["function", "bar "],
["punctuation", "("],
["punctuation", ")"],

["function", "foo_bar"],
["punctuation", "("],
["punctuation", ")"],

["function", "foo_bar "],
["punctuation", "("],
["punctuation", ")"],

["function", "f42"],
["punctuation", "("],
["punctuation", ")"],

["function", "_"],
["punctuation", "("],
["punctuation", ")"],

["function", "$"],
["punctuation", "("],
["punctuation", ")"],

["function", "ä"],
["punctuation", "("],
["punctuation", ")"],

["function", "Ý"],
["punctuation", "("],
["punctuation", ")"],

["function", "foo"],
["punctuation", "("],
["punctuation", "{"],
" x",
["operator", ":"],
["number", "1"],
["punctuation", "}"],
["punctuation", ")"],

["function", "foo"],
["punctuation", "("],
["punctuation", "{"],
" y",
["operator", ":"],
["function", "fun"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "}"],
["punctuation", ")"],

["function", "foo"],
["punctuation", "("],
["function", "bar"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ")"]
]

----------------------------------------------------

Checks for functions. Also checks for unicode characters in identifiers.
23 changes: 23 additions & 0 deletions tests/languages/jexl/keyword_feature.test
@@ -0,0 +1,23 @@
in
"foo" in ["foo"]
"f" in "foo"

----------------------------------------------------

[
["keyword", "in"],

["string", "\"foo\""],
["keyword", "in"],
["punctuation", "["],
["string", "\"foo\""],
["punctuation", "]"],

["string", "\"f\""],
["keyword", "in"],
["string", "\"foo\""]
]

----------------------------------------------------

Checks for all keywords.
15 changes: 15 additions & 0 deletions tests/languages/jexl/number_feature.test
@@ -0,0 +1,15 @@
42
3.14159
-5.7

----------------------------------------------------

[
["number", "42"],
["number", "3.14159"],
["operator", "-"], ["number", "5.7"]
]

----------------------------------------------------

Checks for integers and floats. Negative numbers are handled differently than in Jexl for simplicity: In Jexl, the minus sign in "-1" is parsed as an operator in "1-1" and as part of the number literal in ("-1").
33 changes: 33 additions & 0 deletions tests/languages/jexl/operator_feature.test
@@ -0,0 +1,33 @@
-
+
< <=
> >=
==
! !=
&& ||
*
/ //
^
? :
%

----------------------------------------------------

[
["operator", "-"],
["operator", "+"],
["operator", "<"], ["operator", "<="],
["operator", ">"], ["operator", ">="],
["operator", "=="],
["operator", "!"], ["operator", "!="],
["operator", "&&"], ["operator", "||"],
["operator", "*"],
["operator", "/"], ["operator", "//"],
["operator", "^"],
["operator", "?"], ["operator", ":"],
["operator", "%"]
]

----------------------------------------------------

Checks for all operators.
35 changes: 35 additions & 0 deletions tests/languages/jexl/string_feature.test
@@ -0,0 +1,35 @@
'a'
'ä'
'本'
'À'
'\''
'""'
'Hello,
world!'

"\n"
"\""
"Hello,
world!"
"日本語"

----------------------------------------------------

[
["string", "'a'"],
["string", "'ä'"],
["string", "'本'"],
["string", "'À'"],
["string", "'\\''"],
["string", "'\"\"'"],
["string", "'Hello,\r\nworld!'"],

["string", "\"\\n\""],
["string", "\"\\\"\""],
["string", "\"Hello,\r\nworld!\""],
["string", "\"日本語\""]
]

----------------------------------------------------

Jexl strings can contain special characters, and even linebreaks.
55 changes: 55 additions & 0 deletions tests/languages/jexl/transform_feature.test
@@ -0,0 +1,55 @@
"foo"|bar
"foo"| bar
"foo"
|
bar

foo|Ý

foo|bar()
foo|bar(1,2)
foo|bar(
1,2
)

----------------------------------------------------

[
["string", "\"foo\""], ["operator", "|"], ["transform", "bar"],
["string", "\"foo\""], ["operator", "|"], ["transform", "bar"],
["string", "\"foo\""],
["operator", "|"],
["transform", "bar"],

"\r\n\r\nfoo", ["operator", "|"], ["transform", "Ý"],

"\r\n\r\nfoo",
["operator", "|"],
["transform", "bar"],
["punctuation", "("],
["punctuation", ")"],

"\r\nfoo",
["operator", "|"],
["transform", "bar"],
["punctuation", "("],
["number", "1"],
["punctuation", ","],
["number", "2"],
["punctuation", ")"],

"\r\nfoo",
["operator", "|"],
["transform", "bar"],
["punctuation", "("],

["number", "1"],
["punctuation", ","],
["number", "2"],

["punctuation", ")"]
]

----------------------------------------------------

Checks for transforms. Transforms follow the same naming rules as functions.

0 comments on commit 7e51b99

Please sign in to comment.