blob: 21bd5414467300b1bec99f0f863e7b48c7695055 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
module Gitlab
module Ci
module Pipeline
module Expression
class Statement
ParserError = 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
def initialize(statement, pipeline)
@pipeline = pipeline
@lexer = Expression::Lexer.new(statement)
@variables = pipeline.variables.map do |variable|
{ variable.key => variable.value }
end
end
def tokens
@tokens ||= @lexer.tokenize
end
def lexemes
@lexemes ||= tokens.map(&:to_lexeme)
end
##
# Our syntax is very simple, so we don't yet need to implement a
# recursive parser, we can use the most simple approach to create
# a reverse descent parse tree "by hand".
#
def parse_tree
raise ParserError if lexemes.empty?
unless GRAMMAR.find { |syntax| syntax == lexemes }
raise ParserError, 'Unknown pipeline expression!'
end
if tokens.many?
Expression::Equals.new(tokens.first.build, tokens.last.build)
else
tokens.first.build
end
end
def evaluate
parse_tree.evaluate(**@variables)
end
end
end
end
end
end
|