Class: Gitlab::Ci::Pipeline::Expression::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/ci/pipeline/expression/parser.rb

Constant Summary collapse

ParseError =
Class.new(Expression::ExpressionError)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokens) ⇒ Parser

Returns a new instance of Parser.


10
11
12
13
# File 'lib/gitlab/ci/pipeline/expression/parser.rb', line 10

def initialize(tokens)
  @tokens = tokens.to_enum
  @nodes = []
end

Class Method Details

.seed(statement) ⇒ Object


47
48
49
# File 'lib/gitlab/ci/pipeline/expression/parser.rb', line 47

def self.seed(statement)
  new(Expression::Lexer.new(statement).tokens)
end

Instance Method Details

#treeObject

Raises:


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
# File 'lib/gitlab/ci/pipeline/expression/parser.rb', line 15

def tree
  results = []

  tokens =
    if ::Gitlab::Ci::Features.ci_if_parenthesis_enabled?
      tokens_rpn
    else
      legacy_tokens_rpn
    end

  tokens.each do |token|
    case token.type
    when :value
      results.push(token.build)
    when :logical_operator
      right_operand = results.pop
      left_operand  = results.pop

      token.build(left_operand, right_operand).tap do |res|
        results.push(res)
      end
    else
      raise ParseError, "Unprocessable token found in parse tree: #{token.type}"
    end
  end

  raise ParseError, 'Unreachable nodes in parse tree'  if results.count > 1
  raise ParseError, 'Empty parse tree'                 if results.count < 1

  results.pop
end