Exemplo n.º 1
0
 public function testUtf8()
 {
     $parser = ParserHelper::compile(new RegexExpression("."));
     $result = $parser->parse("€");
     $this->assertNotNull($result);
     $this->assertEquals("€", $result->getString());
 }
Exemplo n.º 2
0
 public function testFlattenerCircular()
 {
     $flattener = ParserHelper::createFlattener();
     $expression = null;
     $expression = new ConcatenatedExpression(array(new ConstantExpression("a"), new AlternativeExpression(array(&$expression, new ConstantExpression("abc")))));
     $flattened = $flattener->flattenExpression($expression);
     $this->assertCount(4, $flattened);
 }
Exemplo n.º 3
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());
 }
Exemplo n.º 4
0
 public function testUtf8()
 {
     $expression = new AnyExpression();
     $parser = ParserHelper::compile($expression);
     $target = "€€€";
     $result = $parser->parse($target);
     $this->assertNotNull($result);
     $this->assertEquals("€", $result->getString());
 }
Exemplo n.º 5
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());
 }
Exemplo n.º 6
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);
 }
Exemplo n.º 7
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);
 }
Exemplo n.º 8
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());
 }
Exemplo n.º 9
0
 public function testBraces()
 {
     $grammar = new Grammar(array("expression" => new ConcatenatedExpression(array(new RepeaterExpression(new NotExpression(new AlternativeExpression(array(new ConstantExpression("("))))), new AlternativeExpression(array(new ConcatenatedExpression(array(new ConstantExpression("("), new NamedExpression("expression"), new ConstantExpression(")"))), new RepeaterExpression(new NotExpression(new AlternativeExpression(array(new ConstantExpression("("))))))), new RepeaterExpression(new NotExpression(new AlternativeExpression(array(new ConstantExpression("(")))))))));
     $no_opening_brace = new RepeaterExpression(new NotExpression(new AlternativeExpression(array(new ConstantExpression("(")))));
     $grammar = new Grammar(array("expression" => new AlternativeExpression(array(new ConcatenatedExpression(array($no_opening_brace, new ConstantExpression("("), new NamedExpression("expression"), new ConstantExpression(")"), $no_opening_brace)), $no_opening_brace))));
     $parser = ParserHelper::compileWithGrammar(new NamedExpression("expression"), $grammar);
     $result = $parser->parse("ab");
     $this->assertNotNull($result);
     $this->assertEquals("ab", $result->getString());
     $result = $parser->parse("ab(");
     $this->assertNotNull($result);
     $this->assertEquals("ab", $result->getString());
     $result = $parser->parse("ab(c(d)ef)g");
     $this->assertNotNull($result);
     $this->assertEquals("ab(c(d)ef)g", $result->getString());
     $this->assertEquals("abg", $result->getResult()->getPart(0)->getString() . $result->getResult()->getPart(4)->getString());
 }
Exemplo n.º 10
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");
 }
 private function getChecker()
 {
     return new HasNoLeftRecursionAsserter(ParserHelper::createFlattener(), ParserHelper::createEmptyChecker());
 }
Exemplo n.º 12
0
 private function getChecker()
 {
     return new HasNoEmptyRepeaterAsserter(ParserHelper::createFlattener(), ParserHelper::createEmptyChecker());
 }
Exemplo n.º 13
0
 public function testStringDoesNotMatch()
 {
     $parser = ParserHelper::compile(new ConstantExpression("abc"));
     $result = $parser->parse("xabcdefg");
     $this->assertNull($result);
 }