I think my problem might be related to the logic around line 982 of https://golang.org/src/go/printer/printer.go. I don't yet understand the distinction between the impliedSemi local variable and the impliedSemi field of printer.
How would I fetch and build the go/printer code and install it in my development environment to try some experiments? On Wednesday, July 4, 2018 at 6:38:44 PM UTC-4, Mark Nahabedian wrote: > > I'm using go/ast and friends to generate go code. I'm having trouble > with spurious newlines being added to the output. These cause the > compiler to balk. > > For broader context, my code can be found at > > https://github.com/MarkNahabedian/Goshua/tree/master/rete/rule_compiler/rule_compiler.go > . > > The point of the code below is to build a map from strings that are > the names of go types to a function that returns true when invoked on > an object of that type. This because I've not found a way to use > types as first class objects. > > // line 214 > e := parseExpression(fmt.Sprintf( > `rete.EnsureTypeTestRegistered("%s", func(i interface{}) bool { _, ok := > i.(%s); return ok })`, > pType, pType)) > initFunc.Body.List = append(initFunc.Body.List, > &ast.ExprStmt{ X: e}) > > // line 221 > newAstFile.Decls = append(newAstFile.Decls, initFunc) > > // line 224 > writeFile(fset, newAstFile, outname) > > > parseExpression is defined in ast_helpers.go. It calls > parser.ParseExpr and panics if there'sd an error. > > writeFile is defined in utils.go. It calls format.Node to > serialize the generated source code. > > > Here is some sample output that illustrates the problem I'm observing. > Note that the spurious whitespace is not added consistently. Two of > these three return statements get the error "not enough arguments to > return" which go away if the extra newlines are removed. > > rete.EnsureTypeTestRegistered("Couple", func(i interface{}) bool { > _, ok := i.(Couple) > return > > ok > }) > rete.EnsureTypeTestRegistered("FaceToFace", func(i interface{}) bool { > _, > ok := i.(FaceToFace) > return ok > }) > rete.EnsureTypeTestRegistered("Tandem", func(i interface{}) bool { > _, ok := i.(Tandem) > return > > ok > }) > > Also observe that in the middle case spurious whitespace is added to > the assignment/declaration statement rather that the return statement. > > Why is this happening and how do I prevent it? > > Thanks. > > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.