Beispiel #1
0
 function mapHeader($line)
 {
     if (!preg_match('/^>\\|([a-zA-Z0-9]*)\\|$/', $line, $matches)) {
         return PEG::failure();
     }
     return $matches[1];
 }
Beispiel #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();
 }
Beispiel #3
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);
 }
Beispiel #4
0
 function parse(PEG_IContext $c)
 {
     if ($c->eos()) {
         return false;
     }
     return PEG::failure();
 }
Beispiel #5
0
 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);
 }
Beispiel #6
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();
 }
Beispiel #7
0
 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();
     }
 }
Beispiel #8
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;
 }
Beispiel #9
0
 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();
 }
Beispiel #10
0
 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;
 }
Beispiel #11
0
 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));
 }
Beispiel #12
0
 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);
 }
Beispiel #13
0
 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();
 }
Beispiel #14
0
 /**
  * @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);
 }
 function map($line)
 {
     return $this->definition->parse(PEG::context($line));
 }
Beispiel #16
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));
 }
Beispiel #17
0
function parser()
{
    return PEG::token('a');
}
Beispiel #18
0
<?php

include_once dirname(__FILE__) . '/t/t.php';
$lime = new lime_test();
$anything = PEG::anything();
$lime->is('a', $anything->parse(PEG::context('a')));
Beispiel #19
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.
"
 */
Beispiel #20
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');
Beispiel #21
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');
Beispiel #22
0
 protected function createParser()
 {
     return $this->nodeCreater(OrgModeSyntax_Node::TYPE_ROOT, PEG::many($this->block));
 }
Beispiel #23
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));
Beispiel #24
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%
---------------------------------------------------------
Beispiel #25
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));
Beispiel #26
0
 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();
 }
Beispiel #27
0
 protected function createParser()
 {
     return $this->nodeCreater('root', PEG::many($this->block));
 }
Beispiel #28
0
 public function map($line)
 {
     return $this->line->parse(PEG::context($line));
 }
Beispiel #29
0
 function parse(PEG_IContext $context)
 {
     return $context->eos() ? PEG::failure() : $context->readElement();
 }
Beispiel #30
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));
 }