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

Fix identifier matching in Rego lexer #1556

Merged
merged 2 commits into from
Jul 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions lib/rouge/lexers/rego.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,51 @@ class Rego < RegexLexer
tag 'rego'
filenames '*.rego'

def self.constants
@constants ||= Set.new %w(
true false null
)
end

def self.operators
@operators ||= Set.new %w(
as default else import not package some with
)
end

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
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

rule %r/[[:word:]]+/ do |m|
if self.class.constants.include? m[0]
token Keyword::Constant
elsif self.class.operators.include? m[0]
token Operator::Word
else
token Name
end
end
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions spec/visual/samples/rego
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,10 @@ allow {
input.path = ["finance", "salary", username]
subordinates[input.user][_] == username
}

not obj.foo.bar.bar

some_rule[msg] {
msg := "hey"
}