Skip to content

Commit

Permalink
More descriptive error messages for invalid lvalues
Browse files Browse the repository at this point in the history
For '(a) = b': 'syntax error' -> 'expected statement'
For 'a, (b) = c': 'syntax error' -> 'expected identifier or field'
For 'a, b() = c': 'syntax error' -> 'expected call or indexing'
  • Loading branch information
mpeterv committed Jan 4, 2016
1 parent bfc0ef5 commit c4b0770
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions spec/parser_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ describe("parser", function()
assert.same({line = 1, column = 2, end_column = 2, msg = "expected '=' near <eof>"}, get_error("a"))
assert.same({line = 1, column = 5, end_column = 5, msg = "expected expression near <eof>"}, get_error("a = "))
assert.same({line = 1, column = 5, end_column = 5, msg = "expected statement near '='"}, get_error("a() = b"))
assert.same({line = 1, column = 5, end_column = 5, msg = "syntax error near '='"}, get_error("(a) = b"))
assert.same({line = 1, column = 1, end_column = 1, msg = "expected statement near '('"}, get_error("(a) = b"))
assert.same({line = 1, column = 1, end_column = 1, msg = "expected statement near '1'"}, get_error("1 = b"))
end)

Expand All @@ -412,8 +412,8 @@ describe("parser", function()
assert.same({line = 1, column = 4, end_column = 4, msg = "expected identifier or field near '='"}, get_error("a, = b"))
assert.same({line = 1, column = 8, end_column = 8, msg = "expected expression near <eof>"}, get_error("a, b = "))
assert.same({line = 1, column = 10, end_column = 10, msg = "expected expression near <eof>"}, get_error("a, b = c,"))
assert.same({line = 1, column = 8, end_column = 8, msg = "syntax error near '='"}, get_error("a, b() = c"))
assert.same({line = 1, column = 8, end_column = 8, msg = "syntax error near '='"}, get_error("a, (b) = c"))
assert.same({line = 1, column = 8, end_column = 8, msg = "expected call or indexing near '='"}, get_error("a, b() = c"))
assert.same({line = 1, column = 4, end_column = 4, msg = "expected identifier or field near '('"}, get_error("a, (b) = c"))
end)
end)

Expand Down
6 changes: 3 additions & 3 deletions src/luacheck/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ local function token_name(token)
end

local function parse_error(state, msg)
msg = msg or "syntax error"
local token_repr, end_column

if state.token == "eof" then
Expand Down Expand Up @@ -626,18 +625,19 @@ local function parse_expression_statement(state, loc)

repeat
local first_token = state.token
local first_loc = lhs and location(state) or loc
local expected = lhs and "identifier or field" or "statement"
local primary_expression, is_prefix = parse_primary_expression(state, expected)

if is_prefix and first_token == "(" then
-- (expr) is invalid.
parse_error(state)
lexer.syntax_error(first_loc, first_loc.column, "expected " .. expected .. " near '('")
end

if primary_expression.tag == "Call" or primary_expression.tag == "Invoke" then
if lhs then
-- This is an assingment, and a call is not a valid lvalue.
parse_error(state)
parse_error(state, "expected call or indexing")
else
-- It is a call.
primary_expression.location = loc
Expand Down

0 comments on commit c4b0770

Please sign in to comment.