diff options
-rw-r--r-- | src/matcher/definition.lua | 41 | ||||
-rw-r--r-- | src/parser/grammar.lua | 9 | ||||
-rw-r--r-- | test/definition/arg.lua | 12 |
3 files changed, 47 insertions, 15 deletions
diff --git a/src/matcher/definition.lua b/src/matcher/definition.lua index 2054afc3..9ac0e047 100644 --- a/src/matcher/definition.lua +++ b/src/matcher/definition.lua @@ -5,28 +5,26 @@ local defs = {} local scopes local result local namePos +local colonPos local DUMMY_TABLE = {} -local function getResult(name, p) - result = {name, p} -end - local function scopeInit() scopes = {{}} end -local function scopeSet(name) +local function scopeSet(obj) local scope = scopes[#scopes] - scope[name[1]] = name[2] + local name = obj[1] + scope[name] = obj end local function scopeGet(name) for i = #scopes, 1, -1 do local scope = scopes[i] - local p = scope[name] - if p then - return p + local obj = scope[name] + if obj then + return obj end end return nil @@ -44,7 +42,7 @@ local function checkDifinition(name, p) if pos < p or pos > p + #name then return end - getResult(name, scopeGet(name)) + result = scopeGet(name) end function defs.NamePos(p) @@ -65,6 +63,15 @@ function defs.DOTS(str) return {str, namePos} end +function defs.COLONPos(p) + colonPos = p +end + +function defs.ColonName(name) + name.colon = colonPos + return name +end + function defs.LocalVar(names) for _, name in ipairs(names) do scopeSet(name) @@ -96,6 +103,13 @@ function defs.FunctionDef(names, args) scopeSet(names[1]) end scopePush() + -- 判断隐藏的局部变量self + if #names > 0 then + local name = names[#names] + if name.colon then + scopeSet {'self', name.colon, name.colon} + end + end for _, arg in ipairs(args) do scopeSet(arg) end @@ -191,9 +205,12 @@ return function (buf, pos_) if not result then return false, 'No word' end - local name, start = result[1], result[2] + local name, start, finish = result[1], result[2], result[3] if not start then return false, 'No match' end - return true, start, start + #name - 1 + if not finish then + finish = start + #name - 1 + end + return true, start, finish end diff --git a/src/parser/grammar.lua b/src/parser/grammar.lua index 5b96978b..748104ac 100644 --- a/src/parser/grammar.lua +++ b/src/parser/grammar.lua @@ -150,7 +150,8 @@ SEMICOLON <- Sp ';' DOTS <- Sp {} -> DOTSPos '...' -> DOTS DOT <- Sp '.' -COLON <- Sp ':' +COLON <- Sp {} -> COLONPos + ':' -> COLON LABEL <- Sp '::' ASSIGN <- Sp '=' ]] @@ -217,8 +218,10 @@ ExpUnit <- Nil Simple <- Prefix (Suffix)* Prefix <- PL Exp PR / Name +ColonName <- (COLON Name) + -> ColonName Suffix <- DOT Name - / COLON Name + / ColonName / Table / String / BL Exp BR @@ -242,7 +245,7 @@ Function <- (FUNCTION FuncName PL ArgList PR) -> FunctionDef FuncName <- (Name (FuncSuffix)*)? -> FuncName FuncSuffix <- DOT Name - / COLON Name + / ColonName -- 纯占位,修改了 `relabel.lua` 使重复定义不抛错 Action <- !. . diff --git a/test/definition/arg.lua b/test/definition/arg.lua index e1ce0891..623b70b7 100644 --- a/test/definition/arg.lua +++ b/test/definition/arg.lua @@ -9,3 +9,15 @@ local function x (x, <!...!>) x = <?...?> end ]] + +TEST [[ +function mt<!:!>x() + <?self?> = 1 +end +]] + +TEST [[ +function mt:x(<!self!>) + <?self?> = 1 +end +]] |