-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathqueries_signature.go
More file actions
411 lines (355 loc) · 11.4 KB
/
Copy pathqueries_signature.go
File metadata and controls
411 lines (355 loc) · 11.4 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package graph
import (
"fmt"
"sort"
"strings"
)
// MethodParam represents a single method parameter.
type MethodParam struct {
Name string `json:"name"`
Direction string `json:"direction"` // IMPORTING, EXPORTING, CHANGING, RETURNING
Type string `json:"type"` // TYPE/type reference
Optional bool `json:"optional,omitempty"`
Default string `json:"default,omitempty"` // DEFAULT value if present
}
// MethodSignature represents the full signature of a method.
type MethodSignature struct {
ClassName string `json:"class_name"`
MethodName string `json:"method_name"`
Visibility string `json:"visibility,omitempty"` // PUBLIC, PROTECTED, PRIVATE
Level string `json:"level,omitempty"` // instance, static, or empty
IsAbstract bool `json:"is_abstract,omitempty"`
IsFinal bool `json:"is_final,omitempty"`
IsRedefined bool `json:"is_redefined,omitempty"`
Params []MethodParam `json:"params"`
Raising []string `json:"raising,omitempty"` // Exception classes
RawDef string `json:"raw_def,omitempty"` // Original definition text
}
// ExtractMethodSignature extracts a method signature from class definition source.
// Parses the METHODS/CLASS-METHODS statement for the target method.
// Source should be the class definition (DEFINITION part), not implementation.
func ExtractMethodSignature(className, methodName, source string) *MethodSignature {
classUpper := strings.ToUpper(strings.TrimSpace(className))
methodUpper := strings.ToUpper(strings.TrimSpace(methodName))
sig := &MethodSignature{
ClassName: classUpper,
MethodName: methodUpper,
}
// Find the method definition statement in source
// Patterns: METHODS method_name ..., CLASS-METHODS method_name ...
rawDef := findMethodDefinition(source, methodUpper)
if rawDef == "" {
return sig // empty sig means method not found in definition
}
sig.RawDef = rawDef
// Detect visibility from section context
sig.Visibility = detectVisibility(source, methodUpper)
// Detect level
defUpper := strings.ToUpper(rawDef)
if strings.HasPrefix(strings.TrimSpace(defUpper), "CLASS-METHODS") {
sig.Level = "static"
} else {
sig.Level = "instance"
}
// Detect modifiers
if strings.Contains(defUpper, " ABSTRACT") {
sig.IsAbstract = true
}
if strings.Contains(defUpper, " FINAL") {
sig.IsFinal = true
}
if strings.Contains(defUpper, " REDEFINITION") {
sig.IsRedefined = true
return sig // redefinitions have no params in definition
}
// Extract parameters by direction
sig.Params = append(sig.Params, extractParamBlock(rawDef, "IMPORTING")...)
sig.Params = append(sig.Params, extractParamBlock(rawDef, "EXPORTING")...)
sig.Params = append(sig.Params, extractParamBlock(rawDef, "CHANGING")...)
sig.Params = append(sig.Params, extractReturning(rawDef)...)
// Extract RAISING
sig.Raising = extractRaising(rawDef)
return sig
}
// FormatMethodSignature renders a human-readable signature.
func FormatMethodSignature(sig *MethodSignature) string {
var sb strings.Builder
// Header
level := ""
if sig.Level == "static" {
level = "CLASS-"
}
mods := ""
if sig.IsAbstract {
mods += " ABSTRACT"
}
if sig.IsFinal {
mods += " FINAL"
}
if sig.IsRedefined {
mods += " REDEFINITION"
}
sb.WriteString(fmt.Sprintf("%s %sMETHODS %s%s\n", sig.Visibility, level, sig.MethodName, mods))
// Group params by direction
byDir := map[string][]MethodParam{}
for _, p := range sig.Params {
byDir[p.Direction] = append(byDir[p.Direction], p)
}
for _, dir := range []string{"IMPORTING", "EXPORTING", "CHANGING", "RETURNING"} {
params := byDir[dir]
if len(params) == 0 {
continue
}
sb.WriteString(fmt.Sprintf(" %s\n", dir))
for _, p := range params {
opt := ""
if p.Optional {
opt = " OPTIONAL"
}
def := ""
if p.Default != "" {
def = " DEFAULT " + p.Default
}
sb.WriteString(fmt.Sprintf(" %s TYPE %s%s%s\n", p.Name, p.Type, opt, def))
}
}
if len(sig.Raising) > 0 {
sb.WriteString(fmt.Sprintf(" RAISING %s\n", strings.Join(sig.Raising, " ")))
}
return sb.String()
}
// --- Internal parsers ---
// findMethodDefinition extracts the full METHODS/CLASS-METHODS statement for a method.
func findMethodDefinition(source, methodUpper string) string {
lines := strings.Split(source, "\n")
// Build a single string of all non-comment lines for multi-line statement matching
var cleanLines []string
for _, line := range lines {
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(trimmed, "*") || strings.HasPrefix(trimmed, "\"") {
continue
}
cleanLines = append(cleanLines, line)
}
clean := strings.Join(cleanLines, "\n")
cleanUpper := strings.ToUpper(clean)
// Find "METHODS method_name" or "CLASS-METHODS method_name"
// Search CLASS-METHODS first to avoid matching the "METHODS" substring within "CLASS-METHODS"
patterns := []string{
"CLASS-METHODS " + methodUpper,
"METHODS " + methodUpper,
}
for _, pat := range patterns {
idx := strings.Index(cleanUpper, pat)
if idx < 0 {
continue
}
// Verify left word boundary: must be start of line or preceded by whitespace
if idx > 0 {
prev := cleanUpper[idx-1]
if prev != ' ' && prev != '\n' && prev != '\r' && prev != '\t' {
continue // e.g., "CLASS-METHODS" matched at "METHODS" inside it
}
}
// Verify right word boundary (not a prefix of another method)
endIdx := idx + len(pat)
if endIdx < len(cleanUpper) {
next := cleanUpper[endIdx]
if next != ' ' && next != '\n' && next != '\r' && next != '.' && next != ',' {
continue
}
}
// Extract from match to next period (statement end)
remaining := clean[idx:]
dotIdx := strings.Index(remaining, ".")
if dotIdx < 0 {
dotIdx = len(remaining)
}
return strings.TrimSpace(remaining[:dotIdx])
}
return ""
}
// detectVisibility finds which section a method is defined in.
func detectVisibility(source, methodUpper string) string {
sourceUpper := strings.ToUpper(source)
// Find section boundaries
pubIdx := strings.Index(sourceUpper, "PUBLIC SECTION")
proIdx := strings.Index(sourceUpper, "PROTECTED SECTION")
priIdx := strings.Index(sourceUpper, "PRIVATE SECTION")
// Find method position
methodIdx := -1
for _, pat := range []string{"METHODS " + methodUpper, "CLASS-METHODS " + methodUpper} {
idx := strings.Index(sourceUpper, pat)
if idx >= 0 {
methodIdx = idx
break
}
}
if methodIdx < 0 {
return ""
}
// Determine which section the method falls in
vis := "PUBLIC"
if pubIdx >= 0 && methodIdx > pubIdx {
vis = "PUBLIC"
}
if proIdx >= 0 && methodIdx > proIdx {
vis = "PROTECTED"
}
if priIdx >= 0 && methodIdx > priIdx {
vis = "PRIVATE"
}
return vis
}
// extractParamBlock extracts parameters for a given direction (IMPORTING/EXPORTING/CHANGING).
func extractParamBlock(rawDef, direction string) []MethodParam {
defUpper := strings.ToUpper(rawDef)
dirUpper := strings.ToUpper(direction)
idx := strings.Index(defUpper, dirUpper)
if idx < 0 {
return nil
}
// Find the block: from direction keyword to next direction/RAISING/end
blockStart := idx + len(dirUpper)
blockEnd := len(rawDef)
for _, next := range []string{"IMPORTING", "EXPORTING", "CHANGING", "RETURNING", "RAISING"} {
if next == dirUpper {
continue
}
nextIdx := strings.Index(defUpper[blockStart:], next)
if nextIdx >= 0 && blockStart+nextIdx < blockEnd {
blockEnd = blockStart + nextIdx
}
}
block := strings.TrimSpace(rawDef[blockStart:blockEnd])
return parseParamList(block, direction)
}
// extractReturning extracts RETURNING VALUE(...) TYPE ... parameter.
func extractReturning(rawDef string) []MethodParam {
defUpper := strings.ToUpper(rawDef)
idx := strings.Index(defUpper, "RETURNING")
if idx < 0 {
return nil
}
// Find block end
blockStart := idx + len("RETURNING")
blockEnd := len(rawDef)
for _, next := range []string{"RAISING"} {
nextIdx := strings.Index(defUpper[blockStart:], next)
if nextIdx >= 0 {
blockEnd = blockStart + nextIdx
}
}
block := strings.TrimSpace(rawDef[blockStart:blockEnd])
// RETURNING VALUE(rv_name) TYPE type_name
blockUpper := strings.ToUpper(block)
valueIdx := strings.Index(blockUpper, "VALUE(")
if valueIdx < 0 {
return nil
}
closeIdx := strings.Index(block[valueIdx:], ")")
if closeIdx < 0 {
return nil
}
name := strings.TrimSpace(block[valueIdx+6 : valueIdx+closeIdx])
typeName := extractTypeName(block[valueIdx+closeIdx+1:])
return []MethodParam{{
Name: strings.ToUpper(name),
Direction: "RETURNING",
Type: typeName,
}}
}
// extractRaising extracts exception class names from RAISING clause.
func extractRaising(rawDef string) []string {
defUpper := strings.ToUpper(rawDef)
idx := strings.Index(defUpper, "RAISING")
if idx < 0 {
return nil
}
block := strings.TrimSpace(rawDef[idx+len("RAISING"):])
// Split by whitespace, filter to identifiers
parts := strings.Fields(block)
var raising []string
for _, p := range parts {
p = strings.TrimSpace(strings.TrimRight(p, ".,"))
if p != "" && isIdentifier(p) {
raising = append(raising, strings.ToUpper(p))
}
}
return raising
}
// parseParamList parses individual parameters from a block like:
// "iv_name TYPE string iv_other TYPE i OPTIONAL"
func parseParamList(block, direction string) []MethodParam {
var params []MethodParam
// Tokenize roughly: split by known parameter pattern "name TYPE ..."
// This is a simplified parser — handles the common patterns
blockUpper := strings.ToUpper(block)
tokens := strings.Fields(block)
tokensUpper := strings.Fields(blockUpper)
i := 0
for i < len(tokens) {
// Look for: name TYPE type_ref [OPTIONAL] [DEFAULT value]
if i+2 < len(tokensUpper) && tokensUpper[i+1] == "TYPE" {
param := MethodParam{
Name: strings.ToUpper(tokens[i]),
Direction: strings.ToUpper(direction),
Type: strings.ToUpper(tokens[i+2]),
}
i += 3
// Check for OPTIONAL/DEFAULT
for i < len(tokensUpper) {
if tokensUpper[i] == "OPTIONAL" {
param.Optional = true
i++
} else if tokensUpper[i] == "DEFAULT" && i+1 < len(tokens) {
param.Default = tokens[i+1]
i += 2
} else {
break
}
}
params = append(params, param)
} else if i+3 < len(tokensUpper) && tokensUpper[i+1] == "TYPE" && tokensUpper[i+2] == "REF" && tokensUpper[i+3] == "TO" && i+4 < len(tokens) {
// name TYPE REF TO type_ref
param := MethodParam{
Name: strings.ToUpper(tokens[i]),
Direction: strings.ToUpper(direction),
Type: "REF TO " + strings.ToUpper(tokens[i+4]),
}
i += 5
for i < len(tokensUpper) {
if tokensUpper[i] == "OPTIONAL" {
param.Optional = true
i++
} else if tokensUpper[i] == "DEFAULT" && i+1 < len(tokens) {
param.Default = tokens[i+1]
i += 2
} else {
break
}
}
params = append(params, param)
} else {
i++ // skip unrecognized token
}
}
// Sort params by name for stability
sort.Slice(params, func(a, b int) bool { return params[a].Name < params[b].Name })
return params
}
// extractTypeName pulls type name after "TYPE" keyword in remaining text.
func extractTypeName(text string) string {
fields := strings.Fields(strings.TrimSpace(text))
for i, f := range fields {
if strings.ToUpper(f) == "TYPE" && i+1 < len(fields) {
typeName := strings.ToUpper(strings.TrimRight(fields[i+1], ".,"))
// Handle TYPE REF TO
if typeName == "REF" && i+3 < len(fields) && strings.ToUpper(fields[i+2]) == "TO" {
return "REF TO " + strings.ToUpper(strings.TrimRight(fields[i+3], ".,"))
}
return typeName
}
}
return ""
}