Commit 17bc8361 authored by Austin Clements's avatar Austin Clements

Add entry points for parsing statements and expressions.

R=gri
APPROVED=gri
DELTA=73  (48 added, 0 deleted, 25 changed)
OCL=31302
CL=31308
parent 668fa7f6
...@@ -1998,7 +1998,7 @@ func (p *parser) parsePackage() *ast.Program { ...@@ -1998,7 +1998,7 @@ func (p *parser) parsePackage() *ast.Program {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Parsing of entire programs. // Parser entry points.
func readSource(src interface{}) ([]byte, os.Error) { func readSource(src interface{}) ([]byte, os.Error) {
if src != nil { if src != nil {
...@@ -2034,6 +2034,39 @@ func scannerMode(mode uint) uint { ...@@ -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. // Parse parses a Go program.
// //
// The program source src may be provided in a variety of formats. At the // The program source src may be provided in a variety of formats. At the
...@@ -2049,31 +2082,46 @@ func scannerMode(mode uint) uint { ...@@ -2049,31 +2082,46 @@ func scannerMode(mode uint) uint {
// describing the syntax errors. // describing the syntax errors.
// //
func Parse(src interface{}, mode uint) (*ast.Program, os.Error) { func Parse(src interface{}, mode uint) (*ast.Program, os.Error) {
data, err := readSource(src); var p parser;
if err != nil { if err := p.init(src, mode); err != nil {
return nil, err; 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; var p parser;
p.errors.Init(0); if err := p.init(src, mode); err != nil {
p.scanner.Init(data, &p, scannerMode(mode)); return nil, err;
p.mode = mode; }
p.trace = mode & Trace != 0; // for convenience (p.trace is used frequently)
p.comments.Init(0);
p.next();
// parse program stmts := p.parseStatementList();
prog := p.parsePackage();
// convert errors list, if any return stmts, p.errorList();
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); // ParseExpr parses a single Go expression.
} func ParseExpr(src interface{}, mode uint) (ast.Expr, os.Error) {
return prog, errors; 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();
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment