|
2 | 2 | # frozen_string_literal: true |
3 | 3 |
|
4 | 4 | module Rouge |
5 | | - module Lexers |
6 | | - class Rego < RegexLexer |
7 | | - title "Rego" |
8 | | - desc "The Rego open-policy-agent (OPA) policy language (openpolicyagent.org)" |
9 | | - tag 'rego' |
10 | | - filenames '*.rego' |
11 | | - |
12 | | - def self.constants |
13 | | - @constants ||= Set.new %w( |
14 | | - true false null |
15 | | - ) |
16 | | - end |
17 | | - |
18 | | - def self.operators |
19 | | - @operators ||= Set.new %w( |
20 | | - as default else import not package some with |
21 | | - ) |
22 | | - end |
23 | | - |
24 | | - state :basic do |
25 | | - rule %r/\s+/, Text |
26 | | - rule %r/#.*/, Comment::Single |
27 | | - |
28 | | - rule %r/[\[\](){}|.,;!]/, Punctuation |
29 | | - |
30 | | - rule %r/"[^"]*"/, Str::Double |
31 | | - |
32 | | - rule %r/-?\d+\.\d+([eE][+-]?\d+)?/, Num::Float |
33 | | - rule %r/-?\d+([eE][+-]?\d+)?/, Num |
34 | | - |
35 | | - rule %r/\\u[0-9a-fA-F]{4}/, Num::Hex |
36 | | - rule %r/\\["\/bfnrt]/, Str::Escape |
37 | | - end |
38 | | - |
39 | | - state :operators do |
40 | | - rule %r/(=|!=|>=|<=|>|<|\+|-|\*|%|\/|\||&|:=)/, Operator |
41 | | - rule %r/[\/:?@^~]+/, Operator |
42 | | - end |
43 | | - |
44 | | - state :root do |
45 | | - mixin :basic |
46 | | - mixin :operators |
47 | | - |
48 | | - rule %r/[[:word:]]+/ do |m| |
49 | | - if self.class.constants.include? m[0] |
50 | | - token Keyword::Constant |
51 | | - elsif self.class.operators.include? m[0] |
52 | | - token Operator::Word |
53 | | - else |
54 | | - token Name |
55 | | - end |
56 | | - end |
57 | | - end |
| 5 | + module Lexers |
| 6 | + class Rego < RegexLexer |
| 7 | + title "Rego" |
| 8 | + desc "The Rego open-policy-agent (OPA) policy language (openpolicyagent.org)" |
| 9 | + tag 'rego' |
| 10 | + filenames '*.rego' |
| 11 | + |
| 12 | + def self.constants |
| 13 | + @constants ||= Set.new %w( |
| 14 | + true false null |
| 15 | + ) |
| 16 | + end |
| 17 | + |
| 18 | + def self.operators |
| 19 | + @operators ||= Set.new %w( |
| 20 | + as default else import not package some with |
| 21 | + ) |
| 22 | + end |
| 23 | + |
| 24 | + state :basic do |
| 25 | + rule %r/\s+/, Text |
| 26 | + rule %r/#.*/, Comment::Single |
| 27 | + |
| 28 | + rule %r/[\[\](){}|.,;!]/, Punctuation |
| 29 | + |
| 30 | + rule %r/"[^"]*"/, Str::Double |
| 31 | + |
| 32 | + rule %r/-?\d+\.\d+([eE][+-]?\d+)?/, Num::Float |
| 33 | + rule %r/-?\d+([eE][+-]?\d+)?/, Num |
| 34 | + |
| 35 | + rule %r/\\u[0-9a-fA-F]{4}/, Num::Hex |
| 36 | + rule %r/\\["\/bfnrt]/, Str::Escape |
| 37 | + end |
| 38 | + |
| 39 | + state :operators do |
| 40 | + rule %r/(=|!=|>=|<=|>|<|\+|-|\*|%|\/|\||&|:=)/, Operator |
| 41 | + rule %r/[\/:?@^~]+/, Operator |
| 42 | + end |
| 43 | + |
| 44 | + state :root do |
| 45 | + mixin :basic |
| 46 | + mixin :operators |
| 47 | + |
| 48 | + rule %r/[[:word:]]+/ do |m| |
| 49 | + if self.class.constants.include? m[0] |
| 50 | + token Keyword::Constant |
| 51 | + elsif self.class.operators.include? m[0] |
| 52 | + token Operator::Word |
| 53 | + else |
| 54 | + token Name |
| 55 | + end |
58 | 56 | end |
| 57 | + end |
59 | 58 | end |
| 59 | + end |
60 | 60 | end |
0 commit comments