mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-05 03:17:50 +08:00
parse: Remove unused funcs; test Quote.
This commit is contained in:
parent
5a1fbf814b
commit
09b77f6127
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user