mirror of
https://github.com/alecthomas/chroma.git
synced 2025-11-27 22:38:42 +02:00
Add support for named capture groups
This commit is contained in:
committed by
Alec Thomas
parent
15f2498398
commit
dcfd826b25
15
regexp.go
15
regexp.go
@@ -258,6 +258,8 @@ type LexerState struct {
|
|||||||
Rule int
|
Rule int
|
||||||
// Group matches.
|
// Group matches.
|
||||||
Groups []string
|
Groups []string
|
||||||
|
// Named Group matches.
|
||||||
|
NamedGroups map[string]string
|
||||||
// Custum context for mutators.
|
// Custum context for mutators.
|
||||||
MutatorContext map[interface{}]interface{}
|
MutatorContext map[interface{}]interface{}
|
||||||
iteratorStack []Iterator
|
iteratorStack []Iterator
|
||||||
@@ -301,7 +303,7 @@ func (l *LexerState) Iterator() Token { // nolint: gocognit
|
|||||||
if !ok {
|
if !ok {
|
||||||
panic("unknown state " + l.State)
|
panic("unknown state " + l.State)
|
||||||
}
|
}
|
||||||
ruleIndex, rule, groups := matchRules(l.Text, l.Pos, selectedRule)
|
ruleIndex, rule, groups, namedGroups := matchRules(l.Text, l.Pos, selectedRule)
|
||||||
// No match.
|
// No match.
|
||||||
if groups == nil {
|
if groups == nil {
|
||||||
// From Pygments :\
|
// From Pygments :\
|
||||||
@@ -319,6 +321,7 @@ func (l *LexerState) Iterator() Token { // nolint: gocognit
|
|||||||
}
|
}
|
||||||
l.Rule = ruleIndex
|
l.Rule = ruleIndex
|
||||||
l.Groups = groups
|
l.Groups = groups
|
||||||
|
l.NamedGroups = namedGroups
|
||||||
l.Pos += utf8.RuneCountInString(groups[0])
|
l.Pos += utf8.RuneCountInString(groups[0])
|
||||||
if rule.Mutator != nil {
|
if rule.Mutator != nil {
|
||||||
if err := rule.Mutator.Mutate(l); err != nil {
|
if err := rule.Mutator.Mutate(l); err != nil {
|
||||||
@@ -490,18 +493,22 @@ func (r *RegexLexer) Tokenise(options *TokeniseOptions, text string) (Iterator,
|
|||||||
return state.Iterator, nil
|
return state.Iterator, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func matchRules(text []rune, pos int, rules []*CompiledRule) (int, *CompiledRule, []string) {
|
func matchRules(text []rune, pos int, rules []*CompiledRule) (int, *CompiledRule, []string, map[string]string) {
|
||||||
for i, rule := range rules {
|
for i, rule := range rules {
|
||||||
match, err := rule.Regexp.FindRunesMatchStartingAt(text, pos)
|
match, err := rule.Regexp.FindRunesMatchStartingAt(text, pos)
|
||||||
if match != nil && err == nil && match.Index == pos {
|
if match != nil && err == nil && match.Index == pos {
|
||||||
groups := []string{}
|
groups := []string{}
|
||||||
|
namedGroups := map[string]string{}
|
||||||
for _, g := range match.Groups() {
|
for _, g := range match.Groups() {
|
||||||
|
if g.Name != `` {
|
||||||
|
namedGroups[g.Name] = g.String()
|
||||||
|
}
|
||||||
groups = append(groups, g.String())
|
groups = append(groups, g.String())
|
||||||
}
|
}
|
||||||
return i, rule, groups
|
return i, rule, groups, namedGroups
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0, &CompiledRule{}, nil
|
return 0, &CompiledRule{}, nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// replace \r and \r\n with \n
|
// replace \r and \r\n with \n
|
||||||
|
|||||||
Reference in New Issue
Block a user