2018-10-26 00:12:43 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-22 07:54:22 -05:00
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
module Pipeline
|
|
|
|
module Expression
|
|
|
|
class Parser
|
2018-02-22 10:04:18 -05:00
|
|
|
def initialize(tokens)
|
2018-02-23 03:52:08 -05:00
|
|
|
@tokens = tokens.to_enum
|
2018-02-22 10:04:18 -05:00
|
|
|
@nodes = []
|
2018-02-22 07:54:22 -05:00
|
|
|
end
|
|
|
|
|
2018-03-01 06:23:27 -05:00
|
|
|
##
|
|
|
|
# This produces a reverse descent parse tree.
|
|
|
|
#
|
|
|
|
# It currently does not support precedence of operators.
|
|
|
|
#
|
2018-02-22 07:54:22 -05:00
|
|
|
def tree
|
2018-02-22 10:04:18 -05:00
|
|
|
while token = @tokens.next
|
|
|
|
case token.type
|
|
|
|
when :operator
|
2018-02-27 04:39:35 -05:00
|
|
|
token.build(@nodes.pop, tree).tap do |node|
|
2018-02-22 10:04:18 -05:00
|
|
|
@nodes.push(node)
|
|
|
|
end
|
|
|
|
when :value
|
|
|
|
token.build.tap do |leaf|
|
|
|
|
@nodes.push(leaf)
|
|
|
|
end
|
|
|
|
end
|
2018-02-22 07:54:22 -05:00
|
|
|
end
|
2018-02-22 10:04:18 -05:00
|
|
|
rescue StopIteration
|
2018-02-23 04:13:43 -05:00
|
|
|
@nodes.last || Lexeme::Null.new
|
2018-02-23 03:52:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.seed(statement)
|
|
|
|
new(Expression::Lexer.new(statement).tokens)
|
2018-02-22 07:54:22 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|