diff options
| author | Peter Collingbourne <peter@pcc.me.uk> | 2014-12-31 00:25:32 +0000 |
|---|---|---|
| committer | Peter Collingbourne <peter@pcc.me.uk> | 2014-12-31 00:25:32 +0000 |
| commit | 9350942b20bf1e6e2b182159f5837ba209485972 (patch) | |
| tree | 667c09eccb6f856dea1f5e9d640655faf15dbb82 /llgo/driver/parser.go | |
| parent | bc405294f047be809dc58639ad4d4af692b8bfaa (diff) | |
| download | bcm5719-llvm-9350942b20bf1e6e2b182159f5837ba209485972.tar.gz bcm5719-llvm-9350942b20bf1e6e2b182159f5837ba209485972.zip | |
irgen, driver: modify Compiler.Compile to take a FileSet and Files
This change allows clients to generate IR using "files" received from locations
other than the file system. The regular file parser is moved to a new library,
"driver", which is intended to eventually contain much of the logic from
the existing driver.
Differential Revision: http://reviews.llvm.org/D6794
llvm-svn: 225026
Diffstat (limited to 'llgo/driver/parser.go')
| -rw-r--r-- | llgo/driver/parser.go | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/llgo/driver/parser.go b/llgo/driver/parser.go new file mode 100644 index 00000000000..07600f41d44 --- /dev/null +++ b/llgo/driver/parser.go @@ -0,0 +1,43 @@ +//===- parser.go - parser wrapper -----------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file contains functions for calling the parser in an appropriate way for +// llgo. +// +//===----------------------------------------------------------------------===// + +package driver + +import ( + "fmt" + "go/ast" + "go/parser" + "go/scanner" + "go/token" +) + +func parseFile(fset *token.FileSet, filename string) (*ast.File, error) { + // Retain comments; this is important for annotation processing. + mode := parser.DeclarationErrors | parser.ParseComments + return parser.ParseFile(fset, filename, nil, mode) +} + +func ParseFiles(fset *token.FileSet, filenames []string) ([]*ast.File, error) { + files := make([]*ast.File, len(filenames)) + for i, filename := range filenames { + file, err := parseFile(fset, filename) + if _, ok := err.(scanner.ErrorList); ok { + return nil, err + } else if err != nil { + return nil, fmt.Errorf("%q: %v", filename, err) + } + files[i] = file + } + return files, nil +} |

