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 Parser
|
2019-09-04 21:01:54 +05:30
|
|
|
ParseError = Class.new(Expression::ExpressionError)
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def initialize(tokens)
|
|
|
|
@tokens = tokens.to_enum
|
|
|
|
@nodes = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def tree
|
2019-09-04 21:01:54 +05:30
|
|
|
if Feature.enabled?(:ci_variables_complex_expressions)
|
|
|
|
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
|
2018-03-27 19:54:05 +05:30
|
|
|
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
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
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)
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|