mirror of
https://github.com/schollz/cowyo.git
synced 2023-08-10 21:13:00 +03:00
35 lines
1.1 KiB
Go
35 lines
1.1 KiB
Go
// Package parserutil offers convenience functions for parsing Go code to AST.
|
|
package parserutil
|
|
|
|
import (
|
|
"errors"
|
|
"go/ast"
|
|
"go/parser"
|
|
"go/token"
|
|
)
|
|
|
|
// ParseStmt is a convenience function for obtaining the AST of a statement x.
|
|
// The position information recorded in the AST is undefined. The filename used
|
|
// in error messages is the empty string.
|
|
func ParseStmt(x string) (ast.Stmt, error) {
|
|
file, err := parser.ParseFile(token.NewFileSet(), "", "package p;func _(){\n//line :1\n"+x+"\n;}", 0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return file.Decls[0].(*ast.FuncDecl).Body.List[0], nil
|
|
}
|
|
|
|
// ParseDecl is a convenience function for obtaining the AST of a declaration x.
|
|
// The position information recorded in the AST is undefined. The filename used
|
|
// in error messages is the empty string.
|
|
func ParseDecl(x string) (ast.Decl, error) {
|
|
file, err := parser.ParseFile(token.NewFileSet(), "", "package p\n//line :1\n"+x+"\n", 0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(file.Decls) == 0 {
|
|
return nil, errors.New("no declaration")
|
|
}
|
|
return file.Decls[0], nil
|
|
}
|