Skip to content

Instantly share code, notes, and snippets.

@franko
Created October 21, 2015 06:51
Show Gist options
  • Save franko/c964d3439c029670670e to your computer and use it in GitHub Desktop.
Save franko/c964d3439c029670670e to your computer and use it in GitHub Desktop.
parser snippet expr_primary
-- Parse primary expression.
function expr_primary(ast, ls)
local v, vk
-- Parse prefix expression.
if ls.token == '(' then
local line = ls.linenumber
ls:next()
vk, v = 'expr', ast:expr_brackets(expr(ast, ls))
lex_match(ls, ')', '(', line)
elseif ls.token == 'TK_name' or (not LJ_52 and ls.token == 'TK_goto') then
vk, v = 'var', var_lookup(ast, ls)
else
err_syntax(ls, "unexpected symbol")
end
while true do -- Parse multiple expression suffixes.
local line = ls.linenumber
if ls.token == '.' then
vk, v = 'indexed', expr_field(ast, ls, v)
elseif ls.token == '[' then
local key = expr_bracket(ast, ls, true)
vk, v = 'indexed', ast:expr_index(v, key)
elseif ls.token == ':' then
ls:next()
local key = lex_str(ls)
local args = parse_args(ast, ls)
vk, v = 'call', ast:expr_method_call(v, key, args, line)
elseif ls.token == '(' or ls.token == 'TK_string' or ls.token == '{' then
local args = parse_args(ast, ls)
vk, v = 'call', ast:expr_function_call(v, args, line)
else
break
end
end
return v, vk
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment