1 | <?php |
||
2 | |||
3 | namespace petitparser; |
||
4 | |||
5 | /** |
||
6 | * A parser that parses a sequence of parsers. |
||
7 | */ |
||
8 | class SequenceParser extends ListParser |
||
9 | { |
||
10 | /** |
||
11 | * @param Context $context |
||
12 | * |
||
13 | * @return Result |
||
14 | */ |
||
15 | 1 | public function parseOn(Context $context) |
|
16 | { |
||
17 | 1 | $current = $context; |
|
18 | 1 | $elements = array(); |
|
19 | |||
20 | 1 | for ($i=0; $i < count($this->_parsers); $i++) { |
|
0 ignored issues
–
show
|
|||
21 | 1 | $result = $this->_parsers[$i]->parseOn($current); |
|
22 | |||
23 | 1 | if ($result->isFailure()) { |
|
24 | 1 | return $result; |
|
25 | } |
||
26 | |||
27 | 1 | $elements[$i] = $result->getValue(); |
|
28 | 1 | $current = $result; |
|
29 | 1 | } |
|
30 | |||
31 | 1 | return $current->success($elements); |
|
32 | } |
||
33 | |||
34 | /** |
||
35 | * @param Parser $other |
||
36 | * |
||
37 | * @return Parser |
||
38 | */ |
||
39 | 1 | public function seq(Parser $other) |
|
40 | { |
||
41 | 1 | $parsers = $this->_parsers; |
|
42 | 1 | $parsers[] = $other; |
|
43 | |||
44 | 1 | return new SequenceParser($parsers); |
|
45 | } |
||
46 | |||
47 | /** |
||
48 | * @return Parser |
||
49 | */ |
||
50 | 1 | public function copy() |
|
51 | { |
||
52 | 1 | return new SequenceParser($this->_parsers); |
|
53 | } |
||
54 | } |
||
55 |
If you have a function call in the test part of a
for
loop, this function is executed on each iteration. Often such a function, can be moved to the initialization part and be cached.