Пример #1
0
 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);
 }
Пример #2
0
 public function mapLine($line)
 {
     if (in_array(substr($line, 0, 1), array('+'), true)) {
         return $this->li->parse(PEG::context($line));
     }
     return PEG::failure();
 }
Пример #3
0
 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();
 }
Пример #4
0
 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;
 }
Пример #5
0
 public function map($line)
 {
     return $this->line->parse(PEG::context($line));
 }
Пример #6
0
<?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');
Пример #7
0
<?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');
Пример #8
0
<?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));
Пример #9
0
<?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.
"
 */
Пример #10
0
<?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());
Пример #11
0
 function map($line)
 {
     return $this->definition->parse(PEG::context($line));
 }
Пример #12
0
<?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());
Пример #13
0
<?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());
Пример #14
0
<?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'))));
Пример #15
0
<?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));
Пример #16
0
<?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());
Пример #17
0
<?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')));
Пример #18
0
<?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')));
Пример #19
0
 /**
  * @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));
 }
Пример #20
0
<?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')))));
Пример #21
0
<?php

include_once dirname(__FILE__) . '/t/t.php';
$lime = new lime_test();
$anything = PEG::anything();
$lime->is('a', $anything->parse(PEG::context('a')));
Пример #22
0
<?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'))));
Пример #23
0
 /**
  * @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));
 }
Пример #24
0
<?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')));
Пример #25
0
$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%
---------------------------------------------------------
Пример #26
0
<?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));
Пример #27
0
<?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');
Пример #28
0
<?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));
Пример #29
0
<?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');
Пример #30
0
<?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')));