mirror of
https://codeberg.org/scip/tablizer.git
synced 2025-12-17 20:41:03 +01:00
added
This commit is contained in:
34
vendor/github.com/shurcooL/go/parserutil/parserutil.go
generated
vendored
Normal file
34
vendor/github.com/shurcooL/go/parserutil/parserutil.go
generated
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
// 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
|
||||
}
|
||||
Reference in New Issue
Block a user