Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
moveForward example
$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
(
)
)
;
$reader
->
moveForward
(
2
)
;
$this
->
assertEquals
(
2,
$reader
->
getPosition
(
)
)
;
}
public
function
testToEnd
(
)
{
$reader
=
new
Reader
(
'hello'
)
;
$reader
->
moveToEnd
(
)
;
$this
->
assertTrue
(
$reader
->
isEOF
(
)
)
;
}
private
function
assignPosition
(
Reader
$reader
, int
$value
)
{
$stream
=
new
TokenStream
(
)
;
while
(
!
$reader
->
isEOF
(
)
)
{
foreach
(
$this
->handlers
as
$handler
)
{
if
(
$handler
->
handle
(
$reader
,
$stream
)
)
{
continue
2;
}
}
$stream
->
push
(
new
Token
(
Token::TYPE_DELIMITER,
$reader
->
getSubstring
(
1
)
,
$reader
->
getPosition
(
)
)
)
;
$reader
->
moveForward
(
1
)
;
}
return
$stream
->
push
(
new
Token
(
Token::TYPE_FILE_END, null,
$reader
->
getPosition
(
)
)
)
->
freeze
(
)
;
}
}
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;
}
}
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;
}
}
public
function
handle
(
Reader
$reader
, TokenStream
$stream
)
: bool
{
if
(
'/*' !==
$reader
->
getSubstring
(
2
)
)
{
return
false;
}
$offset
=
$reader
->
getOffset
(
'*/'
)
;
if
(
false ===
$offset
)
{
$reader
->
moveToEnd
(
)
;
}
else
{
$reader
->
moveForward
(
$offset
+ 2
)
;
}
return
true;
}
}
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;
}
}
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;
}
}
$this
->escaping =
$escaping
;
}
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
)
;
}