PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1
APPENDIX B ■ A SIMPLE PARSER

// word characters
private function eatWordChars( $char ) {
$val = $char;
while ( $this->isWordChar( $char=$this->getChar() )) {
$val .= $char;
}
if ( $char ) {
$this->pushBackChar( );
}
return $val;
}


// get all characters until they stop being space
// characters
private function eatSpaceChars( $char ) {
$val = $char;
while ( $this->isSpaceChar( $char=$this->getChar() )) {
$val .= $char;
}
$this->pushBackChar( );
return $val;
}


// move back one character in source
private function pushBackChar( ) {
$this->r->pushBackChar();
}


// argument is a word character
private function isWordChar( $char ) {
return pregmatch( "/[A-Za-z0-9-]/", $char );
}


// argument is a space character
private function isSpaceChar( $char ) {
return preg_match( "/\t| /", $char );
}


// argument is an end of line character
private function isEolChar( $char ) {
return preg_match( "/\n|\r/", $char );
}


// swallow either \n, \r or \r\n
private function manageEolChars( $char ) {
if ( $char == "\r" ) {
$next_char=$this->getChar();
if ( $next_char == "\n" ) {
return "{$char}{$next_char}";
} else {
$this->pushBackChar();
}
}

Free download pdf