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
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 ...
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ìm kiếm theo từ khóa liên quan:
thủ thuật lập trình lập trình php lập trình web giáo trình cơ bản lập trình html ngôn ngữ javaGợi ý tài liệu liên quan:
-
Thủ thuật giúp giải phóng dung lượng ổ cứng
4 trang 210 0 0 -
NGÂN HÀNG CÂU HỎI TRẮC NGHIỆM THIẾT KẾ WEB
8 trang 202 0 0 -
Bài toán phân luồng giao thông và ứng dụng
11 trang 179 1 0 -
Hướng dẫn lập trình với Android part 4
5 trang 154 0 0 -
[Thảo luận] Học PHP như thế nào khi bạn chưa biết gì về lập trình?
5 trang 131 0 0 -
161 trang 129 1 0
-
142 trang 129 0 0
-
Bài giảng Lập trình web nâng cao: Chương 8 - Trường ĐH Văn Hiến
36 trang 109 1 0 -
MỘT SỐ ĐIỂM CẦN CHÚ Ý KHI THIẾT KẾ WEB
5 trang 108 0 0 -
150 trang 103 0 0