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

81 lines
2.3 KiB
Ruby
Raw Normal View History

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
results = []
2020-11-24 15:15:51 +05:30
tokens = tokens_rpn
2020-10-24 23:57:45 +05:30
tokens.each do |token|
2019-09-04 21:01:54 +05:30
case token.type
when :value
results.push(token.build)
2020-10-24 23:57:45 +05:30
when :logical_operator
2019-09-04 21:01:54 +05:30
right_operand = results.pop
left_operand = results.pop
token.build(left_operand, right_operand).tap do |res|
results.push(res)
end
else
2020-10-24 23:57:45 +05:30
raise ParseError, "Unprocessable token found in parse tree: #{token.type}"
2019-09-04 21:01:54 +05:30
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
2019-09-30 21:07:59 +05:30
def self.seed(statement)
new(Expression::Lexer.new(statement).tokens)
end
private
2019-09-04 21:01:54 +05:30
# Parse the expression into Reverse Polish Notation
# (See: Shunting-yard algorithm)
2020-10-24 23:57:45 +05:30
# Taken from: https://en.wikipedia.org/wiki/Shunting-yard_algorithm#The_algorithm_in_detail
2019-09-04 21:01:54 +05:30
def tokens_rpn
output = []
operators = []
@tokens.each do |token|
case token.type
when :value
output.push(token)
2020-10-24 23:57:45 +05:30
when :logical_operator
output.push(operators.pop) while token.lexeme.consume?(operators.last&.lexeme)
operators.push(token)
when :parenthesis_open
operators.push(token)
when :parenthesis_close
output.push(operators.pop) while token.lexeme.consume?(operators.last&.lexeme)
raise ParseError, 'Unmatched parenthesis' unless operators.last
operators.pop if operators.last.lexeme.type == :parenthesis_open
end
end
output.concat(operators.reverse)
end
2018-03-27 19:54:05 +05:30
end
end
end
end
end