Fix bug parsing empty arrays, make golint and go vet happy.
Add comments to make golint happy. Also, because comments are a good thing to
have.
Turn += 1 and -= 1 into ++ and --, respectively, so golint will be happy.
Fix an improperly formated errorf, where a rune was being treated as a string.
Thanks, go vet!
Fix whitespace parsing, returning the parse functions again instead of just
skipping the one character. Now if we have more than one whitespace character in
a row, they'll all be skipped.
Add a parseStringOrNullOrEnd parse function that will be called after the
tokenArrayStart character, to fix a bug where empty arrays were expecting a
string or null and getting the array end character. This is only valid after
tokenArrayStart, however; in other places where parseSeparatorOrDelim is used,
it wouldn't be appropriate.
Add a parser test for an empty array.
7 func parse(l *lexer) ([]*string, error) {
9 pchan := make(chan *string)
10 errchan := make(chan error)
11 done := make(chan struct{})
12 go runParse(l, pchan, errchan, done)
15 case err := <-errchan:
18 parsed = append(parsed, item)
25 func runParse(l *lexer, parsed chan *string, err chan error, done chan struct{}) {
26 var state parseFunc = parseStart
29 state, e = state(l, parsed)
41 type parseFunc func(*lexer, chan *string) (parseFunc, error)
43 func parseEOF(l *lexer, parsed chan *string) (parseFunc, error) {
45 if tok.typ == tokenWhitespace {
48 if tok.typ != tokenEOF {
49 return nil, errors.New("expected EOF, got " + tok.typ.String())
54 func parseStringOrNull(l *lexer, parsed chan *string) (parseFunc, error) {
56 if tok.typ == tokenWhitespace {
57 return parseStringOrNull, nil
58 } else if tok.typ == tokenString {
60 return parseSeparatorOrDelim, nil
61 } else if tok.typ == tokenNull {
63 return parseSeparatorOrDelim, nil
65 return nil, errors.New("expected string, got " + tok.typ.String())
68 func parseStringOrNullOrEnd(l *lexer, parsed chan *string) (parseFunc, error) {
70 if tok.typ == tokenWhitespace {
71 return parseStringOrNullOrEnd, nil
72 } else if tok.typ == tokenString {
74 return parseSeparatorOrDelim, nil
75 } else if tok.typ == tokenNull {
77 return parseSeparatorOrDelim, nil
78 } else if tok.typ == tokenArrayEnd {
81 return nil, errors.New("Expected string or end, got " + tok.typ.String())
84 func parseSeparatorOrDelim(l *lexer, parsed chan *string) (parseFunc, error) {
86 if tok.typ == tokenWhitespace {
87 return parseSeparatorOrDelim, nil
88 } else if tok.typ == tokenSeparator {
89 return parseStringOrNull, nil
90 } else if tok.typ == tokenArrayEnd {
93 return nil, errors.New("expected separator or delim, got " + tok.typ.String())
96 func parseStart(l *lexer, parsed chan *string) (parseFunc, error) {
98 if tok.typ == tokenWhitespace {
99 return parseStart, nil
100 } else if tok.typ == tokenArrayStart {
101 return parseStringOrNullOrEnd, nil
103 return nil, errors.New("expected separator or delim, got " + tok.typ.String())