diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-11-19 17:11:20 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-11-19 17:11:20 +0800 |
commit | 2687363810809f350038acfab2f52f27a3dc51a2 (patch) | |
tree | ed90b24e07e5ce2ec3a096f3b2b459c1d677fb19 | |
parent | c28a101f62fe7a118baa3eeccf13839249cae760 (diff) | |
download | lua-language-server-2687363810809f350038acfab2f52f27a3dc51a2.zip |
支持参数
-rw-r--r-- | src/matcher/definition.lua | 14 | ||||
-rw-r--r-- | src/parser/grammar.lua | 10 | ||||
-rw-r--r-- | test/definition/arg.lua | 11 | ||||
-rw-r--r-- | test/definition/init.lua | 214 | ||||
-rw-r--r-- | test/definition/local.lua | 208 |
5 files changed, 243 insertions, 214 deletions
diff --git a/src/matcher/definition.lua b/src/matcher/definition.lua index bf7457e7..b18e1a01 100644 --- a/src/matcher/definition.lua +++ b/src/matcher/definition.lua @@ -54,6 +54,15 @@ function defs.Name(str) return {str, namePos} end +function defs.DOTSPos(p) + namePos = p +end + +function defs.DOTS(str) + checkDifinition(str, namePos) + return {str, namePos} +end + function defs.LocalVar(names) for _, name in ipairs(names) do scopeSet(name) @@ -66,10 +75,13 @@ function defs.LocalSet(names) end end -function defs.FunctionDef(names) +function defs.FunctionDef(names, args) if #names == 1 then scopeSet(names[1]) end + for _, arg in ipairs(args) do + scopeSet(arg) + end scopePush() end diff --git a/src/parser/grammar.lua b/src/parser/grammar.lua index 1e3a37c1..cb14fe6d 100644 --- a/src/parser/grammar.lua +++ b/src/parser/grammar.lua @@ -147,7 +147,8 @@ TL <- Sp '{' TR <- Sp '}' COMMA <- Sp ',' SEMICOLON <- Sp ';' -DOTS <- Sp '...' +DOTS <- Sp {} -> DOTSPos + '...' -> DOTS DOT <- Sp '.' COLON <- Sp ':' LABEL <- Sp '::' @@ -185,7 +186,8 @@ Float16 <- ('.' X16*)? ([pP] [+-]? [1-9]? [0-9]*)? ]] grammar 'Name' [[ -Name <- Sp {} -> NamePos {[a-zA-Z_] [a-zA-Z0-9_]*} -> Name +Name <- Sp {} -> NamePos + {[a-zA-Z_] [a-zA-Z0-9_]*} -> Name ]] grammar 'Exp' [[ @@ -222,7 +224,7 @@ Suffix <- DOT Name / BL Exp BR / PL ArgList? PR -ArgList <- Arg (COMMA Arg)* +ArgList <- (Arg (COMMA Arg)*) Arg <- DOTS / Exp @@ -233,7 +235,7 @@ TableField <- NewIndex / NewField / Exp NewIndex <- BL Exp BR ASSIGN Exp NewField <- Name ASSIGN Exp -Function <- (FUNCTION {| FuncName? |} PL ArgList? PR) -> FunctionDef +Function <- (FUNCTION {| FuncName? |} PL {| ArgList? |} PR) -> FunctionDef (!END Action)* -> Function END FuncName <- Name (FuncSuffix)* diff --git a/test/definition/arg.lua b/test/definition/arg.lua new file mode 100644 index 00000000..e1ce0891 --- /dev/null +++ b/test/definition/arg.lua @@ -0,0 +1,11 @@ +TEST [[ +local function x (<!x!>) + <?x?> = 1 +end +]] + +TEST [[ +local function x (x, <!...!>) + x = <?...?> +end +]] diff --git a/test/definition/init.lua b/test/definition/init.lua index f9a0bf3e..88d6a1b9 100644 --- a/test/definition/init.lua +++ b/test/definition/init.lua @@ -1,6 +1,8 @@ local matcher = require 'matcher' -local function test(script) +rawset(_G, 'TEST', true) + +function TEST(script) local start = script:find('<!', 1, true) + 2 local finish = script:find('!>', 1, true) - 1 local pos = script:find('<?', 1, true) + 2 @@ -12,211 +14,5 @@ local function test(script) assert(b == finish) end -test [[ -local <!x!> -<?x?> = 1 -]] - -test [[ -local z, y, <!x!> -<?x?> = 1 -]] - -test [[ -local <!x!> = 1 -<?x?> = 1 -]] - -test [[ -local z, y, <!x!> = 1 -<?x?> = 1 -]] - -test [[ -function <!x!> () end -<?x?> = 1 -]] - -test [[ -local function <!x!> () end -<?x?> = 1 -]] - -test [[ -local x -local <!x!> -<?x?> = 1 -]] - -test [[ -local <!x!> -do - <?x?> = 1 -end -]] - -test [[ -local <!x!> -do - local x -end -<?x?> = 1 -]] - -test [[ -local <!x!> -if <?x?> then - local x -end -]] - -test [[ -local <!x!> -if x then - local x -elseif <?x?> then - local x -end -]] - -test [[ -local <!x!> -if x then - local x -elseif x then - local x -else - local x -end -<?x?> = 1 -]] - -test [[ -local <!x!> -if x then - <?x?> = 1 -elseif x then - local x -else - local x -end -]] - -test [[ -local <!x!> -for x = 1, 10 do -end -<?x?> = 1 -]] - -test [[ -local x -for <!x!> = 1, 10 do - <?x?> = 1 -end -]] - -test [[ -local <!x!> -for x in x do -end -<?x?> = 1 -]] - -test [[ -local <!x!> -for x in <?x?> do -end -]] - -test [[ -local x -for <!x!> in x do - <?x?> = 1 -end -]] - -test [[ -local x -for z, y, <!x!> in x do - <?x?> = 1 -end -]] - -test [[ -local <!x!> -while <?x?> do -end -]] - -test [[ -local <!x!> -while x do - <?x?> = 1 -end -]] - -test [[ -local <!x!> -while x do - local x -end -<?x?> = 1 -]] - -test [[ -local <!x!> -repeat - <?x?> = 1 -until true -]] - -test [[ -local <!x!> -repeat - local x -until true -<?x?> = 1 -]] - -test [[ -local <!x!> -repeat -until <?x?> -]] - -test [[ -local x -repeat - local <!x!> -until <?x?> -]] - -test [[ -local <!x!> -function _() - local x -end -<?x?> = 1 -]] - -test [[ -local <!x!> -return function () - <?x?> = 1 -end -]] - -test [[ -local <!x!> -local x = function () - <?x?> = 1 -end -]] - -test [[ -local x -local function <!x!> () - <?x?> = 1 -end -]] +require 'definition.local' +require 'definition.arg' diff --git a/test/definition/local.lua b/test/definition/local.lua new file mode 100644 index 00000000..bbfd3752 --- /dev/null +++ b/test/definition/local.lua @@ -0,0 +1,208 @@ +TEST [[ +local <!x!> +<?x?> = 1 +]] + +TEST [[ +local z, y, <!x!> +<?x?> = 1 +]] + +TEST [[ +local <!x!> = 1 +<?x?> = 1 +]] + +TEST [[ +local z, y, <!x!> = 1 +<?x?> = 1 +]] + +TEST [[ +function <!x!> () end +<?x?> = 1 +]] + +TEST [[ +local function <!x!> () end +<?x?> = 1 +]] + +TEST [[ +local x +local <!x!> +<?x?> = 1 +]] + +TEST [[ +local <!x!> +do + <?x?> = 1 +end +]] + +TEST [[ +local <!x!> +do + local x +end +<?x?> = 1 +]] + +TEST [[ +local <!x!> +if <?x?> then + local x +end +]] + +TEST [[ +local <!x!> +if x then + local x +elseif <?x?> then + local x +end +]] + +TEST [[ +local <!x!> +if x then + local x +elseif x then + local x +else + local x +end +<?x?> = 1 +]] + +TEST [[ +local <!x!> +if x then + <?x?> = 1 +elseif x then + local x +else + local x +end +]] + +TEST [[ +local <!x!> +for x = 1, 10 do +end +<?x?> = 1 +]] + +TEST [[ +local x +for <!x!> = 1, 10 do + <?x?> = 1 +end +]] + +TEST [[ +local <!x!> +for x in x do +end +<?x?> = 1 +]] + +TEST [[ +local <!x!> +for x in <?x?> do +end +]] + +TEST [[ +local x +for <!x!> in x do + <?x?> = 1 +end +]] + +TEST [[ +local x +for z, y, <!x!> in x do + <?x?> = 1 +end +]] + +TEST [[ +local <!x!> +while <?x?> do +end +]] + +TEST [[ +local <!x!> +while x do + <?x?> = 1 +end +]] + +TEST [[ +local <!x!> +while x do + local x +end +<?x?> = 1 +]] + +TEST [[ +local <!x!> +repeat + <?x?> = 1 +until true +]] + +TEST [[ +local <!x!> +repeat + local x +until true +<?x?> = 1 +]] + +TEST [[ +local <!x!> +repeat +until <?x?> +]] + +TEST [[ +local x +repeat + local <!x!> +until <?x?> +]] + +TEST [[ +local <!x!> +function _() + local x +end +<?x?> = 1 +]] + +TEST [[ +local <!x!> +return function () + <?x?> = 1 +end +]] + +TEST [[ +local <!x!> +local x = function () + <?x?> = 1 +end +]] + +TEST [[ +local x +local function <!x!> () + <?x?> = 1 +end +]] |