2014-11-27 08:06:42 +08:00
|
|
|
//===- parser.go - parser wrapper -----------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-11-27 08:06:42 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains functions for calling the parser in an appropriate way for
|
|
|
|
// llgo.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-12-31 08:25:32 +08:00
|
|
|
package driver
|
2014-11-27 08:06:42 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/parser"
|
|
|
|
"go/scanner"
|
|
|
|
"go/token"
|
|
|
|
)
|
|
|
|
|
|
|
|
func parseFile(fset *token.FileSet, filename string) (*ast.File, error) {
|
2014-12-31 08:25:32 +08:00
|
|
|
// Retain comments; this is important for annotation processing.
|
2014-11-27 08:06:42 +08:00
|
|
|
mode := parser.DeclarationErrors | parser.ParseComments
|
|
|
|
return parser.ParseFile(fset, filename, nil, mode)
|
|
|
|
}
|
|
|
|
|
2014-12-31 08:25:32 +08:00
|
|
|
func ParseFiles(fset *token.FileSet, filenames []string) ([]*ast.File, error) {
|
2014-11-27 08:06:42 +08:00
|
|
|
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
|
|
|
|
}
|