-
Notifications
You must be signed in to change notification settings - Fork 1
/
compile.go
45 lines (38 loc) · 897 Bytes
/
compile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package fantasyname
import (
"fmt"
"strings"
"github.com/s0rg/fantasyname/wrappers"
)
// Compile creates fmt.Stringer from given pattern, you can call it as many times as
// you wish, it will produce new name on every call.
func Compile(pattern string, opts ...Option) (rv fmt.Stringer, err error) {
if pattern = strings.TrimSpace(pattern); pattern == "" {
return nil, ErrEmptyStack
}
p := newParser(opts...)
for pos, r := range pattern {
switch r {
case '<':
p.OnGroupStart(false)
case '>':
err = p.OnGroupEnd(false)
case '(':
p.OnGroupStart(true)
case ')':
err = p.OnGroupEnd(true)
case '|':
err = p.OnGroupSplit()
case '!':
err = p.Wrap(wrappers.Capitalized)
case '~':
err = p.Wrap(wrappers.Reversed)
default:
err = p.OnSymbol(r)
}
if err != nil {
return nil, fmt.Errorf("pos: %d error: %w", pos, err)
}
}
return p.Build()
}