mirror of
https://github.com/alecthomas/chroma.git
synced 2025-07-05 00:38:52 +02:00
Add ByGroupNames function, same as ByGroups but use named groups (#519)
For named groups that are not given, an Error will be emitted anyway. This also handles the case when an Emitter for group `0` is provided or not. Since numbers can also be used for names. But it might be over-doing, because why would anyone use ByGroupNames if they wanted to assign a token to the whole match?!
This commit is contained in:
committed by
GitHub
parent
22cbca546a
commit
10329f849e
@ -99,3 +99,88 @@ func TestEnsureLFFunc(t *testing.T) {
|
||||
assert.Equal(t, out, test.out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestByGroupNames(t *testing.T) {
|
||||
l := Coalesce(MustNewLexer(nil, Rules{ // nolint: forbidigo
|
||||
"root": {
|
||||
{
|
||||
`(?<key>\w+)(?<operator>=)(?<value>\w+)`,
|
||||
ByGroupNames(map[string]Emitter{
|
||||
`key`: String,
|
||||
`operator`: Operator,
|
||||
`value`: String,
|
||||
}),
|
||||
nil,
|
||||
},
|
||||
},
|
||||
}))
|
||||
it, err := l.Tokenise(nil, `abc=123`)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []Token{{String, `abc`}, {Operator, `=`}, {String, `123`}}, it.Tokens())
|
||||
|
||||
l = Coalesce(MustNewLexer(nil, Rules{ // nolint: forbidigo
|
||||
"root": {
|
||||
{
|
||||
`(?<key>\w+)(?<operator>=)(?<value>\w+)`,
|
||||
ByGroupNames(map[string]Emitter{
|
||||
`key`: String,
|
||||
`value`: String,
|
||||
}),
|
||||
nil,
|
||||
},
|
||||
},
|
||||
}))
|
||||
it, err = l.Tokenise(nil, `abc=123`)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []Token{{String, `abc`}, {Error, `=`}, {String, `123`}}, it.Tokens())
|
||||
|
||||
l = Coalesce(MustNewLexer(nil, Rules{ // nolint: forbidigo
|
||||
"root": {
|
||||
{
|
||||
`(?<key>\w+)=(?<value>\w+)`,
|
||||
ByGroupNames(map[string]Emitter{
|
||||
`key`: String,
|
||||
`value`: String,
|
||||
}),
|
||||
nil,
|
||||
},
|
||||
},
|
||||
}))
|
||||
it, err = l.Tokenise(nil, `abc=123`)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []Token{{String, `abc123`}}, it.Tokens())
|
||||
|
||||
l = Coalesce(MustNewLexer(nil, Rules{ // nolint: forbidigo
|
||||
"root": {
|
||||
{
|
||||
`(?<key>\w+)(?<op>=)(?<value>\w+)`,
|
||||
ByGroupNames(map[string]Emitter{
|
||||
`key`: String,
|
||||
`operator`: Operator,
|
||||
`value`: String,
|
||||
}),
|
||||
nil,
|
||||
},
|
||||
},
|
||||
}))
|
||||
it, err = l.Tokenise(nil, `abc=123`)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []Token{{String, `abc`}, {Error, `=`}, {String, `123`}}, it.Tokens())
|
||||
|
||||
l = Coalesce(MustNewLexer(nil, Rules{ // nolint: forbidigo
|
||||
"root": {
|
||||
{
|
||||
`\w+=\w+`,
|
||||
ByGroupNames(map[string]Emitter{
|
||||
`key`: String,
|
||||
`operator`: Operator,
|
||||
`value`: String,
|
||||
}),
|
||||
nil,
|
||||
},
|
||||
},
|
||||
}))
|
||||
it, err = l.Tokenise(nil, `abc=123`)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []Token{{Error, `abc=123`}}, it.Tokens())
|
||||
}
|
||||
|
Reference in New Issue
Block a user