Skip to content

Commit

Permalink
Made comment parsing optional
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchiecarroll committed Oct 17, 2024
1 parent 1c09ddf commit e1034ed
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 22 deletions.
15 changes: 13 additions & 2 deletions src/go2cs2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
type Options struct {
indentSpaces int
preferVarDecl bool
includeComments bool
parseCgoTargets bool
showParseTree bool
}
Expand Down Expand Up @@ -125,6 +126,7 @@ func main() {
// Define command line flags for options
indentSpaces := commandLine.Int("indent", 4, "Number of spaces for indentation")
preferVarDecl := commandLine.Bool("var", true, "Prefer \"var\" declarations")
includeComments := commandLine.Bool("comments", false, "Include comments in output")
parseCgoTargets := commandLine.Bool("cgo", false, "Parse cgo targets")
showParseTree := commandLine.Bool("tree", false, "Show parse tree")

Expand Down Expand Up @@ -158,6 +160,7 @@ Examples:
options := Options{
indentSpaces: *indentSpaces,
preferVarDecl: *preferVarDecl,
includeComments: *includeComments,
parseCgoTargets: *parseCgoTargets,
showParseTree: *showParseTree,
}
Expand All @@ -172,6 +175,14 @@ Examples:
log.Fatalf("Failed to access input file path \"%s\": %s\n", inputFilePath, err)
}

var parseMode parser.Mode

if options.includeComments {
parseMode = parser.ParseComments | parser.SkipObjectResolution
} else {
parseMode = parser.SkipObjectResolution
}

if fileInfo.IsDir() {
// If the input is a directory, parse all .go files in the directory
err := filepath.Walk(inputFilePath, func(path string, info os.FileInfo, err error) error {
Expand All @@ -180,7 +191,7 @@ Examples:
}

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

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

file, err := parser.ParseFile(fset, inputFilePath, nil, parser.ParseComments|parser.SkipObjectResolution)
file, err := parser.ParseFile(fset, inputFilePath, nil, parseMode)

if err != nil {
log.Fatalf("Failed to parse input source file \"%s\": %s\n", inputFilePath, err)
Expand Down
33 changes: 19 additions & 14 deletions src/go2cs2/visitBlockStmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,34 @@ func (v *Visitor) visitBlockStmt(blockStmt *ast.BlockStmt, context BlockStmtCont
var lastStmt ast.Stmt
prefix := v.newline + v.indent(v.indentLevel)

if len(blockStmt.List) > 0 {
if len(blockStmt.List) > 0 && v.options.includeComments {
v.writeStandAloneCommentString(v.targetFile, blockStmt.List[0].Pos(), nil, prefix)
}

for _, stmt := range blockStmt.List {
if lastStmt != nil {
if v.options.includeComments {
if lastStmt != nil {
if v.file == nil {
v.file = v.fset.File(stmt.Pos())
}

currentLine := v.file.Line(stmt.Pos())
lastLine := v.file.Line(lastStmt.End())
currentLine := v.file.Line(stmt.Pos())
lastLine := v.file.Line(lastStmt.End())

comments := &strings.Builder{}
var wrote bool
comments := &strings.Builder{}
var wrote bool

if wrote, lines := v.writeStandAloneCommentString(comments, stmt.Pos(), nil, prefix); wrote {
lastLine -= lines - 1
}
if wrote, lines := v.writeStandAloneCommentString(comments, stmt.Pos(), nil, prefix); wrote {
lastLine -= lines - 1
}

if wrote && currentLine-lastLine > 1 {
v.targetFile.WriteString(strings.Repeat(v.newline, currentLine-lastLine-1))
}
if wrote && currentLine-lastLine > 1 {
v.targetFile.WriteString(strings.Repeat(v.newline, currentLine-lastLine-1))
}

if comments.Len() > 0 {
v.targetFile.WriteString(comments.String())
if comments.Len() > 0 {
v.targetFile.WriteString(comments.String())
}
}
}

Expand Down
23 changes: 18 additions & 5 deletions src/go2cs2/visitSwitchStmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ func (v *Visitor) visitSwitchStmt(switchStmt *ast.SwitchStmt, source ParentBlock

// Check if all case clauses are constant values
for _, expr := range caseClause.List {
if !v.info.Types[expr].IsValue() {
// Check if the expression is a function call or a non-value type
_, isCallExpr := expr.(*ast.CallExpr)

if !v.info.Types[expr].IsValue() || isCallExpr {
allConst = false
break
}
Expand All @@ -46,7 +49,7 @@ func (v *Visitor) visitSwitchStmt(switchStmt *ast.SwitchStmt, source ParentBlock
}
}

hasSwitchInit := switchStmt.Init != nil || hasFallthroughs || !allConst || switchStmt.Tag == nil
hasSwitchInit := switchStmt.Init != nil || hasFallthroughs || !allConst && switchStmt.Tag != nil

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

v.targetFile.WriteString(v.newline)

if hasFallthroughs {
if hasFallthroughs || (!allConst && switchStmt.Tag != nil) {
// Most complex scenario with standalone if's, and fallthrough
exprVarName := v.getTempVarName("expr")
matchVarName := v.getTempVarName("match")
Expand Down Expand Up @@ -109,13 +112,23 @@ func (v *Visitor) visitSwitchStmt(switchStmt *ast.SwitchStmt, source ParentBlock
if i == 0 {
if switchStmt.Tag != nil {
v.targetFile.WriteString(exprVarName)
v.targetFile.WriteString(" is ")

if usePattenMatch {
v.targetFile.WriteString(" is ")
} else {
v.targetFile.WriteString(" == ")
}
}
} else {
if usePattenMatch || switchStmt.Tag != nil {
if usePattenMatch {
v.targetFile.WriteString(" or ")
} else {
v.targetFile.WriteString(" || ")

if switchStmt.Tag != nil {
v.targetFile.WriteString(exprVarName)
v.targetFile.WriteString(" == ")
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/go2cs2/writeOperations.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func (v *Visitor) writeStandAloneCommentString(builder *strings.Builder, targetP
wroteStandAloneComment := false
lines := 0

if !v.options.includeComments {
return wroteStandAloneComment, lines
}

// Handle standalone comments that may precede the target position
if targetPos != token.NoPos {
if v.file == nil {
Expand Down Expand Up @@ -102,6 +106,10 @@ func (v *Visitor) writeStandAloneCommentString(builder *strings.Builder, targetP
}

func (v *Visitor) writeDocString(builder *strings.Builder, doc *ast.CommentGroup, targetPos token.Pos) {
if !v.options.includeComments {
return
}

wroteStandAloneComment, _ := v.writeStandAloneCommentString(builder, targetPos, doc, "")

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

func (v *Visitor) writeCommentString(builder *strings.Builder, comment *ast.CommentGroup, targetPos token.Pos) {
if comment == nil {
if comment == nil || !v.options.includeComments {
return
}

Expand Down

0 comments on commit e1034ed

Please sign in to comment.