aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/ast
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2017-08-18 18:38:07 +0200
committerDmitry Vyukov <dvyukov@google.com>2017-08-18 18:47:39 +0200
commit4802b0fb7440d76d78efc586b87ff6eea46b6b00 (patch)
tree7a1e39eb906a18e0be375f39ec45b0679cee39c4 /pkg/ast
parent19b893936bebc6189c7627d56d1dc454fbd42714 (diff)
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.
Diffstat (limited to 'pkg/ast')
-rw-r--r--pkg/ast/parser.go31
-rw-r--r--pkg/ast/scanner.go8
2 files changed, 36 insertions, 3 deletions
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()