Skip to content
This repository has been archived by the owner on Sep 8, 2023. It is now read-only.

Commit

Permalink
Fix identifier matching in Rego lexer (rouge-ruby#1556)
Browse files Browse the repository at this point in the history
The Rego lexer matches certain identifiers using `\b`. This will match
identifiers that include `_`. This commit replaces these rules with a
more robust generic regular expression. In addition, it changes the
token used for variable names to `Name`.
  • Loading branch information
pyrmont authored and mattt committed May 19, 2021
1 parent 43946b6 commit ddcc199
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
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"
}

0 comments on commit ddcc199

Please sign in to comment.