SVN
| Revision 1490,
1.7 KB
checked in by anatoo, 10 months ago
(diff) |
|
0.2ブランチのリビジョン1489,1488の変更をtrunkに取り込んだ
|
| 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: ArrayContext.php 822 2009-05-12 16:27:55Z anatoo $ |
|---|
| 7 | */ |
|---|
| 8 | |
|---|
| 9 | /** |
|---|
| 10 | * PEG_IContextの実装クラス |
|---|
| 11 | * 配列をパースするためのもの |
|---|
| 12 | */ |
|---|
| 13 | class PEG_ArrayContext implements PEG_IContext |
|---|
| 14 | { |
|---|
| 15 | protected $arr, $i = 0, $len, $cache; |
|---|
| 16 | |
|---|
| 17 | /** |
|---|
| 18 | * |
|---|
| 19 | * @param Array $arr 配列 |
|---|
| 20 | */ |
|---|
| 21 | function __construct(Array $arr) { |
|---|
| 22 | $this->arr = array_values($arr); |
|---|
| 23 | $this->len = count($arr); |
|---|
| 24 | $this->cache = new PEG_Cache; |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | /** |
|---|
| 28 | * @param int $i |
|---|
| 29 | * @return Array |
|---|
| 30 | */ |
|---|
| 31 | function read($i) |
|---|
| 32 | { |
|---|
| 33 | if ($this->eos() && $i > 0) return false; |
|---|
| 34 | $this->i += $i; |
|---|
| 35 | return array_slice($this->arr, $this->i - $i, $i); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | function readElement() |
|---|
| 39 | { |
|---|
| 40 | list($elt) = $this->read(1); |
|---|
| 41 | return $elt; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | /** |
|---|
| 45 | * @param int $i |
|---|
| 46 | * @return bool |
|---|
| 47 | */ |
|---|
| 48 | function seek($i) |
|---|
| 49 | { |
|---|
| 50 | if ($this->len < $i) return false; |
|---|
| 51 | $this->i = $i; |
|---|
| 52 | return true; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | /** |
|---|
| 56 | * @return int |
|---|
| 57 | */ |
|---|
| 58 | function tell() |
|---|
| 59 | { |
|---|
| 60 | return $this->i; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * @return bool |
|---|
| 65 | */ |
|---|
| 66 | function eos() |
|---|
| 67 | { |
|---|
| 68 | return $this->len <= $this->i; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | /** |
|---|
| 72 | * @return array |
|---|
| 73 | */ |
|---|
| 74 | function get() |
|---|
| 75 | { |
|---|
| 76 | return $this->arr; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | function save(PEG_IParser $parser, $start, $end, $val) |
|---|
| 81 | { |
|---|
| 82 | $this->cache->save($parser, $start, $end, $val); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | function cache(PEG_IParser $parser) |
|---|
| 86 | { |
|---|
| 87 | return $this->cache->cache($parser, $this->tell()); |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
Note: See
TracBrowser
for help on using the repository browser.