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

rego file parser #1468

Merged
merged 5 commits into from Apr 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions lib/rouge/demos/rego
@@ -0,0 +1,8 @@
package httpapi.authz

subordinates = {"alice": [], "charlie": [], "bob": ["alice"], "betty": ["charlie"]}

# HTTP API request
import input

default allow = false
45 changes: 45 additions & 0 deletions lib/rouge/lexers/rego.rb
@@ -0,0 +1,45 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

module Rouge
module Lexers
class Rego < RegexLexer
title "Rego"
desc "The Rego open-policy-agent (OPA) policy language (openpolicyagent.org)"
tag 'rego'
filenames '*.rego'

state :basic do
rule %r/\s+/, Text
rule %r/#.*/, Comment::Single

rule %r/[\[\](){}|.,;!]/, Punctuation

rule %r/"[^"]*"/, Str::Double

rule %r/-?\d+\.\d+([eE][+-]?\d+)?/, Num::Float
pyrmont marked this conversation as resolved.
Show resolved Hide resolved
rule %r/-?\d+([eE][+-]?\d+)?/, Num

rule %r/\\u[0-9a-fA-F]{4}/, Num::Hex
rule %r/\\["\/bfnrt]/, Str::Escape
end

state :atoms do
rule %r/(true|false|null)/, Keyword::Constant
rule %r/[[:word:]]*/, Str::Symbol
end

state :operators do
rule %r/(=|!=|>=|<=|>|<|\+|-|\*|%|\/|\||&|:=)/, Operator
rule %r/(default|not|package|import|as|with|else|some)/, Operator
rule %r/[\/:?@^~]+/, Operator
end

state :root do
mixin :basic
mixin :operators
mixin :atoms
end
end
end
end
15 changes: 15 additions & 0 deletions spec/lexers/rego_spec.rb
@@ -0,0 +1,15 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

describe Rouge::Lexers::Rego do
let(:subject) { Rouge::Lexers::Rego.new }

describe 'guessing' do
include Support::Guessing

it 'guesses by filename' do
assert_guess :filename => 'foo.rego'
end
end
end

30 changes: 30 additions & 0 deletions spec/visual/samples/rego
@@ -0,0 +1,30 @@
package httpapi.authz
deltamualpha marked this conversation as resolved.
Show resolved Hide resolved

subordinates = {"alice": [], "charlie": [], "bob": ["alice"], "betty": ["charlie"]}

# HTTP API request
import input
# input = { # example input
# "path": ["finance", "salary", "alice"],
# "user": "alice",
# "method": "GET"
# "version": 1
# }

default allow = false

# Allow users to get their own salaries.
allow {
input.version = 1.0e1
input.method = "GET"
input.path = ["finance", "salary", username]
input.user == username
}

# Allow managers to get their subordinates' salaries.
allow {
input.version = 1.0
input.method = "GET"
input.path = ["finance", "salary", username]
subordinates[input.user][_] == username
}