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

View File

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

View File

@ -7,10 +7,13 @@ type exception struct {
err error
}
// Panic panics with err wrapped properly so that it can be catched by Recover.
func Panic(err error) {
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) {
r := recover()
if r == nil {

View File

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

View File

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