Permalink
Browse files

Add optional whitespace skipper to RegexParsers.

  • Loading branch information...
nielssp committed Nov 14, 2015
1 parent 211d37a commit 43a776cdb145bf4421ca545fd5956156b04ee391
Showing with 401 additions and 309 deletions.
  1. +19 −0 LICENSE
  2. +3 −0 README.md
  3. +6 −7 examples/calculator.php
  4. +169 −197 src/Combinator/Parsers.php
  5. +112 −50 src/Combinator/RegexParsers.php
  6. +47 −55 src/Parser.php
  7. +45 −0 tests/Combinator/RegexParsersTest.php
19 LICENSE
@@ -0,0 +1,19 @@
Copyright (C) 2015 Niels Sonnich Poulsen (http://nielssp.dk)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@@ -0,0 +1,3 @@
# Parco – PHP parser combinators


@@ -30,7 +30,7 @@ public function number()
*/
public function factor()
{
$expr = $this->elem('(')->seqR($this->expr)->seqL($this->elem(')'));
$expr = $this->char('(')->seqR($this->expr)->seqL($this->char(')'));
return $expr->alt($this->number);
}
@@ -41,8 +41,8 @@ public function factor()
public function term()
{
return $this->factor->seq($this->rep($this->alt(
$this->elem("*")->seq($this->factor),
$this->elem("/")->seq($this->factor)
$this->char("*")->seq($this->factor),
$this->char("/")->seq($this->factor)
)))->map(function ($numbers) {
// $numbers = array(factor, array(array("*", factor), ...))
$x = $numbers[0];
@@ -64,8 +64,8 @@ public function term()
public function expr()
{
return $this->term->seq($this->rep($this->alt(
$this->elem("+")->seq($this->term),
$this->elem("-")->seq($this->term)
$this->char("+")->seq($this->term),
$this->char("-")->seq($this->term)
)))->map(function ($numbers) {
// $numbers = array(term, array(array("+", term), ...))
$x = $numbers[0];
@@ -101,5 +101,4 @@ public function __invoke($input)
$calculator = new Calculator();
// TODO: ignore whitespace
echo $calculator('2+4/2-3*(6+2)');
echo $calculator('2 + 4 / 2 - 3 * ( 6 + 2 ) ');
Oops, something went wrong.

0 comments on commit 43a776c

Please sign in to comment.