示例#1
0
    function it_should_create_a_literal_from_a_node()
    {
        $literalNode = new LiteralNode('"foo"', false);
        $literalCode = <<<EOS
if (substr(\$this->string, \$this->position, strlen("foo")) === "foo") {
    \$_success = true;
    \$this->value = substr(\$this->string, \$this->position, strlen("foo"));
    \$this->position += strlen("foo");
} else {
    \$_success = false;

    \$this->report(\$this->position, '"foo"');
}
EOS;
        $literalNode->accept($this->getWrappedObject());
        $this->getResult()->shouldBe($literalCode);
        $literalNode = new LiteralNode('"foo"', true);
        $literalCode = <<<EOS
if (strtolower(substr(\$this->string, \$this->position, strlen("foo"))) === strtolower("foo")) {
    \$_success = true;
    \$this->value = substr(\$this->string, \$this->position, strlen("foo"));
    \$this->position += strlen("foo");
} else {
    \$_success = false;

    \$this->report(\$this->position, '"foo"');
}
EOS;
        $literalNode->accept($this->getWrappedObject());
        $this->getResult()->shouldBe($literalCode);
        $literalNode = new LiteralNode('"\\n"', false);
        $literalCode = <<<EOS
if (substr(\$this->string, \$this->position, strlen("\\n")) === "\\n") {
    \$_success = true;
    \$this->value = substr(\$this->string, \$this->position, strlen("\\n"));
    \$this->position += strlen("\\n");
} else {
    \$_success = false;

    \$this->report(\$this->position, '"\\\\n"');
}
EOS;
        $literalNode->accept($this->getWrappedObject());
        $this->getResult()->shouldBe($literalCode);
    }
示例#2
0
    public function visitLiteral(LiteralNode $node)
    {
        $expecting = var_export($node->getString(), true);
        $match = "substr(\$this->string, \$this->position, strlen({$node->getString()}))";
        if ($node->isCaseInsensitive()) {
            $cond = "strtolower({$match}) === strtolower({$node->getString()})";
        } else {
            $cond = "{$match} === {$node->getString()}";
        }
        $this->results[] = <<<EOS
if ({$cond}) {
    \$_success = true;
    \$this->value = {$match};
    \$this->position += strlen({$node->getString()});
} else {
    \$_success = false;

    \$this->report(\$this->position, {$expecting});
}
EOS;
    }