add err field to exprErr

This commit is contained in:
Brian Picciano 2016-07-23 20:26:26 -06:00
parent 0de50c47ba
commit 09ab9399e5

View File

@ -221,12 +221,18 @@ func (b Block) Equal(e Expr) bool {
type exprErr struct { type exprErr struct {
reason string reason string
err error
tok lexer.Token tok lexer.Token
tokCtx string // e.g. "block starting at" or "open paren at" tokCtx string // e.g. "block starting at" or "open paren at"
} }
func (e exprErr) Error() string { func (e exprErr) Error() string {
msg := e.reason var msg string
if e.err != nil {
msg = e.err.Error()
} else {
msg = e.reason
}
if err := e.tok.Err(); err != nil { if err := e.tok.Err(); err != nil {
msg += " - token error: " + err.Error() msg += " - token error: " + err.Error()
} else if (e.tok != lexer.Token{}) { } else if (e.tok != lexer.Token{}) {
@ -272,6 +278,8 @@ func sliceEnclosedToks(toks []lexer.Token, start, end lexer.Token) ([]lexer.Toke
} }
} }
// TODO we want to be able to have like ParseAsBlock as well
func Parse(r io.Reader) ([]Expr, error) { func Parse(r io.Reader) ([]Expr, error) {
toks := readAllToks(r) toks := readAllToks(r)
var ret []Expr var ret []Expr
@ -391,8 +399,7 @@ func parseIdentifier(t lexer.Token) (Expr, error) {
n, err := strconv.ParseInt(t.Val, 10, 64) n, err := strconv.ParseInt(t.Val, 10, 64)
if err != nil { if err != nil {
return nil, exprErr{ return nil, exprErr{
reason: "error parsing number", err: err,
// TODO err: err,
tok: t, tok: t,
} }
} }
@ -412,8 +419,7 @@ func parseString(t lexer.Token) (Expr, error) {
str, err := strconv.Unquote(t.Val) str, err := strconv.Unquote(t.Val)
if err != nil { if err != nil {
return nil, exprErr{ return nil, exprErr{
reason: "error parsing string", err: err,
// TODO err: err,
tok: t, tok: t,
} }
} }