summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2018-11-19 21:14:44 +0800
committer最萌小汐 <sumneko@hotmail.com>2018-11-19 21:14:44 +0800
commit15f76378dbe93a3cbff155c8e98f9bed59af75b0 (patch)
tree37d18868909bd3c9cbd96378af6343ee82ce92b3 /src
parent2712ffefa8275ad9039b6355846378e5f2019d38 (diff)
downloadlua-language-server-15f76378dbe93a3cbff155c8e98f9bed59af75b0.zip
全局变量在第一次赋值时定义
Diffstat (limited to 'src')
-rw-r--r--src/matcher/definition.lua89
-rw-r--r--src/method/initialize.lua1
-rw-r--r--src/parser/grammar.lua22
3 files changed, 85 insertions, 27 deletions
diff --git a/src/matcher/definition.lua b/src/matcher/definition.lua
index 9da52e83..04d01135 100644
--- a/src/matcher/definition.lua
+++ b/src/matcher/definition.lua
@@ -13,7 +13,7 @@ local function scopeInit()
scopes = {{}}
end
-local function scopeSet(obj)
+local function scopeDef(obj)
local scope = scopes[#scopes]
local name = obj[1]
scope[name] = obj
@@ -30,6 +30,14 @@ local function scopeGet(name)
return nil
end
+local function scopeSet(obj)
+ local name = obj[1]
+ if not scopeGet(name) then
+ local scope = scopes[#scopes]
+ scope[name] = obj
+ end
+end
+
local function scopePush()
scopes[#scopes+1] = {}
end
@@ -51,7 +59,7 @@ end
function defs.Name(str)
checkDifinition(str, namePos)
- return {str, namePos}
+ return {str, namePos, type = 'name'}
end
function defs.DOTSPos(p)
@@ -60,7 +68,7 @@ end
function defs.DOTS(str)
checkDifinition(str, namePos)
- return {str, namePos}
+ return {str, namePos, type = 'name'}
end
function defs.COLONPos(p)
@@ -74,44 +82,87 @@ end
function defs.LocalVar(names)
for _, name in ipairs(names) do
- scopeSet(name)
+ scopeDef(name)
end
end
function defs.LocalSet(names)
for _, name in ipairs(names) do
- scopeSet(name)
+ scopeDef(name)
+ end
+end
+
+function defs.Set(simples)
+ for _, simple in ipairs(simples) do
+ if simple.type == 'simple' and #simple == 1 then
+ local obj = simple[1]
+ local name = obj[1]
+ scopeSet(obj)
+ end
end
end
+function defs.Simple(...)
+ return { type = 'simple', ... }
+end
+
function defs.ArgList(...)
if ... == '' then
return DUMMY_TABLE
end
- return {...}
+ return { type = 'list', ... }
end
function defs.FuncName(...)
if ... == '' then
return DUMMY_TABLE
end
- return {...}
+ return { type = 'simple', ... }
+end
+
+function defs.FunctionDef(simple, args)
+ if #simple == 1 then
+ scopeSet(simple[1])
+ end
+ scopePush()
+ -- 判断隐藏的局部变量self
+ if #simple > 0 then
+ local name = simple[#simple]
+ if name.colon then
+ scopeDef {'self', name.colon, name.colon, type = 'name'}
+ end
+ end
+ for _, arg in ipairs(args) do
+ if arg.type == 'simple' and #arg == 1 then
+ local name = arg[1]
+ scopeDef(name)
+ end
+ if arg.type == 'name' then
+ scopeDef(arg)
+ end
+ end
end
-function defs.FunctionDef(names, args)
- if #names == 1 then
- scopeSet(names[1])
+function defs.FunctionLoc(simple, args)
+ if #simple == 1 then
+ scopeDef(simple[1])
end
scopePush()
-- 判断隐藏的局部变量self
- if #names > 0 then
- local name = names[#names]
+ if #simple > 0 then
+ local name = simple[#simple]
if name.colon then
- scopeSet {'self', name.colon, name.colon}
+ scopeDef {'self', name.colon, name.colon, type = 'name'}
end
end
for _, arg in ipairs(args) do
- scopeSet(arg)
+ if arg.type == 'simple' and #arg == 1 then
+ local name = arg[1]
+ scopeDef(name)
+ end
+ if arg.type == 'name' then
+ scopeDef(arg)
+ end
end
end
@@ -153,7 +204,7 @@ end
function defs.LoopDef(name)
scopePush()
- scopeSet(name)
+ scopeDef(name)
end
function defs.Loop()
@@ -165,13 +216,17 @@ function defs.LoopStart(name, exp)
end
function defs.NameList(...)
- return {...}
+ return { type = 'list', ... }
+end
+
+function defs.SimpleList(...)
+ return { type = 'list', ... }
end
function defs.InDef(names)
scopePush()
for _, name in ipairs(names) do
- scopeSet(name)
+ scopeDef(name)
end
end
diff --git a/src/method/initialize.lua b/src/method/initialize.lua
index f16365c0..15b6ca6e 100644
--- a/src/method/initialize.lua
+++ b/src/method/initialize.lua
@@ -1,6 +1,5 @@
return function (lsp, data)
lsp._inited = true
-
return {
capabilities = {
-- 支持“转到定义”
diff --git a/src/parser/grammar.lua b/src/parser/grammar.lua
index 7f20b5c2..ef2fbcd3 100644
--- a/src/parser/grammar.lua
+++ b/src/parser/grammar.lua
@@ -215,7 +215,8 @@ ExpUnit <- Nil
/ Function
/ Simple
-Simple <- Prefix (Suffix)*
+Simple <- (Prefix (Suffix)*)
+ -> Simple
Prefix <- PL Exp PR
/ Name
ColonName <- (COLON Name)
@@ -239,8 +240,12 @@ TableField <- NewIndex / NewField / Exp
NewIndex <- BL Exp BR ASSIGN Exp
NewField <- Name ASSIGN Exp
-Function <- (FUNCTION FuncName PL ArgList PR) -> FunctionDef
- (!END Action)* -> Function
+Function <- FunctionLoc / FunctionDef
+FunctionLoc <- (LOCAL FUNCTION FuncName PL ArgList PR) -> FunctionLoc
+ (!END Action)* -> Function
+ END
+FunctionDef <- (FUNCTION FuncName PL ArgList PR) -> FunctionDef
+ (!END Action)* -> Function
END
FuncName <- (Name (FuncSuffix)*)?
-> FuncName
@@ -252,7 +257,7 @@ Action <- !. .
]]
grammar 'Action' [[
-Action <- SEMICOLON / Do / Break / Return / Label / GoTo / If / For / While / Repeat / Set / Local / Function / Call
+Action <- SEMICOLON / Do / Break / Return / Label / GoTo / If / For / While / Repeat / Function / Set / Local / Call
ExpList <- Exp (COMMA Exp)*
NameList <- (Name (COMMA Name)*) -> NameList
@@ -302,13 +307,12 @@ Repeat <- REPEAT -> RepeatDef
(!UNTIL Action)* -> Repeat
(UNTIL Exp) -> Until
-Set <- (LOCAL !FUNCTION NameList ASSIGN ExpList)
+Set <- (LOCAL NameList ASSIGN ExpList)
-> LocalSet
- / SimpleList ASSIGN ExpList
+ / (SimpleList ASSIGN ExpList)
+ -> Set
-Local <- LOCAL Function
- -> LocalFunction
- / LOCAL NameList
+Local <- LOCAL NameList
-> LocalVar
Call <- Prefix (Suffix)*