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 {
// ----------------------------------------------------------------------------
// 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();
}
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