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
|
2004-04-10 07:53:52 +00:00
|
|
|
$mQueuedToken; # Tokens that were already found, but not
|
|
|
|
|
# returned yet.
|
2004-02-28 23:38:08 +00:00
|
|
|
|
|
|
|
|
/* private */ function Tokenizer()
|
|
|
|
|
{
|
|
|
|
|
$this->mPos=0;
|
2004-04-10 07:53:52 +00:00
|
|
|
$this->mTokenQueue=array();
|
2004-02-28 23:38:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# factory function
|
|
|
|
|
function newFromString( $s )
|
|
|
|
|
{
|
2004-04-20 08:59:51 +00:00
|
|
|
$fname = "Tokenizer::newFromString";
|
|
|
|
|
wfProfileIn( $fname );
|
|
|
|
|
|
2004-02-28 23:38:08 +00:00
|
|
|
$t = new Tokenizer();
|
|
|
|
|
$t->mText = $s;
|
|
|
|
|
$t->mTextLength = strlen( $s );
|
2004-04-20 08:59:51 +00:00
|
|
|
|
|
|
|
|
wfProfileOut( $fname );
|
2004-02-28 23:38:08 +00:00
|
|
|
return $t;
|
|
|
|
|
}
|
|
|
|
|
|
2004-02-29 13:33:51 +00:00
|
|
|
|
2004-04-10 07:53:52 +00:00
|
|
|
// Return the next token, but do not increase the pointer. The next call
|
|
|
|
|
// to previewToken or nextToken will return the same token again.
|
|
|
|
|
// Actually, the pointer is increased, but the token is queued. The next
|
|
|
|
|
// call to previewToken or nextToken will check the queue and return
|
|
|
|
|
// the stored token.
|
|
|
|
|
function previewToken()
|
2004-02-28 23:38:08 +00:00
|
|
|
{
|
2004-04-20 08:59:51 +00:00
|
|
|
$fname = "Tokenizer::previewToken";
|
|
|
|
|
wfProfileIn( $fname );
|
|
|
|
|
|
2004-04-10 07:53:52 +00:00
|
|
|
if ( count( $this->mQueuedToken ) != 0 ) {
|
|
|
|
|
// still one token from the last round around. Return that one first.
|
|
|
|
|
$token = $this->mQueuedToken[0];
|
|
|
|
|
} else {
|
|
|
|
|
$token = $this->nextToken();
|
|
|
|
|
array_unshift( $this->mQueuedToken, $token );
|
2004-02-28 23:38:08 +00:00
|
|
|
}
|
2004-04-20 08:59:51 +00:00
|
|
|
|
|
|
|
|
wfProfileOut( $fname );
|
2004-02-28 23:38:08 +00:00
|
|
|
return $token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-04-10 07:53:52 +00:00
|
|
|
// get the next token
|
|
|
|
|
// proceeds character by character through the text, looking for characters needing
|
|
|
|
|
// special attention. Those are currently: I, R, ', [, ], newline
|
|
|
|
|
//
|
|
|
|
|
// TODO: prefixed links for Arabic wikipedia not implemented yet
|
|
|
|
|
// handling of French blanks not yet implemented
|
|
|
|
|
function nextToken()
|
2004-02-28 23:38:08 +00:00
|
|
|
{
|
2004-04-20 08:59:51 +00:00
|
|
|
$fname = "Tokenizer::nextToken";
|
|
|
|
|
wfProfileIn( $fname );
|
|
|
|
|
|
2004-04-10 07:53:52 +00:00
|
|
|
if ( count( $this->mQueuedToken ) != 0 ) {
|
|
|
|
|
// still one token from the last round around. Return that one first.
|
|
|
|
|
$token = array_shift( $this->mQueuedToken );
|
2004-04-20 08:59:51 +00:00
|
|
|
} else if ( $this->mPos > $this->mTextLength )
|
|
|
|
|
{ // If no text is left, return "false".
|
|
|
|
|
$token = false;
|
2004-02-28 23:38:08 +00:00
|
|
|
} else {
|
2004-04-10 07:53:52 +00:00
|
|
|
|
|
|
|
|
$token["text"]="";
|
|
|
|
|
$token["type"]="text";
|
|
|
|
|
|
|
|
|
|
while ( $this->mPos <= $this->mTextLength ) {
|
2004-04-10 11:25:24 +00:00
|
|
|
switch ( @$ch = $this->mText[$this->mPos] ) {
|
2004-04-10 07:53:52 +00:00
|
|
|
case 'R': // for "RFC "
|
2004-04-20 08:59:51 +00:00
|
|
|
if ( $this->continues("FC ") ) {
|
2004-04-10 07:53:52 +00:00
|
|
|
$queueToken["type"] = $queueToken["text"] = "RFC ";
|
|
|
|
|
$this->mQueuedToken[] = $queueToken;
|
|
|
|
|
$this->mPos += 3;
|
|
|
|
|
break 2; // switch + while
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'I': // for "ISBN "
|
2004-04-20 08:59:51 +00:00
|
|
|
if ( $this->continues("SBN ") ) {
|
2004-04-10 07:53:52 +00:00
|
|
|
$queueToken["type"] = $queueToken["text"] = "ISBN ";
|
|
|
|
|
$this->mQueuedToken[] = $queueToken;
|
|
|
|
|
$this->mPos += 4;
|
|
|
|
|
break 2; // switch + while
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "[": // for links "[["
|
2004-04-20 08:59:51 +00:00
|
|
|
if ( $this->continues("[[") ) {
|
2004-04-10 07:53:52 +00:00
|
|
|
$queueToken["type"] = "[[[";
|
|
|
|
|
$queueToken["text"] = "";
|
|
|
|
|
$this->mQueuedToken[] = $queueToken;
|
|
|
|
|
$this->mPos += 3;
|
|
|
|
|
break 2; // switch + while
|
2004-04-20 08:59:51 +00:00
|
|
|
} else if ( $this->continues("[") ) {
|
2004-04-10 07:53:52 +00:00
|
|
|
$queueToken["type"] = "[[";
|
|
|
|
|
$queueToken["text"] = "";
|
|
|
|
|
$this->mQueuedToken[] = $queueToken;
|
|
|
|
|
$this->mPos += 2;
|
|
|
|
|
break 2; // switch + while
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "]": // for end of links "]]"
|
2004-04-20 08:59:51 +00:00
|
|
|
if ( $this->continues("]") ) {
|
2004-04-10 07:53:52 +00:00
|
|
|
$queueToken["type"] = "]]";
|
|
|
|
|
$queueToken["text"] = "";
|
|
|
|
|
$this->mQueuedToken[] = $queueToken;
|
|
|
|
|
$this->mPos += 2;
|
|
|
|
|
break 2; // switch + while
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "'": // for all kind of em's and strong's
|
2004-04-20 08:59:51 +00:00
|
|
|
if ( $this->continues("'") ) {
|
2004-04-10 07:53:52 +00:00
|
|
|
$queueToken["type"] = "'";
|
|
|
|
|
$queueToken["text"] = "";
|
2004-04-20 08:59:51 +00:00
|
|
|
while( ($this->mPos+1 < $this->mTextLength)
|
|
|
|
|
&& $this->mText[$this->mPos+1] == "'" )
|
|
|
|
|
{
|
2004-04-10 07:53:52 +00:00
|
|
|
$queueToken["type"] .= "'";
|
|
|
|
|
$this->mPos ++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->mQueuedToken[] = $queueToken;
|
|
|
|
|
$this->mPos ++;
|
|
|
|
|
break 2; // switch + while
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "\n": // for block levels, actually, only "----" is handled.
|
|
|
|
|
case "\r":
|
2004-04-20 08:59:51 +00:00
|
|
|
if ( $this->continues( "----" ) )
|
|
|
|
|
{
|
2004-04-10 07:53:52 +00:00
|
|
|
$queueToken["type"] = "----";
|
|
|
|
|
$queueToken["text"] = "";
|
|
|
|
|
$this->mQueuedToken[] = $queueToken;
|
|
|
|
|
$this->mPos += 5;
|
2004-04-20 08:59:51 +00:00
|
|
|
while ( $this->mPos<$this->mTextLength
|
|
|
|
|
and $this->mText[$this->mPos] == "-" )
|
|
|
|
|
{
|
2004-04-10 07:53:52 +00:00
|
|
|
$this->mPos ++;
|
|
|
|
|
}
|
|
|
|
|
break 2;
|
|
|
|
|
}
|
|
|
|
|
} /* switch */
|
|
|
|
|
$token["text"].=$ch;
|
|
|
|
|
$this->mPos ++;
|
|
|
|
|
// echo $this->mPos . "<br>\n";
|
|
|
|
|
} /* while */
|
|
|
|
|
} /* if (nothing left in queue) */
|
2004-04-20 08:59:51 +00:00
|
|
|
|
|
|
|
|
wfProfileOut( $fname );
|
2004-02-28 23:38:08 +00:00
|
|
|
return $token;
|
|
|
|
|
}
|
|
|
|
|
|
2004-04-20 08:59:51 +00:00
|
|
|
// function continues
|
|
|
|
|
// checks whether the mText continues with $cont from mPos+1
|
|
|
|
|
function continues( $cont )
|
|
|
|
|
{
|
|
|
|
|
// If string is not long enough to contain $cont, return false
|
|
|
|
|
if ( $this->mTextLength < $this->mPos + strlen( $cont ) )
|
|
|
|
|
return false;
|
|
|
|
|
for ( $i=0; $i < strlen( $cont ); $i++ )
|
|
|
|
|
{
|
|
|
|
|
if ( $this->mText[$this->mPos+1+$i] != $cont[$i] )
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2004-02-28 23:38:08 +00:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|