How to use ParseGlob method of ast Package

Best Syzkaller code snippet using ast.ParseGlob

template.go

Source:template.go Github

copy

Full Screen

...46 return t.ParseTextGlob(line, args...)47 case "parseHtmlGlob":48 return t.ParseHtmlGlob(line, args...)49 case "parseGlob":50 return t.ParseGlob(line, args...)51 case "clone":52 return t.Clone(line, args...)53 case "definedTemplates":54 return t.DefinedTemplates(line, args...)55 case "delims":56 return t.Delims(line, args...)57 case "execute":58 return t.Execute(line, args...)59 case "executeTemplate":60 return t.ExecuteTemplate(line, args...)61 case "funcs":62 return t.Funcs(line, scope, args...)63 case "lookup":64 return t.Lookup(line, args...)65 case "name":66 return t.Name(line, args...)67 case "option":68 return t.Option(line, args...)69 case "templates":70 return t.Templates(line, args...)71 case "htmlEscape":72 return t.HTMLEscape(line, args...)73 case "htmlEscaper":74 return t.HTMLEscaper(line, args...)75 case "htmlEscapeString":76 return t.HTMLEscapeString(line, args...)77 case "jsEscapeString":78 return t.JSEscapeString(line, args...)79 case "jsEscape":80 return t.JSEscape(line, args...)81 case "jsEscaper":82 return t.JSEscaper(line, args...)83 case "urlQueryEscaper":84 return t.URLQueryEscaper(line, args...)85 }86 return NewError(line, NOMETHODERROR, method, t.Type())87}88func (t *TemplateObj) NewText(line string, args ...Object) Object {89 argLen := len(args)90 if argLen != 1 {91 return NewError(line, ARGUMENTERROR, "1", argLen)92 }93 strObj, ok := args[0].(*String)94 if !ok {95 return NewError(line, PARAMTYPEERROR, "first", "newText", "*String", args[0].Type())96 }97 t.TmplType = T_TEXT98 t.TextTemplate = text.New(strObj.String)99 return t100}101func (t *TemplateObj) NewHtml(line string, args ...Object) Object {102 argLen := len(args)103 if argLen != 1 {104 return NewError(line, ARGUMENTERROR, "1", argLen)105 }106 strObj, ok := args[0].(*String)107 if !ok {108 return NewError(line, PARAMTYPEERROR, "first", "newHtml", "*String", args[0].Type())109 }110 t.TmplType = T_HTML111 t.HTMLTemplate = html.New(strObj.String)112 return t113}114func (t *TemplateObj) New(line string, args ...Object) Object {115 argLen := len(args)116 if argLen != 2 {117 return NewError(line, ARGUMENTERROR, "2", argLen)118 }119 intObj, ok := args[0].(*Integer)120 if !ok {121 return NewError(line, PARAMTYPEERROR, "first", "new", "*Integer", args[0].Type())122 }123 tmplType := intObj.Int64124 if tmplType != T_TEXT && tmplType != T_HTML {125 return NewError(line, GENERICERROR, "First parameter of new() should be 0(text)|1(html).")126 }127 var name string = ""128 strObj, ok := args[1].(*String)129 if !ok {130 return NewError(line, PARAMTYPEERROR, "second", "new", "*String", args[1].Type())131 }132 name = strObj.String133 if tmplType == T_TEXT {134 t.TmplType = tmplType135 t.TextTemplate = text.New(name)136 } else if tmplType == T_HTML {137 t.TmplType = tmplType138 t.HTMLTemplate = html.New(name)139 }140 return t141}142func (t *TemplateObj) Parse(line string, args ...Object) Object {143 if len(args) != 1 {144 return NewError(line, ARGUMENTERROR, "1", len(args))145 }146 strObj, ok := args[0].(*String)147 if !ok {148 return NewError(line, PARAMTYPEERROR, "first", "parse", "*String", args[0].Type())149 }150 if t.TextTemplate == nil && t.HTMLTemplate == nil {151 return NewNil("Before calling parse(), you should first call 'new|parseFiles|parseGlob' function")152 }153 if t.TmplType == T_TEXT {154 temp, err := t.TextTemplate.Parse(strObj.String)155 if err != nil {156 return NewNil(err.Error())157 }158 return &TemplateObj{TmplType: t.TmplType, TextTemplate: temp}159 } else if t.TmplType == T_HTML {160 temp, err := t.HTMLTemplate.Parse(strObj.String)161 if err != nil {162 return NewNil(err.Error())163 }164 return &TemplateObj{TmplType: t.TmplType, HTMLTemplate: temp}165 }166 return NIL167}168func (t *TemplateObj) ParseTextFiles(line string, args ...Object) Object {169 if len(args) != 1 {170 return NewError(line, ARGUMENTERROR, "1", len(args))171 }172 strObj, ok := args[0].(*String)173 if !ok {174 return NewError(line, PARAMTYPEERROR, "first", "parseTextFiles", "*String", args[0].Type())175 }176 temp, err := text.ParseFiles(strObj.String)177 if err != nil {178 return NewNil(err.Error())179 }180 return &TemplateObj{TmplType: T_TEXT, TextTemplate: temp}181}182func (t *TemplateObj) ParseHtmlFiles(line string, args ...Object) Object {183 if len(args) != 1 {184 return NewError(line, ARGUMENTERROR, "1", len(args))185 }186 strObj, ok := args[0].(*String)187 if !ok {188 return NewError(line, PARAMTYPEERROR, "first", "parseHtmlFiles", "*String", args[0].Type())189 }190 temp, err := html.ParseFiles(strObj.String)191 if err != nil {192 return NewNil(err.Error())193 }194 return &TemplateObj{TmplType: T_HTML, HTMLTemplate: temp}195}196func (t *TemplateObj) ParseFiles(line string, args ...Object) Object {197 if len(args) != 2 {198 return NewError(line, ARGUMENTERROR, "2", len(args))199 }200 intObj, ok := args[0].(*Integer)201 if !ok {202 return NewError(line, PARAMTYPEERROR, "first", "parseFiles", "*Integer", args[0].Type())203 }204 tmplType := intObj.Int64205 if tmplType != T_TEXT && tmplType != T_HTML {206 return NewError(line, GENERICERROR, "First parameter of parseFiles() should be 0(text)|1(html).")207 }208 strObj, ok := args[1].(*String)209 if !ok {210 return NewError(line, PARAMTYPEERROR, "second", "parseFiles", "*String", args[1].Type())211 }212 if tmplType == T_TEXT {213 temp, err := text.ParseFiles(strObj.String)214 if err != nil {215 return NewNil(err.Error())216 }217 return &TemplateObj{TmplType: tmplType, TextTemplate: temp}218 } else if tmplType == T_HTML {219 temp, err := html.ParseFiles(strObj.String)220 if err != nil {221 return NewNil(err.Error())222 }223 return &TemplateObj{TmplType: tmplType, HTMLTemplate: temp}224 }225 return NIL226}227func (t *TemplateObj) ParseTextGlob(line string, args ...Object) Object {228 if len(args) != 1 {229 return NewError(line, ARGUMENTERROR, "1", len(args))230 }231 strObj, ok := args[0].(*String)232 if !ok {233 return NewError(line, PARAMTYPEERROR, "first", "parseTextGlob", "*String", args[0].Type())234 }235 temp, err := text.ParseGlob(strObj.String)236 if err != nil {237 return NewNil(err.Error())238 }239 return &TemplateObj{TmplType: t.TmplType, TextTemplate: temp}240}241func (t *TemplateObj) ParseHtmlGlob(line string, args ...Object) Object {242 if len(args) != 1 {243 return NewError(line, ARGUMENTERROR, "1", len(args))244 }245 strObj, ok := args[0].(*String)246 if !ok {247 return NewError(line, PARAMTYPEERROR, "first", "parseHtmlGlob", "*String", args[0].Type())248 }249 temp, err := html.ParseGlob(strObj.String)250 if err != nil {251 return NewNil(err.Error())252 }253 return &TemplateObj{TmplType: t.TmplType, HTMLTemplate: temp}254}255func (t *TemplateObj) ParseGlob(line string, args ...Object) Object {256 if len(args) != 1 {257 return NewError(line, ARGUMENTERROR, "1", len(args))258 }259 strObj, ok := args[0].(*String)260 if !ok {261 return NewError(line, PARAMTYPEERROR, "first", "parseGlob", "*String", args[0].Type())262 }263 if t.TmplType == T_TEXT {264 temp, err := text.ParseGlob(strObj.String)265 if err != nil {266 return NewNil(err.Error())267 }268 return &TemplateObj{TmplType: t.TmplType, TextTemplate: temp}269 } else if t.TmplType == T_HTML {270 temp, err := html.ParseGlob(strObj.String)271 if err != nil {272 return NewNil(err.Error())273 }274 return &TemplateObj{TmplType: t.TmplType, HTMLTemplate: temp}275 }276 return NIL277}278func (t *TemplateObj) Clone(line string, args ...Object) Object {279 if len(args) != 0 {280 return NewError(line, ARGUMENTERROR, "0", len(args))281 }282 if t.TextTemplate == nil && t.HTMLTemplate == nil {283 return NewNil("Before calling clone(), you should first call 'new|parseFiles|parseGlob' function")284 }...

Full Screen

Full Screen

cmd.go

Source:cmd.go Github

copy

Full Screen

1package cmd2import (3 "context"4 "fmt"5 "io"6 "os"7 "strings"8 "sync"9 "github.com/gobwas/glob"10 "github.com/gobwas/glob/syntax"11 "github.com/gobwas/glob/syntax/ast"12 "github.com/grailbio/base/errors"13 "github.com/grailbio/base/file"14)15var commands = []struct {16 name string17 callback func(ctx context.Context, out io.Writer, args []string) error18 help string19}{20 {"cat", Cat, `Cat prints contents of the files to the stdout. It supports globs defined in https://github.com/gobwas/glob.`},21 {"put", Put, `Put stores stdin to the provided path.`},22 {"ls", Ls, `List files`},23 {"rm", Rm, `Rm removes files. It supports globs defined in https://github.com/gobwas/glob.`},24 {"cp", Cp, `Cp copies files. It can be invoked in three forms:251. cp src dst262. cp src dst/273. cp src.... dstdir28The first form first tries to copy file src to dst. If dst exists as a29directory, it copies src to dst/<base>, where <base> is the basename of the30source file.31The second form copies file src to dst/<base>.32The third form copies each of "src" to destdir/<base>.33This command supports globs defined in https://github.com/gobwas/glob.`},34}35func PrintHelp() {36 fmt.Fprintln(os.Stderr, "Subcommands:")37 for _, c := range commands {38 fmt.Fprintf(os.Stderr, "%s: %s\n", c.name, c.help)39 }40}41func Run(ctx context.Context, args []string) error {42 if len(args) == 0 {43 PrintHelp()44 return errors.E("No subcommand given")45 }46 for _, c := range commands {47 if c.name == args[0] {48 return c.callback(ctx, os.Stdout, args[1:])49 }50 }51 PrintHelp()52 return errors.E("unknown command", args[0])53}54const parallelism = 12855// forEachFile runs the callback for every file under the directory in56// parallel. It returns any of the errors returned by the callback.57func forEachFile(ctx context.Context, dir string, callback func(path string) error) error {58 err := errors.Once{}59 wg := sync.WaitGroup{}60 ch := make(chan string, parallelism*100)61 for i := 0; i < parallelism; i++ {62 wg.Add(1)63 go func() {64 for path := range ch {65 err.Set(callback(path))66 }67 wg.Done()68 }()69 }70 lister := file.List(ctx, dir, true /*recursive*/)71 for lister.Scan() {72 if !lister.IsDir() {73 ch <- lister.Path()74 }75 }76 close(ch)77 err.Set(lister.Err())78 wg.Wait()79 return err.Err()80}81// parseGlob parses a string that potentially contains glob metacharacters, and82// returns (nonglobprefix, hasglob). If the string does not contain any glob83// metacharacter, this function returns (str, false). Else, it returns the84// prefix of path elements up to the element containing a glob character.85//86// For example, parseGlob("foo/bar/baz*/*.txt" returns ("foo/bar", true).87func parseGlob(str string) (string, bool) {88 node, err := syntax.Parse(str)89 if err != nil {90 panic(err)91 }92 if node.Kind != ast.KindPattern || len(node.Children) == 0 {93 panic(node)94 }95 if node.Children[0].Kind != ast.KindText {96 return "", true97 }98 if len(node.Children) == 1 {99 return str, false100 }101 nonGlobPrefix := node.Children[0].Value.(ast.Text).Text102 if i := strings.LastIndexByte(nonGlobPrefix, '/'); i > 0 {103 nonGlobPrefix = nonGlobPrefix[:i+1]104 }105 return nonGlobPrefix, true106}107// expandGlob expands the given glob string. If the string does not contain a108// glob metacharacter, or on any error, it returns {str}.109func expandGlob(ctx context.Context, str string) []string {110 nonGlobPrefix, hasGlob := parseGlob(str)111 if !hasGlob {112 return []string{str}113 }114 m, err := glob.Compile(str)115 if err != nil {116 return []string{str}117 }118 globSuffix := str[len(nonGlobPrefix):]119 if strings.HasSuffix(globSuffix, "/") {120 globSuffix = globSuffix[:len(globSuffix)-1]121 }122 recursive := len(strings.Split(globSuffix, "/")) > 1 || strings.Contains(globSuffix, "**")123 lister := file.List(ctx, nonGlobPrefix, recursive)124 matches := []string{}125 for lister.Scan() {126 if m.Match(lister.Path()) {127 matches = append(matches, lister.Path())128 }129 }130 if err := lister.Err(); err != nil {131 return []string{str}132 }133 if len(matches) == 0 {134 return []string{str}135 }136 return matches137}138// expandGlobs calls expandGlob on each string and unions the results.139func expandGlobs(ctx context.Context, patterns []string) []string {140 matches := []string{}141 for _, pattern := range patterns {142 matches = append(matches, expandGlob(ctx, pattern)...)143 }144 return matches145}...

Full Screen

Full Screen

ParseGlob

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fset := token.NewFileSet()4 f, err := parser.ParseFile(fset, "1.go", nil, parser.ImportsOnly)5 if err != nil {6 log.Fatal(err)7 }8 fmt.Println("Imports:")9 for _, s := range f.Imports {10 fmt.Println(s.Path.Value)11 }12}13import (14func main() {15 f, err := parser.ParseFile(fset, "1.go", nil, parser.ParseComments)16 if err != nil {17 log.Fatal(err)18 }19 fmt.Println("Imports:")20 for _, s := range f.Imports {21 fmt.Println(s.Path.Value)22 }23 fmt.Println("Doc:")24 fmt.Println(f.Doc.Text())25 fmt.Println("GenDecl.Doc:")26 fmt.Println(f.Decls[0].(*ast.GenDecl).Doc.Text())27 fmt.Println("FuncDecl.Doc:")28 fmt.Println(f.Decls[1].(*ast.FuncDecl).Doc.Text())29 fmt.Println("Field.Doc:")30 fmt.Println(f.Decls[0].(*ast.GenDecl).Specs[0].(*ast.ValueSpec).Names[0].Obj.Decl.(*ast.Field).Doc.Text())31 fmt.Println("ValueSpec.Doc:")32 fmt.Println(f.Decls[0].(*ast.GenDecl

Full Screen

Full Screen

ParseGlob

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 files, err := filepath.Glob("*.go")4 if err != nil {5 fmt.Println(err)6 }7 astFiles, err := parser.ParseFiles(fset, files...)8 if err != nil {9 fmt.Println(err)10 }11 for _, file := range astFiles {12 for _, s := range file.Imports {13 fmt.Println(s.Path.Value)14 }15 }16}17import (18func main() {19 files, err := filepath.Glob("*.go")20 if err != nil {21 fmt.Println(err)22 }23 astFiles, err := parser.ParseFiles(fset, files...)24 if err != nil {25 fmt.Println(err)26 }27 for _, file := range astFiles {28 for _, s := range file.Imports {29 fmt.Println(s.Path.Value)30 }31 }32}33import (34func main() {35 files, err := filepath.Glob("*.go")36 if err != nil {37 fmt.Println(err)38 }

Full Screen

Full Screen

ParseGlob

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 tmpl := template.New("test")4 tmpl, err := tmpl.ParseGlob("*.gohtml")5 if err != nil {6 log.Fatalln(err)7 }8 err = tmpl.ExecuteTemplate(os.Stdout, "one.gohtml", nil)9 if err != nil {10 log.Fatalln(err)11 }12}

Full Screen

Full Screen

ParseGlob

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 http.HandleFunc("/", index)4 http.HandleFunc("/about", about)5 http.HandleFunc("/contact", contact)6 http.Handle("/favicon.ico", http.NotFoundHandler())7 http.ListenAndServe(":8080", nil)8}9func index(w http.ResponseWriter, req *http.Request) {10 tpl, err := template.ParseGlob("templates/*.gohtml")11 if err != nil {12 log.Fatalln(err)13 }14 err = tpl.ExecuteTemplate(w, "index.gohtml", nil)15 if err != nil {16 log.Fatalln(err)17 }18}19func about(w http.ResponseWriter, req *http.Request) {20 tpl, err := template.ParseGlob("templates/*.gohtml")21 if err != nil {22 log.Fatalln(err)23 }24 err = tpl.ExecuteTemplate(w, "about.gohtml", nil)25 if err != nil {26 log.Fatalln(err)27 }28}29func contact(w http.ResponseWriter, req *http.Request) {30 tpl, err := template.ParseGlob("templates/*.gohtml")31 if err != nil {32 log.Fatalln(err)33 }34 err = tpl.ExecuteTemplate(w, "contact.gohtml", nil)35 if err != nil {36 log.Fatalln(err)37 }38}39import (40func main() {41 http.HandleFunc("/", index)42 http.HandleFunc("/about", about)43 http.HandleFunc("/contact", contact)44 http.Handle("/favicon.ico", http.NotFoundHandler())45 http.ListenAndServe(":8080", nil)46}47func index(w http.ResponseWriter, req *http.Request) {48 tpl, err := template.ParseGlob("templates/*.gohtml")49 if err != nil {50 log.Fatalln(err)51 }52 err = tpl.ExecuteTemplate(w, "index.gohtml", nil)53 if err != nil {54 log.Fatalln(err)55 }56}57func about(w http.ResponseWriter, req *http.Request) {58 tpl, err := template.ParseGlob("templates/*.gohtml")59 if err != nil {

Full Screen

Full Screen

ParseGlob

Using AI Code Generation

copy

Full Screen

1import (2func init() {3 templates = template.Must(template.ParseGlob("templates/*.gohtml"))4}5func main() {6 http.HandleFunc("/", index)7 http.HandleFunc("/about", about)8 http.HandleFunc("/contact", contact)9 http.HandleFunc("/apply", apply)10 log.Fatal(http.ListenAndServe(":8080", nil))11}12func index(w http.ResponseWriter, r *http.Request) {13 templates.ExecuteTemplate(w, "index.gohtml", nil)14}15func about(w http.ResponseWriter, r *http.Request) {16 templates.ExecuteTemplate(w, "about.gohtml", nil)17}18func contact(w http.ResponseWriter, r *http.Request) {19 templates.ExecuteTemplate(w, "contact.gohtml", nil)20}21func apply(w http.ResponseWriter, r *http.Request) {22 templates.ExecuteTemplate(w, "apply.gohtml", nil)23}

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