]> Cypherpunks repositories - gostls13.git/commitdiff
Add entry points for parsing statements and expressions.
authorAustin Clements <aclements@csail.mit.edu>
Tue, 7 Jul 2009 22:47:01 +0000 (15:47 -0700)
committerAustin Clements <aclements@csail.mit.edu>
Tue, 7 Jul 2009 22:47:01 +0000 (15:47 -0700)
R=gri
APPROVED=gri
DELTA=73  (48 added, 0 deleted, 25 changed)
OCL=31302
CL=31308

src/pkg/go/parser/parser.go

index 273b36607be2064fae5db2dc19e0ce3932c0d044..7c7f8c32bd8b94164e4bdbab132411c95ae548f0 100644 (file)
@@ -1998,7 +1998,7 @@ func (p *parser) parsePackage() *ast.Program {
 
 
 // ----------------------------------------------------------------------------
-// Parsing of entire programs.
+// Parser entry points.
 
 func readSource(src interface{}) ([]byte, os.Error) {
        if src != nil {
@@ -2034,6 +2034,39 @@ func scannerMode(mode uint) uint {
 }
 
 
+func (p *parser) init(src interface{}, mode uint) os.Error {
+       data, err := readSource(src);
+       if err != nil {
+               return err;
+       }
+
+       // initialize parser state
+       p.errors.Init(0);
+       p.scanner.Init(data, p, scannerMode(mode));
+       p.mode = mode;
+       p.trace = mode & Trace != 0;  // for convenience (p.trace is used frequently)
+       p.comments.Init(0);
+       p.next();
+
+       return nil;
+}
+
+
+// errorList converts parsing errors to an errors list.  Returns nil
+// if there are no errors.
+func (p *parser) errorList() os.Error {
+       if p.errors.Len() == 0 {
+               return nil;
+       }
+
+       errors := make(ErrorList, p.errors.Len());
+       for i := 0; i < p.errors.Len(); i++ {
+               errors[i] = p.errors.At(i).(*Error);
+       }
+       return errors;
+}
+
+
 // Parse parses a Go program.
 //
 // The program source src may be provided in a variety of formats. At the
@@ -2049,31 +2082,46 @@ func scannerMode(mode uint) uint {
 // describing the syntax errors.
 //
 func Parse(src interface{}, mode uint) (*ast.Program, os.Error) {
-       data, err := readSource(src);
-       if err != nil {
+       var p parser;
+       if err := p.init(src, mode); err != nil {
                return nil, err;
        }
 
-       // initialize parser state
+       prog := p.parsePackage();
+
+       return prog, p.errorList();
+}
+
+
+// ParseStmts parses a list of Go statement.
+func ParseStmts(src interface{}, mode uint) ([]ast.Stmt, os.Error) {
+       if mode & (PackageClauseOnly | ImportsOnly) != 0 {
+               return nil, nil;
+       }
+
        var p parser;
-       p.errors.Init(0);
-       p.scanner.Init(data, &p, scannerMode(mode));
-       p.mode = mode;
-       p.trace = mode & Trace != 0;  // for convenience (p.trace is used frequently)
-       p.comments.Init(0);
-       p.next();
+       if err := p.init(src, mode); err != nil {
+               return nil, err;
+       }
 
-       // parse program
-       prog := p.parsePackage();
+       stmts := p.parseStatementList();
 
-       // convert errors list, if any
-       if p.errors.Len() > 0 {
-               errors := make(ErrorList, p.errors.Len());
-               for i := 0; i < p.errors.Len(); i++ {
-                       errors[i] = p.errors.At(i).(*Error);
-               }
-               return prog, errors;
+       return stmts, p.errorList();
+}
+
+
+// ParseExpr parses a single Go expression.
+func ParseExpr(src interface{}, mode uint) (ast.Expr, os.Error) {
+       if mode & (PackageClauseOnly | ImportsOnly) != 0 {
+               return nil, nil;
+       }
+
+       var p parser;
+       if err := p.init(src, mode); err != nil {
+               return nil, err;
        }
 
-       return prog, nil;
+       expr := p.parseExpression();
+
+       return expr, p.errorList();
 }