Created
December 31, 2016 12:24
-
-
Save tenntenn/cd010559f51c801c4c783d13890bfe7f to your computer and use it in GitHub Desktop.
抽象構文木(AST)をいじってフォーマットをかける #golang ref: http://qiita.com/tenntenn/items/8953f2ae80c610b353c8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*ast.BinaryExpr (+) | |
├── *ast.Ident (x) | |
└── *ast.Ident (y) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*ast.BinaryExpr (+) | |
├── *ast.BasicLit (10) | |
└── *ast.BasicLit (20) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type BasicLit struct { | |
ValuePos token.Pos // literal position | |
Kind token.Token // token.INT, token.FLOAT, token.IMAG, token.CHAR, or token.STRING | |
Value string // literal string; e.g. 42, 0x7f, 3.14, 1e-9, 2.4i, 'a', '\x7f', "foo" or `\m\n\o` | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fset := token.NewFileSet() | |
expr, err := parser.ParseExprFrom(fset, "sample.go", `x+y`, 0) | |
if err != nil { | |
log.Fatalln("Error:", err) | |
} | |
fmt.Println("==== BEFORE ====") | |
ast.Print(fset, expr) | |
binaryExpr := expr.(*ast.BinaryExpr) | |
binaryExpr.X = &ast.BasicLit{ | |
Kind: token.INT, | |
Value: "10", | |
} | |
binaryExpr.Y = &ast.BasicLit{ | |
Kind: token.INT, | |
Value: "20", | |
} | |
fmt.Println("==== AFTER ====") | |
ast.Print(fset, expr) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
==== BEFORE ==== | |
0 *ast.BinaryExpr { | |
1 . X: *ast.Ident { | |
2 . . NamePos: sample.go:1:1 | |
3 . . Name: "x" | |
4 . . Obj: *ast.Object { | |
5 . . . Kind: bad | |
6 . . . Name: "" | |
7 . . } | |
8 . } | |
9 . OpPos: sample.go:1:2 | |
10 . Op: + | |
11 . Y: *ast.Ident { | |
12 . . NamePos: sample.go:1:3 | |
13 . . Name: "y" | |
14 . . Obj: *(obj @ 4) | |
15 . } | |
16 } | |
==== AFTER ==== | |
0 *ast.BinaryExpr { | |
1 . X: *ast.BasicLit { | |
2 . . ValuePos: - | |
3 . . Kind: INT | |
4 . . Value: "10" | |
5 . } | |
6 . OpPos: sample.go:1:2 | |
7 . Op: + | |
8 . Y: *ast.BasicLit { | |
9 . . ValuePos: - | |
10 . . Kind: INT | |
11 . . Value: "20" | |
12 . } | |
13 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func Node(dst io.Writer, fset *token.FileSet, node interface{}) error | |
func Source(src []byte) ([]byte, error) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if err := format.Node(os.Stdout, fset, expr); err != nil { | |
log.Fatalln("Error:", err) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 + 20 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"go/ast" | |
"go/format" | |
"go/parser" | |
"go/token" | |
"log" | |
"os" | |
) | |
func main() { | |
fset := token.NewFileSet() | |
expr, err := parser.ParseExprFrom(fset, "sample.go", `x+y`, 0) | |
if err != nil { | |
log.Fatalln("Error:", err) | |
} | |
fmt.Println("==== BEFORE ====") | |
ast.Print(fset, expr) | |
binaryExpr := expr.(*ast.BinaryExpr) | |
binaryExpr.X = &ast.BasicLit{ | |
Kind: token.INT, | |
Value: "10", | |
} | |
binaryExpr.Y = &ast.BasicLit{ | |
Kind: token.INT, | |
Value: "20", | |
} | |
fmt.Println("==== AFTER ====") | |
ast.Print(fset, expr) | |
if err := format.Node(os.Stdout, fset, expr); err != nil { | |
log.Fatalln("Error:", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment