Skip to content

Commit e38986f

Browse files
authored
Added support for Rego (#2624)
1 parent 8dbbbb3 commit e38986f

14 files changed

+636
-1
lines changed

components.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components.json

+4
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,10 @@
10281028
"title": "Regex",
10291029
"owner": "RunDevelopment"
10301030
},
1031+
"rego": {
1032+
"title": "Rego",
1033+
"owner": "JordanSh"
1034+
},
10311035
"renpy": {
10321036
"title": "Ren'py",
10331037
"alias": "rpy",

components/prism-rego.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// https://www.openpolicyagent.org/docs/latest/policy-reference/
2+
3+
Prism.languages.rego = {
4+
'comment': /#.*/,
5+
'property': {
6+
pattern: /(^|[^\\.])(?:"(?:\\.|[^\\"\r\n])*"|`[^`]*`|\b[a-z_]\w*\b)(?=\s*:(?!=))/i,
7+
lookbehind: true,
8+
greedy: true
9+
},
10+
'string': {
11+
pattern: /(^|[^\\])"(?:\\.|[^\\"\r\n])*"|`[^`]*`/,
12+
lookbehind: true,
13+
greedy: true
14+
},
15+
16+
'keyword': /\b(?:as|default|else|import|package|not|null|some|with|set(?=\s*\())\b/,
17+
'boolean': /\b(?:true|false)\b/,
18+
19+
'function': {
20+
pattern: /\b[a-z_]\w*\b(?:\s*\.\s*\b[a-z_]\w*\b)*(?=\s*\()/i,
21+
inside: {
22+
'namespace': /\b\w+\b(?=\s*\.)/,
23+
'punctuation': /\./
24+
}
25+
},
26+
27+
'number': /-?\b\d+(?:\.\d+)?(?:e[+-]?\d+)?\b/i,
28+
'operator': /[-+*/%|&]|[<>:=]=?|!=|\b_\b/,
29+
'punctuation': /[,;.\[\]{}()]/
30+
};

components/prism-rego.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/prism-rego.html

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<h2>Full example</h2>
2+
<pre><code># Role-based Access Control (RBAC)
3+
4+
# By default, deny requests.
5+
default allow = false
6+
7+
# Allow admins to do anything.
8+
allow {
9+
user_is_admin
10+
}
11+
12+
# Allow the action if the user is granted permission to perform the action.
13+
allow {
14+
# Find grants for the user.
15+
some grant
16+
user_is_granted[grant]
17+
18+
# Check if the grant permits the action.
19+
input.action == grant.action
20+
input.type == grant.type
21+
}
22+
23+
# user_is_admin is true if...
24+
user_is_admin {
25+
26+
# for some `i`...
27+
some i
28+
29+
# "admin" is the `i`-th element in the user->role mappings for the identified user.
30+
data.user_roles[input.user][i] == "admin"
31+
}
32+
33+
# user_is_granted is a set of grants for the user identified in the request.
34+
# The `grant` will be contained if the set `user_is_granted` for every...
35+
user_is_granted[grant] {
36+
some i, j
37+
38+
# `role` assigned an element of the user_roles for this user...
39+
role := data.user_roles[input.user][i]
40+
41+
# `grant` assigned a single grant from the grants list for 'role'...
42+
grant := data.role_grants[role][j]
43+
}
44+
</code></pre>
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
true
2+
false
3+
4+
----------------------------------------------------
5+
6+
[
7+
["boolean", "true"],
8+
["boolean", "false"]
9+
]
10+
11+
----------------------------------------------------
12+
13+
Checks for booleans.
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#
2+
# foobar
3+
4+
----------------------------------------------------
5+
6+
[
7+
["comment", "#"],
8+
["comment", "# foobar"]
9+
]
10+
11+
----------------------------------------------------
12+
13+
Checks for comments.
+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
object.remove({"a": {"b": {"c": 2}}, "x": 123}, {"a": 1}) == {"x": 123}
2+
3+
output := is_set(x)
4+
output := intersection(set[set])
5+
output := regex.match(pattern, value)
6+
output := glob.match("*.github.com", [], "api.github.com")
7+
output := bits.rsh(x, s)
8+
output := io.jwt.verify_ps384(string, certificate)
9+
10+
io.jwt.encode_sign({
11+
"typ": "JWT",
12+
"alg": "HS256"},
13+
{}, {
14+
"kty": "oct",
15+
"k": "AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow"
16+
})
17+
18+
----------------------------------------------------
19+
20+
[
21+
["function", [
22+
["namespace", "object"],
23+
["punctuation", "."],
24+
"remove"
25+
]],
26+
["punctuation", "("],
27+
["punctuation", "{"],
28+
["property", "\"a\""],
29+
["operator", ":"],
30+
["punctuation", "{"],
31+
["property", "\"b\""],
32+
["operator", ":"],
33+
["punctuation", "{"],
34+
["property", "\"c\""],
35+
["operator", ":"],
36+
["number", "2"],
37+
["punctuation", "}"],
38+
["punctuation", "}"],
39+
["punctuation", ","],
40+
["property", "\"x\""],
41+
["operator", ":"],
42+
["number", "123"],
43+
["punctuation", "}"],
44+
["punctuation", ","],
45+
["punctuation", "{"],
46+
["property", "\"a\""],
47+
["operator", ":"],
48+
["number", "1"],
49+
["punctuation", "}"],
50+
["punctuation", ")"],
51+
["operator", "=="],
52+
["punctuation", "{"],
53+
["property", "\"x\""],
54+
["operator", ":"],
55+
["number", "123"],
56+
["punctuation", "}"],
57+
58+
"\r\n\r\noutput ",
59+
["operator", ":="],
60+
["function", ["is_set"]],
61+
["punctuation", "("],
62+
"x",
63+
["punctuation", ")"],
64+
65+
"\r\noutput ",
66+
["operator", ":="],
67+
["function", ["intersection"]],
68+
["punctuation", "("],
69+
"set",
70+
["punctuation", "["],
71+
"set",
72+
["punctuation", "]"],
73+
["punctuation", ")"],
74+
75+
"\r\noutput ",
76+
["operator", ":="],
77+
["function", [
78+
["namespace", "regex"],
79+
["punctuation", "."],
80+
"match"
81+
]],
82+
["punctuation", "("],
83+
"pattern",
84+
["punctuation", ","],
85+
" value",
86+
["punctuation", ")"],
87+
88+
"\r\noutput ",
89+
["operator", ":="],
90+
["function", [
91+
["namespace", "glob"],
92+
["punctuation", "."],
93+
"match"
94+
]],
95+
["punctuation", "("],
96+
["string", "\"*.github.com\""],
97+
["punctuation", ","],
98+
["punctuation", "["],
99+
["punctuation", "]"],
100+
["punctuation", ","],
101+
["string", "\"api.github.com\""],
102+
["punctuation", ")"],
103+
104+
"\r\noutput ",
105+
["operator", ":="],
106+
["function", [
107+
["namespace", "bits"],
108+
["punctuation", "."],
109+
"rsh"
110+
]],
111+
["punctuation", "("],
112+
"x",
113+
["punctuation", ","],
114+
" s",
115+
["punctuation", ")"],
116+
117+
"\r\noutput ",
118+
["operator", ":="],
119+
["function", [
120+
["namespace", "io"],
121+
["punctuation", "."],
122+
["namespace", "jwt"],
123+
["punctuation", "."],
124+
"verify_ps384"
125+
]],
126+
["punctuation", "("],
127+
"string",
128+
["punctuation", ","],
129+
" certificate",
130+
["punctuation", ")"],
131+
132+
["function", [
133+
["namespace", "io"],
134+
["punctuation", "."],
135+
["namespace", "jwt"],
136+
["punctuation", "."],
137+
"encode_sign"
138+
]],
139+
["punctuation", "("],
140+
["punctuation", "{"],
141+
142+
["property", "\"typ\""],
143+
["operator", ":"],
144+
["string", "\"JWT\""],
145+
["punctuation", ","],
146+
147+
["property", "\"alg\""],
148+
["operator", ":"],
149+
["string", "\"HS256\""],
150+
["punctuation", "}"],
151+
["punctuation", ","],
152+
153+
["punctuation", "{"],
154+
["punctuation", "}"],
155+
["punctuation", ","],
156+
["punctuation", "{"],
157+
158+
["property", "\"kty\""],
159+
["operator", ":"],
160+
["string", "\"oct\""],
161+
["punctuation", ","],
162+
163+
["property", "\"k\""],
164+
["operator", ":"],
165+
["string", "\"AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow\""],
166+
167+
["punctuation", "}"],
168+
["punctuation", ")"]
169+
]
170+
171+
----------------------------------------------------
172+
173+
Checks for all functions.
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
as
2+
default
3+
else
4+
import
5+
package
6+
not
7+
null
8+
some
9+
with
10+
11+
set()
12+
13+
----------------------------------------------------
14+
15+
[
16+
["keyword", "as"],
17+
["keyword", "default"],
18+
["keyword", "else"],
19+
["keyword", "import"],
20+
["keyword", "package"],
21+
["keyword", "not"],
22+
["keyword", "null"],
23+
["keyword", "some"],
24+
["keyword", "with"],
25+
26+
["keyword", "set"], ["punctuation", "("], ["punctuation", ")"]
27+
]
28+
29+
----------------------------------------------------
30+
31+
Checks for all keywords.
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
0
2+
123
3+
3.14159
4+
5.0e8
5+
0.2E+2
6+
47e-5
7+
-1.23
8+
-2.34E33
9+
-4.34E-33
10+
11+
----------------------------------------------------
12+
13+
[
14+
["number", "0"],
15+
["number", "123"],
16+
["number", "3.14159"],
17+
["number", "5.0e8"],
18+
["number", "0.2E+2"],
19+
["number", "47e-5"],
20+
["number", "-1.23"],
21+
["number", "-2.34E33"],
22+
["number", "-4.34E-33"]
23+
]
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
:= = :
2+
== != < <= > >=
3+
+ - / * %
4+
& |
5+
_
6+
7+
----------------------------------------------------
8+
9+
[
10+
["operator", ":="],
11+
["operator", "="],
12+
["operator", ":"],
13+
14+
["operator", "=="],
15+
["operator", "!="],
16+
["operator", "<"],
17+
["operator", "<="],
18+
["operator", ">"],
19+
["operator", ">="],
20+
21+
["operator", "+"],
22+
["operator", "-"],
23+
["operator", "/"],
24+
["operator", "*"],
25+
["operator", "%"],
26+
27+
["operator", "&"],
28+
["operator", "|"],
29+
30+
["operator", "_"]
31+
]
32+
33+
----------------------------------------------------
34+
35+
Checks for operators.

0 commit comments

Comments
 (0)