Commit 5ee43097 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Simplify pipelines expression parser

parent 6fe4d2c6
......@@ -6,7 +6,7 @@ module Gitlab
PATTERN = /null/.freeze
TYPE = :value
def initialize(value)
def initialize(value = nil)
@value = value
end
......
......@@ -4,9 +4,7 @@ module Gitlab
module Expression
class Parser
def initialize(tokens)
# raise ArgumentError unless tokens.enumerator?
@tokens = tokens
@tokens = tokens.to_enum
@nodes = []
end
......@@ -14,10 +12,7 @@ module Gitlab
while token = @tokens.next
case token.type
when :operator
lookbehind = @nodes.last
lookahead = Parser.new(@tokens).tree
token.build(lookbehind, lookahead).tap do |node|
token.build(@nodes.last, tree).tap do |node|
@nodes.push(node)
end
when :value
......@@ -27,7 +22,11 @@ module Gitlab
end
end
rescue StopIteration
@nodes.last
@nodes.last || Expression::Null.new
end
def self.seed(statement)
new(Expression::Lexer.new(statement).tokens)
end
end
end
......
......@@ -29,7 +29,7 @@ module Gitlab
raise StatementError, 'Unknown pipeline expression!'
end
Expression::Parser.new(@lexer.tokens.to_enum).tree
Expression::Parser.new(@lexer.tokens).tree
end
def evaluate
......
......@@ -67,5 +67,4 @@ describe Gitlab::Ci::Pipeline::Expression::Lexer do
expect(lexer.lexemes).to eq %w[variable string]
end
end
end
......@@ -2,22 +2,25 @@ require 'spec_helper'
describe Gitlab::Ci::Pipeline::Expression::Parser do
describe '#tree' do
context 'when using an operator' do
context 'when using operators' do
it 'returns a reverse descent parse tree' do
expect(described_class.new(tokens('$VAR == "123"')).tree)
expect(described_class.seed('$VAR1 == "123" == $VAR2').tree)
.to be_a Gitlab::Ci::Pipeline::Expression::Equals
end
end
context 'when using a single token' do
it 'returns a single token instance' do
expect(described_class.new(tokens('$VAR')).tree)
expect(described_class.seed('$VAR').tree)
.to be_a Gitlab::Ci::Pipeline::Expression::Variable
end
end
end
def tokens(statement)
Gitlab::Ci::Pipeline::Expression::Lexer.new(statement).tokens.to_enum
context 'when expression is empty' do
it 'returns a null token' do
expect(described_class.seed('').tree)
.to be_a Gitlab::Ci::Pipeline::Expression::Null
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment