Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
findPattern example
class
NumberHandler
implements
HandlerInterface
{
private
TokenizerPatterns
$patterns
;
public
function
__construct
(
TokenizerPatterns
$patterns
)
{
$this
->patterns =
$patterns
;
}
public
function
handle
(
Reader
$reader
, TokenStream
$stream
)
: bool
{
$match
=
$reader
->
findPattern
(
$this
->patterns->
getNumberPattern
(
)
)
;
if
(
!
$match
)
{
return
false;
}
$stream
->
push
(
new
Token
(
Token::TYPE_NUMBER,
$match
[
0
]
,
$reader
->
getPosition
(
)
)
)
;
$reader
->
moveForward
(
\
strlen
(
$match
[
0
]
)
)
;
return
true;
}
}
class
WhitespaceHandler
implements
HandlerInterface
{
public
function
handle
(
Reader
$reader
, TokenStream
$stream
)
: bool
{
$match
=
$reader
->
findPattern
(
'~^[ \t\r\n\f]+~'
)
;
if
(
false ===
$match
)
{
return
false;
}
$stream
->
push
(
new
Token
(
Token::TYPE_WHITESPACE,
$match
[
0
]
,
$reader
->
getPosition
(
)
)
)
;
$reader
->
moveForward
(
\
strlen
(
$match
[
0
]
)
)
;
return
true;
}
}
$this
->
assertFalse
(
$reader
->
getOffset
(
'w'
)
)
;
$this
->
assignPosition
(
$reader
, 2
)
;
$this
->
assertEquals
(
0,
$reader
->
getOffset
(
'll'
)
)
;
$this
->
assertFalse
(
$reader
->
getOffset
(
'he'
)
)
;
}
public
function
testFindPattern
(
)
{
$reader
=
new
Reader
(
'hello'
)
;
$this
->
assertFalse
(
$reader
->
findPattern
(
'/world/'
)
)
;
$this
->
assertEquals
(
[
'hello', 'h'
]
,
$reader
->
findPattern
(
'/^([a-z]).*/'
)
)
;
$this
->
assignPosition
(
$reader
, 2
)
;
$this
->
assertFalse
(
$reader
->
findPattern
(
'/^h.*/'
)
)
;
$this
->
assertEquals
(
[
'llo'
]
,
$reader
->
findPattern
(
'/^llo$/'
)
)
;
}
public
function
testMoveForward
(
)
{
$reader
=
new
Reader
(
'hello'
)
;
$this
->
assertEquals
(
0,
$reader
->
getPosition
(
)
)
;
private
TokenizerPatterns
$patterns
;
private
TokenizerEscaping
$escaping
;
public
function
__construct
(
TokenizerPatterns
$patterns
, TokenizerEscaping
$escaping
)
{
$this
->patterns =
$patterns
;
$this
->escaping =
$escaping
;
}
public
function
handle
(
Reader
$reader
, TokenStream
$stream
)
: bool
{
$match
=
$reader
->
findPattern
(
$this
->patterns->
getHashPattern
(
)
)
;
if
(
!
$match
)
{
return
false;
}
$value
=
$this
->escaping->
escapeUnicode
(
$match
[
1
]
)
;
$stream
->
push
(
new
Token
(
Token::TYPE_HASH,
$value
,
$reader
->
getPosition
(
)
)
)
;
$reader
->
moveForward
(
\
strlen
(
$match
[
0
]
)
)
;
return
true;
}
}
public
function
handle
(
Reader
$reader
, TokenStream
$stream
)
: bool
{
$quote
=
$reader
->
getSubstring
(
1
)
;
if
(
!\
in_array
(
$quote
,
[
"'", '"'
]
)
)
{
return
false;
}
$reader
->
moveForward
(
1
)
;
$match
=
$reader
->
findPattern
(
$this
->patterns->
getQuotedStringPattern
(
$quote
)
)
;
if
(
!
$match
)
{
throw
new
InternalErrorException
(
sprintf
(
'Should have found at least an empty match at %d.',
$reader
->
getPosition
(
)
)
)
;
}
// check unclosed strings
if
(
\
strlen
(
$match
[
0
]
)
===
$reader
->
getRemainingLength
(
)
)
{
throw
SyntaxErrorException::
unclosedString
(
$reader
->
getPosition
(
)
- 1
)
;
}
// check quotes pairs validity
private
TokenizerPatterns
$patterns
;
private
TokenizerEscaping
$escaping
;
public
function
__construct
(
TokenizerPatterns
$patterns
, TokenizerEscaping
$escaping
)
{
$this
->patterns =
$patterns
;
$this
->escaping =
$escaping
;
}
public
function
handle
(
Reader
$reader
, TokenStream
$stream
)
: bool
{
$match
=
$reader
->
findPattern
(
$this
->patterns->
getIdentifierPattern
(
)
)
;
if
(
!
$match
)
{
return
false;
}
$value
=
$this
->escaping->
escapeUnicode
(
$match
[
0
]
)
;
$stream
->
push
(
new
Token
(
Token::TYPE_IDENTIFIER,
$value
,
$reader
->
getPosition
(
)
)
)
;
$reader
->
moveForward
(
\
strlen
(
$match
[
0
]
)
)
;
return
true;
}
}