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); }
public function mapLine($line) { if (in_array(substr($line, 0, 1), array('+'), true)) { return $this->li->parse(PEG::context($line)); } 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(); }
function map(array $pre) { list($body, $end) = $pre; if ($end !== '|<') { $body[] = substr($end, 0, -2); } foreach ($body as &$line) { $line = $this->line->parse(PEG::context($line)); } return $body; }
public function map($line) { return $this->line->parse(PEG::context($line)); }
<?php include_once dirname(__FILE__) . '/t/t.php'; $t = new lime_test(); $p = PEG::ref($p_ref); $p_ref = PEG::token('hoge'); $t->is($p->parse(PEG::context('hoge')), 'hoge'); $p = PEG::ref($_); $ref =& $p->getRef(); $ref = PEG::token('hoge'); $t->is($p->parse(PEG::context('hoge')), 'hoge'); $p = PEG::ref($_); $p->is(PEG::token('hoge')); $t->is($p->parse(PEG::context('hoge')), 'hoge');
<?php $cwd = dirname(__FILE__); include_once $cwd . '/../../t.php'; $t = new lime_test(); $p = OrgModeSyntax_Locator::it()->bracket; $c = PEG::context('[[http://google.com/][Google]]'); $result = $p->parse($c); $t->is($result->at('href'), 'http://google.com/'); $t->is($result->at('title'), 'Google'); $c = PEG::context('[[http://example.com/]]'); $result = $p->parse($c); $t->is($result->at('href'), 'http://example.com/'); $t->is($result->at('title'), false); $c = PEG::context('[[foo.png]]'); $result = $p->parse($c); $t->is($result->at('src'), 'foo.png'); $t->is($result->at('alt'), false); $c = PEG::context('[[http://example.com/sample.jpg][sample image]]'); $result = $p->parse($c); $t->is($result->at('src'), 'http://example.com/sample.jpg'); $t->is($result->at('alt'), 'sample image');
<?php include_once dirname(__FILE__) . '/t/t.php'; $lime = new lime_test(); $lime->is(false, PEG::eos()->parse(PEG::context(''))); $context = PEG::context('hoge'); $context->read(4); $lime->is(false, PEG::eos()->parse($context));
<?php /** * 行頭に#があったら無視するパーサのサンプル */ include_once dirname(__FILE__) . '/../code/PEG.php'; $line = PEG::line(); $ignore = PEG::drop(PEG::andalso('#', $line)); $parser = PEG::join(PEG::many(PEG::choice($ignore, $line))); $context = PEG::context(' Lorem ipsum dolor sit amet, #consectetur adipisicing elit, #sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. '); var_dump($parser->parse($context)); /* 結果 string(64) " Lorem ipsum dolor sit amet, labore et dolore magna aliqua. " */
<?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());
function map($line) { return $this->definition->parse(PEG::context($line)); }
<?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.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.php'; $t = new lime_test(); $p = new OrgModeSyntax_DefinitionList(PEG::anything()); $c = PEG::context(array('- foo : bar', '- baz : qux')); $t->is($p->parse($c), array(array(str_split(' foo '), str_split(' bar')), array(str_split(' baz '), str_split(' qux'))));
<?php include_once dirname(__FILE__) . '/../../t.php'; $t = new lime_test(); $p = HatenaSyntax_Locator::it()->bracket; $c = PEG::context('[http://google.com:title=hoge]'); $t->ok($p->parse($c));
<?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());
<?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')));
<?php include_once dirname(__FILE__) . '/../../t.php'; $t = new lime_test(); $p = new HatenaSyntax_Pre(PEG::anything()); $c = PEG::context(array('>|', 'a|<')); $t->is($p->parse($c), array(array('a'))); $c = PEG::context(array('>|', 'a', '|<')); $t->is($p->parse($c), array(array('a')));
/** * @param string * @return PEG_IContext */ protected static function context($str) { $str = str_replace(array("\r\n", "\r"), "\n", $str); return PEG::context(preg_split("{\n}", $str)); }
<?php include_once dirname(__FILE__) . '/../../t.php'; $t = new lime_test(); $p = new HatenaSyntax_Table(PEG::anything()); $c = PEG::context(array('|*a|*b|', '|c|d|')); $t->is($p->parse($c), array(array(array('*', array('a')), array('*', array('b'))), array(array(false, array('c')), array(false, array('d'))))); $c = PEG::context(array('|a|')); $t->is($p->parse($c), array(array(array(false, array('a')))));
<?php include_once dirname(__FILE__) . '/t/t.php'; $lime = new lime_test(); $anything = PEG::anything(); $lime->is('a', $anything->parse(PEG::context('a')));
<?php include_once dirname(__FILE__) . '/../../t.php'; $t = new lime_test(); $p = new HatenaSyntax_DefinitionList(PEG::anything()); $c = PEG::context(array(':a:b', ':a:b')); $t->is($p->parse($c), array(array(array('a'), array('b')), array(array('a'), array('b'))));
/** * @param string * @return PEG_IContext */ protected static function context($str) { $str = str_replace(array("\r\n", "\r"), "\n", $str); $str = strpos('<!--', $str) === false ? $str : HatenaSyntax_CommentRemover::remove($str); return PEG::context(preg_split("/\n/", $str)); }
<?php $cwd = dirname(__FILE__); include_once $cwd . '/../../t.php'; $t = new lime_test(); $p = new OrgModeSyntax_Header(PEG::anything()); $c = PEG::context(array('* Top level headline')); $t->is($p->parse($c), array(0, str_split(' Top level headline'))); $c = PEG::context(array('** Second level')); $t->is($p->parse($c), array(1, str_split(' Second level'))); $c = PEG::context(array('*** 3rd level', 'some text')); $t->is($p->parse($c), array(2, str_split(' 3rd level')));
$t = new Benchmark_Timer(); $str = '((((((((1))))))))'; $t->start(); // メモ化していないパーサ $a = PEG::ref($a_ref); $p = PEG::ref($p_ref); $a_ref = PEG::choice(PEG::seq($p, '+', $a), PEG::seq($p, '-', $a), $p); $p_ref = PEG::choice(PEG::seq('(', $a, ')'), '1'); $a->parse(PEG::context($str)); $t->setMarker('no memoize'); // メモ化しているパーサ $a = PEG::ref($a_ref); $p = PEG::ref($p_ref); $a_ref = PEG::memo(PEG::choice(PEG::seq($p, '+', $a), PEG::seq($p, '-', $a), $p)); $p_ref = PEG::memo(PEG::choice(PEG::seq('(', $a, ')'), '1')); $a->parse($c = PEG::context($str)); $t->setMarker('memoize'); $t->stop(); $t->display(); /* 結果 --------------------------------------------------------- marker time index ex time perct --------------------------------------------------------- Start 1242400475.10093900 - 0.00% --------------------------------------------------------- no memoize 1242400476.30805000 1.207111 99.74% --------------------------------------------------------- memoize 1242400476.31115500 0.003105 0.26% --------------------------------------------------------- Stop 1242400476.31117700 0.000022 0.00% ---------------------------------------------------------
<?php include_once dirname(__FILE__) . '/t/t.php'; $lime = new lime_test(); $context = PEG::context(array(1, 2, 3)); $lime->is($context->eos(), false); $lime->is($context->read(1), array(1));
<?php include_once dirname(__FILE__) . '/t/t.php'; class MyActionParser extends PEG_Action { function process($result) { return join('', $result); } } $lime = new lime_test(); $action = new MyActionParser(PEG::many(PEG::token('hoge'))); $lime->is($action->parse(PEG::context('hogehogehogehoge')), 'hogehogehogehoge');
<?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));
<?php include_once dirname(__FILE__) . '/../../t.php'; $t = new lime_test(); $p = new HatenaSyntax_Quote(PEG::anything()); $c = PEG::context(array('>>', 'a', '<<')); $t->is($p->parse($c), array(false, array('a'))); $c = PEG::context(array('>http://google.com>', 'a', '<<')); $result = $p->parse($c); $t->is($result[1], array('a')); $t->is($result[0]->at('href'), 'http://google.com'); $t->is($result[0]->at('title'), false); $c = PEG::context(array('>http://google.com:title>', 'a', '<<')); $result = $p->parse($c); $t->is($result[1], array('a')); $t->is($result[0]->at('href'), 'http://google.com'); $t->is($result[0]->at('title'), ''); $c = PEG::context(array('>http://google.com:title=hoge>', 'a', '<<')); $result = $p->parse($c); $t->is($result[1], array('a')); $t->is($result[0]->at('href'), 'http://google.com'); $t->is($result[0]->at('title'), 'hoge');
<?php include_once dirname(__FILE__) . '/../../t.php'; $t = new lime_test(); $p = new HatenaSyntax_SuperPre(); $c = PEG::context(array('>||', 'a||<')); $t->is($p->parse($c), array('', array('a'))); $c = PEG::context(array('>|a|', 'b', '||<')); $t->is($p->parse($c), array('a', array('b')));