Danh mục

PHP Objects, Patterns, and Practice- P11

Số trang: 38      Loại file: pdf      Dung lượng: 1.12 MB      Lượt xem: 10      Lượt tải: 0    
Jamona

Xem trước 4 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

PHP Objects, Patterns, and Practice- P11: This book takes you beyond the PHP basics to the enterprise development practices used by professional programmers. Updated for PHP 5.3 with new sections on closures, namespaces, and continuous integration, this edition will teach you about object features such as abstract classes, reflection, interfaces, and error handling. You’ll also discover object tools to help you learn more about your classes, objects, and methods.
Nội dung trích xuất từ tài liệu:
PHP Objects, Patterns, and Practice- P11 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 matchclass AlternationParse extends CollectionParse { function trigger( Scanner $scanner ) { foreach ( $this->parsers as $parser ) { if ( $parser->trigger( $scanner ) ) { return true; } } return false; } 479 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; }480 APPENDIX B ■ A SIMPLE PARSER function trigger( Scanner $scanner ) { if ( $scanner->tokenType() != Scanner::WORD ) { return false; } if ( is_null( $this->word ) ) { return true; } return ( $this->word == $scanner->token() ); } protected function doScan( Scanner $scanner ) { $ret = ( $this->trigger( $scanner ) ); return $ret; }} By combining terminal and nonterminal Parser objects, I can build a reasonably sophisticatedparser. You can see all the Parser classes I use for this example in Figure B–1.Figure B–1. The Parser classes 481 APPENDIX B ■ A SIMPLE PARSER The idea behind this use of the Composite pattern is that a client can build up a grammar in code that closely matches EBNF notation. Table B–1 shows the parallels between these classes and EBNF fragments. Table B–1. Composite Parsers and EBNF Class EBNF Example Description AlternationParse orExpr | andExpr Either one or another ...

Tài liệu được xem nhiều: