]> Cypherpunks repositories - gostls13.git/commitdiff
add ParseDeclList
authorRuss Cox <rsc@golang.org>
Fri, 4 Sep 2009 00:01:10 +0000 (17:01 -0700)
committerRuss Cox <rsc@golang.org>
Fri, 4 Sep 2009 00:01:10 +0000 (17:01 -0700)
R=austin
DELTA=34  (34 added, 0 deleted, 0 changed)
OCL=34280
CL=34352

src/pkg/go/parser/interface.go
src/pkg/go/parser/parser.go

index 94835b8eceb3dbf544c14eaff299f34ed2f0874c..d840e9a4a02390c28ed1e225fcba3d63213aa896 100644 (file)
@@ -8,9 +8,11 @@ package parser
 
 import (
        "bytes";
+       "container/vector";
        "fmt";
        "go/ast";
        "go/scanner";
+       "go/token";
        "io";
        "os";
        pathutil "path";
@@ -86,6 +88,24 @@ func ParseStmtList(filename string, src interface{}) ([]ast.Stmt, os.Error) {
 }
 
 
+// ParseDeclList parses a list of Go declarations and returns the list
+// of corresponding AST nodes.  The filename and src arguments have the same
+// interpretation as for ParseFile. If there is an error, the node
+// list may be nil or contain partial ASTs.
+//
+func ParseDeclList(filename string, src interface{}) ([]ast.Decl, os.Error) {
+       data, err := readSource(filename, src);
+       if err != nil {
+               return nil, err;
+       }
+
+       var p parser;
+       p.init(filename, data, 0);
+       list := p.parseDeclList();  // TODO 6g bug - function call order in expr lists
+       return list, p.GetError(scanner.Sorted);
+}
+
+
 // ParseFile parses a Go source file and returns a File node.
 //
 // If src != nil, ParseFile parses the file source from src. src may
index f9d38a4b0c9b51d3f959b38b52cfeaab5ec9f6f2..de44ed865184df329ccec7df683dd4df47c435a6 100644 (file)
@@ -1889,6 +1889,20 @@ func (p *parser) parseDecl(getSemi bool) (decl ast.Decl, gotSemi bool) {
 }
 
 
+func (p *parser) parseDeclList() []ast.Decl {
+       var list vector.Vector;
+       for p.tok != token.EOF {
+               decl, _ := p.parseDecl(true);   // consume optional semicolon
+               list.Push(decl);
+       }
+       decls := make([]ast.Decl, list.Len());
+       for i := 0; i < list.Len(); i++ {
+               decls[i] = list.At(i).(ast.Decl);
+       }
+       return decls;
+}
+
+
 // ----------------------------------------------------------------------------
 // Source files