-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrparse.go
59 lines (52 loc) · 1.56 KB
/
strparse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Package strparse provides convenience wrappers around `go/parser` for simple
// expression, statement and declaration parsing from string.
//
// Can be used to construct AST nodes using source syntax.
package strparse
import (
"go/ast"
"go/parser"
"go/token"
)
var (
// BadExpr is returned as a parse result for malformed expressions.
// Should be treated as constant or readonly variable.
BadExpr = &ast.BadExpr{}
// BadStmt is returned as a parse result for malformed statmenents.
// Should be treated as constant or readonly variable.
BadStmt = &ast.BadStmt{}
// BadDecl is returned as a parse result for malformed declarations.
// Should be treated as constant or readonly variable.
BadDecl = &ast.BadDecl{}
)
// Expr parses single expression node from s.
// In case of parse error, BadExpr is returned.
func Expr(s string) ast.Expr {
node, err := parser.ParseExpr(s)
if err != nil {
return BadExpr
}
return node
}
// Stmt parses single statement node from s.
// In case of parse error, BadStmt is returned.
func Stmt(s string) ast.Stmt {
node, err := parser.ParseFile(token.NewFileSet(), "", "package main;func main() {"+s+"}", 0)
if err != nil {
return BadStmt
}
fn := node.Decls[0].(*ast.FuncDecl)
if len(fn.Body.List) != 1 {
return BadStmt
}
return fn.Body.List[0]
}
// Decl parses single declaration node from s.
// In case of parse error, BadDecl is returned.
func Decl(s string) ast.Decl {
node, err := parser.ParseFile(token.NewFileSet(), "", "package main;"+s, 0)
if err != nil || len(node.Decls) != 1 {
return BadDecl
}
return node.Decls[0]
}