pattern.rb 801 Bytes
Newer Older
1 2 3 4 5
module Gitlab
  module Ci
    module Pipeline
      module Expression
        module Lexeme
6 7
          require_dependency 're2'

8
          class Pattern < Lexeme::Value
9
            PATTERN = %r{^/.+/[ismU]*$}.freeze
10 11 12

            def initialize(regexp)
              @value = regexp
13 14 15 16

              unless Gitlab::UntrustedRegexp.valid?(@value)
                raise Lexer::SyntaxError, 'Invalid regular expression!'
              end
17 18 19
            end

            def evaluate(variables = {})
20
              Gitlab::UntrustedRegexp.fabricate(@value)
21
            rescue RegexpError
22
              raise Expression::RuntimeError, 'Invalid regular expression!'
23 24 25
            end

            def self.build(string)
26
              new(string)
27 28 29 30 31 32 33
            end
          end
        end
      end
    end
  end
end