Skip to content

Commit

Permalink
syntax: don't let Lit/LitWord leak to errors
Browse files Browse the repository at this point in the history
These are internal terms and not useful to the user. Printing the actual
value of the literal is more helpful.
  • Loading branch information
mvdan committed Nov 8, 2016
1 parent 5df05d1 commit 706914c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
15 changes: 13 additions & 2 deletions syntax/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,23 @@ func (p *parser) stmtEnd(n Node, start, end string) Pos {
return pos
}

func (p *parser) descTok() string {
switch p.tok {
case _Lit, _LitWord:
return strconv.Quote(p.val)
default:
return p.tok.String()
}
}

func (p *parser) quoteErr(lpos Pos, quote token) {
p.posErr(lpos, "reached %s without closing quote %s", p.tok, quote)
p.posErr(lpos, "reached %s without closing quote %s",
p.descTok(), quote)
}

func (p *parser) matchingErr(lpos Pos, left, right interface{}) {
p.posErr(lpos, "reached %s without matching %s with %s", p.tok, left, right)
p.posErr(lpos, "reached %s without matching %s with %s",
p.descTok(), left, right)
}

func (p *parser) matched(lpos Pos, left, right token) Pos {
Expand Down
4 changes: 2 additions & 2 deletions syntax/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ var bashTests = []errorCase{
},
{
"echo ${foo[1 2]}",
`1:11: reached LitWord without matching [ with ]`,
`1:11: reached "2" without matching [ with ]`,
},
{
"echo ${foo[}",
Expand All @@ -911,7 +911,7 @@ var bashTests = []errorCase{
},
{
"echo ${foo:1 2} #INVBASH lazy eval",
`1:6: reached LitWord without matching ${ with }`,
`1:6: reached "2" without matching ${ with }`,
},
{
"echo ${foo:1",
Expand Down

0 comments on commit 706914c

Please sign in to comment.