github.com/axw/llgo@v0.0.0-20160805011314-95b5fe4dca20/driver/parser.go (about)

     1  //===- 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  
    15  package driver
    16  
    17  import (
    18  	"fmt"
    19  	"go/ast"
    20  	"go/parser"
    21  	"go/scanner"
    22  	"go/token"
    23  )
    24  
    25  func parseFile(fset *token.FileSet, filename string) (*ast.File, error) {
    26  	// Retain comments; this is important for annotation processing.
    27  	mode := parser.DeclarationErrors | parser.ParseComments
    28  	return parser.ParseFile(fset, filename, nil, mode)
    29  }
    30  
    31  func ParseFiles(fset *token.FileSet, filenames []string) ([]*ast.File, error) {
    32  	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  }