Introduce shorthand function parse.Parse

This commit is contained in:
Cheer Xiao 2014-01-28 12:10:21 +08:00
parent 33643163a3
commit 2ee8efc1b4
2 changed files with 13 additions and 3 deletions

View File

@ -52,14 +52,13 @@ func main() {
panic(lr.Err)
}
p := parse.NewParser(name)
pe := p.Parse(lr.Line, false)
n, pe := parse.Parse(name, lr.Line)
if pe != nil {
fmt.Print(pe.(*util.ContextualError).Pprint())
continue
}
ee := ev.Eval(name, lr.Line, p.Root)
ee := ev.Eval(name, lr.Line, n)
if ee != nil {
fmt.Println(ee.(*util.ContextualError).Pprint())
continue

View File

@ -150,6 +150,17 @@ func (p *Parser) Parse(text string, tab bool) (err error) {
return nil
}
// Parse is a shorthand for constructing a Paser, call Parse and take out its
// Root.
func Parse(name, text string) (Node, error) {
p := NewParser(name)
err := p.Parse(text, false)
if err != nil {
return nil, err
}
return p.Root, nil
}
// parse parses a chunk and ensures there are no trailing tokens
func (p *Parser) parse() *ListNode {
chunk := p.chunk()