summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/pipeline
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2018-02-22 13:54:22 +0100
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2018-02-22 13:54:22 +0100
commit867a4f68cbbe58652e8389e05edfd36df81cb72b (patch)
treeac3451c33a5f812f3405bc2fdabbe8eca82090f8 /lib/gitlab/ci/pipeline
parentb92ce0ccb68d1dd39a1dd06f4a57979db7299526 (diff)
downloadgitlab-ce-867a4f68cbbe58652e8389e05edfd36df81cb72b.tar.gz
Extract pipeline expressions parser to a separate class
Diffstat (limited to 'lib/gitlab/ci/pipeline')
-rw-r--r--lib/gitlab/ci/pipeline/expression/lexer.rb8
-rw-r--r--lib/gitlab/ci/pipeline/expression/parser.rb25
-rw-r--r--lib/gitlab/ci/pipeline/expression/statement.rb23
3 files changed, 35 insertions, 21 deletions
diff --git a/lib/gitlab/ci/pipeline/expression/lexer.rb b/lib/gitlab/ci/pipeline/expression/lexer.rb
index 1536374f0da..9334358cd8e 100644
--- a/lib/gitlab/ci/pipeline/expression/lexer.rb
+++ b/lib/gitlab/ci/pipeline/expression/lexer.rb
@@ -17,7 +17,9 @@ module Gitlab
@tokens = []
end
- def tokenize
+ def tokens
+ return @tokens if @tokens.any?
+
MAX_CYCLES.times do
LEXEMES.each do |lexeme|
@scanner.skip(/\s+/) # ignore whitespace
@@ -32,6 +34,10 @@ module Gitlab
raise Lexer::SyntaxError unless @scanner.eos?
end
+
+ def lexemes
+ tokens.map(&:to_lexeme)
+ end
end
end
end
diff --git a/lib/gitlab/ci/pipeline/expression/parser.rb b/lib/gitlab/ci/pipeline/expression/parser.rb
new file mode 100644
index 00000000000..f3201ec0979
--- /dev/null
+++ b/lib/gitlab/ci/pipeline/expression/parser.rb
@@ -0,0 +1,25 @@
+module Gitlab
+ module Ci
+ module Pipeline
+ module Expression
+ class Parser
+ def initialize(syntax)
+ if syntax.is_a?(Expression::Lexer)
+ @tokens = syntax.tokens
+ else
+ @tokens = syntax.to_a
+ end
+ end
+
+ def tree
+ if @tokens.many?
+ Expression::Equals.new(@tokens.first.build, @tokens.last.build)
+ else
+ @tokens.first.build
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/ci/pipeline/expression/statement.rb b/lib/gitlab/ci/pipeline/expression/statement.rb
index fabfcd0393d..e1e37f0f5cb 100644
--- a/lib/gitlab/ci/pipeline/expression/statement.rb
+++ b/lib/gitlab/ci/pipeline/expression/statement.rb
@@ -23,31 +23,14 @@ module Gitlab
end
end
- def tokens
- @tokens ||= @lexer.tokenize
- end
-
- def lexemes
- @lexemes ||= tokens.map(&:to_lexeme)
- end
-
- ##
- # Our syntax is very simple, so we don't yet need to implement a
- # recursive parser, we can use the most simple approach to create
- # a reverse descent parse tree "by hand".
- #
def parse_tree
- raise StatementError if lexemes.empty?
+ raise StatementError if @lexer.lexemes.empty?
- unless GRAMMAR.find { |syntax| syntax == lexemes }
+ unless GRAMMAR.find { |syntax| syntax == @lexer.lexemes }
raise StatementError, 'Unknown pipeline expression!'
end
- if tokens.many?
- Expression::Equals.new(tokens.first.build, tokens.last.build)
- else
- tokens.first.build
- end
+ Expression::Parser.new(@lexer).tree
end
def evaluate