/
convert.go
147 lines (119 loc) · 3.39 KB
/
convert.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package parse
import (
"fmt"
"github.com/rickb777/enumeration/v2/internal/model"
"github.com/rickb777/enumeration/v2/internal/transform"
"github.com/rickb777/enumeration/v2/internal/util"
"go/token"
"io"
"regexp"
"strings"
)
var UsingTable string
var AliasTable string
var MainType string
var fset *token.FileSet
var tagRE = regexp.MustCompile(`[a-z]:"`)
// https://go.dev/doc/go1.17_spec#Type_declarations (without type parameters)
// TypeDecl = "type" ( TypeSpec | "(" { TypeSpec ";" } ")" ) .
// TypeSpec = AliasDecl | TypeDef .
//
// TypeDef = identifier Type .
//
// Type = TypeName | TypeLit | "(" Type ")" .
// TypeName = identifier | QualifiedIdent .
// TypeLit = ArrayType | StructType | PointerType | FunctionType | InterfaceType | SliceType | MapType | ChannelType .
func Convert(in io.Reader, input string, xCase transform.Case, config model.Config) (model.Model, error) {
src, err := io.ReadAll(in)
if err != nil {
return model.Model{}, err
}
MainType = config.MainType
m := model.Model{
Config: config,
LcType: strings.ToLower(config.MainType),
BaseType: "int",
Version: util.Version,
Case: xCase,
TagTable: UsingTable,
AliasTable: AliasTable,
Extra: make(map[string]string),
}
s := newFileScanner(input, src)
var foundMainType = false
var constItems []constItem
for s.Scan() != token.EOF {
switch s.Tok {
case token.TYPE:
s.Scan()
if s.Tok == token.IDENT && s.Lit == MainType {
foundMainType = true
s.Scan()
if s.Tok == token.IDENT {
m.BaseType = s.Lit
util.Debug("type %s %s\n", MainType, m.BaseType)
}
}
case token.CONST:
constItems = parseConst(s, constItems)
}
}
debugConstItems(constItems)
m.Values, _ = filterExportedItems(config.MainType, constItems)
debugValues(m.Values)
if s.gs.ErrorCount > 0 {
return model.Model{}, fmt.Errorf("Syntax error in %s", input)
}
if !foundMainType {
return model.Model{}, fmt.Errorf("Failed to find %s in %s", config.MainType, input)
}
if len(m.Values) == 0 {
return model.Model{}, fmt.Errorf("Failed to find any values for %s in %s", config.MainType, input)
}
if e2 := m.CheckBadPrefixSuffix(); e2 != nil {
return model.Model{}, e2
}
if e2 := m.CheckBadTags(); e2 != nil {
return model.Model{}, e2
}
return m, nil
}
func filterExportedItems(mainType string, ids []constItem) (exported model.Values, defaultValue string) {
var currentType string
exported = make(model.Values, 0, len(ids))
for _, v := range ids {
if v.typ == mainType {
if token.IsExported(v.id) {
exported = exported.Append(v.id, v.tag)
switch v.expression {
case "0", "iota":
defaultValue = v.id
}
}
} else if v.typ == "" && v.expression == "" && currentType == mainType {
if token.IsExported(v.id) {
exported = exported.Append(v.id, v.tag)
}
}
if v.typ != "" {
currentType = v.typ
}
}
return exported, defaultValue
}
func debugConstItems(ids []constItem) {
if util.Dbg {
util.Debug("\n %-25s %-25s %-25s %s\n", "id", "type", "expression", "tag")
for i, v := range ids {
util.Debug("%-2d %-25s %-25s %-25s %s\n", i, v.id, v.typ, v.expression, v.tag)
}
}
}
func debugValues(values model.Values) {
if util.Dbg {
util.Debug("\n %-25s %-25s %-25s %s\n", "id", "short", "json", "sql")
for i, v := range values {
util.Debug("%-2d %-25s %-25s %-25s %s\n", i, v.Identifier, v.Shortened, v.JSON, v.SQL)
}
}
}