Skip to content

Commit 029b866

Browse files
xaionaro@dx.centerxaionaro@dx.center
authored andcommitted
fix: spec2go — paren-depth-aware operator splitting in constant expressions
1 parent 1b57872 commit 029b866

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

tools/cmd/spec2go/main.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ func run(
9595
}
9696
}
9797

98+
// Generate service name constants and accessor files from the
99+
// servicemanager spec, if present.
100+
if err := generateServiceNamesFile(specs, outputDir); err != nil {
101+
return fmt.Errorf("generating service names: %w", err)
102+
}
103+
if err := generateAccessorFiles(specs, outputDir); err != nil {
104+
return fmt.Errorf("generating accessor files: %w", err)
105+
}
106+
98107
genCount, err := countGoFiles(outputDir)
99108
if err != nil {
100109
return fmt.Errorf("counting Go files: %w", err)
@@ -727,7 +736,7 @@ func tryParseBinaryExpr(
727736

728737
for _, op := range group.Ops {
729738
padded := " " + op.Symbol + " "
730-
idx := strings.LastIndex(value, padded)
739+
idx := lastIndexAtDepthZero(value, padded)
731740
if idx < 0 {
732741
continue
733742
}
@@ -757,6 +766,28 @@ func tryParseBinaryExpr(
757766
return nil
758767
}
759768

769+
// lastIndexAtDepthZero returns the last position of needle in s
770+
// that occurs at parenthesis depth 0. Returns -1 if not found.
771+
func lastIndexAtDepthZero(
772+
s string,
773+
needle string,
774+
) int {
775+
best := -1
776+
depth := 0
777+
for i := 0; i <= len(s)-len(needle); i++ {
778+
switch s[i] {
779+
case '(':
780+
depth++
781+
case ')':
782+
depth--
783+
}
784+
if depth == 0 && s[i:i+len(needle)] == needle {
785+
best = i
786+
}
787+
}
788+
return best
789+
}
790+
760791
// findMatchingParen returns the index of the closing ')' that matches
761792
// the opening '(' at index 0. Returns -1 if not found.
762793
func findMatchingParen(

0 commit comments

Comments
 (0)