From 0a055ec758a6b8f2879230df73baab43cc107598 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Fri, 19 Jul 2024 10:31:51 +0200 Subject: [PATCH] Add preLexer --- src/Environment.php | 11 +++++++++++ src/PreLexerInterface.php | 8 ++++++++ 2 files changed, 19 insertions(+) create mode 100644 src/PreLexerInterface.php 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 @@ +