本文整理汇总了Golang中go/parser.ParseExpr函数的典型用法代码示例。如果您正苦于以下问题:Golang ParseExpr函数的具体用法?Golang ParseExpr怎么用?Golang ParseExpr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ParseExpr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ParseStmt
// A convenience function for parsing a Stmt by wrapping it in a func literal
func ParseStmt(stmt string) (ast.Stmt, error) {
// parser.ParseExpr has some quirks, for example it won't parse unused map literals as
// ExprStmts. Unused map literals are technically illegal, but it would be
// nice to check them at a later stage. Therefore, we want to parse expressions
// as expressions, and stmts and stmts. We try both.
// However, there is a bug in parser.ParseExpr that it does not detect excess input.
// Therefore, the _ of _ = 1 will be parsed as an expression. To avoid this, attempt
// to parse the input as a statement first, and fall back to an expression
expr := "func(){" + stmt + ";}"
if e, err := parser.ParseExpr(expr); err != nil {
if e, err := parser.ParseExpr(stmt); err == nil {
return &ast.ExprStmt{X: e}, nil
}
errs := err.(scanner.ErrorList)
for i := range errs {
errs[i].Pos.Offset -= 7
errs[i].Pos.Column -= 7
}
return nil, errs
} else {
node := e.(*ast.FuncLit).Body.List[0]
if stmt, ok := node.(Stmt); !ok {
return nil, fmt.Errorf("%T not supported", node)
} else {
return stmt, nil
}
}
}
示例2: Inline
// Inline replaces each instance of identifier k with v.Ident in ast.File f,
// for k, v := range m.
// For all inlines that were triggeres it also adds imports from v.Imports to f.
// In addition, it removes top level type declarations of the form
// type k ...
// for all k in m.
//
// Every k in m should be a valid identifier.
// Every v.Ident should be a valid expression.
func Inline(fset *token.FileSet, f *ast.File, m map[string]Target) error {
// Build the inline map.
im := map[string]reflect.Value{}
for k, v := range m {
expr, err := parser.ParseExpr(k)
if err != nil {
return fmt.Errorf("failed to parse `%s`: %s", k, err)
}
if _, ok := expr.(*ast.Ident); !ok {
return fmt.Errorf("expected identifier, got %s which is %T", k, expr)
}
expr, err = parser.ParseExpr(v.Ident)
if err != nil {
return fmt.Errorf("failed to parse `%s`: %s", v.Ident, err)
}
s := v.Ident
if _, ok := expr.(*ast.StarExpr); ok {
s = fmt.Sprintf("(%s)", s)
}
im[k] = reflect.ValueOf(ast.Ident{Name: s})
}
// Filter `type XXX ...` declarations out if we are inlining XXX.
cmap := ast.NewCommentMap(fset, f, f.Comments)
to := 0
for _, d := range f.Decls {
skip := false
if t, ok := d.(*ast.GenDecl); ok {
for _, s := range t.Specs {
ts, ok := s.(*ast.TypeSpec)
if !ok {
continue
}
if _, ok = im[ts.Name.String()]; ok {
skip = true
}
}
}
if !skip {
f.Decls[to] = d
to++
}
}
if to != len(f.Decls) {
f.Decls = f.Decls[:to]
// Remove comments for the declarations that were filtered out.
f.Comments = cmap.Filter(f).Comments()
}
// Add imports for the inlines that were triggered.
for k := range inline(im, f) {
for _, imp := range m[k].Imports {
astutil.AddImport(fset, f, imp)
}
}
return nil
}
示例3: compareTwoTrees
func compareTwoTrees(src string) {
v1 := &channelPusher{}
v1.fileSet = token.NewFileSet()
v1.queue = make(chan *ast.Node)
v2 := &channelPusher{}
v2.fileSet = token.NewFileSet()
v2.queue = make(chan *ast.Node)
tree1, err := parser.ParseExpr(src)
if err != nil {
panic(err)
}
src2 := "x + 2*y"
tree2, err := parser.ParseExpr(src2)
if err != nil {
panic(err)
}
done := make(chan struct{})
defer close(done)
go func() {
ast.Walk(v1, tree1)
close(v1.queue)
done <- struct{}{}
}()
go func() {
ast.Walk(v2, tree2)
close(v2.queue)
done <- struct{}{}
}()
var n1, n2 *ast.Node
quit := false
for !quit {
select {
case n1 = <-v1.queue:
case n2 = <-v2.queue:
case <-done:
quit = true
}
if n1 != nil && n2 != nil {
if !equalNodes(n1, n2) {
println("!equalNodes")
break
}
println("equalNodes")
n1 = nil
n2 = nil
}
}
}
示例4: parseExpr
// parseExpr returns an ast expression from the source s
// stolen from http://golang.org/src/cmd/fix/fix.go
func parseExpr(s string) ast.Expr {
exp, err := parser.ParseExpr(s)
if err != nil {
log.Println("Cannot parse expression %s :%s", s, err.Error())
}
return exp
}
示例5: Compile
// compiles source consisting of a number of statements. E.g.:
// src = "a = 1; b = sin(x)"
// code, err := world.Compile(src)
// code.Eval()
func (w *World) Compile(src string) (code *BlockStmt, e error) {
// parse
exprSrc := "func(){\n" + src + "\n}" // wrap in func to turn into expression
tree, err := parser.ParseExpr(exprSrc)
if err != nil {
return nil, fmt.Errorf("script line %v: ", err)
}
// catch compile errors and decode line number
if !Debug {
defer func() {
err := recover()
if err == nil {
return
}
if compErr, ok := err.(*compileErr); ok {
code = nil
e = fmt.Errorf("script %v: %v", pos2line(compErr.pos, exprSrc), compErr.msg)
} else {
panic(err)
}
}()
}
// compile
stmts := tree.(*ast.FuncLit).Body.List // strip func again
if Debug {
ast.Print(nil, stmts)
}
block := new(BlockStmt)
for _, s := range stmts {
block.append(w.compile(s), s)
}
return block, nil
}
示例6: CompileExpr
// Compiles an expression, which can then be evaluated. E.g.:
// expr, err := world.CompileExpr("1+1")
// expr.Eval() // returns 2
func (w *World) CompileExpr(src string) (code Expr, e error) {
// parse
tree, err := parser.ParseExpr(src)
if err != nil {
return nil, fmt.Errorf(`parse "%s": %v`, src, err)
}
if Debug {
ast.Print(nil, tree)
}
// catch compile errors
if !Debug {
defer func() {
err := recover()
if err == nil {
return
}
if er, ok := err.(*compileErr); ok {
code = nil
e = fmt.Errorf(`parse "%s": %v`, src, er)
} else {
panic(err)
}
}()
}
return w.compile(tree), nil
}
示例7: asExpr
func asExpr(s string) ast.Expr {
expr, err := parser.ParseExpr(s)
if err != nil {
panic(err)
}
return expr
}
示例8: parseTemplateAndArgs
// Parse the arguments string Template(A, B, C)
func parseTemplateAndArgs(s string) (name string, args []string) {
expr, err := parser.ParseExpr(s)
if err != nil {
fatalf("Failed to parse %q: %v", s, err)
}
debugf("expr = %#v\n", expr)
callExpr, ok := expr.(*ast.CallExpr)
if !ok {
fatalf("Failed to parse %q: expecting Identifier(...)", s)
}
debugf("fun = %#v", callExpr.Fun)
fn, ok := callExpr.Fun.(*ast.Ident)
if !ok {
fatalf("Failed to parse %q: expecting Identifier(...)", s)
}
name = fn.Name
for i, arg := range callExpr.Args {
var buf bytes.Buffer
debugf("arg[%d] = %#v", i, arg)
format.Node(&buf, token.NewFileSet(), arg)
s := buf.String()
debugf("parsed = %q", s)
args = append(args, s)
}
return
}
示例9: expr
func expr(s string) ast.Expr {
x, err := parser.ParseExpr(s)
if err != nil {
panic("parsing " + s + ": " + err.Error())
}
return x
}
示例10: Eval
// Eval evaluates a string
func (s *Scope) Eval(str string) (interface{}, error) {
expr, err := parser.ParseExpr("func(){" + str + "}()")
if err != nil {
return nil, err
}
return s.Interpret(expr.(*ast.CallExpr).Fun.(*ast.FuncLit).Body)
}
示例11: parseImport
// This parses an import statement, such as "./foo/bar(int,baz.T(foo.T))",
// returning a list of types and a list of imports that are needed
// (e.g. baz and foo in the above example).
func parseImport(s string) (types []string) {
// First, I want to cut off any preliminary directories, so the
// import should look like a function call.
n := strings.Index(s, "(")
if n < 1 {
return
}
start := 0
for i := n - 1; i >= 0; i-- {
if s[i] == '/' {
start = i + 1
break
}
}
s = s[start:]
// Now we just need to parse the apparent function call...
x, _ := parser.ParseExpr(s)
callexpr, ok := x.(*ast.CallExpr)
if !ok {
return
} // FIXME: need error handling?
for _, texpr := range callexpr.Args {
types = append(types, pretty(texpr))
}
return types
}
示例12: WhatisCommand
func WhatisCommand(args []string) {
line := repl.CmdLine[len(args[0]):len(repl.CmdLine)]
ctx := &eval.Ctx{line}
if expr, err := parser.ParseExpr(line); err != nil {
if pair := eval.FormatErrorPos(line, err.Error()); len(pair) == 2 {
repl.Msg(pair[0])
repl.Msg(pair[1])
}
repl.Errmsg("parse error: %s\n", err)
} else {
cexpr, errs := eval.CheckExpr(ctx, expr, repl.Env)
if len(errs) != 0 {
for _, cerr := range errs {
repl.Msg("%v", cerr)
}
} else {
repl.Section(cexpr.String())
if cexpr.IsConst() {
repl.Msg("constant:\t%s", cexpr.Const())
}
knownTypes := cexpr.KnownType()
if len(knownTypes) == 1 {
repl.Msg("type:\t%s", knownTypes[0])
} else {
for i, v := range knownTypes {
repl.Msg("type[%d]:\t%s", i, v)
}
}
}
}
}
示例13: deduce_cursor_decl
// this function is called when the cursor is at the '.' and you need to get the
// declaration before that dot
func (c *auto_complete_context) deduce_cursor_decl(iter *token_iterator) *decl {
expr, err := parser.ParseExpr(iter.extract_go_expr())
if err != nil {
return nil
}
return expr_to_decl(expr, c.current.scope)
}
示例14: main
func main() {
// src is the input for which we want to inspect the AST.
exprs := []string{
"\"quoted\" string with backslash \\",
"f(3.14)*2 + c",
"-2 ", // trailing spaces to be devius
" 5 == 6",
"5\t< 6", // that's a tab in there
"1+2",
"(1+2)*3",
"1 << n",
"1 << 8",
"y(",
}
for _, expr := range exprs {
// Create the AST by parsing expr.
f, err := parser.ParseExpr(expr)
if err != nil {
fmt.Printf("Error parsing %s: %s", expr, err.Error())
continue
}
// Inspect the AST and print all identifiers and literals.
if v := evalAction(f); v != nil {
fmt.Printf("Eval: '%s' ok; value: %s\n", expr, v)
} else {
fmt.Printf("Eval '%s' no good\n", expr)
}
fmt.Println("--------------")
}
}
示例15: parse
func parse(x string) ast.Expr {
expr, err := parser.ParseExpr(x)
if err != nil {
panic(err)
}
return expr
}