Commit 8c7374ca authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add operator / value types in pipeline expressions

parent 85176274
...@@ -3,9 +3,8 @@ module Gitlab ...@@ -3,9 +3,8 @@ module Gitlab
module Pipeline module Pipeline
module Expression module Expression
module Lexeme module Lexeme
class Equals < Lexeme::Base class Equals < Lexeme::Operator
PATTERN = /==/.freeze PATTERN = /==/.freeze
TYPE = :operator
def initialize(left, right) def initialize(left, right)
@left = left @left = left
......
...@@ -3,9 +3,8 @@ module Gitlab ...@@ -3,9 +3,8 @@ module Gitlab
module Pipeline module Pipeline
module Expression module Expression
module Lexeme module Lexeme
class Null < Lexeme::Base class Null < Lexeme::Value
PATTERN = /null/.freeze PATTERN = /null/.freeze
TYPE = :value
def initialize(value = nil) def initialize(value = nil)
@value = value @value = value
......
module Gitlab
module Ci
module Pipeline
module Expression
module Lexeme
class Operator < Lexeme::Base
def self.type
:operator
end
end
end
end
end
end
end
...@@ -3,9 +3,8 @@ module Gitlab ...@@ -3,9 +3,8 @@ module Gitlab
module Pipeline module Pipeline
module Expression module Expression
module Lexeme module Lexeme
class String < Lexeme::Base class String < Lexeme::Value
PATTERN = /"(?<string>.+?)"/.freeze PATTERN = /"(?<string>.+?)"/.freeze
TYPE = :value
def initialize(value) def initialize(value)
@value = value @value = value
......
module Gitlab
module Ci
module Pipeline
module Expression
module Lexeme
class Value < Lexeme::Base
def self.type
:value
end
end
end
end
end
end
end
...@@ -3,9 +3,8 @@ module Gitlab ...@@ -3,9 +3,8 @@ module Gitlab
module Pipeline module Pipeline
module Expression module Expression
module Lexeme module Lexeme
class Variable < Lexeme::Base class Variable < Lexeme::Value
PATTERN = /\$(?<name>\w+)/.freeze PATTERN = /\$(?<name>\w+)/.freeze
TYPE = :value
def initialize(name) def initialize(name)
@name = name @name = name
......
...@@ -10,7 +10,7 @@ module Gitlab ...@@ -10,7 +10,7 @@ module Gitlab
].freeze ].freeze
MAX_CYCLES = 5 MAX_CYCLES = 5
SyntaxError = Class.new(StandardError) SyntaxError = Class.new(Statement::StatementError)
def initialize(statement) def initialize(statement)
@scanner = StringScanner.new(statement) @scanner = StringScanner.new(statement)
......
...@@ -22,7 +22,7 @@ module Gitlab ...@@ -22,7 +22,7 @@ module Gitlab
end end
end end
rescue StopIteration rescue StopIteration
@nodes.last || Expression::Lexeme::Null.new @nodes.last || Lexeme::Null.new
end end
def self.seed(statement) def self.seed(statement)
......
...@@ -23,11 +23,20 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do ...@@ -23,11 +23,20 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do
end end
context 'when expression grammar is incorrect' do context 'when expression grammar is incorrect' do
let(:text) { '$VAR "text"' } table = [
'$VAR "text"', # missing operator
it 'raises an error' do '== "123"', # invalid right side
expect { subject.parse_tree } "'single quotes'", # single quotes string
.to raise_error described_class::StatementError '$VAR ==', # invalid right side
'12345', # unknown syntax
'' # empty statement
]
table.each do |syntax|
it "raises an error when syntax is `#{syntax}`" do
expect { described_class.new(syntax, pipeline).parse_tree }
.to raise_error described_class::StatementError
end
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