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
|
|
|
|
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?
|
|
|
|
|
|
|
|
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?
|
2019-09-04 21:01:54 +05:30
|
|
|
evaluate
|
2018-05-09 12:01:36 +05:30
|
|
|
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
|