Discussion:
go/ast 'else' keyword Position
Paul Jolly
2014-08-26 22:06:01 UTC
Permalink
Apologies if I am missing something obvious, but is there a way to
determine the token.Position of the 'else' keyword in a go/ast IfStmt?

Given the following:

package main
func main() {
if true {
} else {
}
}

The relevant part of the AST looks like this:

27 . . . . . 0: *ast.IfStmt {
28 . . . . . . If:
/tmp/67948a64-58db-625e-1cf6-ed449ec3e2cf.go:3:3
29 . . . . . . Cond: *ast.Ident {
30 . . . . . . . NamePos:
/tmp/67948a64-58db-625e-1cf6-ed449ec3e2cf.go:3:6
31 . . . . . . . Name: "true"
32 . . . . . . }
33 . . . . . . Body: *ast.BlockStmt {
34 . . . . . . . Lbrace:
/tmp/67948a64-58db-625e-1cf6-ed449ec3e2cf.go:3:11
35 . . . . . . . Rbrace:
/tmp/67948a64-58db-625e-1cf6-ed449ec3e2cf.go:4:3
36 . . . . . . }
37 . . . . . . Else: *ast.BlockStmt {
38 . . . . . . . Lbrace:
/tmp/67948a64-58db-625e-1cf6-ed449ec3e2cf.go:4:10
39 . . . . . . . Rbrace:
/tmp/67948a64-58db-625e-1cf6-ed449ec3e2cf.go:5:3
40 . . . . . . }
41 . . . . . }

Clearly the 'else' keyword is somewhere between the Rbrace of the IfStmt
Body and the Lbrace of the Else.

But is there a way to know exactly?

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+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/d/optout.
Rob Pike
2014-08-26 22:07:16 UTC
Permalink
No, although if the file is gofmt'ed it's five bytes before the
opening brace, or two bytes after the closing brace of the "if" block.

-rob
Apologies if I am missing something obvious, but is there a way to determine
the token.Position of the 'else' keyword in a go/ast IfStmt?
package main
func main() {
if true {
} else {
}
}
27 . . . . . 0: *ast.IfStmt {
/tmp/67948a64-58db-625e-1cf6-ed449ec3e2cf.go:3:3
29 . . . . . . Cond: *ast.Ident {
/tmp/67948a64-58db-625e-1cf6-ed449ec3e2cf.go:3:6
31 . . . . . . . Name: "true"
32 . . . . . . }
33 . . . . . . Body: *ast.BlockStmt {
/tmp/67948a64-58db-625e-1cf6-ed449ec3e2cf.go:3:11
/tmp/67948a64-58db-625e-1cf6-ed449ec3e2cf.go:4:3
36 . . . . . . }
37 . . . . . . Else: *ast.BlockStmt {
/tmp/67948a64-58db-625e-1cf6-ed449ec3e2cf.go:4:10
/tmp/67948a64-58db-625e-1cf6-ed449ec3e2cf.go:5:3
40 . . . . . . }
41 . . . . . }
Clearly the 'else' keyword is somewhere between the Rbrace of the IfStmt
Body and the Lbrace of the Else.
But is there a way to know exactly?
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
For more options, visit https://groups.google.com/d/optout.
--
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+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/d/optout.
Paul Jolly
2014-08-26 22:11:25 UTC
Permalink
Thanks for confirming.. and for the quick response
Post by Rob Pike
No, although if the file is gofmt'ed it's five bytes before the
opening brace, or two bytes after the closing brace of the "if" block.
-rob
Post by Paul Jolly
Apologies if I am missing something obvious, but is there a way to
determine
Post by Paul Jolly
the token.Position of the 'else' keyword in a go/ast IfStmt?
package main
func main() {
if true {
} else {
}
}
27 . . . . . 0: *ast.IfStmt {
/tmp/67948a64-58db-625e-1cf6-ed449ec3e2cf.go:3:3
29 . . . . . . Cond: *ast.Ident {
/tmp/67948a64-58db-625e-1cf6-ed449ec3e2cf.go:3:6
31 . . . . . . . Name: "true"
32 . . . . . . }
33 . . . . . . Body: *ast.BlockStmt {
/tmp/67948a64-58db-625e-1cf6-ed449ec3e2cf.go:3:11
/tmp/67948a64-58db-625e-1cf6-ed449ec3e2cf.go:4:3
36 . . . . . . }
37 . . . . . . Else: *ast.BlockStmt {
/tmp/67948a64-58db-625e-1cf6-ed449ec3e2cf.go:4:10
/tmp/67948a64-58db-625e-1cf6-ed449ec3e2cf.go:5:3
40 . . . . . . }
41 . . . . . }
Clearly the 'else' keyword is somewhere between the Rbrace of the IfStmt
Body and the Lbrace of the Else.
But is there a way to know exactly?
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
For more options, visit https://groups.google.com/d/optout.
--
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+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/d/optout.
Loading...