Visual Studio 语法检查

lua-users home
wiki


[!] 版本说明: 以下代码适用于较旧的 Lua 版本,Lua 4。它不能在 Lua 5 下直接运行。现代版本可以基于 在 Visual Studio 中编译 Lua 脚本


以下是我(PeterPrade)在 MS Visual Studio 6.0 中用于对 Lua 文件进行语法检查的脚本文件。

将其命名为 LuaCheck.lua 并将其放在你的 lua 目录中。使用说明包含在文件中。


-- uses the lua command line compiler to check the syntax of a lua file
-- formats the output so as to MS DevStudio can understand the output
-- and jump to the next error line
--
-- how to make it work?
-- 
-- just set up a new Tool (Tools/Customize/Tools) with the following options:
-- Command           : lua.exe
-- Arguments         : -f LuaCheck.lua "$(FilePath)"
-- Initial directory : c:\lua (or wherever your lua.exe is located)
-- Use Output Window : (checked)
--
-- After that, you can add a keyboard shortcut for this tool:
-- (Tools/Customize/Tools) (select from Category: Tools, then from 
-- Commands: UserToolX (where X is the number of the newly defined tool)
-- then assign a new key to it...
--
-- Have Fun! 
-- Peter Prade

print("Lua Syntax Checking")
print("--------------------")
print()

-- get argument - the file name
if arg and arg[1] then
  file = arg[1]
  if strsub(file,-4,-1) ~= ".lua" then
    print("warning: file has no .lua extension!")
  end
end

-- define 3 tool functions out of my "standardlibrary":
function readfile(name)
  local f = openfile(name, "rt")
  local s = read(f, "*a")
  closefile(f)
  return s
end

function tfind(t, s)
  return foreachi(t, function(i, v) if v==%s then return i end end)
end

function tadd(t, v) 
  if not tfind(t, v) then tinsert(t, v) end
end

-- reformat errors so that visual studio understands them:
function outputerror(msg, last, line, file)
  print(file .. "(" .. line .. "): error: " .. msg )
  -- check if the error refers to another line:
  gsub(msg, "%((.-) at line (.-)%)%;$", function(msg, line)
    print(%file .. "(" .. line .. "): error: ... " .. msg ) 
  end)
  print(file .. "(" .. line .. "): error: " .. last)
end

-- format list of globals nicely:
function printnice(t)
  for i = 1, getn(t) do
    write(t[i])
    if i < getn(t)-1 then 
      write(", ")
    elseif i < getn(t) then 
      write(" and ")
    end
    if mod(i,5)==0 then 
      write("\n") 
    end
  end
  if mod(getn(t),5)~=0 then 
    write("\n") 
  end
end

if file then -- check the specified file:
  print("Calling lua compiler with "..file.." ...")
  retval = execute("luac -p "..file.." 2>LuaCheck.log")
  _, errors = gsub(readfile("LuaCheck.log"), 
    "luac%:(.-)\n  (last token read: `.-') at line (.-) in file `(.-)'",  
    outputerror)
  print(errors .. " compile error(s) found.")
  if errors == 0 then
    retval = execute("luac -l "..file.." >LuaCheck.log")
    s = readfile("LuaCheck.log")
    names_set = {}
    gsub(s, "%d+	%[%d+%]	SETGLOBAL  	%d+	; (.-)\n",  
      function(name) tadd(names_set,name) end)
    if getn(names_set)>0 then
      sort(names_set)
      print(getn(names_set) .. " global variable(s) are created in the file: ")
      printnice(names_set)
    end
  end
else
  print("error: no file to scan specified")
end
print() -- add empty line

最近更改 · 偏好设置
编辑 · 历史记录
最后编辑于 2007 年 4 月 20 日凌晨 12:38 GMT (差异)