Move pipeline expression lexemes to a separate module

This commit is contained in:
Grzegorz Bizon 2018-02-23 10:01:27 +01:00
parent 5ee43097fd
commit 8517627410
15 changed files with 138 additions and 128 deletions

View file

@ -1,25 +0,0 @@
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

View file

@ -1,27 +0,0 @@
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

View file

@ -0,0 +1,29 @@
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

View file

@ -0,0 +1,27 @@
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

View file

@ -0,0 +1,22 @@
module Gitlab
module Ci
module Pipeline
module Expression
module Lexeme
class Null < Lexeme::Base
PATTERN = /null/.freeze
TYPE = :value
def initialize(value = nil)
@value = value
end
def evaluate(**_)
nil
end
end
end
end
end
end
end

View file

@ -0,0 +1,26 @@
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

View file

@ -0,0 +1,25 @@
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

View file

@ -4,9 +4,9 @@ module Gitlab
module Expression
class Lexer
LEXEMES = [
Expression::Variable,
Expression::String,
Expression::Equals
Expression::Lexeme::Variable,
Expression::Lexeme::String,
Expression::Lexeme::Equals
].freeze
MAX_CYCLES = 5

View file

@ -1,20 +0,0 @@
module Gitlab
module Ci
module Pipeline
module Expression
class Null < Expression::Lexeme
PATTERN = /null/.freeze
TYPE = :value
def initialize(value = nil)
@value = value
end
def evaluate(**_)
nil
end
end
end
end
end
end

View file

@ -22,7 +22,7 @@ module Gitlab
end
end
rescue StopIteration
@nodes.last || Expression::Null.new
@nodes.last || Expression::Lexeme::Null.new
end
def self.seed(statement)

View file

@ -1,24 +0,0 @@
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

View file

@ -1,23 +0,0 @@
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

View file

@ -5,21 +5,21 @@ describe Gitlab::Ci::Pipeline::Expression::Parser do
context 'when using operators' do
it 'returns a reverse descent parse tree' do
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
context 'when using a single token' do
it 'returns a single token instance' do
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
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
.to be_a Gitlab::Ci::Pipeline::Expression::Lexeme::Null
end
end
end

View file

@ -37,7 +37,7 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do
it 'returns a reverse descent parse tree' do
expect(subject.parse_tree)
.to be_a Gitlab::Ci::Pipeline::Expression::Equals
.to be_a Gitlab::Ci::Pipeline::Expression::Lexeme::Equals
end
end
@ -46,7 +46,7 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do
it 'returns a single token instance' do
expect(subject.parse_tree)
.to be_a Gitlab::Ci::Pipeline::Expression::Variable
.to be_a Gitlab::Ci::Pipeline::Expression::Lexeme::Variable
end
end
end