-
Notifications
You must be signed in to change notification settings - Fork 448
Expand file tree
/
Copy pathconstparser.go
More file actions
202 lines (169 loc) · 5.08 KB
/
constparser.go
File metadata and controls
202 lines (169 loc) · 5.08 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package extgen
import (
"bufio"
"errors"
"fmt"
"os"
"regexp"
"strconv"
"strings"
)
var constRegex = regexp.MustCompile(`//\s*export_php:const$`)
var classConstRegex = regexp.MustCompile(`//\s*export_php:classconst\s+(\w+)$`)
var constDeclRegex = regexp.MustCompile(`const\s+(\w+)\s*=\s*(.+)`)
type ConstantParser struct{}
func (cp *ConstantParser) parse(filename string) (constants []phpConstant, err error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer func() {
err = errors.Join(err, file.Close())
}()
scanner := bufio.NewScanner(file)
lineNumber := 0
expectConstDecl := false
expectClassConstDecl := false
currentClassName := ""
currentConstantValue := 0
inConstBlock := false
exportAllInBlock := false
lastConstValue := ""
lastConstWasIota := false
for scanner.Scan() {
lineNumber++
line := strings.TrimSpace(scanner.Text())
if constRegex.MatchString(line) {
expectConstDecl = true
expectClassConstDecl = false
currentClassName = ""
continue
}
if matches := classConstRegex.FindStringSubmatch(line); len(matches) == 2 {
expectClassConstDecl = true
expectConstDecl = false
currentClassName = matches[1]
continue
}
if strings.HasPrefix(line, "const (") {
inConstBlock = true
if expectConstDecl || expectClassConstDecl {
exportAllInBlock = true
}
continue
}
if inConstBlock && line == ")" {
inConstBlock = false
exportAllInBlock = false
expectConstDecl = false
expectClassConstDecl = false
currentClassName = ""
lastConstValue = ""
lastConstWasIota = false
continue
}
if (expectConstDecl || expectClassConstDecl) && strings.HasPrefix(line, "const ") && !inConstBlock {
matches := constDeclRegex.FindStringSubmatch(line)
if len(matches) == 3 {
name := matches[1]
value := strings.TrimSpace(matches[2])
constant := phpConstant{
Name: name,
Value: value,
IsIota: value == "iota",
lineNumber: lineNumber,
ClassName: currentClassName,
}
constant.PhpType = determineConstantType(value)
if constant.IsIota {
constant.Value = fmt.Sprintf("%d", currentConstantValue)
constant.PhpType = phpInt
currentConstantValue++
lastConstWasIota = true
lastConstValue = constant.Value
}
constants = append(constants, constant)
} else {
return nil, fmt.Errorf("invalid constant declaration at line %d: %s", lineNumber, line)
}
expectConstDecl = false
expectClassConstDecl = false
} else if inConstBlock && (expectConstDecl || expectClassConstDecl || exportAllInBlock) {
constBlockDeclRegex := regexp.MustCompile(`^(\w+)\s*=\s*(.+)$`)
if matches := constBlockDeclRegex.FindStringSubmatch(line); len(matches) == 3 {
name := matches[1]
value := strings.TrimSpace(matches[2])
constant := phpConstant{
Name: name,
Value: value,
IsIota: value == "iota",
lineNumber: lineNumber,
ClassName: currentClassName,
}
constant.PhpType = determineConstantType(value)
if constant.IsIota {
constant.Value = fmt.Sprintf("%d", currentConstantValue)
constant.PhpType = phpInt
currentConstantValue++
lastConstWasIota = true
lastConstValue = constant.Value
} else {
lastConstWasIota = false
lastConstValue = value
}
constants = append(constants, constant)
expectConstDecl = false
expectClassConstDecl = false
} else {
constNameRegex := regexp.MustCompile(`^(\w+)$`)
if matches := constNameRegex.FindStringSubmatch(line); len(matches) == 2 {
name := matches[1]
constant := phpConstant{
Name: name,
Value: "",
IsIota: lastConstWasIota,
lineNumber: lineNumber,
ClassName: currentClassName,
}
if lastConstWasIota {
constant.Value = fmt.Sprintf("%d", currentConstantValue)
constant.PhpType = phpInt
currentConstantValue++
lastConstValue = constant.Value
} else {
constant.Value = lastConstValue
constant.PhpType = determineConstantType(lastConstValue)
}
constants = append(constants, constant)
expectConstDecl = false
expectClassConstDecl = false
}
}
} else if (expectConstDecl || expectClassConstDecl) && !strings.HasPrefix(line, "//") && line != "" && !inConstBlock {
// we expected a const declaration but found something else, reset
expectConstDecl = false
expectClassConstDecl = false
currentClassName = ""
}
}
return constants, scanner.Err()
}
// determineConstantType analyzes the value and determines its type
func determineConstantType(value string) phpType {
value = strings.TrimSpace(value)
if (strings.HasPrefix(value, `"`) && strings.HasSuffix(value, `"`)) ||
(strings.HasPrefix(value, "`") && strings.HasSuffix(value, "`")) {
return phpString
}
if value == "true" || value == "false" {
return phpBool
}
// check for integer literals, including hex, octal, binary
if _, err := strconv.ParseInt(value, 0, 64); err == nil {
return phpInt
}
if _, err := strconv.ParseFloat(value, 64); err == nil {
return phpFloat
}
return phpInt
}