SVN
| Line | |
|---|
| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * @package PEG |
|---|
| 4 | * @author anatoo<anatoo@nequal.jp> |
|---|
| 5 | * @license http://www.opensource.org/licenses/mit-license.php MIT License |
|---|
| 6 | * @version $Id: Choice.php 655 2009-04-12 07:26:42Z anatoo $ |
|---|
| 7 | */ |
|---|
| 8 | |
|---|
| 9 | /** |
|---|
| 10 | * 選択。正規表現でいう"|"。 |
|---|
| 11 | * |
|---|
| 12 | */ |
|---|
| 13 | class PEG_Choice implements PEG_IParser |
|---|
| 14 | { |
|---|
| 15 | protected $parsers = array(); |
|---|
| 16 | |
|---|
| 17 | function __construct(Array $parsers = array()) |
|---|
| 18 | { |
|---|
| 19 | foreach ($parsers as $parser) $this->with($parser); |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | protected function with(PEG_IParser $p) |
|---|
| 23 | { |
|---|
| 24 | $this->parsers[] = $p; |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | function parse(PEG_IContext $c) |
|---|
| 28 | { |
|---|
| 29 | $offset = $c->tell(); |
|---|
| 30 | foreach ($this->parsers as $p) { |
|---|
| 31 | $result = $p->parse($c); |
|---|
| 32 | |
|---|
| 33 | if ($result instanceof PEG_Failure) $c->seek($offset); |
|---|
| 34 | else return $result; |
|---|
| 35 | } |
|---|
| 36 | return PEG::failure(); |
|---|
| 37 | } |
|---|
| 38 | } |
|---|
Note: See
TracBrowser
for help on using the repository browser.