PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1
APPENDIX B ■ A SIMPLE PARSER

// return true if the current token is a word
function isWord( ) {
return ( $this->token_type == self::WORD );
}


// return true if the current token is a quote character
function isQuote( ) {
return ( $this->token_type == self::APOS ||
$this->token_type == self::QUOTE );
}


// current line number in source
function line_no() {
return $this->line_no;
}


// current character number in source
function char_no() {
return $this->char_no;
}


// clone this object
function __clone() {
$this->r = clone($this->r);
}


// move on to the next token in the source. Set the current
// token and track the line and character numbers
function nextToken() {
$this->token = null;
$type;
while (! is_bool($char=$this->getChar()) ) {
if ( $this->isEolChar( $char ) ) {
$this->token = $this->manageEolChars( $char );
$this->line_no++;
$this->char_no = 0;
$type = self::EOL;
return ( $this->token_type = self::EOL );


} else if ( $this->isWordChar( $char ) ) {
$this->token = $this->eatWordChars( $char );
$type = self::WORD;


} else if ( $this->isSpaceChar( $char ) ) {
$this->token = $char;
$type = self::WHITESPACE;


} else if ( $char == "'" ) {
$this->token = $char;
$type = self::APOS;


} else if ( $char == '"' ) {

Free download pdf