diff options
Diffstat (limited to 'server/src/parser')
-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 |
4 files changed, 66 insertions, 41 deletions
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 |