diff --git a/lib/gitlab/ci/pipeline/expression/equals.rb b/lib/gitlab/ci/pipeline/expression/equals.rb deleted file mode 100644 index 93883982b63..00000000000 --- a/lib/gitlab/ci/pipeline/expression/equals.rb +++ /dev/null @@ -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 diff --git a/lib/gitlab/ci/pipeline/expression/lexeme.rb b/lib/gitlab/ci/pipeline/expression/lexeme.rb deleted file mode 100644 index d44bf9e419d..00000000000 --- a/lib/gitlab/ci/pipeline/expression/lexeme.rb +++ /dev/null @@ -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 diff --git a/lib/gitlab/ci/pipeline/expression/lexeme/base.rb b/lib/gitlab/ci/pipeline/expression/lexeme/base.rb new file mode 100644 index 00000000000..29b73c94358 --- /dev/null +++ b/lib/gitlab/ci/pipeline/expression/lexeme/base.rb @@ -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 diff --git a/lib/gitlab/ci/pipeline/expression/lexeme/equals.rb b/lib/gitlab/ci/pipeline/expression/lexeme/equals.rb new file mode 100644 index 00000000000..0cb21131d69 --- /dev/null +++ b/lib/gitlab/ci/pipeline/expression/lexeme/equals.rb @@ -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 diff --git a/lib/gitlab/ci/pipeline/expression/lexeme/null.rb b/lib/gitlab/ci/pipeline/expression/lexeme/null.rb new file mode 100644 index 00000000000..21286151d79 --- /dev/null +++ b/lib/gitlab/ci/pipeline/expression/lexeme/null.rb @@ -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 diff --git a/lib/gitlab/ci/pipeline/expression/lexeme/string.rb b/lib/gitlab/ci/pipeline/expression/lexeme/string.rb new file mode 100644 index 00000000000..470a5a15cc1 --- /dev/null +++ b/lib/gitlab/ci/pipeline/expression/lexeme/string.rb @@ -0,0 +1,26 @@ +module Gitlab + module Ci + module Pipeline + module Expression + module Lexeme + class String < Lexeme::Base + PATTERN = /"(?.+?)"/.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 diff --git a/lib/gitlab/ci/pipeline/expression/lexeme/variable.rb b/lib/gitlab/ci/pipeline/expression/lexeme/variable.rb new file mode 100644 index 00000000000..5fb5dd4bbbe --- /dev/null +++ b/lib/gitlab/ci/pipeline/expression/lexeme/variable.rb @@ -0,0 +1,25 @@ +module Gitlab + module Ci + module Pipeline + module Expression + module Lexeme + class Variable < Lexeme::Base + PATTERN = /\$(?\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 diff --git a/lib/gitlab/ci/pipeline/expression/lexer.rb b/lib/gitlab/ci/pipeline/expression/lexer.rb index 9334358cd8e..038c0e7e330 100644 --- a/lib/gitlab/ci/pipeline/expression/lexer.rb +++ b/lib/gitlab/ci/pipeline/expression/lexer.rb @@ -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 diff --git a/lib/gitlab/ci/pipeline/expression/null.rb b/lib/gitlab/ci/pipeline/expression/null.rb deleted file mode 100644 index 31dcba05b48..00000000000 --- a/lib/gitlab/ci/pipeline/expression/null.rb +++ /dev/null @@ -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 diff --git a/lib/gitlab/ci/pipeline/expression/parser.rb b/lib/gitlab/ci/pipeline/expression/parser.rb index 350c9e543ac..b395e4692f1 100644 --- a/lib/gitlab/ci/pipeline/expression/parser.rb +++ b/lib/gitlab/ci/pipeline/expression/parser.rb @@ -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) diff --git a/lib/gitlab/ci/pipeline/expression/string.rb b/lib/gitlab/ci/pipeline/expression/string.rb deleted file mode 100644 index bbd75cb4632..00000000000 --- a/lib/gitlab/ci/pipeline/expression/string.rb +++ /dev/null @@ -1,24 +0,0 @@ -module Gitlab - module Ci - module Pipeline - module Expression - class String < Expression::Lexeme - PATTERN = /"(?.+?)"/.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 diff --git a/lib/gitlab/ci/pipeline/expression/variable.rb b/lib/gitlab/ci/pipeline/expression/variable.rb deleted file mode 100644 index 379f8168ca9..00000000000 --- a/lib/gitlab/ci/pipeline/expression/variable.rb +++ /dev/null @@ -1,23 +0,0 @@ -module Gitlab - module Ci - module Pipeline - module Expression - class Variable < Expression::Lexeme - PATTERN = /\$(?\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 diff --git a/spec/lib/gitlab/ci/pipeline/expression/equals_spec.rb b/spec/lib/gitlab/ci/pipeline/expression/lexeme/equals_spec.rb similarity index 100% rename from spec/lib/gitlab/ci/pipeline/expression/equals_spec.rb rename to spec/lib/gitlab/ci/pipeline/expression/lexeme/equals_spec.rb diff --git a/spec/lib/gitlab/ci/pipeline/expression/parser_spec.rb b/spec/lib/gitlab/ci/pipeline/expression/parser_spec.rb index 63c3393f07a..e8e6f585310 100644 --- a/spec/lib/gitlab/ci/pipeline/expression/parser_spec.rb +++ b/spec/lib/gitlab/ci/pipeline/expression/parser_spec.rb @@ -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 diff --git a/spec/lib/gitlab/ci/pipeline/expression/statement_spec.rb b/spec/lib/gitlab/ci/pipeline/expression/statement_spec.rb index 3c2cbbac80d..672ee9d75c0 100644 --- a/spec/lib/gitlab/ci/pipeline/expression/statement_spec.rb +++ b/spec/lib/gitlab/ci/pipeline/expression/statement_spec.rb @@ -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