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 = [
|
2018-11-08 19:23:39 +05:30
|
|
|
%w[variable],
|
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],
|
2018-11-08 19:23:39 +05:30
|
|
|
%w[variable matches 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
|