PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1
APPENDIX B ■ A SIMPLE PARSER

}


function trigger( Scanner $scanner ) {
return true;
}


protected function doScan( Scanner $scanner ) {
$start_state = $scanner->getState();
if ( empty( $this->parsers ) ) {
return true;
}
$parser = $this->parsers[0];
$count = 0;


while ( true ) {
if ( $this->max > 0 && $count >= $this->max ) {
return true;
}


if (! $parser->trigger( $scanner ) ) {
if ( $this->min == 0 || $count >= $this->min ) {
return true;
} else {
$scanner->setState( $start_state );
return false;
}
}
if (! $parser->scan( $scanner ) ) {
if ( $this->min == 0 || $count >= $this->min ) {
return true;
} else {
$scanner->setState( $start_state );
return false;
}
}
$count++;
}
return true;
}
}


// This matches if one or other of two subparsers match
class AlternationParse extends CollectionParse {


function trigger( Scanner $scanner ) {
foreach ( $this->parsers as $parser ) {
if ( $parser->trigger( $scanner ) ) {
return true;
}
}
return false;
}

Free download pdf