forked from alecthomas/participle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct.go
111 lines (99 loc) · 2.44 KB
/
struct.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package participle
import (
"reflect"
"github.com/alecthomas/participle/lexer"
)
// A structLexer lexes over the tags of struct fields while tracking the current field.
type structLexer struct {
s reflect.Type
field int
indexes [][]int
lexer lexer.PeekingLexer
}
func lexStruct(s reflect.Type) *structLexer {
slex := &structLexer{
s: s,
indexes: collectFieldIndexes(s),
}
if len(slex.indexes) > 0 {
tag := fieldLexerTag(slex.Field().StructField)
slex.lexer = lexer.Upgrade(lexer.LexString(tag))
}
return slex
}
// NumField returns the number of fields in the struct associated with this structLexer.
func (s *structLexer) NumField() int {
return len(s.indexes)
}
type structLexerField struct {
reflect.StructField
Index []int
}
// Field returns the field associated with the current token.
func (s *structLexer) Field() structLexerField {
return s.GetField(s.field)
}
func (s *structLexer) GetField(field int) structLexerField {
if field >= len(s.indexes) {
field = len(s.indexes) - 1
}
return structLexerField{
StructField: s.s.FieldByIndex(s.indexes[field]),
Index: s.indexes[field],
}
}
func (s *structLexer) Peek() lexer.Token {
field := s.field
lex := s.lexer
for {
token := lex.Peek(0)
if !token.EOF() {
token.Pos.Line = field + 1
return token
}
field++
if field >= s.NumField() {
return lexer.EOFToken(token.Pos)
}
tag := fieldLexerTag(s.GetField(field).StructField)
lex = lexer.Upgrade(lexer.LexString(tag))
}
}
func (s *structLexer) Next() lexer.Token {
token := s.lexer.Next()
if !token.EOF() {
token.Pos.Line = s.field + 1
return token
}
if s.field+1 >= s.NumField() {
return lexer.EOFToken(token.Pos)
}
s.field++
tag := fieldLexerTag(s.Field().StructField)
s.lexer = lexer.Upgrade(lexer.LexString(tag))
return s.Next()
}
func fieldLexerTag(field reflect.StructField) string {
if tag, ok := field.Tag.Lookup("parser"); ok {
return tag
}
return string(field.Tag)
}
// Recursively collect flattened indices for top-level fields and embedded fields.
func collectFieldIndexes(s reflect.Type) (out [][]int) {
if s.Kind() != reflect.Struct {
panicf("expected a struct but got %q", s)
}
defer decorate(s.String)
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
if f.Anonymous {
for _, idx := range collectFieldIndexes(f.Type) {
out = append(out, append(f.Index, idx...))
}
} else if fieldLexerTag(f) != "" {
out = append(out, f.Index)
}
}
return
}