summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2018-11-29 15:06:55 +0800
committer最萌小汐 <sumneko@hotmail.com>2018-11-29 15:06:55 +0800
commit5db32ce42188ec69858c34715a8e4f5778ddc9df (patch)
tree876fde365e19d183a9b82b4eb37a448c01811e0e
parent5a608c99b024b09d295b176c098dd251d782c35d (diff)
downloadlua-language-server-5db32ce42188ec69858c34715a8e4f5778ddc9df.zip
支持set
-rw-r--r--server/src/matcher/definition.lua93
-rw-r--r--server/src/parser/ast.lua3
-rw-r--r--server/src/parser/grammar.lua2
3 files changed, 91 insertions, 7 deletions
diff --git a/server/src/matcher/definition.lua b/server/src/matcher/definition.lua
index e5ab178d..a4c8fd3d 100644
--- a/server/src/matcher/definition.lua
+++ b/server/src/matcher/definition.lua
@@ -8,7 +8,10 @@ end
function mt:checkName(name)
if self:isContainPos(name) then
- return self.env.var[name]
+ local str = name[1]
+ self.result = self.env.var[str] or self.env.var._ENV[str]
+ self.stop = true
+ return self.result
else
return nil
end
@@ -108,6 +111,71 @@ function mt:checkThenSearchActionsIn(actions)
end
end
+function mt:markSet(simple)
+ if simple.type == 'name' then
+ local str = simple[1]
+ local var = self.env.var[str] -- 是否是一个局部变量?
+ if not var then
+ var = self.env.var._ENV[str] -- 是否是一个全局变量?
+ if not var then
+ -- 创建一个全局变量
+ var = {}
+ self.env.var._ENV[str] = var
+ end
+ end
+ -- 插入赋值信息
+ var[#var+1] = {
+ type = 'set',
+ source = simple,
+ }
+ self:checkName(simple)
+ else
+ self:searchSimple(simple)
+ end
+end
+
+function mt:createLocal(str, type, source)
+ local var = {}
+ self.env.var[str] = var
+ var[1] = {
+ type = type,
+ source = source,
+ }
+end
+
+function mt:markLocal(simple)
+ if simple.type == 'name' then
+ local str = simple[1]
+ -- 创建一个局部变量
+ self:createLocal(str, 'local', simple)
+ self:checkName(simple)
+ else
+ slef:searchSimple(simple)
+ end
+end
+
+function mt:markSets(action)
+ local simples = action[1]
+ if simples.type == 'list' then
+ for _, simple in ipairs(simples) do
+ self:markSet(simple)
+ end
+ else
+ self:markSet(simples)
+ end
+end
+
+function mt:markLocals(action)
+ local simples = action[1]
+ if simple.type == 'list' then
+ for _, simple in ipairs(simples) do
+ self:markLocal(simple)
+ end
+ else
+ self:markLocal(simples)
+ end
+end
+
function mt:searchAction(action)
local tp = action.type
local result
@@ -116,15 +184,21 @@ function mt:searchAction(action)
elseif tp == 'break' then
elseif tp == 'return' then
result = self:searchReturn(action)
+ elseif tp == 'label' then
+ elseif tp == 'goto' then
+ elseif tp == 'set' then
+ self:markSets(action)
+ elseif tp == 'local' then
+ self:markLocals(action)
end
return result
end
function mt:searchActionsIn(actions)
for _, action in ipairs(actions) do
- local result = self:searchAction(action)
- if result then
- return result
+ self:searchAction(action)
+ if self.stop then
+ return self.result
end
end
return nil
@@ -133,8 +207,15 @@ end
return function (ast, pos)
local searcher = setmetatable({
pos = pos,
- env = env {var = {}, usable = {}},
+ env = env {var = {}, usable = {}, label = {}},
}, mt)
+ searcher.env.var._ENV = {}
searcher:searchActionsIn(ast)
- return false
+
+ if not searcher.result then
+ return false
+ end
+
+ local source = searcher.result[1].source
+ return true, source.start, source.finish
end
diff --git a/server/src/parser/ast.lua b/server/src/parser/ast.lua
index 9bd406b7..77edeb5e 100644
--- a/server/src/parser/ast.lua
+++ b/server/src/parser/ast.lua
@@ -368,6 +368,9 @@ local defs = {
obj[max-1] = nil
return obj
end,
+ Lua = function (...)
+ return {...}
+ end,
}
return function (self, lua, mode)
diff --git a/server/src/parser/grammar.lua b/server/src/parser/grammar.lua
index 0efe4056..e2dad474 100644
--- a/server/src/parser/grammar.lua
+++ b/server/src/parser/grammar.lua
@@ -388,7 +388,7 @@ LocalFunction
]]
grammar 'Lua' [[
-Lua <- (Sp Action)* Sp
+Lua <- (Sp Action)* -> Lua Sp
]]
return function (lua, mode, parser_)