BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
gitlab-ce
Commits
867a4f68
Commit
867a4f68
authored
Feb 22, 2018
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extract pipeline expressions parser to a separate class
parent
b92ce0cc
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
62 additions
and
45 deletions
+62
-45
lexer.rb
lib/gitlab/ci/pipeline/expression/lexer.rb
+7
-1
parser.rb
lib/gitlab/ci/pipeline/expression/parser.rb
+25
-0
statement.rb
lib/gitlab/ci/pipeline/expression/statement.rb
+3
-20
lexer_spec.rb
spec/lib/gitlab/ci/pipeline/expression/lexer_spec.rb
+21
-12
parser_spec.rb
spec/lib/gitlab/ci/pipeline/expression/parser_spec.rb
+6
-0
statement_spec.rb
spec/lib/gitlab/ci/pipeline/expression/statement_spec.rb
+0
-12
No files found.
lib/gitlab/ci/pipeline/expression/lexer.rb
View file @
867a4f68
...
...
@@ -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
...
...
lib/gitlab/ci/pipeline/expression/parser.rb
0 → 100644
View file @
867a4f68
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
lib/gitlab/ci/pipeline/expression/statement.rb
View file @
867a4f68
...
...
@@ -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
...
...
spec/lib/gitlab/ci/pipeline/expression/lexer_spec.rb
View file @
867a4f68
...
...
@@ -5,30 +5,30 @@ describe Gitlab::Ci::Pipeline::Expression::Lexer do
Gitlab
::
Ci
::
Pipeline
::
Expression
::
Token
end
describe
'#token
ize
'
do
it
'token
ize
s single value'
do
tokens
=
described_class
.
new
(
'$VARIABLE'
).
token
ize
describe
'#token
s
'
do
it
'token
s
s single value'
do
tokens
=
described_class
.
new
(
'$VARIABLE'
).
token
s
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 "
).
token
ize
tokens
=
described_class
.
new
(
"
\t
$VARIABLE "
).
token
s
expect
(
tokens
).
to
be_one
expect
(
tokens
).
to
all
(
be_an_instance_of
(
token_class
))
end
it
'token
ize
s multiple values of the same token'
do
tokens
=
described_class
.
new
(
"$VARIABLE1 $VARIABLE2"
).
token
ize
it
'token
s
s multiple values of the same token'
do
tokens
=
described_class
.
new
(
"$VARIABLE1 $VARIABLE2"
).
token
s
expect
(
tokens
.
size
).
to
eq
2
expect
(
tokens
).
to
all
(
be_an_instance_of
(
token_class
))
end
it
'token
ize
s multiple values with different tokens'
do
tokens
=
described_class
.
new
(
'$VARIABLE "text" "value"'
).
token
ize
it
'token
s
s multiple values with different tokens'
do
tokens
=
described_class
.
new
(
'$VARIABLE "text" "value"'
).
token
s
expect
(
tokens
.
size
).
to
eq
3
expect
(
tokens
.
first
.
value
).
to
eq
'$VARIABLE'
...
...
@@ -36,8 +36,8 @@ describe Gitlab::Ci::Pipeline::Expression::Lexer do
expect
(
tokens
.
third
.
value
).
to
eq
'"value"'
end
it
'token
ize
s tokens and operators'
do
tokens
=
described_class
.
new
(
'$VARIABLE == "text"'
).
token
ize
it
'token
s
s tokens and operators'
do
tokens
=
described_class
.
new
(
'$VARIABLE == "text"'
).
token
s
expect
(
tokens
.
size
).
to
eq
3
expect
(
tokens
.
first
.
value
).
to
eq
'$VARIABLE'
...
...
@@ -48,15 +48,24 @@ describe Gitlab::Ci::Pipeline::Expression::Lexer do
it
'limits statement to 5 tokens'
do
lexer
=
described_class
.
new
(
"$V1 $V2 $V3 $V4 $V5 $V6"
)
expect
{
lexer
.
token
ize
}
expect
{
lexer
.
token
s
}
.
to
raise_error
described_class
::
SyntaxError
end
it
'raises syntax error in case of finding unknown tokens'
do
lexer
=
described_class
.
new
(
'$V1 123 $V2'
)
expect
{
lexer
.
token
ize
}
expect
{
lexer
.
token
s
}
.
to
raise_error
described_class
::
SyntaxError
end
end
describe
'#lexemes'
do
it
'returns an array of syntax lexemes'
do
lexer
=
described_class
.
new
(
'$VAR "text"'
)
expect
(
lexer
.
lexemes
).
to
eq
%w[variable string]
end
end
end
spec/lib/gitlab/ci/pipeline/expression/parser_spec.rb
0 → 100644
View file @
867a4f68
require
'spec_helper'
describe
Gitlab
::
Ci
::
Pipeline
::
Expression
::
Parser
do
describe
'#tree'
do
end
end
spec/lib/gitlab/ci/pipeline/expression/statement_spec.rb
View file @
867a4f68
...
...
@@ -12,18 +12,6 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do
pipeline
.
variables
.
build
([
key:
'VARIABLE'
,
value:
'my variable'
])
end
describe
'#tokens'
do
it
'returns raw tokens'
do
expect
(
subject
.
tokens
.
size
).
to
eq
2
end
end
describe
'#lexemes'
do
it
'returns an array of syntax lexemes'
do
expect
(
subject
.
lexemes
).
to
eq
%w[variable string]
end
end
describe
'#parse_tree'
do
context
'when expression is empty'
do
let
(
:text
)
{
''
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment