diff options
-rw-r--r-- | server/locale/en-US/script.lni | 2 | ||||
-rw-r--r-- | server/locale/zh-CN/script.lni | 2 | ||||
-rw-r--r-- | server/src/core/completion.lua | 4 | ||||
-rw-r--r-- | server/src/method/textDocument/publishDiagnostics.lua | 6 | ||||
-rw-r--r-- | server/src/parser/ast.lua | 38 | ||||
-rw-r--r-- | server/src/parser/grammar.lua | 27 | ||||
-rw-r--r-- | server/src/parser/init.lua | 2 | ||||
-rw-r--r-- | server/src/parser/parse.lua | 40 | ||||
-rw-r--r-- | server/src/service.lua | 2 | ||||
-rw-r--r-- | server/test/completion/init.lua | 2 | ||||
-rw-r--r-- | server/test/definition/init.lua | 2 | ||||
-rw-r--r-- | server/test/diagnostics/init.lua | 2 | ||||
-rw-r--r-- | server/test/document_symbol/init.lua | 2 | ||||
-rw-r--r-- | server/test/find_lib/init.lua | 2 | ||||
-rw-r--r-- | server/test/full/init.lua | 2 | ||||
-rw-r--r-- | server/test/highlight/init.lua | 2 | ||||
-rw-r--r-- | server/test/hover/init.lua | 2 | ||||
-rw-r--r-- | server/test/references/init.lua | 2 | ||||
-rw-r--r-- | server/test/rename/init.lua | 2 | ||||
-rw-r--r-- | server/test/signature/init.lua | 2 | ||||
-rw-r--r-- | server/test/type_inference/init.lua | 2 |
21 files changed, 87 insertions, 60 deletions
diff --git a/server/locale/en-US/script.lni b/server/locale/en-US/script.lni index ddc80ac9..26d4b16f 100644 --- a/server/locale/en-US/script.lni +++ b/server/locale/en-US/script.lni @@ -18,7 +18,7 @@ DIAG_LOWERCASE_GLOBAL = 'Global variable in lowercase initial.' DIAG_EMPTY_BLOCK = 'Empty block.' DIAG_DIAGNOSTICS = 'Lua Diagnostics.' DIAG_SYNTAX_CHECK = 'Lua Syntax Check.' -DIAG_NEED_VERSION = 'Supported in {}.' +DIAG_NEED_VERSION = 'Supported in {}, current is {}.' DIAG_DEFINED_VERSION = 'Defined in {}, current is {}.' DIAG_DEFINED_CUSTOM = 'Defined in {}.' DIAG_DUPLICATE_CLASS = 'Duplicate class.' diff --git a/server/locale/zh-CN/script.lni b/server/locale/zh-CN/script.lni index 1c0040a0..20a45855 100644 --- a/server/locale/zh-CN/script.lni +++ b/server/locale/zh-CN/script.lni @@ -18,7 +18,7 @@ DIAG_LOWERCASE_GLOBAL = '首字母小写的全局变量' DIAG_EMPTY_BLOCK = '空代码块' DIAG_DIAGNOSTICS = 'Lua 诊断' DIAG_SYNTAX_CHECK = 'Lua 语法检查' -DIAG_NEED_VERSION = '在 {} 中是合法的' +DIAG_NEED_VERSION = '在 {} 中是合法的,当前为 {}' DIAG_DEFINED_VERSION = '在 {} 中有定义,当前为 {}' DIAG_DEFINED_CUSTOM = '在 {} 中有定义' DIAG_DUPLICATE_CLASS = '重复定义的 Class 。' diff --git a/server/src/core/completion.lua b/server/src/core/completion.lua index 678ad591..e2db64d9 100644 --- a/server/src/core/completion.lua +++ b/server/src/core/completion.lua @@ -686,7 +686,7 @@ local function searchEnumAsLib(vm, source, word, callback, pos, args, lib) for _, enum in ipairs(lib.enums) do if enum.name and enum.name == name and enum.enum then if matchKey(word, enum.enum) then - local strSource = parser:ast(tostring(enum.enum), 'String') + local strSource = parser:parse(tostring(enum.enum), 'String') if strSource then if source.type == 'string' then local data = buildTextEdit(source.start, source.finish, strSource[1], source[2]) @@ -743,7 +743,7 @@ local function searchEnumAsEmmyParams(vm, source, word, callback, pos, args, fun param:eachEnum(function (enum) local str = enum[1] if matchKey(word, str) then - local strSource = parser:ast(tostring(str), 'String') + local strSource = parser:parse(tostring(str), 'String') if strSource then if source.type == 'string' then local data = buildTextEdit(source.start, source.finish, strSource[1], source[2]) diff --git a/server/src/method/textDocument/publishDiagnostics.lua b/server/src/method/textDocument/publishDiagnostics.lua index 6c949ceb..c767e934 100644 --- a/server/src/method/textDocument/publishDiagnostics.lua +++ b/server/src/method/textDocument/publishDiagnostics.lua @@ -1,5 +1,6 @@ local core = require 'core' local lang = require 'language' +local config = require 'config' local DiagnosticSeverity = { Error = 1, @@ -87,10 +88,11 @@ local function buildError(err, lines, uri) message = lang.script('PARSER_'..err.type, err.info) } if err.version then + local currentVersion = err.info and err.info.version or config.config.runtime.version if type(err.version) == 'table' then - diagnostic.message = ('%s(%s)'):format(diagnostic.message, lang.script('DIAG_NEED_VERSION', table.concat(err.version, '/'))) + diagnostic.message = ('%s(%s)'):format(diagnostic.message, lang.script('DIAG_NEED_VERSION', table.concat(err.version, '/'), currentVersion)) else - diagnostic.message = ('%s(%s)'):format(diagnostic.message, lang.script('DIAG_NEED_VERSION', err.version)) + diagnostic.message = ('%s(%s)'):format(diagnostic.message, lang.script('DIAG_NEED_VERSION', err.version, currentVersion)) end end if err.level == 'error' then diff --git a/server/src/parser/ast.lua b/server/src/parser/ast.lua index a67a16cb..8ac4e0f4 100644 --- a/server/src/parser/ast.lua +++ b/server/src/parser/ast.lua @@ -410,6 +410,10 @@ local Defs = { type = 'ERR_ESC', start = pos-1, finish = pos, + version = {'Lua 5.2', 'Lua 5.3', 'Lua 5.4', 'LuaJIT'}, + info = { + version = State.Version, + } } return char end @@ -424,6 +428,10 @@ local Defs = { type = 'ERR_ESC', start = pos-3, finish = pos-2, + version = {'Lua 5.3', 'Lua 5.4', 'LuaJIT'}, + info = { + version = State.Version, + } } return char end @@ -751,7 +759,7 @@ local Defs = { checkMissEnd(start) return obj end, - RTTable = function (_, _, start, ...) + Table = function (start, ...) local args = {...} local max = #args local finish = args[max] - 1 @@ -790,7 +798,7 @@ local Defs = { start = arg end end - return true, table + return table end, NewField = function (key, value) return { @@ -1880,22 +1888,12 @@ local Defs = { end, } -return function (self, lua, mode, version) - Errs = {} - State= { - Break = 0, - Label = {{}}, - Dots = {true}, - Version = version, - Lua = lua, - } - local suc, res, err = xpcall(self.grammar, debug.traceback, self, lua, mode, Defs) - if not suc then - return nil, res - end - if not res then - pushError(err) - return nil, Errs - end - return res, Errs +local function init(state, errs) + State = state + Errs = errs end + +return { + defs = Defs, + init = init, +} diff --git a/server/src/parser/grammar.lua b/server/src/parser/grammar.lua index 5183803b..37f3c483 100644 --- a/server/src/parser/grammar.lua +++ b/server/src/parser/grammar.lua @@ -1,10 +1,11 @@ local re = require 'parser.relabel' local m = require 'lpeglabel' - +local ast = require 'parser.ast' local scriptBuf = '' local compiled = {} local parser +local defs = ast.defs -- goto 可以作为名字,合法性之后处理 local RESERVED = { @@ -31,20 +32,7 @@ local RESERVED = { ['while'] = true, } -local defs = setmetatable({}, {__index = function (self, key) - self[key] = function (...) - if parser[key] then - return parser[key](...) - end - end - return self[key] -end}) - -defs.nl = (m.P'\r\n' + m.S'\r\n') / function () - if parser.nl then - return parser.nl() - end -end +defs.nl = (m.P'\r\n' + m.S'\r\n') defs.s = m.S' \t' defs.S = - defs.s defs.ea = '\a' @@ -60,7 +48,7 @@ defs.NotReserved = function (_, _, str) if RESERVED[str] then return false end - return true, str + return true end defs.Reserved = function (_, _, str) if RESERVED[str] then @@ -289,7 +277,7 @@ grammar 'Name' [[ Name <- Sp ({} NameBody {}) -> Name NameBody <- {[a-zA-Z_] [a-zA-Z0-9_]*} -FreeName <- Sp ({} NameBody=>NotReserved {}) +FreeName <- Sp ({} {NameBody=>NotReserved} {}) -> Name MustName <- Name / DirtyName DirtyName <- {} -> DirtyName @@ -346,7 +334,7 @@ ArgList <- (DOTS -> DotsAsArg / Name / Sp {} COMMA)* -> ArgList Table <- Sp ({} TL TableFields? DirtyTR) - => RTTable + -> Table TableFields <- (Emmy / TableSep {} / TableField)+ TableSep <- COMMA / SEMICOLON TableField <- NewIndex / NewField / Exp @@ -631,8 +619,7 @@ Lua <- Head? Head <- '#' (!%nl .)* ]] -return function (self, lua, mode, parser_) - parser = parser_ or {} +return function (self, lua, mode) local gram = compiled[mode] or compiled['Lua'] local r, _, pos = gram:match(lua) if not r then diff --git a/server/src/parser/init.lua b/server/src/parser/init.lua index 9a98b787..30596dbe 100644 --- a/server/src/parser/init.lua +++ b/server/src/parser/init.lua @@ -1,8 +1,8 @@ local api = { grammar = require 'parser.grammar', + parse = require 'parser.parse', split = require 'parser.split', calcline = require 'parser.calcline', - ast = require 'parser.ast', lines = require 'parser.lines', } diff --git a/server/src/parser/parse.lua b/server/src/parser/parse.lua new file mode 100644 index 00000000..65464724 --- /dev/null +++ b/server/src/parser/parse.lua @@ -0,0 +1,40 @@ +local ast = require 'parser.ast' + +local Errs +local State + +local function pushError(err) + if err.finish < err.start then + err.finish = err.start + end + local last = Errs[#Errs] + if last then + if last.start <= err.start and last.finish >= err.finish then + return + end + end + err.level = err.level or 'error' + Errs[#Errs+1] = err + return err +end + +return function (self, lua, mode, version) + Errs = {} + State= { + Break = 0, + Label = {{}}, + Dots = {true}, + Version = version, + Lua = lua, + } + ast.init(State, Errs) + local suc, res, err = xpcall(self.grammar, debug.traceback, self, lua, mode) + if not suc then + return nil, res + end + if not res then + pushError(err) + return nil, Errs + end + return res, Errs +end diff --git a/server/src/service.lua b/server/src/service.lua index a6ca2705..7245b4e7 100644 --- a/server/src/service.lua +++ b/server/src/service.lua @@ -405,7 +405,7 @@ end ---@param file file ---@return table function mt:compileAst(file) - local ast, err = parser:ast(file:getText(), 'lua', config.config.runtime.version) + local ast, err = parser:parse(file:getText(), 'lua', config.config.runtime.version) if ast then file:setAstErr(err) else diff --git a/server/test/completion/init.lua b/server/test/completion/init.lua index 20436845..4f2b3b8f 100644 --- a/server/test/completion/init.lua +++ b/server/test/completion/init.lua @@ -64,7 +64,7 @@ function TEST(script) return function (expect) local pos = script:find('$', 1, true) - 1 local new_script = script:gsub('%$', '') - local ast = parser:ast(new_script, 'lua', 'Lua 5.4') + local ast = parser:parse(new_script, 'lua', 'Lua 5.4') local vm = buildVM(ast) assert(vm) local result = core.completion(vm, new_script, pos) diff --git a/server/test/definition/init.lua b/server/test/definition/init.lua index 1ff5881e..022b1d3b 100644 --- a/server/test/definition/init.lua +++ b/server/test/definition/init.lua @@ -40,7 +40,7 @@ function TEST(script) local finish = script:find('?>', 1, true) local pos = (start + finish) // 2 + 1 local new_script = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') - local ast = parser:ast(new_script, 'lua', 'Lua 5.3') + local ast = parser:parse(new_script, 'lua', 'Lua 5.3') assert(ast) local vm = buildVM(ast) assert(vm) diff --git a/server/test/diagnostics/init.lua b/server/test/diagnostics/init.lua index 70a6c586..f0f7096f 100644 --- a/server/test/diagnostics/init.lua +++ b/server/test/diagnostics/init.lua @@ -49,7 +49,7 @@ end function TEST(script, ...) local new_script, target = catch_target(script, ...) local lsp = service() - local ast = parser:ast(new_script, 'lua', 'Lua 5.3') + local ast = parser:parse(new_script, 'lua', 'Lua 5.3') assert(ast) local lines = parser:lines(new_script) local vm = buildVM(ast, lsp, 'test') diff --git a/server/test/document_symbol/init.lua b/server/test/document_symbol/init.lua index 3955a54f..f677d857 100644 --- a/server/test/document_symbol/init.lua +++ b/server/test/document_symbol/init.lua @@ -76,7 +76,7 @@ end function TEST(script) return function (expect) - local ast = parser:ast(script, 'lua', 'Lua 5.3') + local ast = parser:parse(script, 'lua', 'Lua 5.3') local vm = buildVM(ast) assert(vm) local result = core.documentSymbol(vm) diff --git a/server/test/find_lib/init.lua b/server/test/find_lib/init.lua index bc60cfad..51f53b75 100644 --- a/server/test/find_lib/init.lua +++ b/server/test/find_lib/init.lua @@ -10,7 +10,7 @@ function TEST(fullkey) local finish = script:find('?>', 1, true) local pos = (start + finish) // 2 + 1 local new_script = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') - local ast = parser:ast(new_script, 'lua', 'Lua 5.3') + local ast = parser:parse(new_script, 'lua', 'Lua 5.3') assert(ast) local vm = buildVM(ast) assert(vm) diff --git a/server/test/full/init.lua b/server/test/full/init.lua index 808d7db3..3d0661cb 100644 --- a/server/test/full/init.lua +++ b/server/test/full/init.lua @@ -4,7 +4,7 @@ local parser = require 'parser' rawset(_G, 'TEST', true) function TEST(script) - local ast = parser:ast(script, 'lua', 'Lua 5.3') + local ast = parser:parse(script, 'lua', 'Lua 5.3') assert(ast) local vm, err = buildVM(ast) assert(vm, err) diff --git a/server/test/highlight/init.lua b/server/test/highlight/init.lua index aa7ab69f..6b8ab355 100644 --- a/server/test/highlight/init.lua +++ b/server/test/highlight/init.lua @@ -39,7 +39,7 @@ function TEST(newName) local finish = script:find('?>', 1, true) local pos = (start + finish) // 2 + 1 local new_script = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') - local ast = parser:ast(new_script, 'lua', 'Lua 5.3') + local ast = parser:parse(new_script, 'lua', 'Lua 5.3') assert(ast) local vm = buildVM(ast) assert(vm) diff --git a/server/test/hover/init.lua b/server/test/hover/init.lua index 391199a5..87b4cc99 100644 --- a/server/test/hover/init.lua +++ b/server/test/hover/init.lua @@ -10,7 +10,7 @@ function TEST(script) local finish = script:find('?>', 1, true) local pos = (start + finish) // 2 + 1 local new_script = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') - local ast = parser:ast(new_script, 'lua', 'Lua 5.3') + local ast = parser:parse(new_script, 'lua', 'Lua 5.3') local vm = buildVM(ast) assert(vm) local source = core.findSource(vm, pos) diff --git a/server/test/references/init.lua b/server/test/references/init.lua index d1eab422..44cd5ab0 100644 --- a/server/test/references/init.lua +++ b/server/test/references/init.lua @@ -38,7 +38,7 @@ function TEST(script) local finish = script:find('?>', 1, true) local pos = (start + finish) // 2 + 1 local new_script = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') - local ast = parser:ast(new_script, 'lua', 'Lua 5.3') + local ast = parser:parse(new_script, 'lua', 'Lua 5.3') assert(ast) local vm = buildVM(ast) assert(vm) diff --git a/server/test/rename/init.lua b/server/test/rename/init.lua index ad7fe650..646401a0 100644 --- a/server/test/rename/init.lua +++ b/server/test/rename/init.lua @@ -39,7 +39,7 @@ function TEST(newName) local finish = script:find('?>', 1, true) local pos = (start + finish) // 2 + 1 local new_script = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') - local ast = parser:ast(new_script, 'lua', 'Lua 5.3') + local ast = parser:parse(new_script, 'lua', 'Lua 5.3') assert(ast) local vm = buildVM(ast) assert(vm) diff --git a/server/test/signature/init.lua b/server/test/signature/init.lua index bc48ba79..a2a647e7 100644 --- a/server/test/signature/init.lua +++ b/server/test/signature/init.lua @@ -8,7 +8,7 @@ function TEST(script) return function (expect) local pos = script:find('$', 1, true) local new_script = script:gsub('%$', '') - local ast = parser:ast(new_script, 'lua', 'Lua 5.3') + local ast = parser:parse(new_script, 'lua', 'Lua 5.3') local vm = buildVM(ast) assert(vm) local hovers = core.signature(vm, pos) diff --git a/server/test/type_inference/init.lua b/server/test/type_inference/init.lua index 9155b3ed..59d853eb 100644 --- a/server/test/type_inference/init.lua +++ b/server/test/type_inference/init.lua @@ -11,7 +11,7 @@ function TEST(res) local finish = script:find('?>', 1, true) local pos = (start + finish) // 2 + 1 local new_script = script:gsub('<[!?]', ' '):gsub('[!?]>', ' ') - local ast = parser:ast(new_script, 'lua', 'Lua 5.3') + local ast = parser:parse(new_script, 'lua', 'Lua 5.3') local vm = buildVM(ast) assert(vm) local result = core.findSource(vm, pos) |