How to use Recursive method of ast Package

Best Syzkaller code snippet using ast.Recursive

main.go

Source:main.go Github

copy

Full Screen

...19 if err != nil {20 fmt.Println(err)21 return22 }23 printDeclRecursive(f.Decls, 0, false)24}25func fprint(composite bool, level int, format string, args ...interface{}) {26 if composite {27 fmt.Printf("%*s- ", (level*2)-2, "")28 fmt.Printf(format, args...)29 } else {30 fmt.Printf("%*s", level*2, "")31 fmt.Printf(format, args...)32 }33}34func printDeclRecursive(decl interface{}, level int, composite bool) {35 switch typeddecl := decl.(type) {36 case []ast.Decl:37 for _, d := range typeddecl {38 fmt.Println("--------")39 printDeclRecursive(d, level, true && level > 0)40 }41 case []ast.Expr:42 for _, e := range typeddecl {43 printDeclRecursive(e, level, true && level > 0)44 }45 case []ast.Stmt:46 for _, e := range typeddecl {47 printDeclRecursive(e, level, true && level > 0)48 }49 case []ast.Spec:50 for _, e := range typeddecl {51 printDeclRecursive(e, level, true && level > 0)52 }53 case []*ast.Ident:54 for _, e := range typeddecl {55 printDeclRecursive(e, level, true && level > 0)56 }57 case []interface{}:58 for _, e := range typeddecl {59 printDeclRecursive(e, level, true && level > 0)60 }61 case *ast.DeclStmt:62 fprint(composite, level, "%T\n", typeddecl)63 fprint(false, level+1, "%T Decl:\n", typeddecl)64 printDeclRecursive(typeddecl.Decl, level+2, false)65 case *ast.GenDecl:66 fprint(composite, level, "%T\n", typeddecl)67 fprint(false, level+1, "%T Specs:\n", typeddecl)68 printDeclRecursive(typeddecl.Specs, level+2, false)69 case *ast.GoStmt:70 fprint(composite, level, "%T\n", typeddecl)71 fprint(false, level+1, "%T Call:\n", typeddecl)72 printDeclRecursive(typeddecl.Call, level+2, false)73 case *ast.ArrayType:74 fprint(composite, level, "%T\n", typeddecl)75 fprint(composite, level+1, "%T Elt:\n", typeddecl)76 printDeclRecursive(typeddecl.Elt, level+2, composite)77 case *ast.FuncDecl:78 fprint(composite, level, "%T name=%s\n", typeddecl, typeddecl.Name)79 fprint(composite, level+1, "%T Body:\n", typeddecl)80 printDeclRecursive(typeddecl.Body, level+2, false)81 case *ast.BlockStmt:82 fprint(composite, level, "%T:\n", typeddecl)83 fprint(composite, level+1, "%T List[]:\n", typeddecl)84 for _, l := range typeddecl.List {85 printDeclRecursive(l, level+2, true)86 }87 case *ast.ReturnStmt:88 fprint(composite, level, "%T\n", typeddecl)89 fprint(false, level+1, "%T Results[]:\n", typeddecl)90 for _, r := range typeddecl.Results {91 printDeclRecursive(r, level+2, true)92 }93 case *ast.ExprStmt:94 fprint(composite, level, "%T\n", typeddecl)95 fprint(false, level+1, "%T X\n", typeddecl)96 printDeclRecursive(typeddecl.X, level+2, false)97 case *ast.AssignStmt:98 fprint(composite, level, "%T\n", typeddecl)99 fprint(false, level+1, "%T Lhs[]:\n", typeddecl)100 printDeclRecursive(typeddecl.Lhs, level+2, true)101 fprint(false, level+1, "%T Rhs[]:\n", typeddecl)102 printDeclRecursive(typeddecl.Rhs, level+2, true)103 case *ast.CallExpr:104 fprint(composite, level, "%T\n", typeddecl)105 fprint(false, level+1, "%T Fun:\n", typeddecl)106 printDeclRecursive(typeddecl.Fun, level+2, false)107 if len(typeddecl.Args) > 0 {108 fprint(false, level+1, "%T Args[]:\n", typeddecl)109 printDeclRecursive(typeddecl.Args, level+2, true)110 }111 case *ast.SelectorExpr:112 x, okx := typeddecl.X.(*ast.Ident)113 if okx && x.Obj == nil && typeddecl.Sel.Obj == nil {114 fprint(composite, level, "%T %s.%s\n", typeddecl, x.Name, typeddecl.Sel.Name)115 } else {116 fprint(composite, level, "%T\n", typeddecl)117 fprint(false, level+1, "%T X:\n", typeddecl)118 printDeclRecursive(typeddecl.X, level+2, false)119 fprint(false, level+1, "%T Sel:\n", typeddecl)120 printDeclRecursive(typeddecl.Sel, level+2, false)121 }122 case *ast.KeyValueExpr:123 key, ok := typeddecl.Key.(*ast.Ident)124 value, ok2 := typeddecl.Value.(*ast.BasicLit)125 if ok && ok2 {126 fprint(composite, level, "ast.KeyValueExpr key=%+#v value=%v\n", key.Name, value.Value)127 } else {128 fprint(composite, level, "%T\n", typeddecl)129 fprint(false, level+1, "%T Key:\n", typeddecl)130 printDeclRecursive(typeddecl.Key, level+2, false)131 fprint(false, level+1, "%T Value:\n", typeddecl)132 printDeclRecursive(typeddecl.Value, level+2, false)133 }134 case *ast.CompositeLit:135 fprint(composite, level, "%T\n", typeddecl)136 fprint(false, level+1, "%T Type:\n", typeddecl)137 printDeclRecursive(typeddecl.Type, level+2, false)138 if len(typeddecl.Elts) > 0 {139 fprint(false, level+1, "%T Elts[]:\n", typeddecl)140 printDeclRecursive(typeddecl.Elts, level+2, true)141 }142 case *ast.BasicLit:143 fprint(composite, level, "ast.BasicLit kind=%+#v value=%v\n", typeddecl.Kind.String(), typeddecl.Value)144 case *ast.TypeSwitchStmt:145 fprint(composite, level, "%T\n", typeddecl)146 fprint(false, level+1, "%T Init:\n", typeddecl)147 printDeclRecursive(typeddecl.Init, level+2, false)148 fprint(false, level+1, "%T Body:\n", typeddecl)149 printDeclRecursive(typeddecl.Body, level+2, false)150 case *ast.CaseClause:151 fprint(composite, level, "%T\n", typeddecl)152 fprint(false, level+1, "%T Body:\n", typeddecl)153 printDeclRecursive(typeddecl.Body, level+2, false)154 if len(typeddecl.List) > 0 {155 fprint(false, level+1, "%T List[]:\n", typeddecl)156 printDeclRecursive(typeddecl.List, level+2, true)157 }158 // X159 case *ast.StarExpr:160 fprint(composite, level, "%T\n", typeddecl)161 fprint(false, level+1, "%T X:\n", typeddecl)162 printDeclRecursive(typeddecl.X, level+2, false)163 case *ast.ParenExpr:164 fprint(composite, level, "%T\n", typeddecl)165 fprint(false, level+1, "%T X:\n", typeddecl)166 printDeclRecursive(typeddecl.X, level+2, false)167 case *ast.IndexExpr:168 fprint(composite, level, "%T\n", typeddecl)169 fprint(false, level+1, "%T X:\n", typeddecl)170 printDeclRecursive(typeddecl.X, level+2, false)171 case *ast.TypeAssertExpr:172 fprint(composite, level, "%T\n", typeddecl)173 fprint(false, level+1, "%T X:\n", typeddecl)174 printDeclRecursive(typeddecl.X, level+2, false)175 case *ast.RangeStmt:176 fprint(composite, level, "%T\n", typeddecl)177 fprint(false, level+1, "%T Value:\n", typeddecl)178 printDeclRecursive(typeddecl.Value, level+2, false)179 fprint(false, level+1, "%T Body:\n", typeddecl)180 printDeclRecursive(typeddecl.Body, level+2, false)181 case *ast.IfStmt:182 fprint(composite, level, "%T\n", typeddecl)183 fprint(false, level+1, "%T Cond:\n", typeddecl)184 printDeclRecursive(typeddecl.Cond, level+2, false)185 fprint(false, level+1, "%T Body:\n", typeddecl)186 printDeclRecursive(typeddecl.Body, level+2, false)187 fprint(false, level+1, "%T Else:\n", typeddecl)188 printDeclRecursive(typeddecl.Else, level+2, false)189 case *ast.BinaryExpr:190 fprint(composite, level, "%T\n", typeddecl)191 fprint(false, level+1, "%T X:\n", typeddecl)192 printDeclRecursive(typeddecl.X, level+2, false)193 fprint(false, level+1, "%T Op: %s\n", typeddecl, typeddecl.Op.String())194 fprint(false, level+1, "%T Y:\n", typeddecl)195 printDeclRecursive(typeddecl.Y, level+2, false)196 case *ast.Ident:197 fprint(composite, level, "%T name=%s\n", typeddecl, typeddecl.Name)198 if typeddecl.Obj != nil {199 //fprint(false, level+1, "%T Object: %#+v\n", typeddecl, typeddecl.Obj)200 }201 case *ast.ImportSpec:202 fprint(composite, level, "%T name=%s value=%v\n", typeddecl, typeddecl.Name.String(), typeddecl.Path.Value)203 if typeddecl.Comment != nil {204 fprint(false, level+1, "%T Comment:\n", typeddecl)205 printDeclRecursive(typeddecl.Comment, level+2, false)206 }207 case *ast.ValueSpec:208 fprint(composite, level, "%T type=%s\n", typeddecl, typeddecl.Type)209 if len(typeddecl.Names) > 0 {210 fprint(false, level+1, "%T Names:\n", typeddecl)211 printDeclRecursive(typeddecl.Names, level+2, false)212 }213 if len(typeddecl.Values) > 0 {214 fprint(false, level+1, "%T Values:\n", typeddecl)215 printDeclRecursive(typeddecl.Values, level+2, false)216 }217 case *ast.Object:218 fprint(composite, level, "%T name=%s type=%v\n", typeddecl, typeddecl.Name, typeddecl.Type)219 if typeddecl.Decl != nil {220 fprint(false, level+1, "%T Decl: %+#v\n", typeddecl, typeddecl.Decl)221 printDeclRecursive(typeddecl.Decl, level+2, false)222 }223 if typeddecl.Data != nil {224 fprint(false, level+1, "%T Data: %+#v\n", typeddecl, typeddecl.Data)225 }226 case ast.Stmt:227 fprint(composite, level, "%+#v\n", typeddecl)228 case ast.Expr:229 fprint(composite, level, "%T %+#v\n", typeddecl, typeddecl)230 case ast.Decl:231 fprint(composite, level, "%T %+#v\n", typeddecl, typeddecl)232 default:233 fprint(composite, level, "%+#v\n", typeddecl)234 }235}...

Full Screen

Full Screen

recursive.go

Source:recursive.go Github

copy

Full Screen

2import (3 "github.com/ark-lang/ark/src/ast"4 "github.com/ark-lang/ark/src/util/log"5)6type RecursiveDefinitionCheck struct {7}8func (_ RecursiveDefinitionCheck) Name() string { return "recursive definition" }9func (v *RecursiveDefinitionCheck) Init(s *SemanticAnalyzer) {}10func (v *RecursiveDefinitionCheck) EnterScope(s *SemanticAnalyzer) {}11func (v *RecursiveDefinitionCheck) ExitScope(s *SemanticAnalyzer) {}12func (v *RecursiveDefinitionCheck) PostVisit(s *SemanticAnalyzer, n ast.Node) {}13func (v *RecursiveDefinitionCheck) Visit(s *SemanticAnalyzer, n ast.Node) {14 if typeDecl, ok := n.(*ast.TypeDecl); ok {15 typ := typeDecl.NamedType16 if ok, path := isTypeRecursive(typ); ok {17 s.Err(n, "Encountered recursive type definition")18 log.Errorln("semantic", "Path taken:")19 for _, typ := range path {20 log.Error("semantic", typ.TypeName())21 log.Error("semantic", " <- ")22 }23 log.Error("semantic", "%s\n\n", typ.TypeName())24 }25 }26}27func (v *RecursiveDefinitionCheck) Finalize(s *SemanticAnalyzer) {28}29func isTypeRecursive(typ ast.Type) (bool, []ast.Type) {30 typ = typ.ActualType()31 var check func(current ast.Type, path *[]ast.Type, traversed map[ast.Type]bool) bool32 check = func(current ast.Type, path *[]ast.Type, traversed map[ast.Type]bool) bool {33 switch current.(type) {34 case *ast.NamedType:35 if traversed[current] {36 return true37 }38 }39 switch typ := current.(type) {40 case ast.StructType:41 for _, mem := range typ.Members {42 if check(mem.Type.BaseType, path, traversed) {43 *path = append(*path, mem.Type.BaseType)...

Full Screen

Full Screen

Recursive

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := parser.ParseFile(fset, "1.go", nil, parser.AllErrors)4 if err != nil {5 fmt.Println("Error Occured")6 }7 ast.Print(fset, f)8}9import "fmt"10func main() {11 fmt.Println("Hello, world.")12}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful