diff --git a/src/Environment.php b/src/Environment.php index ead76e3707f..8b122c5a150 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -56,6 +56,8 @@ class Environment private $autoReload; private $cache; private $lexer; + /** @var array */ + private $preLexers = []; private $parser; private $compiler; /** @var array */ @@ -488,6 +490,11 @@ public function resolveTemplate($names): TemplateWrapper throw new LoaderError(\sprintf('Unable to find one of the following templates: "%s".', implode('", "', $names))); } + public function addPreLexer(PreLexerInterface $preLexer): void + { + $this->preLexers[] = $preLexer; + } + public function setLexer(Lexer $lexer) { $this->lexer = $lexer; @@ -502,6 +509,10 @@ public function tokenize(Source $source): TokenStream $this->lexer = new Lexer($this); } + foreach ($this->preLexers as $preLexer) { + $source = $preLexer->preLex($source); + } + return $this->lexer->tokenize($source); } diff --git a/src/PreLexerInterface.php b/src/PreLexerInterface.php new file mode 100644 index 00000000000..b199fd4aad1 --- /dev/null +++ b/src/PreLexerInterface.php @@ -0,0 +1,8 @@ +