Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
26167c24
Commit
26167c24
authored
Mar 01, 2018
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve pipeline expressions lexer
parent
886988c9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
17 deletions
+29
-17
lib/gitlab/ci/pipeline/expression/lexer.rb
lib/gitlab/ci/pipeline/expression/lexer.rb
+27
-15
spec/lib/gitlab/ci/pipeline/expression/lexer_spec.rb
spec/lib/gitlab/ci/pipeline/expression/lexer_spec.rb
+2
-2
No files found.
lib/gitlab/ci/pipeline/expression/lexer.rb
View file @
26167c24
...
@@ -3,6 +3,8 @@ module Gitlab
...
@@ -3,6 +3,8 @@ module Gitlab
module
Pipeline
module
Pipeline
module
Expression
module
Expression
class
Lexer
class
Lexer
include
::
Gitlab
::
Utils
::
StrongMemoize
LEXEMES
=
[
LEXEMES
=
[
Expression
::
Lexeme
::
Variable
,
Expression
::
Lexeme
::
Variable
,
Expression
::
Lexeme
::
String
,
Expression
::
Lexeme
::
String
,
...
@@ -10,34 +12,44 @@ module Gitlab
...
@@ -10,34 +12,44 @@ module Gitlab
Expression
::
Lexeme
::
Equals
Expression
::
Lexeme
::
Equals
].
freeze
].
freeze
MAX_CYCLES
=
5
SyntaxError
=
Class
.
new
(
Statement
::
StatementError
)
SyntaxError
=
Class
.
new
(
Statement
::
StatementError
)
MAX_TOKENS
=
100
def
initialize
(
statement
)
def
initialize
(
statement
)
@scanner
=
StringScanner
.
new
(
statement
)
@scanner
=
StringScanner
.
new
(
statement
)
@tokens
=
[]
end
end
def
tokens
def
tokens
(
max:
MAX_TOKENS
)
return
@tokens
if
@tokens
.
any?
strong_memoize
(
:tokens
)
{
tokenize
(
max
)
}
end
def
lexemes
tokens
.
map
(
&
:to_lexeme
)
end
private
MAX_CYCLES
.
times
do
def
tokenize
(
max_tokens
)
LEXEMES
.
each
do
|
lexeme
|
tokens
=
[]
@scanner
.
skip
(
/\s+/
)
# ignore whitespace
lexeme
.
scan
(
@scanner
).
tap
do
|
token
|
max_tokens
.
times
do
@tokens
.
push
(
token
)
if
token
.
present?
@scanner
.
skip
(
/\s+/
)
# ignore whitespace
return
tokens
if
@scanner
.
eos?
lexeme
=
LEXEMES
.
find
do
|
type
|
type
.
scan
(
@scanner
).
tap
do
|
token
|
tokens
.
push
(
token
)
if
token
.
present?
end
end
end
return
@tokens
if
@scanner
.
eos?
unless
lexeme
.
present?
raise
Lexer
::
SyntaxError
,
'Unknown lexeme found!'
end
end
end
end
raise
Lexer
::
SyntaxError
unless
@scanner
.
eos?
raise
Lexer
::
SyntaxError
,
'Too many tokens!'
end
def
lexemes
tokens
.
map
(
&
:to_lexeme
)
end
end
end
end
end
end
...
...
spec/lib/gitlab/ci/pipeline/expression/lexer_spec.rb
View file @
26167c24
...
@@ -45,10 +45,10 @@ describe Gitlab::Ci::Pipeline::Expression::Lexer do
...
@@ -45,10 +45,10 @@ describe Gitlab::Ci::Pipeline::Expression::Lexer do
expect
(
tokens
.
third
.
value
).
to
eq
'"text"'
expect
(
tokens
.
third
.
value
).
to
eq
'"text"'
end
end
it
'limits statement to
5
tokens'
do
it
'limits statement to
specified amount of
tokens'
do
lexer
=
described_class
.
new
(
"$V1 $V2 $V3 $V4 $V5 $V6"
)
lexer
=
described_class
.
new
(
"$V1 $V2 $V3 $V4 $V5 $V6"
)
expect
{
lexer
.
tokens
}
expect
{
lexer
.
tokens
(
max:
5
)
}
.
to
raise_error
described_class
::
SyntaxError
.
to
raise_error
described_class
::
SyntaxError
end
end
...
...
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