From 4802b0fb7440d76d78efc586b87ff6eea46b6b00 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Fri, 18 Aug 2017 18:38:07 +0200 Subject: sys/syz-sysgen: switch to new parser For now we just generate the old structs from the new AST. But this allows to delete the old parser entirely. --- pkg/ast/parser.go | 31 +++++++++++++++++++++++++++++++ pkg/ast/scanner.go | 8 +++++--- 2 files changed, 36 insertions(+), 3 deletions(-) (limited to 'pkg') diff --git a/pkg/ast/parser.go b/pkg/ast/parser.go index 10f389424..fc05378b2 100644 --- a/pkg/ast/parser.go +++ b/pkg/ast/parser.go @@ -6,6 +6,8 @@ package ast import ( "errors" "fmt" + "io/ioutil" + "path/filepath" "strconv" "strings" ) @@ -41,6 +43,35 @@ func Parse(data []byte, filename string, errorHandler func(pos Pos, msg string)) return } +func ParseGlob(glob string, errorHandler func(pos Pos, msg string)) (top []interface{}, ok bool) { + if errorHandler == nil { + errorHandler = loggingHandler + } + files, err := filepath.Glob(glob) + if err != nil { + errorHandler(Pos{}, fmt.Sprintf("failed to find input files: %v", err)) + return nil, false + } + if len(files) == 0 { + errorHandler(Pos{}, fmt.Sprintf("no files matched by glob %q", glob)) + return nil, false + } + ok = true + for _, f := range files { + data, err := ioutil.ReadFile(f) + if err != nil { + errorHandler(Pos{}, fmt.Sprintf("failed to read input file: %v", err)) + return nil, false + } + top1, ok1 := Parse(data, filepath.Base(f), errorHandler) + if !ok1 { + ok = false + } + top = append(top, top1...) + } + return +} + type parser struct { s *scanner diff --git a/pkg/ast/scanner.go b/pkg/ast/scanner.go index d3e35895c..372d2df3e 100644 --- a/pkg/ast/scanner.go +++ b/pkg/ast/scanner.go @@ -103,9 +103,7 @@ type scanner struct { func newScanner(data []byte, filename string, errorHandler func(pos Pos, msg string)) *scanner { if errorHandler == nil { - errorHandler = func(pos Pos, msg string) { - fmt.Fprintf(os.Stderr, "%v:%v:%v: %v\n", pos.File, pos.Line, pos.Col, msg) - } + errorHandler = loggingHandler } s := &scanner{ data: data, @@ -117,6 +115,10 @@ func newScanner(data []byte, filename string, errorHandler func(pos Pos, msg str return s } +func loggingHandler(pos Pos, msg string) { + fmt.Fprintf(os.Stderr, "%v:%v:%v: %v\n", pos.File, pos.Line, pos.Col, msg) +} + func (s *scanner) Scan() (tok token, lit string, pos Pos) { s.skipWhitespace() pos = s.pos() -- cgit mrf-deployment