@@ -156,21 +156,26 @@ SyntaxStateRestored SyntaxTokenizer::retrieveSyntaxState( const SyntaxDefinition
156
156
if ( state.state [0 ].state > 0 &&
157
157
( state.state [1 ].state > 0 ||
158
158
( ( curPattern = syntax.getPatternFromState ( state.state [0 ] ) ) &&
159
- curPattern->hasSyntax () ) ) ) {
159
+ curPattern->hasSyntaxOrContentScope () ) ) ) {
160
160
for ( size_t i = 0 ; i < MAX_SUB_SYNTAXS - 1 ; ++i ) {
161
161
if ( i != 0 || curPattern == nullptr )
162
162
curPattern = syntaxState.currentSyntax ->getPatternFromState ( state.state [i] );
163
163
if ( curPattern && state.state [i].state != SYNTAX_TOKENIZER_STATE_NONE ) {
164
- if ( curPattern->hasSyntax () ) {
164
+ if ( curPattern->hasSyntaxOrContentScope () ) {
165
165
syntaxState.subsyntaxInfo = curPattern;
166
166
auto langIndex = state.langStack [i];
167
167
syntaxState.currentSyntax =
168
168
langIndex != 0
169
169
? &SyntaxDefinitionManager::instance ()->getByLanguageIndex ( langIndex )
170
170
: &SyntaxDefinitionManager::instance ()->getByLanguageName (
171
171
syntaxState.subsyntaxInfo ->syntax );
172
- syntaxState.currentPatternIdx = {};
173
- syntaxState.currentLevel ++;
172
+ if ( curPattern->hasContentScope () ) {
173
+ syntaxState.currentPatternIdx = state.state [i];
174
+ syntaxState.currentLevel = i;
175
+ } else {
176
+ syntaxState.currentPatternIdx = {};
177
+ syntaxState.currentLevel ++;
178
+ }
174
179
} else {
175
180
syntaxState.currentPatternIdx = state.state [i];
176
181
}
@@ -186,33 +191,65 @@ static inline void setSubsyntaxPatternIdx( SyntaxStateRestored& curState, Syntax
186
191
const SyntaxStateType& patternIndex ) {
187
192
curState.currentPatternIdx = patternIndex;
188
193
retState.state [curState.currentLevel ] = patternIndex;
189
- };
194
+ }
190
195
191
- static inline void pushSubsyntax ( SyntaxStateRestored& curState, SyntaxState& retState,
192
- const SyntaxPattern& enteringSubsyntax,
193
- const SyntaxStateType& patternIndex,
194
- std::string_view patternTextStr ) {
196
+ static inline void pushStack ( SyntaxStateRestored& curState, SyntaxState& retState,
197
+ const SyntaxPattern& enteringSubsyntax,
198
+ const SyntaxStateType& patternIndex,
199
+ std::string_view patternTextStr ) {
195
200
if ( curState.currentLevel == MAX_SUB_SYNTAXS - 1 )
196
201
return ;
202
+
203
+ if ( !enteringSubsyntax.hasSyntax () ) {
204
+ if ( retState.langStack [curState.currentLevel ] != 0 ||
205
+ retState.state [curState.currentLevel ].state != 0 )
206
+ curState.currentLevel ++;
207
+ curState.subsyntaxInfo = &enteringSubsyntax;
208
+ retState.langStack [curState.currentLevel ] = curState.currentSyntax ->getLanguageIndex ();
209
+ setSubsyntaxPatternIdx ( curState, retState, patternIndex );
210
+ return ;
211
+ }
212
+
197
213
setSubsyntaxPatternIdx ( curState, retState, patternIndex );
214
+
198
215
curState.subsyntaxInfo = &enteringSubsyntax;
199
- curState.currentSyntax = &SyntaxDefinitionManager::instance ()->getByLanguageName (
200
- curState.subsyntaxInfo ->dynSyntax
201
- ? curState.subsyntaxInfo ->dynSyntax ( enteringSubsyntax, patternTextStr )
202
- : curState.subsyntaxInfo ->syntax );
216
+ curState.currentSyntax =
217
+ !enteringSubsyntax.hasSyntax ()
218
+ ? curState.currentSyntax
219
+ : ( &SyntaxDefinitionManager::instance ()->getByLanguageName (
220
+ curState.subsyntaxInfo ->dynSyntax
221
+ ? curState.subsyntaxInfo ->dynSyntax ( enteringSubsyntax, patternTextStr )
222
+ : curState.subsyntaxInfo ->syntax ) );
223
+
203
224
retState.langStack [curState.currentLevel ] = curState.currentSyntax ->getLanguageIndex ();
204
225
curState.currentLevel ++;
226
+
205
227
setSubsyntaxPatternIdx ( curState, retState, SyntaxStateType{} );
206
- };
228
+ }
229
+
230
+ static inline void popStack ( SyntaxStateRestored& curState, SyntaxState& retState,
231
+ const SyntaxDefinition& syntax, const SyntaxPattern& fromPattern ) {
232
+ if ( curState.currentLevel == 0 && retState.state [0 ].state == SYNTAX_TOKENIZER_STATE_NONE ) {
233
+ Log::debug ( " Attempted to pop base stack level or already at an empty base." );
234
+ return ;
235
+ }
207
236
208
- static inline void popSubsyntax ( SyntaxStateRestored& curState, SyntaxState& retState,
209
- const SyntaxDefinition& syntax ) {
210
237
setSubsyntaxPatternIdx ( curState, retState, SyntaxStateType{} );
238
+
239
+ if ( fromPattern.isSimpleRangedMatch () )
240
+ return ;
241
+
211
242
retState.langStack [curState.currentLevel ] = 0 ;
212
- curState.currentLevel --;
213
- setSubsyntaxPatternIdx ( curState, retState, SyntaxStateType{} );
243
+ if ( curState.currentLevel > 0 )
244
+ curState.currentLevel --;
245
+
246
+ if ( retState.langStack [curState.currentLevel ] != 0 &&
247
+ retState.langStack [curState.currentLevel ] != syntax.getLanguageIndex () ) {
248
+ setSubsyntaxPatternIdx ( curState, retState, SyntaxStateType{} );
249
+ }
250
+
214
251
curState = SyntaxTokenizer::retrieveSyntaxState ( syntax, retState );
215
- };
252
+ }
216
253
217
254
template <typename T>
218
255
static inline void pushTokensToOpenCloseSubsyntax ( int i, std::string_view textv,
@@ -313,7 +350,7 @@ _tokenize( const SyntaxDefinition& syntax, const std::string& text, const Syntax
313
350
pushTokensToOpenCloseSubsyntax ( startIdx, textv, curState.subsyntaxInfo ,
314
351
*shouldCloseSubSyntax, tokens );
315
352
}
316
- popSubsyntax ( curState, retState, syntax );
353
+ popStack ( curState, retState, syntax, *curState. subsyntaxInfo );
317
354
startIdx = shouldCloseSubSyntax->range .second ;
318
355
shouldCloseSubSyntax = {};
319
356
return true ;
@@ -385,18 +422,15 @@ _tokenize( const SyntaxDefinition& syntax, const std::string& text, const Syntax
385
422
}
386
423
}
387
424
388
- if ( !( skipSubSyntaxSeparator && pattern.hasSyntax () ) )
425
+ if ( !( skipSubSyntaxSeparator && pattern.hasSyntaxOrContentScope () ) )
389
426
pushToken ( tokens, currentType, segmentText );
390
427
391
428
currentBytePos = segmentEndBytePos;
392
429
}
393
430
394
- if ( pattern.hasSyntax () ) {
395
- pushSubsyntax (
396
- curState, retState, pattern, patternIndex,
397
- textv.substr ( fullMatchStart, fullMatchEnd - fullMatchStart ) );
398
- } else if ( pattern.patterns .size () > 1 ) {
399
- setSubsyntaxPatternIdx ( curState, retState, patternIndex );
431
+ if ( pattern.isRangedMatch () ) {
432
+ pushStack ( curState, retState, pattern, patternIndex,
433
+ textv.substr ( fullMatchStart, fullMatchEnd - fullMatchStart ) );
400
434
}
401
435
402
436
startIdx = fullMatchEnd;
@@ -438,7 +472,7 @@ _tokenize( const SyntaxDefinition& syntax, const std::string& text, const Syntax
438
472
( patternTextStr = patternText ) )
439
473
: SyntaxStyleEmpty ();
440
474
441
- if ( !skipSubSyntaxSeparator || !pattern.hasSyntax () ) {
475
+ if ( !skipSubSyntaxSeparator || !pattern.hasSyntaxOrContentScope () ) {
442
476
pushToken ( tokens,
443
477
type == SyntaxStyleEmpty ()
444
478
? ( curMatch < pattern.types .size ()
@@ -448,13 +482,11 @@ _tokenize( const SyntaxDefinition& syntax, const std::string& text, const Syntax
448
482
patternText );
449
483
}
450
484
451
- if ( pattern.hasSyntax () && curMatch == numMatches - 1 &&
485
+ if ( pattern.isRangedMatch () && curMatch == numMatches - 1 &&
452
486
end == fullMatchEnd ) {
453
- pushSubsyntax (
487
+ pushStack (
454
488
curState, retState, pattern, patternIndex,
455
489
textv.substr ( fullMatchStart, fullMatchEnd - fullMatchStart ) );
456
- } else if ( pattern.patterns .size () > 1 ) {
457
- setSubsyntaxPatternIdx ( curState, retState, patternIndex );
458
490
}
459
491
460
492
startIdx = end;
@@ -464,8 +496,8 @@ _tokenize( const SyntaxDefinition& syntax, const std::string& text, const Syntax
464
496
textv.substr ( end, fullMatchEnd - end ) );
465
497
startIdx = fullMatchEnd;
466
498
467
- if ( pattern.hasSyntax () && curMatch == numMatches - 1 ) {
468
- pushSubsyntax (
499
+ if ( pattern.isRangedMatch () && curMatch == numMatches - 1 ) {
500
+ pushStack (
469
501
curState, retState, pattern, patternIndex,
470
502
textv.substr ( fullMatchStart, fullMatchEnd - fullMatchStart ) );
471
503
}
@@ -503,15 +535,15 @@ _tokenize( const SyntaxDefinition& syntax, const std::string& text, const Syntax
503
535
? curState.currentSyntax ->getSymbol ( ( patternTextStr = patternText ) )
504
536
: SyntaxStyleEmpty ();
505
537
506
- if ( !skipSubSyntaxSeparator || !pattern.hasSyntax () ) {
538
+ if ( !skipSubSyntaxSeparator || !pattern.hasSyntaxOrContentScope () ) {
507
539
pushToken ( tokens, type == SyntaxStyleEmpty () ? pattern.types [0 ] : type,
508
540
patternText );
509
541
}
510
- if ( pattern.hasSyntax () ) {
511
- pushSubsyntax ( curState, retState, pattern, patternIndex, patternText );
512
- } else if ( pattern.patterns .size () > 1 ) {
513
- setSubsyntaxPatternIdx ( curState, retState, patternIndex );
542
+
543
+ if ( pattern.isRangedMatch () ) {
544
+ pushStack ( curState, retState, pattern, patternIndex, patternText );
514
545
}
546
+
515
547
startIdx = end;
516
548
return true ;
517
549
}
@@ -552,7 +584,7 @@ _tokenize( const SyntaxDefinition& syntax, const std::string& text, const Syntax
552
584
if ( endRange.range .first == static_cast <Int64>( startIdx ) ) {
553
585
pushTokensToOpenCloseSubsyntax ( startIdx, textv, activePattern, endRange,
554
586
tokens, true );
555
- setSubsyntaxPatternIdx ( curState, retState, SyntaxStateType{} );
587
+ popStack ( curState, retState, syntax, *activePattern );
556
588
startIdx = endRange.range .second ;
557
589
continue ;
558
590
}
@@ -646,7 +678,7 @@ _tokenize( const SyntaxDefinition& syntax, const std::string& text, const Syntax
646
678
pushTokensToOpenCloseSubsyntax ( startIdx, textv, curState.subsyntaxInfo ,
647
679
rangeSubsyntax, tokens, true );
648
680
}
649
- popSubsyntax ( curState, retState, syntax );
681
+ popStack ( curState, retState, syntax, *curState. subsyntaxInfo );
650
682
startIdx = rangeSubsyntax.range .second ;
651
683
skip = true ;
652
684
}
@@ -656,7 +688,7 @@ _tokenize( const SyntaxDefinition& syntax, const std::string& text, const Syntax
656
688
if ( endRange.range .first != -1 ) {
657
689
pushTokensToOpenCloseSubsyntax ( startIdx, textv, activePattern, endRange,
658
690
tokens, true );
659
- setSubsyntaxPatternIdx ( curState, retState, SyntaxStateType{} );
691
+ popStack ( curState, retState, syntax, *activePattern );
660
692
startIdx = endRange.range .second ;
661
693
continue ;
662
694
} else {
@@ -722,7 +754,7 @@ _tokenize( const SyntaxDefinition& syntax, const std::string& text, const Syntax
722
754
pushTokensToOpenCloseSubsyntax ( startIdx, textv, curState.subsyntaxInfo ,
723
755
*shouldCloseSubSyntax, tokens );
724
756
}
725
- popSubsyntax ( curState, retState, syntax );
757
+ popStack ( curState, retState, syntax, *curState. subsyntaxInfo );
726
758
startIdx = shouldCloseSubSyntax->range .second ;
727
759
matched = true ;
728
760
shouldCloseSubSyntax = {};
0 commit comments