Skip to content

Commit

Permalink
Upgraded metalua to v0.7.2.
Browse files Browse the repository at this point in the history
  • Loading branch information
pkulchenko committed Jun 9, 2014
1 parent b0eabd6 commit 0775e1b
Show file tree
Hide file tree
Showing 33 changed files with 2,581 additions and 5,268 deletions.
33 changes: 8 additions & 25 deletions lualibs/luainspect/ast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,7 @@

--! require 'luainspect.typecheck' (context)

-- boilerplate/utility
-- LUA_PATH="?.lua;/path/to/metalua/src/compiler/?.lua;/path/to/metalua/src/lib/?.lua"
-- import modules -- order is important
require "lexer"
require "gg"
require "mlp_lexer"
require "mlp_misc"
require "mlp_table"
require "mlp_meta"
require "mlp_expr"
require "mlp_stat"
--require "mlp_ext"
_G.mlc = {} -- make gg happy
-- Metalua:IMPROVE: make above imports simpler
local mlc = require 'metalua.compiler'.new()

local M = {}

Expand Down Expand Up @@ -102,10 +89,7 @@ end
-- FIX? filename currently ignored in Metalua
-- CATEGORY: Lua parsing
local function ast_from_string_helper(src, filename)
filename = filename or '(string)'
local lx = mlp.lexer:newstream (src, filename)
local ast = mlp.chunk(lx)
return ast
return mlc:src_to_ast(src, filename)
end


Expand Down Expand Up @@ -370,7 +354,7 @@ function M.get_keywords(ast, src)
-- Some binary operations have arguments reversed from lexical order.
-- For example, `a > b` becomes `Op{'lt', `Id 'b', `Id 'a'}
local oast =
(ast.tag == 'Op' and #ast == 3 and ast[2].lineinfo.first[3] > ast[3].lineinfo.first[3])
(ast.tag == 'Op' and #ast == 3 and tostring(ast[2].lineinfo.first):match('|L(%d+)') > tostring(ast[3].lineinfo.first):match('|L(%d+)'))
and {ast[1], ast[3], ast[2]} or ast

local i = 0
Expand All @@ -381,18 +365,17 @@ function M.get_keywords(ast, src)
-- Get position range [fpos,lpos] between subsequent children.
local fpos
if i == 0 then -- before first child
fpos = ast.lineinfo.first[3]
fpos = tostring(ast.lineinfo.first):match('|L(%d+)')
else
local last = oast[i].lineinfo.last; local c = last.comments
fpos = (c and #c > 0 and c[#c][3] or last[3]) + 1
fpos = (c and #c > 0 and c[#c][3] or tostring(last):match('|L(%d+)')) + 1
end
local lpos
if j == #ast+1 then -- after last child
lpos = ast.lineinfo.last[3]
lpos = tostring(ast.lineinfo.last):match('|L(%d+)')
else
local first = oast[j].lineinfo.first; local c = first.comments
--DEBUG('first', ast.tag, first[3], src:sub(first[3], first[3]+3))
lpos = (c and #c > 0 and c[1][2] or first[3]) - 1
lpos = (c and #c > 0 and c[1][2] or tostring(first):match('|L(%d+)')) - 1
end

-- Find keyword in range.
Expand Down Expand Up @@ -441,7 +424,7 @@ function M.ast_to_tokenlist(top_ast, src)
if isterminal[ast.tag] then -- Extract terminal
local token = ast
if ast.lineinfo then
token.fpos, token.lpos, token.ast = ast.lineinfo.first[3], ast.lineinfo.last[3], ast
token.fpos, token.lpos, token.ast = tostring(ast.lineinfo.first):match('|L(%d+)'), tostring(ast.lineinfo.last):match('|L(%d+)'), ast
table.insert(tokens, token)
end
else -- Extract non-terminal
Expand Down
6 changes: 3 additions & 3 deletions lualibs/luainspect/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ function M.infer_values(top_ast, tokenlist, src, report)
end
-- Any call to require is handled specially (source analysis).
if func == require and type(argvalues[1]) == 'string' then
local spath = ast.lineinfo.first[4] -- a HACK? relies on AST lineinfo
local spath = tostring(ast.lineinfo.first):gsub('<C|','<'):match('<([^|]+)') -- a HACK? relies on AST lineinfo
local val = M.require_inspect(argvalues[1], report, spath:gsub('[^\\/]+$', ''))
if known(val) and val ~= nil then
ast.value = val
Expand Down Expand Up @@ -820,7 +820,7 @@ function M.infer_values(top_ast, tokenlist, src, report)
local x
local val = function() x=nil end
local fpos = LA.ast_pos_range(ast, tokenlist)
local source = ast.lineinfo.first[4] -- a HACK? relies on AST lineinfo
local source = tostring(ast.lineinfo.first):gsub('<C|','<'):match('<([^|]+)') -- a HACK? relies on AST lineinfo
local linenum = LA.pos_to_linecol(fpos, src)
local retvals
if ENABLE_RETURN_ANALYSIS then
Expand Down Expand Up @@ -1269,7 +1269,7 @@ function M.ast_to_definition_position(ast, tokenlist)
if local_ast then
local tidx = LA.ast_idx_range_in_tokenlist(tokenlist, local_ast)
if tidx then
local spath = ast.lineinfo.first[4] -- a HACK? using lineinfo
local spath = tostring(ast.lineinfo.first):gsub('<C|','<'):match('<([^|]+)') -- a HACK? using lineinfo
fpos = tokenlist[tidx].fpos; path = spath
end
end
Expand Down

0 comments on commit 0775e1b

Please sign in to comment.