parse: Remove unused funcs; test Quote.

This commit is contained in:
Qi Xiao 2016-02-20 17:03:53 +01:00
parent 5a1fbf814b
commit 09b77f6127
2 changed files with 33 additions and 20 deletions

View File

@ -999,12 +999,6 @@ func addSep(n Node, ps *parser) {
addChild(n, &Sep{node{nil, begin, ps.pos, ps.src[begin:ps.pos], nil}})
}
func eatRun(ps *parser, r rune) {
for ps.peek() == r {
ps.next()
}
}
func parseSep(n Node, ps *parser, sep rune) bool {
if ps.peek() == sep {
ps.next()
@ -1014,21 +1008,18 @@ func parseSep(n Node, ps *parser, sep rune) bool {
return false
}
func parseRunAsSep(n Node, ps *parser, isSep func(rune) bool) {
if !isSep(ps.peek()) {
func parseSpaces(n Node, ps *parser) {
// TODO parse comments here.
if !isSpace(ps.peek()) {
return
}
ps.next()
for isSep(ps.peek()) {
for isSpace(ps.peek()) {
ps.next()
}
addSep(n, ps)
}
func parseSpaces(n Node, ps *parser) {
parseRunAsSep(n, ps, isSpace)
}
// Helpers.
func addChild(p Node, ch Node) {
@ -1036,16 +1027,13 @@ func addChild(p Node, ch Node) {
ch.n().parent = p
}
type runePred func(rune) bool
func (rp runePred) matches(r rune) bool {
return rp != nil && rp(r)
}
// Quote returns a representation of s in elvish syntax. Bareword is tried
// first, then single quoted string and finally double quoted string.
func Quote(s string) string {
bare := true
if s == "" {
return `""`
}
bare := s[0] != '~'
for _, r := range s {
if !unicode.IsPrint(r) && r != '\n' {
return quoteDouble(s)

View File

@ -341,3 +341,28 @@ func TestParseError(t *testing.T) {
}
}
}
var quoteTests = []struct {
text, quoted string
}{
// Empty string is quoted with double quote.
{"", `""`},
// Bareword when possible.
{"x-y,z@h/d", "x-y,z@h/d"},
// Single quote when there is special char but no unprintable.
{"x$y[]\nef'", "'x$y[]\nef'''"},
// Tilde needs quoting only when appearing at the beginning
{"~x", "'~x'"},
{"x~", "x~"},
// Double quote when there is unprintable char.
{"\x1b\"\\", `"\x1b\"\\"`},
}
func TestQuote(t *testing.T) {
for _, tc := range quoteTests {
got := Quote(tc.text)
if got != tc.quoted {
t.Errorf("Quote(%q) => %s, want %s", tc.text, got, tc.quoted)
}
}
}