debian-mirror-gitlab/lib/gitlab/ci/pipeline/expression/statement.rb

42 lines
925 B
Ruby
Raw Normal View History

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
2021-04-17 20:07:23 +05:30
def initialize(statement, variables = nil)
2018-03-27 19:54:05 +05:30
@lexer = Expression::Lexer.new(statement)
2022-01-26 12:08:38 +05:30
@variables = variables || {}
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
2022-01-26 12:08:38 +05:30
parse_tree.evaluate(@variables)
2018-03-27 19:54:05 +05:30
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