summaryrefslogtreecommitdiff
path: root/server/src
diff options
context:
space:
mode:
Diffstat (limited to 'server/src')
-rw-r--r--server/src/core/completion.lua4
-rw-r--r--server/src/method/textDocument/publishDiagnostics.lua6
-rw-r--r--server/src/parser/ast.lua38
-rw-r--r--server/src/parser/grammar.lua27
-rw-r--r--server/src/parser/init.lua2
-rw-r--r--server/src/parser/parse.lua40
-rw-r--r--server/src/service.lua2
7 files changed, 73 insertions, 46 deletions
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