PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1

APPENDIX B ■ A SIMPLE PARSER


$this->token = $char;
$type = self::QUOTE;


} else {
$type = self::CHAR;
$this->token = $char;
}


$this->char_no += strlen( $this->token() );
return ( $this->token_type = $type );
}
return ( $this->token_type = self::EOF );
}


// return an array of token type and token content for the NEXT token
function peekToken() {
$state = $this->getState();
$type = $this->nextToken();
$token = $this->token();
$this->setState( $state );
return array( $type, $token );
}


// get a ScannerState object that stores the parser's current
// position in the source, and data about the current token
function getState() {
$state = new ScannerState();
$state->line_no = $this->line_no;
$state->char_no = $this->char_no;
$state->token = $this->token;
$state->token_type = $this->token_type;
$state->r = clone($this->r);
$state->context = clone($this->context);
return $state;
}


// use a ScannerState object to restore the scanner's
// state
function setState( ScannerState $state ) {
$this->line_no = $state->line_no;
$this->char_no = $state->char_no;
$this->token = $state->token;
$this->token_type = $state->token_type;
$this->r = $state->r;
$this->context = $state->context;
}


// get the next character from source
private function getChar() {
return $this->r->getChar();
}


// get all characters until they stop being

Free download pdf