SimpleLexer::addSpecialPattern PHP Method

addSpecialPattern() public method

Acts as an entry and exit pattern in one go, effectively calling a special parser handler for this token only.
public addSpecialPattern ( string $pattern, string $mode, string $special )
$pattern string Perl style regex, but ( and ) lose the usual meaning.
$mode string Should only apply this pattern when dealing with this type of input.
$special string Use this mode for this one token.
    public function addSpecialPattern($pattern, $mode, $special)
    {
        if (!isset($this->regexes[$mode])) {
            $this->regexes[$mode] = new ParallelRegex($this->case);
        }
        $this->regexes[$mode]->addPattern($pattern, "_{$special}");
        if (!isset($this->mode_handlers[$special])) {
            $this->mode_handlers[$special] = $special;
        }
    }

Usage Example

Example #1
0
 function testSingular()
 {
     $handler = new MockTestParser();
     $handler->setReturnValue("a", true);
     $handler->setReturnValue("b", true);
     $handler->expectAt(0, "a", array("aa", LEXER_MATCHED));
     $handler->expectAt(1, "a", array("aa", LEXER_MATCHED));
     $handler->expectAt(2, "a", array("xx", LEXER_UNMATCHED));
     $handler->expectAt(3, "a", array("xx", LEXER_UNMATCHED));
     $handler->expectAt(0, "b", array("b", LEXER_SPECIAL));
     $handler->expectAt(1, "b", array("bbb", LEXER_SPECIAL));
     $handler->expectCallCount("a", 4);
     $handler->expectCallCount("b", 2);
     $lexer = new SimpleLexer($handler, "a");
     $lexer->addPattern("a+", "a");
     $lexer->addSpecialPattern("b+", "a", "b");
     $this->assertTrue($lexer->parse("aabaaxxbbbxx"));
 }
All Usage Examples Of SimpleLexer::addSpecialPattern