<?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__) . '/../code/PEG.php'; /** * 括弧の対応をとる再帰的なパーサのサンプル。 * 認識した括弧を用いて文字列を階層化する。 * * パーサのEBNFはこんな感じ * item := paren | anything * paren_item := (?! ")") item * paren := "(" paren_item* ")" * parser := item* */ $paren = PEG::ref($paren_ref); $item = PEG::choice($paren, PEG::anything()); $paren_item = PEG::andalso(PEG::not(')'), $item); $paren_ref = PEG::pack('(', PEG::many($paren_item), ')'); $parser = PEG::many($item); $str = 'abc(def(ghi)(jkl(mno)))pq'; var_dump($parser->parse(PEG::context($str))); /* 結果 array(6) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" [3]=> array(5) { [0]=>