summaryrefslogtreecommitdiff
path: root/vendor/bundle/ruby/3.4.0/gems/rouge-4.7.0/lib/rouge/lexers/prometheus.rb
blob: c27f7bccfd892e0d3f0a481185ac33907157a0cf (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# frozen_string_literal: true

module Rouge
  module Lexers
    class Prometheus < RegexLexer
      desc 'prometheus'
      tag 'prometheus'
      aliases 'prometheus'
      filenames '*.prometheus'

      mimetypes 'text/x-prometheus', 'application/x-prometheus'

      def self.functions
        @functions ||= Set.new %w(
          abs absent ceil changes clamp_max clamp_min count_scalar day_of_month
          day_of_week days_in_month delta deriv drop_common_labels exp floor
          histogram_quantile holt_winters hour idelta increase irate label_replace
          ln log2 log10 month predict_linear rate resets round scalar sort
          sort_desc sqrt time vector year avg_over_time min_over_time
          max_over_time sum_over_time count_over_time quantile_over_time
          stddev_over_time stdvar_over_time
        )
      end

      state :root do
        mixin :strings
        mixin :whitespace

        rule %r/-?\d+\.\d+/, Num::Float
        rule %r/-?\d+[smhdwy]?/, Num::Integer

        mixin :operators

        rule %r/(ignoring|on)(\()/ do
          groups Keyword::Pseudo, Punctuation
          push :label_list
        end
        rule %r/(group_left|group_right)(\()/ do
          groups Keyword::Type, Punctuation
        end
        rule %r/(bool|offset)\b/, Keyword
        rule %r/(without|by)\b/, Keyword, :label_list
        rule %r/[\w:]+/ do |m|
          if self.class.functions.include?(m[0])
            token Name::Builtin
          else
            token Name
          end
        end

        mixin :metrics
      end

      state :metrics do
        rule %r/[a-zA-Z0-9_-]+/, Name

        rule %r/[\(\)\]:.,]/, Punctuation
        rule %r/\{/, Punctuation, :filters
        rule %r/\[/, Punctuation
      end

      state :strings do
        rule %r/"/, Str::Double, :double_string_escaped
        rule %r/'/, Str::Single, :single_string_escaped
        rule %r/`.*`/, Str::Backtick
      end

      [
        [:double, Str::Double, '"'],
        [:single, Str::Single, "'"]
      ].each do |name, tok, fin|
        state :"#{name}_string_escaped" do
          rule %r/\\[\\abfnrtv#{fin}]/, Str::Escape
          rule %r/[^\\#{fin}]+/m, tok
          rule %r/#{fin}/, tok, :pop!
        end
      end

      state :filters do
        mixin :inline_whitespace
        rule %r/,/, Punctuation
        mixin :labels
        mixin :filter_matching_operators
        mixin :strings
        rule %r/}/, Punctuation, :pop!
      end

      state :label_list do
        rule %r/\(/, Punctuation
        rule %r/[a-zA-Z0-9_:-]+/, Name::Attribute
        rule %r/,/, Punctuation
        mixin :whitespace
        rule %r/\)/, Punctuation, :pop!
      end

      state :labels do
        rule %r/[a-zA-Z0-9_:-]+/, Name::Attribute
      end

      state :operators do
        rule %r([+\-\*/%\^]), Operator  # Arithmetic
        rule %r(=|==|!=|<|>|<=|>=), Operator # Comparison
        rule %r/and|or|unless/, Operator # Logical/Set
        rule %r/(sum|min|max|avg|stddev|stdvar|count|count_values|bottomk|topk)\b/, Name::Function
      end

      state :filter_matching_operators do
        rule %r/!(=|~)|=~?/, Operator
      end

      state :inline_whitespace do
        rule %r/[ \t\r]+/, Text
      end

      state :whitespace do
        mixin :inline_whitespace
        rule %r/\n\s*/m, Text
        rule %r/#.*?$/, Comment
      end
    end
  end
end