How to use parseTypeDef method of ast Package

Best Syzkaller code snippet using ast.parseTypeDef

typedef_test.go

Source:typedef_test.go Github

copy

Full Screen

1// Copyright (c) 2015 Uber Technologies, Inc.2//3// Permission is hereby granted, free of charge, to any person obtaining a copy4// of this software and associated documentation files (the "Software"), to deal5// in the Software without restriction, including without limitation the rights6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell7// copies of the Software, and to permit persons to whom the Software is8// furnished to do so, subject to the following conditions:9//10// The above copyright notice and this permission notice shall be included in11// all copies or substantial portions of the Software.12//13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN19// THE SOFTWARE.20package compile21import (22 "fmt"23 "testing"24 "github.com/stretchr/testify/assert"25 "go.uber.org/thriftrw/ast"26 "go.uber.org/thriftrw/idl"27 "go.uber.org/thriftrw/wire"28)29func parseTypedef(s string) *ast.Typedef {30 prog, err := idl.Parse([]byte(s))31 if err != nil {32 panic(fmt.Sprintf("failure to parse: %v: %s", err, s))33 }34 if len(prog.Definitions) != 1 {35 panic("parseTypedef may be used to parse a single typedef only")36 }37 return prog.Definitions[0].(*ast.Typedef)38}39func TestCompileTypedef(t *testing.T) {40 tests := []struct {41 src string42 scope Scope43 code wire.Type44 spec *TypedefSpec45 }{46 {47 `typedef i64 (js.type = "Long") timestamp (foo = "bar")`,48 nil,49 wire.TI64,50 &TypedefSpec{51 Name: "timestamp",52 File: "test.thrift",53 Target: &I64Spec{Annotations: Annotations{"js.type": "Long"}},54 Annotations: Annotations{"foo": "bar"},55 },56 },57 {58 "typedef Bar Foo",59 scope("Bar", &TypedefSpec{60 Name: "Bar",61 File: "test.thrift",62 Target: &I32Spec{},63 }),64 wire.TI32,65 &TypedefSpec{66 Name: "Foo",67 File: "test.thrift",68 Target: &TypedefSpec{69 Name: "Bar",70 File: "test.thrift",71 Target: &I32Spec{},72 },73 },74 },75 }76 for _, tt := range tests {77 expected := mustLink(t, tt.spec, defaultScope)78 src := parseTypedef(tt.src)79 typedefSpec, err := compileTypedef("test.thrift", src)80 if !assert.NoError(t, err, tt.src) {81 continue82 }83 scope := scopeOrDefault(tt.scope)84 spec, err := typedefSpec.Link(scope)85 if assert.NoError(t, err) {86 assert.Equal(t, tt.code, spec.TypeCode())87 assert.Equal(t, expected, spec)88 }89 }90}91func TestCompileTypedefFailure(t *testing.T) {92 tests := []struct {93 desc string94 src string95 scope Scope96 messages []string97 }{98 {99 "unknown type",100 "typedef foo bar",101 nil,102 []string{103 `could not resolve reference "foo"`,104 },105 },106 {107 "conflicting annotations",108 `typedef i32 bar (a = "b", a, b)`,109 nil,110 []string{111 `cannot compile "bar" on line 1:`,112 `annotation conflict: the name "a" has already been used on line 1`,113 },114 },115 }116 for _, tt := range tests {117 src := parseTypedef(tt.src)118 scope := scopeOrDefault(tt.scope)119 spec, err := compileTypedef("test.thrift", src)120 if err == nil {121 _, err = spec.Link(scope)122 }123 assert.Error(t, err, tt.desc)124 for _, msg := range tt.messages {125 assert.Contains(t, err.Error(), msg, tt.desc)126 }127 }128}...

Full Screen

Full Screen

parser.go

Source:parser.go Github

copy

Full Screen

...116 p.expect(token.IDENT)117 }118 return ast.Ident{Name: name}119}120func (p *parser) parseTypeDef() ast.TypeDef {121 if !p.tok.IsTypeDef() {122 p.errorf("expected one of %v, got %q", token.TypeDefTokens(), p.tok)123 p.next()124 }125 switch p.tok {126 case token.RECORD:127 return p.parseRecord()128 case token.INTERFACE:129 return p.parseInterface()130 case token.ENUM:131 return p.parseEnum(false)132 case token.FLAGS:133 return p.parseEnum(true)134 default:135 return nil136 }137}138// All decls should be in the form IDENT = KEYWORD [EXT] { }139func (p *parser) parseDecl() (decl ast.TypeDecl) {140 decl.Ident = p.parseIdent()141 p.expect(token.ASSIGN)142 decl.Body = p.parseTypeDef()143 return144}145func (p *parser) parseFile() *ast.IDLFile {146 // import decls147 var imports []string148 for p.tok == token.IMPORT {149 imports = append(imports, p.parseImport())150 }151 // rest of body152 var decls []ast.TypeDecl153 for p.tok != token.EOF {154 decls = append(decls, p.parseDecl())155 }156 return &ast.IDLFile{...

Full Screen

Full Screen

typedef.go

Source:typedef.go Github

copy

Full Screen

1package ast2// Typedef struct3type Typedef struct {4 Addr Address5 Type string6 ChildNodes []Node7}8func parseTypedef(line string) *Typedef {9 groups := groupsFromRegex(10 "'(?P<type>.*)'",11 line,12 )13 return &Typedef{14 Addr: ParseAddress(groups["address"]),15 Type: groups["type"],16 ChildNodes: []Node{},17 }18}19// AddChild adds a new child node. Child nodes can then be accessed with the20// Children attribute.21func (n *Typedef) AddChild(node Node) {22 n.ChildNodes = append(n.ChildNodes, node)23}24// Address returns the numeric address of the node. See the documentation for25// the Address type for more information.26func (n *Typedef) Address() Address {27 return n.Addr28}29// Children returns the child nodes. If this node does not have any children or30// this node does not support children it will always return an empty slice.31func (n *Typedef) Children() []Node {32 return n.ChildNodes33}34// Position returns the position in the original source code.35func (n *Typedef) Position() Position {36 return Position{}37}...

Full Screen

Full Screen

parseTypeDef

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := parser.ParseFile(fset, "2.go", nil, 0)4 if err != nil {5 fmt.Println(err)6 }7 for _, d := range f.Decls {8 switch d := d.(type) {9 for _, s := range d.Specs {10 switch s := s.(type) {11 fmt.Println(s.Name.Name)12 fmt.Println(s.Type)13 }14 }15 }16 }17}18import (19func main() {20 f, err := parser.ParseFile(fset, "2.go", nil, 0)21 if err != nil {22 fmt.Println(err)23 }24 for _, d := range f.Decls {25 switch d := d.(type) {26 for _, s := range d.Specs {27 switch s := s.(type) {28 fmt.Println(s.Name.Name)29 fmt.Println(s.Type)30 }31 }32 }33 }34}35import (36func main() {37 f, err := parser.ParseFile(fset, "2.go", nil,

Full Screen

Full Screen

parseTypeDef

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(err)6 }7 ast.Print(fset, f)8}9import (10func main() {11 f, err := parser.ParseFile(fset, "1.go", nil, parser.AllErrors)12 if err != nil {13 fmt.Println(err)14 }15 ast.Print(fset, f)16}17import (18func main() {19 f, err := parser.ParseFile(fset, "1.go", nil, parser.AllErrors)20 if err != nil {21 fmt.Println(err)22 }23 ast.Print(fset, f)24}25import (26func main() {27 f, err := parser.ParseFile(fset, "1.go", nil, parser.AllErrors)28 if err != nil {29 fmt.Println(err)30 }31 ast.Print(fset, f)32}33import (34func main() {35 f, err := parser.ParseFile(fset, "1.go", nil, parser.AllErrors)36 if err != nil {37 fmt.Println(err)38 }

Full Screen

Full Screen

parseTypeDef

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := parser.ParseFile(fset, "2.go", nil, 0)4 if err != nil {5 fmt.Println(err)6 }7 ast.Print(fset, f)8 for _, d := range f.Decls {9 if g, ok := d.(*ast.GenDecl); ok {10 for _, s := range g.Specs {11 if t, ok := s.(*ast.TypeSpec); ok {12 fmt.Println(t.Name.Name)13 }14 }15 }16 }17}18import (19func main() {20 f, err := parser.ParseFile(fset, "2.go", nil, 0)21 if err != nil {22 fmt.Println(err)23 }24 ast.Print(fset, f)25 for _, d := range f.Decls {26 if g, ok := d.(*ast.GenDecl); ok {27 for _, s := range g.Specs {28 if t, ok := s.(*ast.TypeSpec); ok {29 fmt.Println(t.Name.Name)30 }31 }32 }33 }34}

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