diff --git a/lib/Doctrine/Common/Lexer/AbstractLexer.php b/lib/Doctrine/Common/Lexer/AbstractLexer.php index ed38b08..00cb1e1 100644 --- a/lib/Doctrine/Common/Lexer/AbstractLexer.php +++ b/lib/Doctrine/Common/Lexer/AbstractLexer.php @@ -67,6 +67,13 @@ abstract class AbstractLexer */ public $token; + /** + * Composed regex for input parsing. + * + * @var string + */ + private $regex; + /** * Sets the input data to be tokenized. * @@ -235,10 +242,8 @@ public function glimpse() */ protected function scan($input) { - static $regex; - - if (! isset($regex)) { - $regex = sprintf( + if (! isset($this->regex)) { + $this->regex = sprintf( '/(%s)|%s/%s', implode(')|(', $this->getCatchablePatterns()), implode('|', $this->getNonCatchablePatterns()), @@ -247,7 +252,7 @@ protected function scan($input) } $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE; - $matches = preg_split($regex, $input, -1, $flags); + $matches = preg_split($this->regex, $input, -1, $flags); if ($matches === false) { // Work around https://bugs.php.net/78122 diff --git a/tests/Doctrine/Common/Lexer/AbstractLexerTest.php b/tests/Doctrine/Common/Lexer/AbstractLexerTest.php index 4b777ad..d06927d 100644 --- a/tests/Doctrine/Common/Lexer/AbstractLexerTest.php +++ b/tests/Doctrine/Common/Lexer/AbstractLexerTest.php @@ -269,4 +269,21 @@ public function testIsA() $this->assertTrue($this->concreteLexer->isA('<', 'operator')); $this->assertTrue($this->concreteLexer->isA('fake_text', 'string')); } + + public function testAddCatchablePatternsToMutableLexer() + { + $mutableLexer = new MutableLexer(); + $mutableLexer->addCatchablePattern('[a-z]'); + $mutableLexer->setInput('one'); + $token = $mutableLexer->glimpse(); + + $this->assertEquals('o', $token['value']); + + $mutableLexer = new MutableLexer(); + $mutableLexer->addCatchablePattern('[a-z]+'); + $mutableLexer->setInput('one'); + $token = $mutableLexer->glimpse(); + + $this->assertEquals('one', $token['value']); + } } diff --git a/tests/Doctrine/Common/Lexer/MutableLexer.php b/tests/Doctrine/Common/Lexer/MutableLexer.php new file mode 100644 index 0000000..2fa27a2 --- /dev/null +++ b/tests/Doctrine/Common/Lexer/MutableLexer.php @@ -0,0 +1,33 @@ +catchablePatterns[] = $pattern; + } + + protected function getCatchablePatterns() + { + return $this->catchablePatterns; + } + + protected function getNonCatchablePatterns() + { + return ['[\s,]+']; + } + + protected function getType(&$value) + { + return 1; + } +}