@@ -258,6 +258,8 @@ type LexerState struct {
258
258
Rule int
259
259
// Group matches.
260
260
Groups []string
261
+ // Named Group matches.
262
+ NamedGroups map [string ]string
261
263
// Custum context for mutators.
262
264
MutatorContext map [interface {}]interface {}
263
265
iteratorStack []Iterator
@@ -301,7 +303,7 @@ func (l *LexerState) Iterator() Token { // nolint: gocognit
301
303
if ! ok {
302
304
panic ("unknown state " + l .State )
303
305
}
304
- ruleIndex , rule , groups := matchRules (l .Text , l .Pos , selectedRule )
306
+ ruleIndex , rule , groups , namedGroups := matchRules (l .Text , l .Pos , selectedRule )
305
307
// No match.
306
308
if groups == nil {
307
309
// From Pygments :\
@@ -319,6 +321,7 @@ func (l *LexerState) Iterator() Token { // nolint: gocognit
319
321
}
320
322
l .Rule = ruleIndex
321
323
l .Groups = groups
324
+ l .NamedGroups = namedGroups
322
325
l .Pos += utf8 .RuneCountInString (groups [0 ])
323
326
if rule .Mutator != nil {
324
327
if err := rule .Mutator .Mutate (l ); err != nil {
@@ -490,18 +493,22 @@ func (r *RegexLexer) Tokenise(options *TokeniseOptions, text string) (Iterator,
490
493
return state .Iterator , nil
491
494
}
492
495
493
- func matchRules (text []rune , pos int , rules []* CompiledRule ) (int , * CompiledRule , []string ) {
496
+ func matchRules (text []rune , pos int , rules []* CompiledRule ) (int , * CompiledRule , []string , map [ string ] string ) {
494
497
for i , rule := range rules {
495
498
match , err := rule .Regexp .FindRunesMatchStartingAt (text , pos )
496
499
if match != nil && err == nil && match .Index == pos {
497
500
groups := []string {}
501
+ namedGroups := map [string ]string {}
498
502
for _ , g := range match .Groups () {
503
+ if g .Name != `` {
504
+ namedGroups [g .Name ] = g .String ()
505
+ }
499
506
groups = append (groups , g .String ())
500
507
}
501
- return i , rule , groups
508
+ return i , rule , groups , namedGroups
502
509
}
503
510
}
504
- return 0 , & CompiledRule {}, nil
511
+ return 0 , & CompiledRule {}, nil , nil
505
512
}
506
513
507
514
// replace \r and \r\n with \n
0 commit comments