easyjson icon indicating copy to clipboard operation
easyjson copied to clipboard

IsDelim wrongly returns true for syntax error

Open eli-darkly opened this issue 3 years ago • 0 comments

This Lexer method appears to have behavior inconsistent with its documentation.

// IsDelim returns true if there was no scanning error and next token is the given delimiter.
func (r *Lexer) IsDelim(c byte) bool {
	if r.token.kind == tokenUndef && r.Ok() {
		r.FetchToken()
	}
	return !r.Ok() || r.token.delimValue == c
}

The problem is in the last line: !r.Ok() || means that if parsing failed (so Ok() is false), then it will return true, even though the delimiter was not found. That's contrary to "true if there was no scanning error and next token is the given delimiter." And regardless of the documentation, it also doesn't seem like very useful behavior: if I want to know whether the next token is c, why would I want an invalid token to be considered a match?

It seems to me that !r.Ok() || should be changed to this: r.Ok() &&

eli-darkly avatar Oct 26 '20 03:10 eli-darkly