Welcome to mirror list, hosted at ThFree Co, Russian Federation.

parser.rb « expression « pipeline « ci « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 589bf32a4d73bf8c03cb34d70e45f730064325f3 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# frozen_string_literal: true

module Gitlab
  module Ci
    module Pipeline
      module Expression
        class Parser
          ParseError = Class.new(Expression::ExpressionError)

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

          def tree
            if Feature.enabled?(:ci_variables_complex_expressions, default_enabled: true)
              rpn_parse_tree
            else
              reverse_descent_parse_tree
            end
          end

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

          private

          # This produces a reverse descent parse tree.
          # It does not support precedence of operators.
          def reverse_descent_parse_tree
            while token = @tokens.next
              case token.type
              when :operator
                token.build(@nodes.pop, tree).tap do |node|
                  @nodes.push(node)
                end
              when :value
                token.build.tap do |leaf|
                  @nodes.push(leaf)
                end
              end
            end
          rescue StopIteration
            @nodes.last || Lexeme::Null.new
          end

          def rpn_parse_tree
            results = []

            tokens_rpn.each do |token|
              case token.type
              when :value
                results.push(token.build)
              when :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'
              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

          # Parse the expression into Reverse Polish Notation
          # (See: Shunting-yard algorithm)
          def tokens_rpn
            output = []
            operators = []

            @tokens.each do |token|
              case token.type
              when :value
                output.push(token)
              when :operator
                if operators.any? && token.lexeme.precedence >= operators.last.lexeme.precedence
                  output.push(operators.pop)
                end

                operators.push(token)
              end
            end

            output.concat(operators.reverse)
          end
        end
      end
    end
  end
end