summaryrefslogtreecommitdiff
path: root/vendor/bundle/ruby/3.4.0/gems/rouge-4.5.2/lib/rouge/lexers/rego.rb
blob: 6ca1a5d5d8e3eb9d42fa84e62d2efabf6b66c0ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# -*- 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'

            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 :operators do
              rule %r/(=|!=|>=|<=|>|<|\+|-|\*|%|\/|\||&|:=)/, Operator
              rule %r/[\/:?@^~]+/, Operator
            end

            state :root do
              mixin :basic
              mixin :operators

              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
end