Ejemplo n.º 1
0
 function it_should_parse_extended_grammars()
 {
     $tree = new GrammarNode('ExtendedFile', array(new RuleNode('Foo', "'Foo'", new LiteralNode('"foo"', false))));
     $tree->setBase('BaseFile');
     $this->parse('grammar ExtendedFile extends BaseFile { Foo = "foo"; }')->shouldBeLike($tree);
 }
Ejemplo n.º 2
0
    function it_should_create_an_extended_grammar_from_a_node()
    {
        $grammarNode = new GrammarNode('FooFile', array(new RuleNode('Foo', '"Foo"', new RuleReferenceNode('Bar'))));
        $grammarNode->setNamespace('Acme\\Factory');
        $grammarNode->setImports(array('Acme\\FactoryInterface', 'Acme\\BaseFile'));
        $grammarNode->setBase('BaseFile');
        $grammarCode = <<<EOS
namespace Acme\\Factory;

use Acme\\FactoryInterface;
use Acme\\BaseFile;

class FooFile extends BaseFile
{
    protected function parseFoo()
    {
        \$_position = \$this->position;

        if (isset(\$this->cache['Foo'][\$_position])) {
            \$_success = \$this->cache['Foo'][\$_position]['success'];
            \$this->position = \$this->cache['Foo'][\$_position]['position'];
            \$this->value = \$this->cache['Foo'][\$_position]['value'];

            return \$_success;
        }

        \$_success = \$this->parseBar();

        \$this->cache['Foo'][\$_position] = array(
            'success' => \$_success,
            'position' => \$this->position,
            'value' => \$this->value
        );

        if (!\$_success) {
            \$this->report(\$_position, "Foo");
        }

        return \$_success;
    }
}
EOS;
        $grammarNode->accept($this->getWrappedObject());
        $this->getResult()->shouldBe($grammarCode);
    }
Ejemplo n.º 3
0
 protected function parseGrammar()
 {
     $_position = $this->position;
     if (isset($this->cache['Grammar'][$_position])) {
         $_success = $this->cache['Grammar'][$_position]['success'];
         $this->position = $this->cache['Grammar'][$_position]['position'];
         $this->value = $this->cache['Grammar'][$_position]['value'];
         return $_success;
     }
     $_value27 = array();
     if (substr($this->string, $this->position, strlen("grammar")) === "grammar") {
         $_success = true;
         $this->value = substr($this->string, $this->position, strlen("grammar"));
         $this->position += strlen("grammar");
     } else {
         $_success = false;
         $this->report($this->position, '"grammar"');
     }
     if ($_success) {
         $_value27[] = $this->value;
         $_success = true;
         $this->value = null;
         $this->cut = true;
     }
     if ($_success) {
         $_value27[] = $this->value;
         $_success = $this->parse_();
     }
     if ($_success) {
         $_value27[] = $this->value;
         $_success = $this->parseIdentifier();
         if ($_success) {
             $name = $this->value;
         }
     }
     if ($_success) {
         $_value27[] = $this->value;
         $_position18 = $this->position;
         $_cut19 = $this->cut;
         $this->cut = false;
         $_value17 = array();
         $_success = $this->parse_();
         if ($_success) {
             $_value17[] = $this->value;
             if (substr($this->string, $this->position, strlen("extends")) === "extends") {
                 $_success = true;
                 $this->value = substr($this->string, $this->position, strlen("extends"));
                 $this->position += strlen("extends");
             } else {
                 $_success = false;
                 $this->report($this->position, '"extends"');
             }
         }
         if ($_success) {
             $_value17[] = $this->value;
             $_success = $this->parse_();
         }
         if ($_success) {
             $_value17[] = $this->value;
             $_success = $this->parseIdentifier();
             if ($_success) {
                 $base = $this->value;
             }
         }
         if ($_success) {
             $_value17[] = $this->value;
             $this->value = $_value17;
         }
         if (!$_success && !$this->cut) {
             $_success = true;
             $this->position = $_position18;
             $this->value = null;
         }
         $this->cut = $_cut19;
     }
     if ($_success) {
         $_value27[] = $this->value;
         $_success = $this->parse_();
     }
     if ($_success) {
         $_value27[] = $this->value;
         if (substr($this->string, $this->position, strlen("{")) === "{") {
             $_success = true;
             $this->value = substr($this->string, $this->position, strlen("{"));
             $this->position += strlen("{");
         } else {
             $_success = false;
             $this->report($this->position, '"{"');
         }
     }
     if ($_success) {
         $_value27[] = $this->value;
         $_position21 = $this->position;
         $_cut22 = $this->cut;
         $this->cut = false;
         $_value20 = array();
         $_success = $this->parse_();
         if ($_success) {
             $_value20[] = $this->value;
             if (substr($this->string, $this->position, strlen("start")) === "start") {
                 $_success = true;
                 $this->value = substr($this->string, $this->position, strlen("start"));
                 $this->position += strlen("start");
             } else {
                 $_success = false;
                 $this->report($this->position, '"start"');
             }
         }
         if ($_success) {
             $_value20[] = $this->value;
             $_success = true;
             $this->value = null;
             $this->cut = true;
         }
         if ($_success) {
             $_value20[] = $this->value;
             $_success = $this->parse_();
         }
         if ($_success) {
             $_value20[] = $this->value;
             $_success = $this->parseRule();
             if ($_success) {
                 $startSymbol = $this->value;
             }
         }
         if ($_success) {
             $_value20[] = $this->value;
             $this->value = $_value20;
         }
         if (!$_success && !$this->cut) {
             $_success = true;
             $this->position = $_position21;
             $this->value = null;
         }
         $this->cut = $_cut22;
     }
     if ($_success) {
         $_value27[] = $this->value;
         $_value25 = array();
         $_cut26 = $this->cut;
         while (true) {
             $_position24 = $this->position;
             $this->cut = false;
             $_value23 = array();
             $_success = $this->parse_();
             if ($_success) {
                 $_value23[] = $this->value;
                 $_success = $this->parseRule();
                 if ($_success) {
                     $rule = $this->value;
                 }
             }
             if ($_success) {
                 $_value23[] = $this->value;
                 $this->value = $_value23;
             }
             if ($_success) {
                 $this->value = call_user_func(function () use(&$name, &$base, &$startSymbol, &$rule) {
                     return $rule;
                 });
             }
             if (!$_success) {
                 break;
             }
             $_value25[] = $this->value;
         }
         if (!$this->cut) {
             $_success = true;
             $this->position = $_position24;
             $this->value = $_value25;
         }
         $this->cut = $_cut26;
         if ($_success) {
             $rules = $this->value;
         }
     }
     if ($_success) {
         $_value27[] = $this->value;
         $_success = $this->parse_();
     }
     if ($_success) {
         $_value27[] = $this->value;
         if (substr($this->string, $this->position, strlen("}")) === "}") {
             $_success = true;
             $this->value = substr($this->string, $this->position, strlen("}"));
             $this->position += strlen("}");
         } else {
             $_success = false;
             $this->report($this->position, '"}"');
         }
     }
     if ($_success) {
         $_value27[] = $this->value;
         $this->value = $_value27;
     }
     if ($_success) {
         $this->value = call_user_func(function () use(&$name, &$base, &$startSymbol, &$rule, &$rules) {
             $rules = array_merge(isset($startSymbol) ? array($startSymbol) : array(), $rules);
             $grammar = new GrammarNode($name, $rules);
             if (isset($base)) {
                 $grammar->setBase($base);
             }
             if (isset($startSymbol)) {
                 $grammar->setStartSymbol($startSymbol->getIdentifier());
             }
             return $grammar;
         });
     }
     $this->cache['Grammar'][$_position] = array('success' => $_success, 'position' => $this->position, 'value' => $this->value);
     if (!$_success) {
         $this->report($_position, 'Grammar');
     }
     return $_success;
 }