Example #1
0
 public function testUtf8()
 {
     $parser = ParserHelper::compile(new RegexExpression("."));
     $result = $parser->parse("€");
     $this->assertNotNull($result);
     $this->assertEquals("€", $result->getString());
 }
Example #2
0
 public function testAlternativeRepeat()
 {
     $n = 500000;
     $expression = new RepeaterExpression(new AlternativeExpression(array(new ConstantExpression("a"), new ConstantExpression("b"))));
     $target = str_repeat("a", $n);
     $parser = ParserHelper::compile($expression);
     $result = $parser->parse($target);
     $this->assertCount($n, $result->getResults());
 }
Example #3
0
 public function testUtf8()
 {
     $expression = new AnyExpression();
     $parser = ParserHelper::compile($expression);
     $target = "€€€";
     $result = $parser->parse($target);
     $this->assertNotNull($result);
     $this->assertEquals("€", $result->getString());
 }
Example #4
0
 public function testAlternativeKey()
 {
     $parser = ParserHelper::compile(new AlternativeExpression(array(1 => new ConstantExpression("a"), "key" => new ConstantExpression("b"))));
     $result = $parser->parse("a");
     $this->assertNotNull($result);
     $this->assertEquals(1, $result->getKey());
     $result = $parser->parse("b");
     $this->assertNotNull($result);
     $this->assertEquals("key", $result->getKey());
 }
Example #5
0
 public function testRecursive()
 {
     $parser = ParserHelper::compile(new JoinedExpression(new JoinedExpression(new ConstantExpression("a"), new ConstantExpression(",")), new ConstantExpression(",")));
     /** @var \PeterVanDommelen\Parser\Expression\Joined\JoinedExpressionResult $result */
     $result = $parser->parse("a,a");
     $this->assertNotNull($result);
     $this->assertEquals("a,a", $result->getString());
     $expected = new JoinedExpressionResult(array(new JoinedExpressionResult(array(new ConstantExpressionResult("a"), new ConstantExpressionResult("a")), array(new ConstantExpressionResult(",")))), array());
     $this->assertEquals($expected, $result);
 }
Example #6
0
 public function testEndOfString()
 {
     $expression = new EndOfStringExpression();
     $parser = ParserHelper::compile($expression);
     $result = $parser->parse("");
     $this->assertNotNull($result);
     $this->assertEquals("", $result->getString());
     $result = $parser->parse("a");
     $this->assertNull($result);
 }
Example #7
0
 public function testUseKeyAsIndex()
 {
     $target = "abcdefg";
     $parser = ParserHelper::compile(new ConcatenatedExpression(array("key" => new ConstantExpression("abc"))));
     /** @var ConcatenatedExpressionResult $result */
     $result = $parser->parse($target);
     $this->assertNotNull($result);
     $this->assertEquals(3, $result->getLength());
     /** @var ConstantExpressionResult $part_result */
     $part_result = $result->getPart("key");
     $this->assertEquals("abc", $part_result->getString());
 }
Example #8
0
 public function testLeftRecursionRepeater()
 {
     $this->expectException(InvalidExpressionException::class);
     $expression = null;
     $expression = new RepeaterExpression(new ConcatenatedExpression(array(&$expression, new ConstantExpression("a"))));
     ParserHelper::compile($expression)->parse("a");
 }
Example #9
0
 public function testStringDoesNotMatch()
 {
     $parser = ParserHelper::compile(new ConstantExpression("abc"));
     $result = $parser->parse("xabcdefg");
     $this->assertNull($result);
 }