Skip to content

Commit e1034ed

Browse files
Made comment parsing optional
1 parent 1c09ddf commit e1034ed

File tree

4 files changed

+59
-22
lines changed

4 files changed

+59
-22
lines changed

src/go2cs2/main.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
type Options struct {
2626
indentSpaces int
2727
preferVarDecl bool
28+
includeComments bool
2829
parseCgoTargets bool
2930
showParseTree bool
3031
}
@@ -125,6 +126,7 @@ func main() {
125126
// Define command line flags for options
126127
indentSpaces := commandLine.Int("indent", 4, "Number of spaces for indentation")
127128
preferVarDecl := commandLine.Bool("var", true, "Prefer \"var\" declarations")
129+
includeComments := commandLine.Bool("comments", false, "Include comments in output")
128130
parseCgoTargets := commandLine.Bool("cgo", false, "Parse cgo targets")
129131
showParseTree := commandLine.Bool("tree", false, "Show parse tree")
130132

@@ -158,6 +160,7 @@ Examples:
158160
options := Options{
159161
indentSpaces: *indentSpaces,
160162
preferVarDecl: *preferVarDecl,
163+
includeComments: *includeComments,
161164
parseCgoTargets: *parseCgoTargets,
162165
showParseTree: *showParseTree,
163166
}
@@ -172,6 +175,14 @@ Examples:
172175
log.Fatalf("Failed to access input file path \"%s\": %s\n", inputFilePath, err)
173176
}
174177

178+
var parseMode parser.Mode
179+
180+
if options.includeComments {
181+
parseMode = parser.ParseComments | parser.SkipObjectResolution
182+
} else {
183+
parseMode = parser.SkipObjectResolution
184+
}
185+
175186
if fileInfo.IsDir() {
176187
// If the input is a directory, parse all .go files in the directory
177188
err := filepath.Walk(inputFilePath, func(path string, info os.FileInfo, err error) error {
@@ -180,7 +191,7 @@ Examples:
180191
}
181192

182193
if !info.IsDir() && strings.HasSuffix(info.Name(), ".go") {
183-
file, err := parser.ParseFile(fset, path, nil, parser.ParseComments|parser.SkipObjectResolution)
194+
file, err := parser.ParseFile(fset, path, nil, parseMode)
184195

185196
if err != nil {
186197
return fmt.Errorf("failed to parse input source file \"%s\": %s", path, err)
@@ -201,7 +212,7 @@ Examples:
201212
log.Fatalln("Invalid file extension for input source file: please provide a .go file as first argument")
202213
}
203214

204-
file, err := parser.ParseFile(fset, inputFilePath, nil, parser.ParseComments|parser.SkipObjectResolution)
215+
file, err := parser.ParseFile(fset, inputFilePath, nil, parseMode)
205216

206217
if err != nil {
207218
log.Fatalf("Failed to parse input source file \"%s\": %s\n", inputFilePath, err)

src/go2cs2/visitBlockStmt.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,34 @@ func (v *Visitor) visitBlockStmt(blockStmt *ast.BlockStmt, context BlockStmtCont
3333
var lastStmt ast.Stmt
3434
prefix := v.newline + v.indent(v.indentLevel)
3535

36-
if len(blockStmt.List) > 0 {
36+
if len(blockStmt.List) > 0 && v.options.includeComments {
3737
v.writeStandAloneCommentString(v.targetFile, blockStmt.List[0].Pos(), nil, prefix)
3838
}
3939

4040
for _, stmt := range blockStmt.List {
41-
if lastStmt != nil {
41+
if v.options.includeComments {
42+
if lastStmt != nil {
43+
if v.file == nil {
44+
v.file = v.fset.File(stmt.Pos())
45+
}
4246

43-
currentLine := v.file.Line(stmt.Pos())
44-
lastLine := v.file.Line(lastStmt.End())
47+
currentLine := v.file.Line(stmt.Pos())
48+
lastLine := v.file.Line(lastStmt.End())
4549

46-
comments := &strings.Builder{}
47-
var wrote bool
50+
comments := &strings.Builder{}
51+
var wrote bool
4852

49-
if wrote, lines := v.writeStandAloneCommentString(comments, stmt.Pos(), nil, prefix); wrote {
50-
lastLine -= lines - 1
51-
}
53+
if wrote, lines := v.writeStandAloneCommentString(comments, stmt.Pos(), nil, prefix); wrote {
54+
lastLine -= lines - 1
55+
}
5256

53-
if wrote && currentLine-lastLine > 1 {
54-
v.targetFile.WriteString(strings.Repeat(v.newline, currentLine-lastLine-1))
55-
}
57+
if wrote && currentLine-lastLine > 1 {
58+
v.targetFile.WriteString(strings.Repeat(v.newline, currentLine-lastLine-1))
59+
}
5660

57-
if comments.Len() > 0 {
58-
v.targetFile.WriteString(comments.String())
61+
if comments.Len() > 0 {
62+
v.targetFile.WriteString(comments.String())
63+
}
5964
}
6065
}
6166

src/go2cs2/visitSwitchStmt.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ func (v *Visitor) visitSwitchStmt(switchStmt *ast.SwitchStmt, source ParentBlock
2929

3030
// Check if all case clauses are constant values
3131
for _, expr := range caseClause.List {
32-
if !v.info.Types[expr].IsValue() {
32+
// Check if the expression is a function call or a non-value type
33+
_, isCallExpr := expr.(*ast.CallExpr)
34+
35+
if !v.info.Types[expr].IsValue() || isCallExpr {
3336
allConst = false
3437
break
3538
}
@@ -46,7 +49,7 @@ func (v *Visitor) visitSwitchStmt(switchStmt *ast.SwitchStmt, source ParentBlock
4649
}
4750
}
4851

49-
hasSwitchInit := switchStmt.Init != nil || hasFallthroughs || !allConst || switchStmt.Tag == nil
52+
hasSwitchInit := switchStmt.Init != nil || hasFallthroughs || !allConst && switchStmt.Tag != nil
5053

5154
if hasSwitchInit {
5255
// Any declared variable will be scoped to switch statement, so create a sub-block for it
@@ -61,7 +64,7 @@ func (v *Visitor) visitSwitchStmt(switchStmt *ast.SwitchStmt, source ParentBlock
6164

6265
v.targetFile.WriteString(v.newline)
6366

64-
if hasFallthroughs {
67+
if hasFallthroughs || (!allConst && switchStmt.Tag != nil) {
6568
// Most complex scenario with standalone if's, and fallthrough
6669
exprVarName := v.getTempVarName("expr")
6770
matchVarName := v.getTempVarName("match")
@@ -109,13 +112,23 @@ func (v *Visitor) visitSwitchStmt(switchStmt *ast.SwitchStmt, source ParentBlock
109112
if i == 0 {
110113
if switchStmt.Tag != nil {
111114
v.targetFile.WriteString(exprVarName)
112-
v.targetFile.WriteString(" is ")
115+
116+
if usePattenMatch {
117+
v.targetFile.WriteString(" is ")
118+
} else {
119+
v.targetFile.WriteString(" == ")
120+
}
113121
}
114122
} else {
115-
if usePattenMatch || switchStmt.Tag != nil {
123+
if usePattenMatch {
116124
v.targetFile.WriteString(" or ")
117125
} else {
118126
v.targetFile.WriteString(" || ")
127+
128+
if switchStmt.Tag != nil {
129+
v.targetFile.WriteString(exprVarName)
130+
v.targetFile.WriteString(" == ")
131+
}
119132
}
120133
}
121134

src/go2cs2/writeOperations.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ func (v *Visitor) writeStandAloneCommentString(builder *strings.Builder, targetP
3636
wroteStandAloneComment := false
3737
lines := 0
3838

39+
if !v.options.includeComments {
40+
return wroteStandAloneComment, lines
41+
}
42+
3943
// Handle standalone comments that may precede the target position
4044
if targetPos != token.NoPos {
4145
if v.file == nil {
@@ -102,6 +106,10 @@ func (v *Visitor) writeStandAloneCommentString(builder *strings.Builder, targetP
102106
}
103107

104108
func (v *Visitor) writeDocString(builder *strings.Builder, doc *ast.CommentGroup, targetPos token.Pos) {
109+
if !v.options.includeComments {
110+
return
111+
}
112+
105113
wroteStandAloneComment, _ := v.writeStandAloneCommentString(builder, targetPos, doc, "")
106114

107115
if doc == nil {
@@ -119,7 +127,7 @@ func (v *Visitor) writeDocString(builder *strings.Builder, doc *ast.CommentGroup
119127
}
120128

121129
func (v *Visitor) writeCommentString(builder *strings.Builder, comment *ast.CommentGroup, targetPos token.Pos) {
122-
if comment == nil {
130+
if comment == nil || !v.options.includeComments {
123131
return
124132
}
125133

0 commit comments

Comments
 (0)