Skip to content

Commit

Permalink
Added support for Rego (#2624)
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanSh committed Mar 7, 2021
1 parent 8dbbbb3 commit e38986f
Show file tree
Hide file tree
Showing 14 changed files with 636 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 @@ -1028,6 +1028,10 @@
"title": "Regex",
"owner": "RunDevelopment"
},
"rego": {
"title": "Rego",
"owner": "JordanSh"
},
"renpy": {
"title": "Ren'py",
"alias": "rpy",
Expand Down
30 changes: 30 additions & 0 deletions components/prism-rego.js
@@ -0,0 +1,30 @@
// https://www.openpolicyagent.org/docs/latest/policy-reference/

Prism.languages.rego = {
'comment': /#.*/,
'property': {
pattern: /(^|[^\\.])(?:"(?:\\.|[^\\"\r\n])*"|`[^`]*`|\b[a-z_]\w*\b)(?=\s*:(?!=))/i,
lookbehind: true,
greedy: true
},
'string': {
pattern: /(^|[^\\])"(?:\\.|[^\\"\r\n])*"|`[^`]*`/,
lookbehind: true,
greedy: true
},

'keyword': /\b(?:as|default|else|import|package|not|null|some|with|set(?=\s*\())\b/,
'boolean': /\b(?:true|false)\b/,

'function': {
pattern: /\b[a-z_]\w*\b(?:\s*\.\s*\b[a-z_]\w*\b)*(?=\s*\()/i,
inside: {
'namespace': /\b\w+\b(?=\s*\.)/,
'punctuation': /\./
}
},

'number': /-?\b\d+(?:\.\d+)?(?:e[+-]?\d+)?\b/i,
'operator': /[-+*/%|&]|[<>:=]=?|!=|\b_\b/,
'punctuation': /[,;.\[\]{}()]/
};
1 change: 1 addition & 0 deletions components/prism-rego.min.js

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

44 changes: 44 additions & 0 deletions examples/prism-rego.html
@@ -0,0 +1,44 @@
<h2>Full example</h2>
<pre><code># Role-based Access Control (RBAC)

# By default, deny requests.
default allow = false

# Allow admins to do anything.
allow {
user_is_admin
}

# Allow the action if the user is granted permission to perform the action.
allow {
# Find grants for the user.
some grant
user_is_granted[grant]

# Check if the grant permits the action.
input.action == grant.action
input.type == grant.type
}

# user_is_admin is true if...
user_is_admin {

# for some `i`...
some i

# "admin" is the `i`-th element in the user->role mappings for the identified user.
data.user_roles[input.user][i] == "admin"
}

# user_is_granted is a set of grants for the user identified in the request.
# The `grant` will be contained if the set `user_is_granted` for every...
user_is_granted[grant] {
some i, j

# `role` assigned an element of the user_roles for this user...
role := data.user_roles[input.user][i]

# `grant` assigned a single grant from the grants list for 'role'...
grant := data.role_grants[role][j]
}
</code></pre>
13 changes: 13 additions & 0 deletions tests/languages/rego/boolean_feature.test
@@ -0,0 +1,13 @@
true
false

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

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

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

Checks for booleans.
13 changes: 13 additions & 0 deletions tests/languages/rego/comment_feature.test
@@ -0,0 +1,13 @@
#
# foobar

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

[
["comment", "#"],
["comment", "# foobar"]
]

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

Checks for comments.
173 changes: 173 additions & 0 deletions tests/languages/rego/function_feature.test
@@ -0,0 +1,173 @@
object.remove({"a": {"b": {"c": 2}}, "x": 123}, {"a": 1}) == {"x": 123}

output := is_set(x)
output := intersection(set[set])
output := regex.match(pattern, value)
output := glob.match("*.github.com", [], "api.github.com")
output := bits.rsh(x, s)
output := io.jwt.verify_ps384(string, certificate)

io.jwt.encode_sign({
"typ": "JWT",
"alg": "HS256"},
{}, {
"kty": "oct",
"k": "AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow"
})

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

[
["function", [
["namespace", "object"],
["punctuation", "."],
"remove"
]],
["punctuation", "("],
["punctuation", "{"],
["property", "\"a\""],
["operator", ":"],
["punctuation", "{"],
["property", "\"b\""],
["operator", ":"],
["punctuation", "{"],
["property", "\"c\""],
["operator", ":"],
["number", "2"],
["punctuation", "}"],
["punctuation", "}"],
["punctuation", ","],
["property", "\"x\""],
["operator", ":"],
["number", "123"],
["punctuation", "}"],
["punctuation", ","],
["punctuation", "{"],
["property", "\"a\""],
["operator", ":"],
["number", "1"],
["punctuation", "}"],
["punctuation", ")"],
["operator", "=="],
["punctuation", "{"],
["property", "\"x\""],
["operator", ":"],
["number", "123"],
["punctuation", "}"],

"\r\n\r\noutput ",
["operator", ":="],
["function", ["is_set"]],
["punctuation", "("],
"x",
["punctuation", ")"],

"\r\noutput ",
["operator", ":="],
["function", ["intersection"]],
["punctuation", "("],
"set",
["punctuation", "["],
"set",
["punctuation", "]"],
["punctuation", ")"],

"\r\noutput ",
["operator", ":="],
["function", [
["namespace", "regex"],
["punctuation", "."],
"match"
]],
["punctuation", "("],
"pattern",
["punctuation", ","],
" value",
["punctuation", ")"],

"\r\noutput ",
["operator", ":="],
["function", [
["namespace", "glob"],
["punctuation", "."],
"match"
]],
["punctuation", "("],
["string", "\"*.github.com\""],
["punctuation", ","],
["punctuation", "["],
["punctuation", "]"],
["punctuation", ","],
["string", "\"api.github.com\""],
["punctuation", ")"],

"\r\noutput ",
["operator", ":="],
["function", [
["namespace", "bits"],
["punctuation", "."],
"rsh"
]],
["punctuation", "("],
"x",
["punctuation", ","],
" s",
["punctuation", ")"],

"\r\noutput ",
["operator", ":="],
["function", [
["namespace", "io"],
["punctuation", "."],
["namespace", "jwt"],
["punctuation", "."],
"verify_ps384"
]],
["punctuation", "("],
"string",
["punctuation", ","],
" certificate",
["punctuation", ")"],

["function", [
["namespace", "io"],
["punctuation", "."],
["namespace", "jwt"],
["punctuation", "."],
"encode_sign"
]],
["punctuation", "("],
["punctuation", "{"],

["property", "\"typ\""],
["operator", ":"],
["string", "\"JWT\""],
["punctuation", ","],

["property", "\"alg\""],
["operator", ":"],
["string", "\"HS256\""],
["punctuation", "}"],
["punctuation", ","],

["punctuation", "{"],
["punctuation", "}"],
["punctuation", ","],
["punctuation", "{"],

["property", "\"kty\""],
["operator", ":"],
["string", "\"oct\""],
["punctuation", ","],

["property", "\"k\""],
["operator", ":"],
["string", "\"AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow\""],

["punctuation", "}"],
["punctuation", ")"]
]

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

Checks for all functions.
31 changes: 31 additions & 0 deletions tests/languages/rego/keyword_feature.test
@@ -0,0 +1,31 @@
as
default
else
import
package
not
null
some
with

set()

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

[
["keyword", "as"],
["keyword", "default"],
["keyword", "else"],
["keyword", "import"],
["keyword", "package"],
["keyword", "not"],
["keyword", "null"],
["keyword", "some"],
["keyword", "with"],

["keyword", "set"], ["punctuation", "("], ["punctuation", ")"]
]

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

Checks for all keywords.
23 changes: 23 additions & 0 deletions tests/languages/rego/number_feature.test
@@ -0,0 +1,23 @@
0
123
3.14159
5.0e8
0.2E+2
47e-5
-1.23
-2.34E33
-4.34E-33

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

[
["number", "0"],
["number", "123"],
["number", "3.14159"],
["number", "5.0e8"],
["number", "0.2E+2"],
["number", "47e-5"],
["number", "-1.23"],
["number", "-2.34E33"],
["number", "-4.34E-33"]
]
35 changes: 35 additions & 0 deletions tests/languages/rego/operator_feature.test
@@ -0,0 +1,35 @@
:= = :
== != < <= > >=
+ - / * %
& |
_

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

[
["operator", ":="],
["operator", "="],
["operator", ":"],

["operator", "=="],
["operator", "!="],
["operator", "<"],
["operator", "<="],
["operator", ">"],
["operator", ">="],

["operator", "+"],
["operator", "-"],
["operator", "/"],
["operator", "*"],
["operator", "%"],

["operator", "&"],
["operator", "|"],

["operator", "_"]
]

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

Checks for operators.

0 comments on commit e38986f

Please sign in to comment.