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

50 lines
1.2 KiB
Ruby
Raw Normal View History

2018-03-27 19:54:05 +05:30
module Gitlab
module Ci
module Pipeline
module Expression
class Statement
StatementError = Class.new(StandardError)
GRAMMAR = [
%w[variable equals string],
%w[variable equals variable],
%w[variable equals null],
%w[string equals variable],
%w[null equals variable],
%w[variable]
].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?
end
def valid?
parse_tree.is_a?(Lexeme::Base)
rescue StatementError
false
end
2018-03-27 19:54:05 +05:30
end
end
end
end
end