APPENDIX B ■ A SIMPLE PARSER
protected function doScan( Scanner $scanner ) {
$type = $scanner->tokenType();
foreach ( $this->parsers as $parser ) {
$start_state = $scanner->getState();
if ( $type == $parser->trigger( $scanner ) &&
$parser->scan( $scanner ) ) {
return true;
}
}
$scanner->setState( $start_state );
return false;
}
}
// this terminal parser matches a string literal
class StringLiteralParse extends Parser {
function trigger( Scanner $scanner ) {
return ( $scanner->tokenType() == Scanner::APOS ||
$scanner->tokenType() == Scanner::QUOTE );
}
protected function push( Scanner $scanner ) {
return;
}
protected function doScan( Scanner $scanner ) {
$quotechar = $scanner->tokenType();
$ret = false;
$string = "";
while ( $token = $scanner->nextToken() ) {
if ( $token == $quotechar ) {
$ret = true;
break;
}
$string .= $scanner->token();
}
if ( $string &&! $this->discard ) {
$scanner->getContext()->pushResult( $string );
}
return $ret;
}
}
// this terminal parser matches a word token
class WordParse extends Parser {
function construct( $word=null, $name=null, $options=null ) {
parent::construct( $name, $options );
$this->word = $word;
}