Document util package

This commit is contained in:
Cheer Xiao 2014-01-30 22:04:16 +08:00
parent 95b0be333f
commit ad90094ac6
5 changed files with 21 additions and 13 deletions

View File

@ -217,11 +217,7 @@ func (p *Parser) pipeline() *ListNode {
// Form = TermList { [ space ] Redir } [ space ] // Form = TermList { [ space ] Redir } [ space ]
func (p *Parser) form() *FormNode { func (p *Parser) form() *FormNode {
fm := newForm(p.peekNonSpace().Pos) fm := newForm(p.peekNonSpace().Pos)
*p.Ctx = Context{CommandContext, ""}
fm.Command = p.term() fm.Command = p.term()
if p.peekNonSpace().Typ != ItemEOF {
*p.Ctx = Context{}
}
fm.Args = p.termList() fm.Args = p.termList()
loop: loop:
for { for {
@ -240,9 +236,13 @@ func (p *Parser) termList() *ListNode {
list := newList(p.peek().Pos) list := newList(p.peek().Pos)
loop: loop:
for { for {
if startsFactor(p.peekNonSpace().Typ) { switch t := p.peekNonSpace().Typ; {
case startsFactor(t):
list.append(p.term()) list.append(p.term())
} else { case t == ItemEOF:
*p.Ctx = Context{NewTermContext, ""}
fallthrough
default:
break loop break loop
} }
} }
@ -313,7 +313,9 @@ func (p *Parser) factor() (fn *FactorNode) {
if token.Typ != ItemBare { if token.Typ != ItemBare {
p.unexpected(token, "factor of variable") p.unexpected(token, "factor of variable")
} }
*p.Ctx = Context{VariableNameContext, token.Val} if p.peek().Typ == ItemEOF {
*p.Ctx = Context{VariableNameContext, token.Val}
}
fn.Typ = VariableFactor fn.Typ = VariableFactor
fn.Node = newString(token.Pos, token.Val, token.Val) fn.Node = newString(token.Pos, token.Val, token.Val)
return return
@ -323,12 +325,10 @@ func (p *Parser) factor() (fn *FactorNode) {
// XXX Errors with unterminated quoted string // XXX Errors with unterminated quoted string
p.errorf(int(token.Pos), "%s", err) p.errorf(int(token.Pos), "%s", err)
} }
if token.End&MayContinue != 0 { if p.peek().Typ == ItemEOF {
if p.Ctx.Typ == NewTermContext { // if token.End&MayContinue != 0 {
// XXX All command arguments are assumed to be FilenameContext // XXX All terms are assumed to be filenames
p.Ctx.Typ = FilenameContext *p.Ctx = Context{FilenameContext, token.Val}
}
p.Ctx.Prefix = token.Val
} }
fn.Typ = StringFactor fn.Typ = StringFactor
fn.Node = newString(token.Pos, token.Val, text) fn.Node = newString(token.Pos, token.Val, text)

View File

@ -1,5 +1,7 @@
package util package util
// CeilDiv computes ceil(float(a)/b) but does not actually use float
// arithmetics.
func CeilDiv(a, b int) int { func CeilDiv(a, b int) int {
return (a + b - 1) / b return (a + b - 1) / b
} }

View File

@ -7,10 +7,13 @@ type exception struct {
err error err error
} }
// Panic panics with err wrapped properly so that it can be catched by Recover.
func Panic(err error) { func Panic(err error) {
panic(exception{err}) panic(exception{err})
} }
// Recover tries to catch an error thrown by Panic and stop the panic. If the
// panic is not caused by Panic, the panic is not stopped.
func Recover(perr *error) { func Recover(perr *error) {
r := recover() r := recover()
if r == nil { if r == nil {

View File

@ -5,6 +5,8 @@ import (
"strings" "strings"
) )
// Getwd returns path of the working directory in a format suitable as the
// prompt.
func Getwd() string { func Getwd() string {
pwd, err := os.Getwd() pwd, err := os.Getwd()
if err != nil { if err != nil {

View File

@ -13,6 +13,7 @@ var (
FdTooBig = errors.New("fd exceeds FD_SETSIZE") FdTooBig = errors.New("fd exceeds FD_SETSIZE")
) )
// TimedReader provides the facility of reading from a fd with timeout.
type TimedReader struct { type TimedReader struct {
File *os.File File *os.File
Timeout time.Duration Timeout time.Duration