2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
module Pipeline
|
|
|
|
module Expression
|
|
|
|
class Statement
|
2018-11-08 19:23:39 +05:30
|
|
|
StatementError = Class.new(Expression::ExpressionError)
|
2018-03-27 19:54:05 +05:30
|
|
|
|
|
|
|
GRAMMAR = [
|
2019-07-31 22:56:46 +05:30
|
|
|
# presence matchers
|
2018-11-08 19:23:39 +05:30
|
|
|
%w[variable],
|
2019-07-31 22:56:46 +05:30
|
|
|
|
|
|
|
# positive matchers
|
2018-03-27 19:54:05 +05:30
|
|
|
%w[variable equals string],
|
|
|
|
%w[variable equals variable],
|
|
|
|
%w[variable equals null],
|
|
|
|
%w[string equals variable],
|
|
|
|
%w[null equals variable],
|
2019-07-31 22:56:46 +05:30
|
|
|
%w[variable matches pattern],
|
|
|
|
|
|
|
|
# negative matchers
|
|
|
|
%w[variable notequals string],
|
|
|
|
%w[variable notequals variable],
|
|
|
|
%w[variable notequals null],
|
|
|
|
%w[string notequals variable],
|
|
|
|
%w[null notequals variable],
|
|
|
|
%w[variable notmatches pattern]
|
2018-03-27 19:54:05 +05:30
|
|
|
].freeze
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
def initialize(statement, variables = {})
|
2018-03-27 19:54:05 +05:30
|
|
|
@lexer = Expression::Lexer.new(statement)
|
2018-05-09 12:01:36 +05:30
|
|
|
@variables = variables.with_indifferent_access
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def parse_tree
|
|
|
|
raise StatementError if @lexer.lexemes.empty?
|
|
|
|
|
|
|
|
unless GRAMMAR.find { |syntax| syntax == @lexer.lexemes }
|
|
|
|
raise StatementError, 'Unknown pipeline expression!'
|
|
|
|
end
|
|
|
|
|
|
|
|
Expression::Parser.new(@lexer.tokens).tree
|
|
|
|
end
|
|
|
|
|
|
|
|
def evaluate
|
|
|
|
parse_tree.evaluate(@variables.to_h)
|
|
|
|
end
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
def truthful?
|
|
|
|
evaluate.present?
|
2018-11-08 19:23:39 +05:30
|
|
|
rescue Expression::ExpressionError
|
|
|
|
false
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def valid?
|
|
|
|
parse_tree.is_a?(Lexeme::Base)
|
2018-11-08 19:23:39 +05:30
|
|
|
rescue Expression::ExpressionError
|
2018-05-09 12:01:36 +05:30
|
|
|
false
|
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|