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
module Ci
module Pipeline
module Expression
class Null < Expression::Lexeme
PATTERN = /null/.freeze
TYPE = :value
module Lexeme
class Null < Lexeme::Base
PATTERN = /null/.freeze
TYPE = :value
def initialize(value = nil)
@value = value
end
def initialize(value = nil)
@value = value
end
def evaluate(**_)
nil
def evaluate(**_)
nil
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
module Expression
class Lexer
LEXEMES = [
Expression::Variable,
Expression::String,
Expression::Equals
Expression::Lexeme::Variable,
Expression::Lexeme::String,
Expression::Lexeme::Equals
].freeze
MAX_CYCLES = 5
......
......@@ -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)
......
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
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
......
......@@ -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
......
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