Commit 9954928c authored by Grzegorz Bizon's avatar Grzegorz Bizon

Implement pipeline expressions lexer

parent 523b84d4
...@@ -8,7 +8,9 @@ module Gitlab ...@@ -8,7 +8,9 @@ module Gitlab
end end
def self.scan(scanner) def self.scan(scanner)
scanner.scan(PATTERN) if scanner.scan(self::PATTERN)
Expression::Token.new(scanner.matched, self)
end
end end
end end
end end
......
...@@ -2,12 +2,32 @@ module Gitlab ...@@ -2,12 +2,32 @@ module Gitlab
module Ci module Ci
module Pipeline module Pipeline
module Expression module Expression
LEXEMES = [
Expression::Variable
]
MAX_CYCLES = 5
class Lexer class Lexer
def initialize(statement) def initialize(statement)
@statement = statement @scanner = StringScanner.new(statement)
@tokens = []
end end
def tokenize def tokenize
@tokens.tap do
MAX_CYCLES.times do
LEXEMES.each do |lexeme|
@scanner.scan(/\s+/) # ignore whitespace
lexeme.scan(@scanner).tap do |token|
@tokens.push(token) if token.present?
end
return @tokens if @scanner.eos?
end
end
end
end end
end end
end end
......
...@@ -9,6 +9,7 @@ module Gitlab ...@@ -9,6 +9,7 @@ module Gitlab
%w[variable equals null], %w[variable equals null],
%w[string equals variable], %w[string equals variable],
%w[null equals variable], %w[null equals variable],
%w[variable]
] ]
def initialize(pipeline, statement) def initialize(pipeline, statement)
......
module Gitlab
module Ci
module Pipeline
module Expression
class Token
def initialize(value, type)
@value = value
@type = type
end
def to_lexeme
end
end
end
end
end
end
...@@ -2,8 +2,8 @@ module Gitlab ...@@ -2,8 +2,8 @@ module Gitlab
module Ci module Ci
module Pipeline module Pipeline
module Expression module Expression
class Equality < Expression::Lexeme class Variable < Expression::Lexeme
PATTERN = /$(?<name>\w+)/.freeze PATTERN = /\$(?<name>\w+)/.freeze
def initialize(value) def initialize(value)
@value = value @value = value
...@@ -11,9 +11,6 @@ module Gitlab ...@@ -11,9 +11,6 @@ module Gitlab
def evaluate(**variables) def evaluate(**variables)
end end
def self.build(string)
end
end end
end end
end end
......
require 'spec_helper'
describe Gitlab::Ci::Pipeline::Expression::Lexer do
let(:token_class) do
Gitlab::Ci::Pipeline::Expression::Token
end
describe '#tokenize' do
it 'tokenizes single value' do
tokens = described_class.new('$VARIABLE').tokenize
expect(tokens).to be_one
expect(tokens).to all(be_an_instance_of(token_class))
end
it 'does ignore whitespace characters' do
tokens = described_class.new("\t$VARIABLE ").tokenize
expect(tokens).to be_one
expect(tokens).to all(be_an_instance_of(token_class))
end
it 'tokenizes multiple values of the same token' do
tokens = described_class.new("$VARIABLE1 $VARIABLE2").tokenize
expect(tokens.size).to eq 2
expect(tokens).to all(be_an_instance_of(token_class))
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