Example #1
0
 public function testbuilder()
 {
     $builder = new SyntaxBuilder();
     $builder->syntax("EBNF defined in itself.", "xis/ebnf v2.0 http://wiki.karmin.ch/ebnf/ gpl3")->rule("syntax")->sequence()->option()->identifier("title")->end()->terminal("{")->loop()->identifier("rule")->end()->terminal("}")->option()->identifier("comment")->end()->end()->rule("rule")->sequence()->identifier("identifier")->choice()->terminal("=")->terminal(":")->terminal(":==")->end()->identifier("expression")->choice()->terminal(".")->terminal(";")->end()->end()->rule("literal")->choice()->sequence()->terminal("'")->identifier("character")->loop()->identifier("character")->end()->terminal("'")->end()->sequence()->terminal('"')->identifier("character")->loop()->identifier("character")->end()->terminal('"')->end()->end();
     $syntax = $builder->getAst();
     $xml = file_get_contents(EBNF_TESTS_FIXTURS . "/visitor/syntax.xml");
     $visitor = new Xml();
     $syntax->accept($visitor);
     $this->assertEquals($xml, $visitor->getXmlString());
 }
Example #2
0
 public function testProbeEquivalenceSyntaxWithRulesAndSubnodes()
 {
     $builder = new SyntaxBuilder();
     $builder->syntax("foo", "bar")->rule("syntax")->sequence()->option()->identifier("title")->end()->terminal("{")->loop()->identifier("rule")->end()->terminal("}")->option()->identifier("comment")->end()->end();
     $syntax1 = $builder->getAst();
     $builder->clear()->syntax("foo", "bar")->rule("syntax")->sequence()->option()->identifier("title")->end()->terminal("{")->loop()->identifier("rule")->end()->terminal("}")->option()->identifier("comment")->end()->end();
     $syntax2 = $builder->getAst();
     $n = new Notification();
     $syntax1->probeEquivalence($syntax2, $n);
     $this->assertTrue($n->isOk(), $n->report());
     $this->assertEquals("", $n->report());
     $n = new Notification();
     $syntax2->probeEquivalence($syntax1, $n);
     $this->assertTrue($n->isOk(), $n->report());
     $this->assertEquals("", $n->report());
     $builder->clear()->syntax("foo", "bar")->rule("syntax")->sequence()->option()->identifier("title")->end()->terminal("{")->loop()->identifier("rule")->end()->terminal("}")->option()->identifier("comment")->end()->end();
     $syntax1 = $builder->getAst();
     $builder->clear()->syntax("snafu", "bar")->rule("syntax")->sequence()->option()->identifier("bla")->end()->terminal("{")->loop()->identifier("snafu")->end()->terminal("}")->option()->identifier("blub")->end()->end();
     $syntax2 = $builder->getAst();
     $n = new Notification();
     $syntax1->probeEquivalence($syntax2, $n);
     $this->assertFalse($n->isOk(), $n->report());
     $this->assertEquals("Titles of syntx differs: 'foo' != 'snafu'!\n" . "Identifier value mismatch: 'title' != 'bla'!\n" . "Identifier value mismatch: 'rule' != 'snafu'!\n" . "Identifier value mismatch: 'comment' != 'blub'!", $n->report());
     $n = new Notification();
     $syntax2->probeEquivalence($syntax1, $n);
     $this->assertFalse($n->isOk(), $n->report());
     $this->assertEquals("Titles of syntx differs: 'snafu' != 'foo'!\n" . "Identifier value mismatch: 'bla' != 'title'!\n" . "Identifier value mismatch: 'snafu' != 'rule'!\n" . "Identifier value mismatch: 'blub' != 'comment'!", $n->report());
 }
Example #3
0
 public function testGenerateText()
 {
     $builder = new SyntaxBuilder();
     $builder->syntax("foo");
     $ast = $builder->getAst();
     $visitor = new TextSyntaxTree();
     $ast->accept($visitor);
     $this->assertEquals("[syntax]" . PHP_EOL, $visitor->getText());
     $visitor = new TextSyntaxTree();
     $builder->rule("rule-1");
     $ast = $builder->getAst();
     $ast->accept($visitor);
     $this->assertEquals("[syntax]" . PHP_EOL . " +--[rule='rule-1']" . PHP_EOL, $visitor->getText());
     $visitor = new TextSyntaxTree();
     $builder->rule("rule-2");
     $ast = $builder->getAst();
     $ast->accept($visitor);
     $this->assertEquals("[syntax]" . PHP_EOL . " +--[rule='rule-1']" . PHP_EOL . " +--[rule='rule-2']" . PHP_EOL, $visitor->getText());
     $choice = $builder->clear()->rule("name")->choice();
     $ast = $builder->getAst();
     $visitor = new TextSyntaxTree();
     $ast->accept($visitor);
     $this->assertEquals("[syntax]" . PHP_EOL . " +--[rule='name']" . PHP_EOL . "     +--[choice]" . PHP_EOL, $visitor->getText());
     $choice->identifier("ident")->terminal("term")->end();
     $ast = $builder->getAst();
     $ast->accept($visitor);
     $this->assertEquals("[syntax]" . PHP_EOL . " +--[rule='name']" . PHP_EOL . "     +--[choice]" . PHP_EOL . "         +--[identifier='ident']" . PHP_EOL . "         +--[terminal='term']" . PHP_EOL, $visitor->getText());
     $visitor = new TextSyntaxTree();
     $builder->clear()->syntax("foobar")->rule("one")->choice()->end()->rule("two");
     $ast = $builder->getAst();
     $ast->accept($visitor);
     $this->assertEquals("[syntax]" . PHP_EOL . " +--[rule='one']" . PHP_EOL . " |   +--[choice]" . PHP_EOL . " +--[rule='two']" . PHP_EOL, $visitor->getText());
     $fixtureDir = EBNF_TESTS_FIXTURS . DIRECTORY_SEPARATOR . "visitor" . DIRECTORY_SEPARATOR . "TextSyntaxTree";
     $file = EBNF_TESTS_FIXTURS . "/rules_with_literals.ebnf";
     $scanner = new Scanner(file_get_contents($file));
     $parser = new Parser($scanner);
     $parser->parse();
     $ast = $parser->getAst();
     $visitor = new TextSyntaxTree();
     $ast->accept($visitor);
     $this->assertEquals(file_get_contents($fixtureDir . DIRECTORY_SEPARATOR . "rules_with_literals"), $visitor->getText());
 }
Example #4
0
 public function testGetText()
 {
     $builder = new SyntaxBuilder();
     $builder->syntax("foobar")->rule("rule")->identifier("identifier")->end();
     $visitor = new TextRailroad();
     $ast = $builder->getAst();
     $ast->accept($visitor);
     $this->assertEquals("rule" . PHP_EOL . "------>--[identifier]-->---|" . PHP_EOL, $visitor->getText());
     $builder->clear()->syntax("foobar")->rule("rule")->terminal("terminal")->end();
     $ast = $builder->getAst();
     $ast->accept($visitor);
     $this->assertEquals("rule" . PHP_EOL . "------>--(terminal)-->---|" . PHP_EOL, $visitor->getText());
     $this->markTestIncomplete();
 }
Example #5
0
 public function testParseAst()
 {
     $p = new Parser(new Scanner($this->loadFixture("rules_with_different_assignment_ops.ebnf")));
     $p->parse();
     $builder = new SyntaxBuilder();
     $builder->syntax("Rules with different assignment operators.")->rule("comment1")->identifier("literal1")->end()->rule("comment2")->identifier("literal2")->end()->rule("comment3")->identifier("literal3")->end();
     $this->assertEquivalentSyntax($builder->getAst(), $p->getAst());
     $p = new Parser(new Scanner($this->loadFixture("rules_with_literals.ebnf")));
     $p->parse();
     $builder->clear()->syntax("Rules with literal.")->rule("literal")->choice()->sequence()->terminal("'")->identifier("character")->loop()->identifier("character")->end()->terminal("'")->end()->sequence()->terminal('"')->identifier("character")->loop()->identifier("character")->end()->terminal('"')->end()->end()->end();
     $this->assertEquivalentSyntax($builder->getAst(), $p->getAst());
     $p = new Parser(new Scanner($this->loadFixture("testgrammar_1.old.ebnf")));
     $p->parse();
     $builder->clear()->syntax("EBNF defined in itself.")->rule("syntax")->sequence()->option()->identifier("title")->end()->terminal("{")->loop()->identifier("rule")->end()->terminal("}")->option()->identifier("comment")->end()->end()->end()->rule("rule")->sequence()->identifier("identifier")->choice()->terminal("=")->terminal(":")->terminal(":==")->end()->identifier("expression")->choice()->terminal(".")->terminal(";")->end()->end()->end()->rule("expression")->sequence()->identifier("term")->loop()->sequence()->terminal("|")->identifier("term")->end()->end()->end()->end()->rule("term")->sequence()->identifier("factor")->loop()->identifier("factor")->end()->end()->end()->rule("factor")->choice()->identifier("identifier")->identifier("literal")->identifier("range")->sequence()->terminal("[")->identifier("expression")->terminal("]")->end()->sequence()->terminal("(")->identifier("expression")->terminal(")")->end()->sequence()->terminal("{")->identifier("expression")->terminal("}")->end()->end()->end()->rule("identifier")->sequence()->identifier("character")->loop()->identifier("character")->end()->end()->end()->rule("range")->sequence()->identifier("character")->terminal("..")->identifier("character")->end()->end()->rule("title")->identifier("literal")->end()->rule("comment")->identifier("literal")->end()->rule("literal")->choice()->sequence()->terminal("'")->identifier("character")->loop()->identifier("character")->end()->terminal("'")->end()->sequence()->terminal('"')->identifier("character")->loop()->identifier("character")->end()->terminal('"')->end()->end()->end()->rule("character")->choice()->terminal("a")->terminal("b")->terminal("c")->terminal("d")->terminal("e")->terminal("f")->terminal("g")->terminal("h")->terminal("i")->terminal("j")->terminal("k")->terminal("l")->terminal("m")->terminal("n")->terminal("o")->terminal("p")->terminal("q")->terminal("r")->terminal("s")->terminal("t")->terminal("u")->terminal("v")->terminal("w")->terminal("x")->terminal("y")->terminal("z")->terminal("A")->terminal("B")->terminal("C")->terminal("D")->terminal("E")->terminal("F")->terminal("G")->terminal("H")->terminal("I")->terminal("J")->terminal("K")->terminal("L")->terminal("M")->terminal("N")->terminal("O")->terminal("P")->terminal("Q")->terminal("R")->terminal("S")->terminal("T")->terminal("U")->terminal("V")->terminal("W")->terminal("X")->terminal("Y")->terminal("Z")->end()->end();
     $this->assertEquivalentSyntax($builder->getAst(), $p->getAst());
 }