From: Andrey Kutejko Date: Wed, 26 Feb 2014 22:19:47 +0000 (+0200) Subject: version 0.1 X-Git-Tag: 0.1 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=b015277817ab535ab4634741dd30425bf267082e;p=pegp.git version 0.1 --- diff --git a/composer.json b/composer.json index 269856a..955208c 100644 --- a/composer.json +++ b/composer.json @@ -9,10 +9,15 @@ } ], "autoload": { - "classmap" : ["lib"] + "classmap" : ["src/"] }, "require-dev": { "phpunit/phpunit": "3.7.*" + }, + "extra": { + "branch-alias": { + "dev-master": "0.x-dev" + } } } diff --git a/composer.lock b/composer.lock index 2b4d50a..fc6fa55 100644 --- a/composer.lock +++ b/composer.lock @@ -3,29 +3,29 @@ "This file locks the dependencies of your project to a known state", "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" ], - "hash": "3e9668f4e1ddee877ae96c3d3e4cd05f", + "hash": "eb85076530a748960ba58f2d2d0e852b", "packages": [ ], "packages-dev": [ { "name": "phpunit/php-code-coverage", - "version": "1.2.12", + "version": "1.2.16", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "1.2.12" + "reference": "69e55e68481cf708a6db43aff0b504e31402fe27" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/1.2.12", - "reference": "1.2.12", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/69e55e68481cf708a6db43aff0b504e31402fe27", + "reference": "69e55e68481cf708a6db43aff0b504e31402fe27", "shasum": "" }, "require": { "php": ">=5.3.3", "phpunit/php-file-iterator": ">=1.3.0@stable", - "phpunit/php-text-template": ">=1.1.1@stable", + "phpunit/php-text-template": ">=1.2.0@stable", "phpunit/php-token-stream": ">=1.1.3@stable" }, "require-dev": { @@ -67,20 +67,20 @@ "testing", "xunit" ], - "time": "2013-07-06 06:26:16" + "time": "2014-02-25 03:34:05" }, { "name": "phpunit/php-file-iterator", - "version": "1.3.3", + "version": "1.3.4", "source": { "type": "git", - "url": "git://github.com/sebastianbergmann/php-file-iterator.git", - "reference": "1.3.3" + "url": "https://github.com/sebastianbergmann/php-file-iterator.git", + "reference": "acd690379117b042d1c8af1fafd61bde001bf6bb" }, "dist": { "type": "zip", - "url": "https://github.com/sebastianbergmann/php-file-iterator/zipball/1.3.3", - "reference": "1.3.3", + "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/acd690379117b042d1c8af1fafd61bde001bf6bb", + "reference": "acd690379117b042d1c8af1fafd61bde001bf6bb", "shasum": "" }, "require": { @@ -107,25 +107,25 @@ } ], "description": "FilterIterator implementation that filters files based on a list of suffixes.", - "homepage": "http://www.phpunit.de/", + "homepage": "https://github.com/sebastianbergmann/php-file-iterator/", "keywords": [ "filesystem", "iterator" ], - "time": "2012-10-11 04:44:38" + "time": "2013-10-10 15:34:57" }, { "name": "phpunit/php-text-template", - "version": "1.1.4", + "version": "1.2.0", "source": { "type": "git", - "url": "git://github.com/sebastianbergmann/php-text-template.git", - "reference": "1.1.4" + "url": "https://github.com/sebastianbergmann/php-text-template.git", + "reference": "206dfefc0ffe9cebf65c413e3d0e809c82fbf00a" }, "dist": { "type": "zip", - "url": "https://github.com/sebastianbergmann/php-text-template/zipball/1.1.4", - "reference": "1.1.4", + "url": "https://api.github.com/repos/sebastianbergmann/php-text-template/zipball/206dfefc0ffe9cebf65c413e3d0e809c82fbf00a", + "reference": "206dfefc0ffe9cebf65c413e3d0e809c82fbf00a", "shasum": "" }, "require": { @@ -156,7 +156,7 @@ "keywords": [ "template" ], - "time": "2012-10-31 11:15:28" + "time": "2014-01-30 17:20:04" }, { "name": "phpunit/php-timer", @@ -204,16 +204,16 @@ }, { "name": "phpunit/php-token-stream", - "version": "1.2.0", + "version": "1.2.1", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-token-stream.git", - "reference": "1.2.0" + "reference": "5220af2a7929aa35cf663d97c89ad3d50cf5fa3e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/1.2.0", - "reference": "1.2.0", + "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/5220af2a7929aa35cf663d97c89ad3d50cf5fa3e", + "reference": "5220af2a7929aa35cf663d97c89ad3d50cf5fa3e", "shasum": "" }, "require": { @@ -250,20 +250,20 @@ "keywords": [ "tokenizer" ], - "time": "2013-08-04 05:57:48" + "time": "2013-09-13 04:58:23" }, { "name": "phpunit/phpunit", - "version": "3.7.23", + "version": "3.7.32", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "3.7.23" + "reference": "2752cbb9ea5bd84c2811b34b6953f76965ec7a2f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/3.7.23", - "reference": "3.7.23", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/2752cbb9ea5bd84c2811b34b6953f76965ec7a2f", + "reference": "2752cbb9ea5bd84c2811b34b6953f76965ec7a2f", "shasum": "" }, "require": { @@ -280,7 +280,7 @@ "symfony/yaml": "~2.0" }, "require-dev": { - "pear-pear/pear": "1.9.4" + "pear-pear.php.net/pear": "1.9.4" }, "suggest": { "ext-json": "*", @@ -324,7 +324,7 @@ "testing", "xunit" ], - "time": "2013-08-02 19:14:44" + "time": "2014-02-25 03:47:29" }, { "name": "phpunit/phpunit-mock-objects", @@ -377,17 +377,17 @@ }, { "name": "symfony/yaml", - "version": "v2.3.2", + "version": "v2.4.2", "target-dir": "Symfony/Component/Yaml", "source": { "type": "git", "url": "https://github.com/symfony/Yaml.git", - "reference": "v2.3.2" + "reference": "bb6ddaf8956139d1b8c360b4b713ed0138e876b3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Yaml/zipball/v2.3.2", - "reference": "v2.3.2", + "url": "https://api.github.com/repos/symfony/Yaml/zipball/bb6ddaf8956139d1b8c360b4b713ed0138e876b3", + "reference": "bb6ddaf8956139d1b8c360b4b713ed0138e876b3", "shasum": "" }, "require": { @@ -396,7 +396,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "2.3-dev" + "dev-master": "2.4-dev" } }, "autoload": { @@ -411,7 +411,9 @@ "authors": [ { "name": "Fabien Potencier", - "email": "fabien@symfony.com" + "email": "fabien@symfony.com", + "homepage": "http://fabien.potencier.org", + "role": "Lead Developer" }, { "name": "Symfony Community", @@ -420,7 +422,7 @@ ], "description": "Symfony Yaml Component", "homepage": "http://symfony.com", - "time": "2013-07-11 19:36:36" + "time": "2014-01-07 13:28:54" } ], "aliases": [ diff --git a/lib/input.php b/lib/input.php deleted file mode 100644 index 9441af4..0000000 --- a/lib/input.php +++ /dev/null @@ -1,28 +0,0 @@ -data = $data; - $this->length = $length ? $length : strlen($data); - $this->pos = $pos; - } - - public function pick($length=PHP_INT_MAX) - { - $length = min($length, $this->length - $this->pos); - if ($length > 0) - return substr($this->data, $this->pos, $length); - else - return ''; - } - - public function advance($len) - { - return new PegpInput($this->data, $this->length, $this->pos + $len); - } -} - diff --git a/lib/parser.php b/lib/parser.php deleted file mode 100644 index 5f2950a..0000000 --- a/lib/parser.php +++ /dev/null @@ -1,260 +0,0 @@ -parse($input); - if (!$result->isSuccess()) - throw new Exception('Parse failed. Expected ' . $result->result->getComment() . ' at ' . $result->input->pos); - if ($input->length != $result->input->pos) - throw new Exception('Parse failed. Unparsed tail at ' . $result->input->pos); - return $result->result; - } - - public function parse(PegpInput $input) - { - $result = $this->doParse($input); - if ($result->isSuccess()) { - return $this->process($result); - } else { - return $result; - } - } - - public abstract function doParse(PegpInput $input); - - protected function process($result) - { - if ($this->drop) { - return PegpResult::success($result->input, null); - } elseif ($this->value !== null) { - return PegpResult::success($result->input, $this->value); - } else { - return $result; - } - } - - public function getComment() - { - return $this->comment; - } - - public function setComment($comment) - { - $this->comment = $comment; - return $this; - } - - public function drop() - { - $this->drop = true; - return $this; - } - - public function value($value) - { - $this->value = $value; - return $this; - } -} - -class PegpString extends PegpParser -{ - private $str, $caseInsensitive; - - public function __construct($str, $caseInsensitive=false) - { - $this->str = $str; - $this->caseInsensitive = $caseInsensitive; - } - - public function doParse(PegpInput $input) - { - $len = strlen($this->str); - if (0 == substr_compare($input->data, $this->str, $input->pos, $len, $this->caseInsensitive)) { // ?? len - return PegpResult::success( - $input->advance($len), - $this->str - ); - } else { - return PegpResult::failure($input, $this); - } - } - - public function getComment() - { - if ($this->comment !== null) - return $this->comment; - else - return 'str.'>'; - } -} - -class PegpRegex extends PegpParser -{ - private $expr; - - public function __construct($expr, $flags='', $delimiter='#') - { - $this->expr = $delimiter . '^' . $expr . $delimiter . $flags; - } - - public function doParse(PegpInput $input) - { - if (preg_match($this->expr, $input->pick(), $m)) { - return PegpResult::success( - $input->advance(strlen($m[0])), - $m - ); - } else { - return PegpResult::failure($input, $this); - } - } - - public function getComment() - { - if ($this->comment !== null) - return $this->comment; - else - return 'expr.'>'; - } -} - -class PegpSequence extends PegpParser -{ - private $parsers, $op = null; - - public function __construct(array $parsers) - { - $this->parsers = $parsers; - } - - public function doParse(PegpInput $input) - { - $inp = $input; - $result = array(); - foreach ($this->parsers as $parser) { - $r = $parser->parse($inp); - if ($r->isSuccess()) { - $inp = $r->input; - if ($r->result !== null) - $result[] = $r->result; - } else { - return PegpResult::failure($inp, $parser); - } - } - return PegpResult::success($inp, $result); - } - - protected function process($result) - { - switch ($this->op) { - case 'join': - $result = PegpResult::success($result->input, implode('', $result->result)); - break; - case 'bitOr': - $value = 0; - foreach ($result->result as $v) - $value |= $v; - $result = PegpResult::success($result->input, $value); - break; - case 'sum': - $value = 0; - foreach ($result->result as $v) - $value += $v; - $result = PegpResult::success($result->input, $value); - break; - case 'product': - $value = 1; - foreach ($result->result as $v) - $value *= $v; - $result = PegpResult::success($result->input, $value); - break; - } - return parent::process($result); - } - - public function join() - { - $this->op = 'join'; - return $this; - } - - public function bitOr() - { - $this->op = 'bitOr'; - return $this; - } - - public function sum() - { - $this->op = 'sum'; - return $this; - } - - public function product() - { - $this->op = 'product'; - return $this; - } -} - -class PegpOneOf extends PegpParser -{ - private $parsers; - - public function __construct(array $parsers) - { - $this->parsers = $parsers; - } - - public function doParse(PegpInput $input) - { - foreach ($this->parsers as $parser) { - $r = $parser->parse($input); - if ($r->isSuccess()) { - return $r; - } - } - return PegpResult::failure($input, $this); - } - - public function getComment() - { - if ($this->comment !== null) - return $this->comment; - $comment = array(); - foreach ($this->parsers as $parser) { - $comment[] = $parser->getComment(); - } - return 'one of: '.implode(', ', $comment); - } -} - -class PegpOptional extends PegpParser -{ - private $parser, $emptyValue; - - public function __construct(PegpParser $parser, $emptyValue='') - { - $this->parser = $parser; - $this->emptyValue = $emptyValue; - } - - public function doParse(PegpInput $input) - { - $r = $this->parser->parse($input); - if ($r->isSuccess()) { - return $r; - } else { - return PegpResult::success($input, $this->emptyValue); - } - } -} - diff --git a/lib/result.php b/lib/result.php deleted file mode 100644 index f2e559f..0000000 --- a/lib/result.php +++ /dev/null @@ -1,30 +0,0 @@ -success = $success; - $this->input = $input; - $this->result = $result; - } - - public static function success(PegpInput $input, $result=null) - { - return new PegpResult(true, $input, $result); - } - - public static function failure(PegpInput $input, $parser=null) - { - return new PegpResult(false, $input, $parser); - } - - public function isSuccess() - { - return $this->success; - } -} - diff --git a/lib/shortcuts.php b/lib/shortcuts.php deleted file mode 100644 index 91efa2c..0000000 --- a/lib/shortcuts.php +++ /dev/null @@ -1,35 +0,0 @@ -data = $data; + $this->length = $length ? $length : strlen($data); + $this->pos = $pos; + } + + public function pick($length=PHP_INT_MAX) + { + $length = min($length, $this->length - $this->pos); + if ($length > 0) + return substr($this->data, $this->pos, $length); + else + return ''; + } + + public function advance($len) + { + return new PegpInput($this->data, $this->length, $this->pos + $len); + } +} + diff --git a/src/parser.php b/src/parser.php new file mode 100644 index 0000000..5f2950a --- /dev/null +++ b/src/parser.php @@ -0,0 +1,260 @@ +parse($input); + if (!$result->isSuccess()) + throw new Exception('Parse failed. Expected ' . $result->result->getComment() . ' at ' . $result->input->pos); + if ($input->length != $result->input->pos) + throw new Exception('Parse failed. Unparsed tail at ' . $result->input->pos); + return $result->result; + } + + public function parse(PegpInput $input) + { + $result = $this->doParse($input); + if ($result->isSuccess()) { + return $this->process($result); + } else { + return $result; + } + } + + public abstract function doParse(PegpInput $input); + + protected function process($result) + { + if ($this->drop) { + return PegpResult::success($result->input, null); + } elseif ($this->value !== null) { + return PegpResult::success($result->input, $this->value); + } else { + return $result; + } + } + + public function getComment() + { + return $this->comment; + } + + public function setComment($comment) + { + $this->comment = $comment; + return $this; + } + + public function drop() + { + $this->drop = true; + return $this; + } + + public function value($value) + { + $this->value = $value; + return $this; + } +} + +class PegpString extends PegpParser +{ + private $str, $caseInsensitive; + + public function __construct($str, $caseInsensitive=false) + { + $this->str = $str; + $this->caseInsensitive = $caseInsensitive; + } + + public function doParse(PegpInput $input) + { + $len = strlen($this->str); + if (0 == substr_compare($input->data, $this->str, $input->pos, $len, $this->caseInsensitive)) { // ?? len + return PegpResult::success( + $input->advance($len), + $this->str + ); + } else { + return PegpResult::failure($input, $this); + } + } + + public function getComment() + { + if ($this->comment !== null) + return $this->comment; + else + return 'str.'>'; + } +} + +class PegpRegex extends PegpParser +{ + private $expr; + + public function __construct($expr, $flags='', $delimiter='#') + { + $this->expr = $delimiter . '^' . $expr . $delimiter . $flags; + } + + public function doParse(PegpInput $input) + { + if (preg_match($this->expr, $input->pick(), $m)) { + return PegpResult::success( + $input->advance(strlen($m[0])), + $m + ); + } else { + return PegpResult::failure($input, $this); + } + } + + public function getComment() + { + if ($this->comment !== null) + return $this->comment; + else + return 'expr.'>'; + } +} + +class PegpSequence extends PegpParser +{ + private $parsers, $op = null; + + public function __construct(array $parsers) + { + $this->parsers = $parsers; + } + + public function doParse(PegpInput $input) + { + $inp = $input; + $result = array(); + foreach ($this->parsers as $parser) { + $r = $parser->parse($inp); + if ($r->isSuccess()) { + $inp = $r->input; + if ($r->result !== null) + $result[] = $r->result; + } else { + return PegpResult::failure($inp, $parser); + } + } + return PegpResult::success($inp, $result); + } + + protected function process($result) + { + switch ($this->op) { + case 'join': + $result = PegpResult::success($result->input, implode('', $result->result)); + break; + case 'bitOr': + $value = 0; + foreach ($result->result as $v) + $value |= $v; + $result = PegpResult::success($result->input, $value); + break; + case 'sum': + $value = 0; + foreach ($result->result as $v) + $value += $v; + $result = PegpResult::success($result->input, $value); + break; + case 'product': + $value = 1; + foreach ($result->result as $v) + $value *= $v; + $result = PegpResult::success($result->input, $value); + break; + } + return parent::process($result); + } + + public function join() + { + $this->op = 'join'; + return $this; + } + + public function bitOr() + { + $this->op = 'bitOr'; + return $this; + } + + public function sum() + { + $this->op = 'sum'; + return $this; + } + + public function product() + { + $this->op = 'product'; + return $this; + } +} + +class PegpOneOf extends PegpParser +{ + private $parsers; + + public function __construct(array $parsers) + { + $this->parsers = $parsers; + } + + public function doParse(PegpInput $input) + { + foreach ($this->parsers as $parser) { + $r = $parser->parse($input); + if ($r->isSuccess()) { + return $r; + } + } + return PegpResult::failure($input, $this); + } + + public function getComment() + { + if ($this->comment !== null) + return $this->comment; + $comment = array(); + foreach ($this->parsers as $parser) { + $comment[] = $parser->getComment(); + } + return 'one of: '.implode(', ', $comment); + } +} + +class PegpOptional extends PegpParser +{ + private $parser, $emptyValue; + + public function __construct(PegpParser $parser, $emptyValue='') + { + $this->parser = $parser; + $this->emptyValue = $emptyValue; + } + + public function doParse(PegpInput $input) + { + $r = $this->parser->parse($input); + if ($r->isSuccess()) { + return $r; + } else { + return PegpResult::success($input, $this->emptyValue); + } + } +} + diff --git a/src/result.php b/src/result.php new file mode 100644 index 0000000..f2e559f --- /dev/null +++ b/src/result.php @@ -0,0 +1,30 @@ +success = $success; + $this->input = $input; + $this->result = $result; + } + + public static function success(PegpInput $input, $result=null) + { + return new PegpResult(true, $input, $result); + } + + public static function failure(PegpInput $input, $parser=null) + { + return new PegpResult(false, $input, $parser); + } + + public function isSuccess() + { + return $this->success; + } +} + diff --git a/src/shortcuts.php b/src/shortcuts.php new file mode 100644 index 0000000..91efa2c --- /dev/null +++ b/src/shortcuts.php @@ -0,0 +1,35 @@ +