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 var testInputs = map[string][]token{
8 ``: []token{{typ: tokenError, val: "expected array to start before "}},
9 `{}`: []token{{typ: tokenArrayStart, val: "{"}, {typ: tokenArrayEnd, val: "}"}},
10 `{lions}`: []token{{typ: tokenArrayStart, val: "{"}, {typ: tokenString, val: "lions"}, {typ: tokenArrayEnd, val: "}"}},
11 `{lions,tigers}`: []token{{typ: tokenArrayStart, val: "{"}, {typ: tokenString, val: "lions"}, {typ: tokenSeparator, val: ","}, {typ: tokenString, val: "tigers"}, {typ: tokenArrayEnd, val: "}"}},
12 `{lions,tigers,bears}`: []token{{typ: tokenArrayStart, val: "{"}, {typ: tokenString, val: "lions"}, {typ: tokenSeparator, val: ","}, {typ: tokenString, val: "tigers"}, {typ: tokenSeparator, val: ","}, {typ: tokenString, val: "bears"}, {typ: tokenArrayEnd, val: "}"}},
13 `{lions,tigers,bears,"oh my!"}`: []token{{typ: tokenArrayStart, val: "{"}, {typ: tokenString, val: "lions"}, {typ: tokenSeparator, val: ","}, {typ: tokenString, val: "tigers"}, {typ: tokenSeparator, val: ","}, {typ: tokenString, val: "bears"}, {typ: tokenSeparator, val: ","}, {typ: tokenString, val: "oh my!"}, {typ: tokenArrayEnd, val: "}"}},
14 `{{two,dimensional},{array,"of items"}}`: []token{{typ: tokenArrayStart, val: "{"}, {typ: tokenArrayStart, val: "{"}, {typ: tokenString, val: "two"}, {typ: tokenSeparator, val: ","}, {typ: tokenString, val: "dimensional"}, {typ: tokenArrayEnd, val: "}"}, {typ: tokenSeparator, val: ","}, {typ: tokenArrayStart, val: "{"}, {typ: tokenString, val: "array"}, {typ: tokenSeparator, val: ","}, {typ: tokenString, val: "of items"}, {typ: tokenArrayEnd, val: "}"}, {typ: tokenArrayEnd, val: "}"}},
17 func TestInputsTable(t *testing.T) {
18 for input, expectedTokens := range testInputs {
23 if tok.typ == tokenEOF {
26 tokens = append(tokens, tok)
27 if tok.typ == tokenError {
31 t.Logf("%#+v\n", tokens)
32 if len(tokens) != len(expectedTokens) {
33 t.Fatalf("Expected %d tokens, got %d\n", len(expectedTokens), len(tokens))
35 for pos, tok := range tokens {
36 if expectedTokens[pos].typ != tok.typ {
37 t.Errorf("Expected token in pos %d to have type of %s, got %s instead.", pos, expectedTokens[pos].typ, tok.typ)
39 if expectedTokens[pos].val != tok.val {
40 t.Errorf("Expected token in pos %d to have value of `%s`, got `%s` instead.", pos, expectedTokens[pos].val, tok.val)