Commit 85176274 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Move pipeline expression lexemes to a separate module

parent 5ee43097
module Gitlab
module Ci
module Pipeline
module Expression
class Equals < Expression::Lexeme
PATTERN = /==/.freeze
TYPE = :operator
def initialize(left, right)
@left = left
@right = right
end
def evaluate(**variables)
@left.evaluate(variables) == @right.evaluate(variables)
end
def self.build(value, behind, ahead)
new(behind, ahead)
end
end
end
end
end
end
module Gitlab
module Ci
module Pipeline
module Expression
class Lexeme
def evaluate(**variables)
raise NotImplementedError
end
def self.build(token)
raise NotImplementedError
end
def self.type
self::TYPE
end
def self.scan(scanner)
if scanner.scan(self::PATTERN)
Expression::Token.new(scanner.matched, self)
end
end
end
end
end
end
end
module Gitlab
module Ci
module Pipeline
module Expression
module Lexeme
class Base
def evaluate(**variables)
raise NotImplementedError
end
def self.build(token)
raise NotImplementedError
end
def self.type
self::TYPE
end
def self.scan(scanner)
if scanner.scan(self::PATTERN)
Expression::Token.new(scanner.matched, self)
end
end
end
end
end
end
end
end
module Gitlab
module Ci
module Pipeline
module Expression
module Lexeme
class Equals < Lexeme::Base
PATTERN = /==/.freeze
TYPE = :operator
def initialize(left, right)
@left = left
@right = right
end
def evaluate(**variables)
@left.evaluate(variables) == @right.evaluate(variables)
end
def self.build(_value, behind, ahead)
new(behind, ahead)
end
end
end
end
end
end
end
...@@ -2,16 +2,18 @@ module Gitlab ...@@ -2,16 +2,18 @@ module Gitlab
module Ci module Ci
module Pipeline module Pipeline
module Expression module Expression
class Null < Expression::Lexeme module Lexeme
PATTERN = /null/.freeze class Null < Lexeme::Base
TYPE = :value PATTERN = /null/.freeze
TYPE = :value
def initialize(value = nil) def initialize(value = nil)
@value = value @value = value
end end
def evaluate(**_) def evaluate(**_)
nil nil
end
end end
end end
end end
......
module Gitlab
module Ci
module Pipeline
module Expression
module Lexeme
class String < Lexeme::Base
PATTERN = /"(?<string>.+?)"/.freeze
TYPE = :value
def initialize(value)
@value = value
end
def evaluate(**_)
@value.to_s
end
def self.build(string)
new(string.match(PATTERN)[:string])
end
end
end
end
end
end
end
module Gitlab
module Ci
module Pipeline
module Expression
module Lexeme
class Variable < Lexeme::Base
PATTERN = /\$(?<name>\w+)/.freeze
TYPE = :value
def initialize(name)
@name = name
end
def evaluate(**variables)
end
def self.build(string)
new(string.match(PATTERN)[:name])
end
end
end
end
end
end
end
...@@ -4,9 +4,9 @@ module Gitlab ...@@ -4,9 +4,9 @@ module Gitlab
module Expression module Expression
class Lexer class Lexer
LEXEMES = [ LEXEMES = [
Expression::Variable, Expression::Lexeme::Variable,
Expression::String, Expression::Lexeme::String,
Expression::Equals Expression::Lexeme::Equals
].freeze ].freeze
MAX_CYCLES = 5 MAX_CYCLES = 5
......
...@@ -22,7 +22,7 @@ module Gitlab ...@@ -22,7 +22,7 @@ module Gitlab
end end
end end
rescue StopIteration rescue StopIteration
@nodes.last || Expression::Null.new @nodes.last || Expression::Lexeme::Null.new
end end
def self.seed(statement) def self.seed(statement)
......
module Gitlab
module Ci
module Pipeline
module Expression
class String < Expression::Lexeme
PATTERN = /"(?<string>.+?)"/.freeze
TYPE = :value
def initialize(value)
@value = value
end
def evaluate(**_)
@value.to_s
end
def self.build(string)
new(string.match(PATTERN)[:string])
end
end
end
end
end
end
module Gitlab
module Ci
module Pipeline
module Expression
class Variable < Expression::Lexeme
PATTERN = /\$(?<name>\w+)/.freeze
TYPE = :value
def initialize(name)
@name = name
end
def evaluate(**variables)
end
def self.build(string)
new(string.match(PATTERN)[:name])
end
end
end
end
end
end
...@@ -5,21 +5,21 @@ describe Gitlab::Ci::Pipeline::Expression::Parser do ...@@ -5,21 +5,21 @@ describe Gitlab::Ci::Pipeline::Expression::Parser do
context 'when using operators' 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.seed('$VAR1 == "123" == $VAR2').tree) expect(described_class.seed('$VAR1 == "123" == $VAR2').tree)
.to be_a Gitlab::Ci::Pipeline::Expression::Equals .to be_a Gitlab::Ci::Pipeline::Expression::Lexeme::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.seed('$VAR').tree) expect(described_class.seed('$VAR').tree)
.to be_a Gitlab::Ci::Pipeline::Expression::Variable .to be_a Gitlab::Ci::Pipeline::Expression::Lexeme::Variable
end end
end end
context 'when expression is empty' do context 'when expression is empty' do
it 'returns a null token' do it 'returns a null token' do
expect(described_class.seed('').tree) expect(described_class.seed('').tree)
.to be_a Gitlab::Ci::Pipeline::Expression::Null .to be_a Gitlab::Ci::Pipeline::Expression::Lexeme::Null
end end
end end
end end
......
...@@ -37,7 +37,7 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do ...@@ -37,7 +37,7 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do
it 'returns a reverse descent parse tree' do it 'returns a reverse descent parse tree' do
expect(subject.parse_tree) expect(subject.parse_tree)
.to be_a Gitlab::Ci::Pipeline::Expression::Equals .to be_a Gitlab::Ci::Pipeline::Expression::Lexeme::Equals
end end
end end
...@@ -46,7 +46,7 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do ...@@ -46,7 +46,7 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do
it 'returns a single token instance' do it 'returns a single token instance' do
expect(subject.parse_tree) expect(subject.parse_tree)
.to be_a Gitlab::Ci::Pipeline::Expression::Variable .to be_a Gitlab::Ci::Pipeline::Expression::Lexeme::Variable
end 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