Simplify pipelines expression parser

This commit is contained in:
Grzegorz Bizon 2018-02-23 09:52:08 +01:00
parent 6fe4d2c6f0
commit 5ee43097fd
5 changed files with 18 additions and 17 deletions

View File

@ -6,7 +6,7 @@ module Gitlab
PATTERN = /null/.freeze PATTERN = /null/.freeze
TYPE = :value TYPE = :value
def initialize(value) def initialize(value = nil)
@value = value @value = value
end end

View File

@ -4,9 +4,7 @@ module Gitlab
module Expression module Expression
class Parser class Parser
def initialize(tokens) def initialize(tokens)
# raise ArgumentError unless tokens.enumerator? @tokens = tokens.to_enum
@tokens = tokens
@nodes = [] @nodes = []
end end
@ -14,10 +12,7 @@ module Gitlab
while token = @tokens.next while token = @tokens.next
case token.type case token.type
when :operator when :operator
lookbehind = @nodes.last token.build(@nodes.last, tree).tap do |node|
lookahead = Parser.new(@tokens).tree
token.build(lookbehind, lookahead).tap do |node|
@nodes.push(node) @nodes.push(node)
end end
when :value when :value
@ -27,7 +22,11 @@ module Gitlab
end end
end end
rescue StopIteration rescue StopIteration
@nodes.last @nodes.last || Expression::Null.new
end
def self.seed(statement)
new(Expression::Lexer.new(statement).tokens)
end end
end end
end end

View File

@ -29,7 +29,7 @@ module Gitlab
raise StatementError, 'Unknown pipeline expression!' raise StatementError, 'Unknown pipeline expression!'
end end
Expression::Parser.new(@lexer.tokens.to_enum).tree Expression::Parser.new(@lexer.tokens).tree
end end
def evaluate def evaluate

View File

@ -67,5 +67,4 @@ describe Gitlab::Ci::Pipeline::Expression::Lexer do
expect(lexer.lexemes).to eq %w[variable string] expect(lexer.lexemes).to eq %w[variable string]
end end
end end
end end

View File

@ -2,22 +2,25 @@ require 'spec_helper'
describe Gitlab::Ci::Pipeline::Expression::Parser do describe Gitlab::Ci::Pipeline::Expression::Parser do
describe '#tree' do describe '#tree' do
context 'when using an operator' do context 'when using operators' do
it 'returns a reverse descent parse tree' 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 .to be_a Gitlab::Ci::Pipeline::Expression::Equals
end end
end end
context 'when using a single token' do context 'when using a single token' do
it 'returns a single token instance' 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 .to be_a Gitlab::Ci::Pipeline::Expression::Variable
end end
end end
end
def tokens(statement) context 'when expression is empty' do
Gitlab::Ci::Pipeline::Expression::Lexer.new(statement).tokens.to_enum it 'returns a null token' do
expect(described_class.seed('').tree)
.to be_a Gitlab::Ci::Pipeline::Expression::Null
end
end
end end
end end