Skip to content

Commit

Permalink
Keep track of line numbers in regex parsers.
Browse files Browse the repository at this point in the history
  • Loading branch information
nielssp committed Nov 14, 2015
1 parent ff35d8b commit 7b7ebfe
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
3 changes: 2 additions & 1 deletion examples/calculator.php
Expand Up @@ -105,4 +105,5 @@ public function __invoke($input)

$calculator = new Calculator();

echo $calculator(' 2 + 4 / 2 - 3 * ( 6 + 2 ) ');
echo 'Result: ';
echo $calculator(' 2 + 4 / 2 - 3 * ( 6 - ( 5 + 3 ) ) ');
7 changes: 6 additions & 1 deletion src/Combinator/Parsers.php
Expand Up @@ -70,7 +70,12 @@ public function elem($e)
}
$input = array_slice($input, 1);
$nextPos = $pos;
$nextPos[1]++;
if ($e === "\n") {
$nextPos[0]++;
$nextPos[1] = 1;
} else {
$nextPos[1]++;
}
return new Success($e, $pos, $input, $nextPos);
});
}
Expand Down
9 changes: 7 additions & 2 deletions src/Combinator/RegexParsers.php
Expand Up @@ -142,6 +142,7 @@ public function string($s)
$pos = $r->nextPos;
}
$length = strlen($s);
$nextPos = $pos;
for ($i = 0; $i < $length; $i++) {
if (! isset($input[$i])) {
return new Failure(
Expand All @@ -159,10 +160,14 @@ public function string($s)
$pos
);
}
if ($input[$i] === "\n") {
$nextPos[0]++;
$nextPos[1] = 1;
} else {
$nextPos[1]++;
}
}
$input = array_slice($input, $length);
$nextPos = $pos;
$nextPos[1] += $length;
return new Success($s, $pos, $input, $nextPos);
});
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Combinator/ParsersTest.php
Expand Up @@ -56,6 +56,14 @@ public function testElem()
$this->assertEquals(1, $result->get());
$this->assertEquals(array(2), $result->nextInput);
$this->assertEquals(array(1, 2), $result->nextPos);

$p2 = $this->elem("\n");

$result = $this->apply($p2, array("\n"));
$this->assertTrue($result->successful);
$this->assertEquals("\n", $result->get());
$this->assertEquals(array(), $result->nextInput);
$this->assertEquals(array(2, 1), $result->nextPos);
}

public function testPhrase()
Expand Down
16 changes: 16 additions & 0 deletions tests/Combinator/RegexParsersTest.php
Expand Up @@ -93,6 +93,14 @@ public function testChar()
$this->assertEquals('a', $result->get());
$this->assertEquals(array('b'), $result->nextInput);
$this->assertEquals(array(1, 2), $result->nextPos);

$p2 = $this->char("\n");

$result = $this->apply($p2, array("\n"));
$this->assertTrue($result->successful);
$this->assertEquals("\n", $result->get());
$this->assertEquals(array(), $result->nextInput);
$this->assertEquals(array(2, 1), $result->nextPos);
}

public function testString()
Expand All @@ -112,6 +120,14 @@ public function testString()
$this->assertEquals('aaa', $result->get());
$this->assertEquals(array(), $result->nextInput);
$this->assertEquals(array(1, 4), $result->nextPos);

$p2 = $this->string("aa\na");

$result = $this->parse($p2, "aa\na");
$this->assertTrue($result->successful);
$this->assertEquals("aa\na", $result->get());
$this->assertEquals(array(), $result->nextInput);
$this->assertEquals(array(2, 2), $result->nextPos);
}

public function testRegex()
Expand Down

0 comments on commit 7b7ebfe

Please sign in to comment.