2004-02-28 23:38:08 +00:00
|
|
|
<?php
|
|
|
|
|
class Tokenizer {
|
2004-02-29 11:00:30 +00:00
|
|
|
/* private */ var $mText, # Text to be processed by the tokenizer
|
|
|
|
|
$mPos, # current position of tokenizer in text
|
|
|
|
|
$mTextLength, # Length of $mText
|
|
|
|
|
$mCount, # token count, computed in preParse
|
|
|
|
|
$mMatch, # matches of tokenizer regex, computed in preParse
|
|
|
|
|
$mMatchPos; # current token position of tokenizer. Each match can
|
|
|
|
|
# be up to two tokens: A matched token and the text after it.
|
2004-02-28 23:38:08 +00:00
|
|
|
|
|
|
|
|
/* private */ function Tokenizer()
|
|
|
|
|
{
|
|
|
|
|
$this->mPos=0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# factory function
|
|
|
|
|
function newFromString( $s )
|
|
|
|
|
{
|
|
|
|
|
$t = new Tokenizer();
|
|
|
|
|
$t->mText = $s;
|
|
|
|
|
$t->preParse();
|
|
|
|
|
$t->mTextLength = strlen( $s );
|
|
|
|
|
return $t;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function preParse()
|
|
|
|
|
{
|
|
|
|
|
$this->mCount = preg_match_all( "/(\[\[|\]\]|\'\'\'\'\'|\'\'\'|\'\')/",
|
2004-02-29 11:00:30 +00:00
|
|
|
$this->mText, $this->mMatch,
|
2004-02-28 23:38:08 +00:00
|
|
|
PREG_PATTERN_ORDER|PREG_OFFSET_CAPTURE);
|
2004-02-29 11:00:30 +00:00
|
|
|
$this->mMatchPos=0;
|
2004-02-28 23:38:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function nextToken()
|
|
|
|
|
{
|
|
|
|
|
$token = $this->previewToken();
|
|
|
|
|
if ( $token ) {
|
|
|
|
|
if ( $token["type"] == "text" ) {
|
|
|
|
|
$this->mPos = $token["mPos"];
|
|
|
|
|
} else {
|
2004-02-29 11:00:30 +00:00
|
|
|
$this->mMatchPos = $token["mMatchPos"];
|
2004-02-28 23:38:08 +00:00
|
|
|
$this->mPos = $token["mPos"];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function previewToken()
|
|
|
|
|
{
|
2004-02-29 11:00:30 +00:00
|
|
|
if ( $this->mMatchPos <= $this->mCount ) {
|
2004-02-28 23:38:08 +00:00
|
|
|
$token["pos"] = $this->mPos;
|
2004-02-29 11:00:30 +00:00
|
|
|
if ( $this->mPos < $this->mMatch[0][$this->mMatchPos][1] ) {
|
2004-02-28 23:38:08 +00:00
|
|
|
$token["type"] = "text";
|
|
|
|
|
$token["text"] = substr( $this->mText, $this->mPos,
|
2004-02-29 11:00:30 +00:00
|
|
|
$this->mMatch[0][$this->mMatchPos][1] - $this->mPos );
|
|
|
|
|
$token["mPos"] = $this->mMatch[0][$this->mMatchPos][1];
|
2004-02-28 23:38:08 +00:00
|
|
|
} else {
|
2004-02-29 11:00:30 +00:00
|
|
|
$token["type"] = $this->mMatch[0][$this->mMatchPos][0];
|
2004-02-28 23:38:08 +00:00
|
|
|
$token["mPos"] = $this->mPos + strlen($token["type"]);
|
2004-02-29 11:00:30 +00:00
|
|
|
$token["mMatchPos"] = $this->mMatchPos + 1;
|
2004-02-28 23:38:08 +00:00
|
|
|
}
|
|
|
|
|
} elseif ( $this->mPos < $this->mTextLength ) {
|
|
|
|
|
$token["type"] = "text";
|
|
|
|
|
$token["text"] = substr( $this->mText, $this->mPos );
|
|
|
|
|
$token["mPos"] = $this->mTextLength;
|
|
|
|
|
} else {
|
|
|
|
|
$token = FALSE;
|
|
|
|
|
}
|
|
|
|
|
return $token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|