public function mapLine($line) { if (in_array(substr($line, 0, 1), array('+'), true)) { return $this->li->parse(PEG::context($line)); } return PEG::failure(); }
function mapHeader($line) { if (!preg_match('/^>\\|([a-zA-Z0-9]*)\\|$/', $line, $matches)) { return PEG::failure(); } return $matches[1]; }
function parse(PEG_IContext $context) { if ($context->eos()) { return PEG::failure(); } $line = $context->readElement(); if (!preg_match('#^><p( +class="([^"]+)")?>#', $line, $matches)) { return PEG::failure(); } $line = substr($line, strlen($matches[0])); $attr = isset($matches[2]) && $matches[2] !== '' ? array('class' => $matches[2]) : array(); // ><p>~~</p><みたいに一行で終わってるとき if (substr($line, -5, 5) === '</p><') { $line = substr($line, 0, -5); $body = $this->lineParser->parse(PEG::context($line)); return array('p', $attr, $body); } $rest = $this->parser->parse($context); if ($rest instanceof PEG_Failure) { return $rest; } $line .= join(PHP_EOL, $rest[0]); $line .= substr($rest[1], 0, -5); $body = $this->lineParser->parse(PEG::context($line)); return array('p', $attr, $body); }
function parse(PEG_IContext $c) { if ($c->eos()) { return false; } return PEG::failure(); }
public function map($line) { if (strpos($line, '*') === 0) { list($level, $rest) = $this->toLevelAndRest((string) substr($line, 1)); $body = $this->child->parse(PEG::context($rest)); return array($level, $body); } return PEG::failure(); }
public function parse(PEG_IContext $context) { $result = $this->parser->parse($context); if ($result instanceof PEG_Failure) { return $result; } list($open, $body, $close) = $result; return $open !== $close ? PEG::failure() : array($open, $body); }
function token(array $args) { list($str, $casesensitive) = $args + array(1 => true); $matched = $this->read(strlen($str)); if ($casesensitive) { return $str === $matched ? $str : PEG::failure(); } else { return strtolower($str) === strtolower($matched) ? $matched : PEG::failure(); } }
function parse(PEG_IContext $c) { $offset = $c->tell(); foreach ($this->parsers as $p) { $result = $p->parse($c); if ($result instanceof PEG_Failure) { $c->seek($offset); } else { return $result; } } return PEG::failure(); }
function parse(PEG_IContext $context) { if ($context->eos()) { return PEG::failure(); } $char = $context->readElement(); if (isset($this->table[$char])) { $offset = $context->tell() - 1; $context->seek($offset); return $this->table[$char]->parse($context); } return $char; }
function parse(PEG_IContext $c) { $arr = $this->arr; if (!$arr) { return PEG::failure(); } for ($i = 0; $i < count($arr) - 1; $i++) { $offset = $c->tell(); if ($arr[$i]->parse($c) instanceof PEG_Failure) { return PEG::failure(); } $c->seek($offset); } return $arr[$i]->parse($c); }
function mapHeader($line) { if (substr($line, 0, 1) !== '>' || substr($line, -1, 1) !== '>') { return PEG::failure(); } if ($line === '>>') { return false; } $link_exp = substr($line, 1, strlen($line) - 2); if (!preg_match('#^(https?://[^>:]+)(:title(=(.+))?)?$#', $link_exp, $matches)) { return PEG::failure(); } $title = !isset($matches[2]) ? false : (isset($matches[4]) ? $matches[4] : ''); return new HatenaSyntax_Node('httplink', array('href' => $matches[1], 'title' => isset($matches[4]) ? $matches[4] : false)); }
function parse(PEG_IContext $context) { if ($context->eos()) { return PEG::failure(); } $offset = $context->tell(); $result = $this->parser->parse($context); if ($result instanceof PEG_Failure) { $i = $context->tell() - $offset; $context->seek($offset); $ret = $context->read($i); $context->seek($offset); return $ret; } return PEG::failure(); }
/** * @param PEG_IContext */ public function parse(PEG_IContext $context) { if ($context->eos()) { return PEG::failure(); } $line = $context->readElement(); $context->seek($context->tell() - 1); // 行ディスパッチ if (isset($this->lineTable[$line])) { return $this->lineTable[$line]->parse($context); } $char = substr($line, 0, 1); if (!isset($this->paragraphCheckTable[$char])) { return $this->paragraph->parse($context); } if (isset($this->firstCharTable[$char])) { return $this->firstCharTable[$char]->parse($context); } return $this->paragraph->parse($context); }
<?php include_once dirname(__FILE__) . '/t/t.php'; $t = new lime_test(null, new lime_output_color()); $token = PEG::token('hoge'); $context = PEG::context('hogehoge'); $t->is($token->parse($context), 'hoge'); $t->is($context->tell(), 4); $t->is($token->parse($context), 'hoge'); $t->is($token->parse($context), PEG::failure()); $t->is(PEG::token('hoge', false)->parse(PEG::context('Hoge')), 'Hoge'); $t->is(PEG::token('hoge', false)->parse(PEG::context('hoge')), 'hoge'); $t->is(PEG::token('hoge', false)->parse(PEG::context('fuga')), PEG::failure());
function parse(PEG_IContext $context) { return $context->eos() ? PEG::failure() : $context->readElement(); }
<?php include_once dirname(__FILE__) . '/t/t.php'; $t = new lime_test(); $parser = PEG::token('hoge'); $t->is(PEG::_match($parser, PEG::context('ahoge')), 'hoge'); $t->is(PEG::_match($parser, PEG::context('ahoge'), true), array('hoge', 1)); $t->is(PEG::_match($parser, PEG::context('a')), PEG::failure()); $t->is(PEG::_match($parser, PEG::context('a'), true), array(PEG::failure(), null));
function parse(PEG_IContext $context) { $char = $context->readElement(); return $char !== false && ($this->except xor isset($this->dict[$char])) ? $char : PEG::failure(); }
function parse(PEG_IContext $context) { $context->logError($this->error); return PEG::failure(); }
<?php include_once dirname(__FILE__) . '/../../t.php'; $t = new lime_test(); $p = new HatenaSyntax_List(PEG::anything()); $c = PEG::context(array('+a', '+c')); $t->ok($p->parse($c) !== PEG::failure());
<?php include_once dirname(__FILE__) . '/t/t.php'; $lime = new lime_test(); $parser = PEG::not(PEG::token('hoge')); $lime->is($parser->parse($c = PEG::context('fuga')), 'fuga'); $lime->is($c->tell(), 0); $lime->is($parser->parse(PEG::context('hoge')), PEG::failure()); $lime->is($parser->parse(PEG::context('')), PEG::failure());
<?php include_once dirname(__FILE__) . '/t/t.php'; $lime = new lime_test(); $many1 = PEG::many1(PEG::token('hoge')); $lime->is($many1->parse(PEG::context('hoge')), array('hoge')); $lime->is($many1->parse(PEG::context('')), PEG::failure()); $lime->is(array('hoge', 'hoge'), $many1->parse(PEG::context('hogehoge')));
function token(array $args) { if (count($args) === 1) { return $this->readElement() === $args[0] ? $args[0] : PEG::failure(); } return $this->read(count($args)) === $args ? $args : PEG::failure(); }
function parse(PEG_IContext $context) { $elt = $context->readElement(); return preg_match($this->regex, $elt) ? $elt : PEG::failure(); }
<?php include_once dirname(__FILE__) . '/t/t.php'; $t = new lime_test(); function parser() { return PEG::token('a'); } $p = PEG::delay('parser'); $t->is($p->parse(PEG::context('a')), 'a'); $t->is($p->parse(PEG::context('b')), PEG::failure());
<?php include_once dirname(__FILE__) . '/t/t.php'; $t = new lime_test(); $parser = PEG::token('hoge'); $t->is(PEG::match($parser, 'hoge'), 'hoge'); $t->is(PEG::match($parser, 'a'), PEG::failure());