blob: 07600f41d441af51a6069881388f9104cd1f177d [file] [log] [blame]
Peter Collingbournead9841e2014-11-27 00:06:42 +00001//===- parser.go - parser wrapper -----------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains functions for calling the parser in an appropriate way for
11// llgo.
12//
13//===----------------------------------------------------------------------===//
14
Peter Collingbourne93509422014-12-31 00:25:32 +000015package driver
Peter Collingbournead9841e2014-11-27 00:06:42 +000016
17import (
18 "fmt"
19 "go/ast"
20 "go/parser"
21 "go/scanner"
22 "go/token"
23)
24
25func parseFile(fset *token.FileSet, filename string) (*ast.File, error) {
Peter Collingbourne93509422014-12-31 00:25:32 +000026 // Retain comments; this is important for annotation processing.
Peter Collingbournead9841e2014-11-27 00:06:42 +000027 mode := parser.DeclarationErrors | parser.ParseComments
28 return parser.ParseFile(fset, filename, nil, mode)
29}
30
Peter Collingbourne93509422014-12-31 00:25:32 +000031func ParseFiles(fset *token.FileSet, filenames []string) ([]*ast.File, error) {
Peter Collingbournead9841e2014-11-27 00:06:42 +000032 files := make([]*ast.File, len(filenames))
33 for i, filename := range filenames {
34 file, err := parseFile(fset, filename)
35 if _, ok := err.(scanner.ErrorList); ok {
36 return nil, err
37 } else if err != nil {
38 return nil, fmt.Errorf("%q: %v", filename, err)
39 }
40 files[i] = file
41 }
42 return files, nil
43}