11#include < eepp/system/log.hpp>
22#include < eepp/system/luapattern.hpp>
3+ #include < eepp/system/regex.hpp>
34#include < eepp/ui/doc/syntaxdefinitionmanager.hpp>
45#include < eepp/ui/doc/syntaxtokenizer.hpp>
6+ #include < variant>
57
68using namespace EE ::System;
79
@@ -84,7 +86,8 @@ static void pushToken( std::vector<T>& tokens, const SyntaxStyleType& type,
8486 }
8587}
8688
87- bool isScaped ( const std::string& text, const size_t & startIndex, const std::string& escapeStr ) {
89+ static bool isScaped ( const std::string& text, const size_t & startIndex,
90+ const std::string& escapeStr ) {
8891 char escapeByte = escapeStr.empty () ? ' \\ ' : escapeStr[0 ];
8992 int count = 0 ;
9093 for ( int i = startIndex - 1 ; i >= 0 ; i-- ) {
@@ -95,12 +98,17 @@ bool isScaped( const std::string& text, const size_t& startIndex, const std::str
9598 return count % 2 == 1 ;
9699}
97100
98- std::pair<int , int > findNonEscaped ( const std::string& text, const std::string& pattern, int offset,
99- const std::string& escapeStr ) {
101+ static std::pair<int , int > findNonEscaped ( const std::string& text, const std::string& pattern,
102+ int offset, const std::string& escapeStr,
103+ bool isRegEx ) {
100104 eeASSERT ( !pattern.empty () );
101105 if ( pattern.empty () )
102106 return std::make_pair ( -1 , -1 );
103- LuaPattern words ( pattern );
107+ std::variant<RegEx, LuaPattern> wordsVar =
108+ isRegEx ? std::variant<RegEx, LuaPattern>( RegEx ( pattern ) )
109+ : std::variant<RegEx, LuaPattern>( LuaPattern ( pattern ) );
110+ PatternMatcher& words =
111+ std::visit ( []( auto & patternType ) -> PatternMatcher& { return patternType; }, wordsVar );
104112 int start, end;
105113 while ( words.find ( text, start, end, offset ) ) {
106114 if ( !escapeStr.empty () && isScaped ( text, start, escapeStr ) ) {
@@ -202,9 +210,9 @@ _tokenize( const SyntaxDefinition& syntax, const std::string& text, const Syntax
202210 if ( curState.currentPatternIdx != SYNTAX_TOKENIZER_STATE_NONE ) {
203211 const SyntaxPattern& pattern =
204212 curState.currentSyntax ->getPatterns ()[curState.currentPatternIdx - 1 ];
205- std::pair<int , int > range =
206- findNonEscaped ( text, pattern.patterns [1 ], i,
207- pattern.patterns .size () >= 3 ? pattern.patterns [2 ] : " " );
213+ std::pair<int , int > range = findNonEscaped (
214+ text, pattern.patterns [1 ], i,
215+ pattern.patterns .size () >= 3 ? pattern.patterns [2 ] : " " , pattern. isRegEx );
208216
209217 bool skip = false ;
210218
@@ -213,7 +221,8 @@ _tokenize( const SyntaxDefinition& syntax, const std::string& text, const Syntax
213221 findNonEscaped ( text, curState.subsyntaxInfo ->patterns [1 ], i,
214222 curState.subsyntaxInfo ->patterns .size () >= 3
215223 ? curState.subsyntaxInfo ->patterns [2 ]
216- : " " );
224+ : " " ,
225+ pattern.isRegEx );
217226
218227 if ( rangeSubsyntax.first != -1 &&
219228 ( range.first == -1 || rangeSubsyntax.first < range.first ) ) {
@@ -249,7 +258,8 @@ _tokenize( const SyntaxDefinition& syntax, const std::string& text, const Syntax
249258 std::pair<int , int > rangeSubsyntax = findNonEscaped (
250259 text, " ^" + curState.subsyntaxInfo ->patterns [1 ], i,
251260 curState.subsyntaxInfo ->patterns .size () >= 3 ? curState.subsyntaxInfo ->patterns [2 ]
252- : " " );
261+ : " " ,
262+ curState.subsyntaxInfo ->isRegEx );
253263
254264 if ( rangeSubsyntax.first != -1 ) {
255265 if ( !skipSubSyntaxSeparator ) {
@@ -270,7 +280,13 @@ _tokenize( const SyntaxDefinition& syntax, const std::string& text, const Syntax
270280 continue ;
271281 patternStr =
272282 pattern.patterns [0 ][0 ] == ' ^' ? pattern.patterns [0 ] : " ^" + pattern.patterns [0 ];
273- LuaPattern words ( patternStr );
283+ std::variant<RegEx, LuaPattern> wordsVar =
284+ pattern.isRegEx ? std::variant<RegEx, LuaPattern>( RegEx ( patternStr ) )
285+ : std::variant<RegEx, LuaPattern>( LuaPattern ( patternStr ) );
286+ PatternMatcher& words = std::visit (
287+ []( auto & patternType ) -> PatternMatcher& { return patternType; }, wordsVar );
288+ if ( !words.isValid () ) // Skip invalid patterns
289+ continue ;
274290 if ( words.matches ( text, matches, i ) && ( numMatches = words.getNumMatches () ) > 0 ) {
275291 if ( numMatches > 1 ) {
276292 int patternMatchStart = matches[0 ].start ;
0 commit comments