pqarrays

Paddy 2016-02-25 Parent:bfe2a4af6bdf Child:9a415db0346a

1:ce9c92fc81ab Go to Latest

pqarrays/parser.go

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.

History
     1.1 --- a/parser.go	Sun Apr 19 23:47:36 2015 -0400
     1.2 +++ b/parser.go	Thu Feb 25 23:52:05 2016 -0800
     1.3 @@ -43,7 +43,7 @@
     1.4  func parseEOF(l *lexer, parsed chan *string) (parseFunc, error) {
     1.5  	tok := l.nextToken()
     1.6  	if tok.typ == tokenWhitespace {
     1.7 -		tok = l.nextToken()
     1.8 +		return parseEOF, nil
     1.9  	}
    1.10  	if tok.typ != tokenEOF {
    1.11  		return nil, errors.New("expected EOF, got " + tok.typ.String())
    1.12 @@ -54,7 +54,7 @@
    1.13  func parseStringOrNull(l *lexer, parsed chan *string) (parseFunc, error) {
    1.14  	tok := l.nextToken()
    1.15  	if tok.typ == tokenWhitespace {
    1.16 -		tok = l.nextToken()
    1.17 +		return parseStringOrNull, nil
    1.18  	} else if tok.typ == tokenString {
    1.19  		parsed <- &tok.val
    1.20  		return parseSeparatorOrDelim, nil
    1.21 @@ -65,6 +65,22 @@
    1.22  	return nil, errors.New("expected string, got " + tok.typ.String())
    1.23  }
    1.24  
    1.25 +func parseStringOrNullOrEnd(l *lexer, parsed chan *string) (parseFunc, error) {
    1.26 +	tok := l.nextToken()
    1.27 +	if tok.typ == tokenWhitespace {
    1.28 +		return parseStringOrNullOrEnd, nil
    1.29 +	} else if tok.typ == tokenString {
    1.30 +		parsed <- &tok.val
    1.31 +		return parseSeparatorOrDelim, nil
    1.32 +	} else if tok.typ == tokenNull {
    1.33 +		parsed <- nil
    1.34 +		return parseSeparatorOrDelim, nil
    1.35 +	} else if tok.typ == tokenArrayEnd {
    1.36 +		return parseEOF, nil
    1.37 +	}
    1.38 +	return nil, errors.New("Expected string or end, got " + tok.typ.String())
    1.39 +}
    1.40 +
    1.41  func parseSeparatorOrDelim(l *lexer, parsed chan *string) (parseFunc, error) {
    1.42  	tok := l.nextToken()
    1.43  	if tok.typ == tokenWhitespace {
    1.44 @@ -82,7 +98,7 @@
    1.45  	if tok.typ == tokenWhitespace {
    1.46  		return parseStart, nil
    1.47  	} else if tok.typ == tokenArrayStart {
    1.48 -		return parseStringOrNull, nil
    1.49 +		return parseStringOrNullOrEnd, nil
    1.50  	}
    1.51  	return nil, errors.New("expected separator or delim, got " + tok.typ.String())
    1.52  }