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 rego support #2624

Merged
merged 10 commits into from
Mar 7, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,10 @@
"title": "Regex",
"owner": "RunDevelopment"
},
"rego": {
"title": "Rego",
"owner": "JordanSh"
},
"renpy": {
"title": "Ren'py",
"alias": "rpy",
Expand Down
20 changes: 20 additions & 0 deletions components/prism-rego.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Prism.languages.rego = {
'comment': /[#].*$/m,
'keyword': /\b(?:input|default)\b/,
'string': /("|')(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
'boolean': /\b(?:true|false|allow|deny)\b/,
'operator': /&&?|\|\|?|\*\*?|>>>?|<<|[:=<>!~]=?|[-/%^]|\+!?|\b(?:not)\b/,
'function': [
{
pattern: /\b(?:package|some)\b/
},
{
pattern: /\b(?:round|abs|count|sum|product|max|min|sort|all|any)\b/,
inside: {
builtin: /(?:[^(])/,
}
},
],
'constant': /[^\[]+(?=\])/m,

};
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.

50 changes: 50 additions & 0 deletions examples/prism-rego.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<h2>Comments</h2>
<pre><code># some comment</code></pre>

<h2>Quoted strings</h2>
<pre><code>"foo""bar"</code></pre>

<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>
17 changes: 17 additions & 0 deletions tests/languages/rego/boolean_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
true
false
allow
deny

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

[
["boolean", "true"],
["boolean", "false"],
["boolean", "allow"],
["boolean", "deny"]
]

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

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

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

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

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

Checks for comments.
14 changes: 14 additions & 0 deletions tests/languages/rego/constant_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
foo[
["constant", "bar"]
]

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

[
"foo[\r\n\t[",
["string", "\"constant\""],
", ",
["string", "\"bar\""],
["constant", "]\r\n"],
"]"
]
13 changes: 13 additions & 0 deletions tests/languages/rego/function_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package some(

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

[
["function", "package"],
["function", "some"],
"("
]

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

Checks for all functions.
12 changes: 12 additions & 0 deletions tests/languages/rego/keyword_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
input
default
----------------------------------------------------

[
["keyword", "input"],
["keyword", "default"]
]

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

Checks for all keywords.
15 changes: 15 additions & 0 deletions tests/languages/rego/operator_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
:= = == !=
< <= > >=
+ - / *

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

[
["operator", ":="], ["operator", "="], ["operator", "=="], ["operator", "!="],
["operator", "<"], ["operator", "<="], ["operator", ">"], ["operator", ">="],
["operator", "+"], ["operator", "-"], ["operator", "/"], ["operator", "*"]
]

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

Checks for operators.
13 changes: 13 additions & 0 deletions tests/languages/rego/string_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
""
"foo\"bar"

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

[
["string", "\"\""],
["string", "\"foo\\\"bar\""]
]

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

Checks for strings.