1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-03-19 21:10:15 +02:00

Implemented a weird little Pygments rule that I missed.

> If the RegexLexer encounters a newline that is flagged as an error
> token, the stack is emptied and the lexer continues scanning in the
> 'root' state. This can help producing error-tolerant highlighting for
> erroneous input, e.g. when a single-line string is not closed.

Fixes .
This commit is contained in:
Alec Thomas 2019-04-22 18:22:58 +10:00
parent 26f03cb449
commit 2105c68ed2

@ -241,6 +241,7 @@ type LexerState struct {
// Custum context for mutators. // Custum context for mutators.
MutatorContext map[interface{}]interface{} MutatorContext map[interface{}]interface{}
iteratorStack []Iterator iteratorStack []Iterator
options *TokeniseOptions
} }
// Set mutator context. // Set mutator context.
@ -278,6 +279,16 @@ func (l *LexerState) Iterator() Token {
ruleIndex, rule, groups := matchRules(l.Text[l.Pos:], selectedRule) ruleIndex, rule, groups := matchRules(l.Text[l.Pos:], selectedRule)
// No match. // No match.
if groups == nil { if groups == nil {
// From Pygments :\
//
// If the RegexLexer encounters a newline that is flagged as an error token, the stack is
// emptied and the lexer continues scanning in the 'root' state. This can help producing
// error-tolerant highlighting for erroneous input, e.g. when a single-line string is not
// closed.
if l.Text[l.Pos] == '\n' && l.State != l.options.State {
l.Stack = []string{l.options.State}
continue
}
l.Pos++ l.Pos++
return Token{Error, string(l.Text[l.Pos-1 : l.Pos])} return Token{Error, string(l.Text[l.Pos-1 : l.Pos])}
} }
@ -394,6 +405,7 @@ func (r *RegexLexer) Tokenise(options *TokeniseOptions, text string) (Iterator,
text += "\n" text += "\n"
} }
state := &LexerState{ state := &LexerState{
options: options,
Lexer: r, Lexer: r,
Text: []rune(text), Text: []rune(text),
Stack: []string{options.State}, Stack: []string{options.State},