diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-12-30 19:19:59 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-12-30 19:19:59 +0800 |
commit | dfbdcbd3f01602307f49f1af098899264d0162e7 (patch) | |
tree | 423be756ec7460bd3fee58a101d92c9690da5788 | |
parent | fadb6538eaadf65764f33a2fd3e14bb640b3af27 (diff) | |
download | lua-language-server-dfbdcbd3f01602307f49f1af098899264d0162e7.zip |
更新语法解析
-rw-r--r-- | server/locale/en-US/script.lni | 3 | ||||
-rw-r--r-- | server/locale/zh-CN/script.lni | 5 | ||||
-rw-r--r-- | server/src/parser/ast.lua | 105 | ||||
-rw-r--r-- | server/src/parser/grammar.lua | 47 |
4 files changed, 120 insertions, 40 deletions
diff --git a/server/locale/en-US/script.lni b/server/locale/en-US/script.lni index c91d0994..604453df 100644 --- a/server/locale/en-US/script.lni +++ b/server/locale/en-US/script.lni @@ -15,7 +15,7 @@ MWS_COMPLETE = 'Workspace is complete now. You may try again...' PARSER_CRASH = 'Parser crashed! Last words:{}' PARSER_UNKNOWN = 'Unknown syntax error...' -PARSER_MISS_NAME = '<name> expected.' +PARSER_MISS_NAME = '<name> expected' PARSER_UNKNOWN_SYMBOL = 'Unexpected symbol `{symbol}`' PARSER_MISS_SYMBOL = 'Missed symbol `{symbol}`' PARSER_MISS_ESC_X = 'Should be 2 hexadecimal digits' @@ -25,5 +25,6 @@ PARSER_UTF8_MAX = 'Should between {min} and {max}' PARSER_ERR_ESC = 'Invalid escape sequence' PARSER_MUST_X16 = 'Should be hexadecimal digits' PARSER_MISS_EXPONENT = 'Missed digits for the exponent' +PARSER_MISS_EXP = '<exp> expected' SYMBOL_ANONYMOUS = '<Anonymous>' diff --git a/server/locale/zh-CN/script.lni b/server/locale/zh-CN/script.lni index 3014b5fa..585780f1 100644 --- a/server/locale/zh-CN/script.lni +++ b/server/locale/zh-CN/script.lni @@ -5,10 +5,10 @@ DIAG_UNUSED_LOCAL = '未使用的局部变量 `{}`。' DIAG_UNDEFINED_GLOBAL = '未定义的全局变量 `{}`。' DIAG_UNUSED_LABEL = '未使用的标签 `{}`。' DIAG_REDEFINED_LOCAL = '重定义局部变量 `{}`。' -DIAG_PREVIOUS_CALL = '解析为了上一行的函数调用。你可能需要在前面加一个 `;`。' +DIAG_PREVIOUS_CALL = '解析为了上一行的函数调用。你可能需要在前面加一个 `;`。' DIAG_OVER_MAX_ARGS = '函数只接收 {:d} 个参数,但你传了 {:d} 个。' -MWS_NOT_SUPPORT = '{} 目前还不支持多工作目录,我可能需要重启才能支持新的工作目录...' +MWS_NOT_SUPPORT = '{} 目前还不支持多工作目录,我可能需要重启才能支持新的工作目录...' MWS_RESTART = '重启' MWS_NOT_COMPLETE = '工作目录还没有准备好,你可以稍后再试一下...' MWS_COMPLETE = '工作目录准备好了,你可以再试一下了...' @@ -25,5 +25,6 @@ PARSER_UTF8_MAX = '必须在 {min} 与 {max} 之间' PARSER_ERR_ESC = '错误的转义符' PARSER_MUST_X16 = '必须是16进制字符' PARSER_MISS_EXPONENT = '缺少指数部分' +PARSER_MISS_EXP = '缺少表达式' SYMBOL_ANONYMOUS = '<匿名函数>' diff --git a/server/src/parser/ast.lua b/server/src/parser/ast.lua index 04a625c1..de305ca3 100644 --- a/server/src/parser/ast.lua +++ b/server/src/parser/ast.lua @@ -4,7 +4,13 @@ local utf8_char = utf8.char local Errs local function pushError(err) - err.level = 'error' + 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 end @@ -83,6 +89,15 @@ local defs = { end local v = tonumber(char, 16) if not v then + for i = 1, #char do + if not tonumber(char:sub(i, i), 16) then + pushError { + type = 'MUST_X16', + start = pos + i - 1, + finish = pos + i - 1, + } + end + end return '' end if v < 0 or v > 0x10ffff then @@ -238,29 +253,18 @@ local defs = { obj[max] = nil return obj end, - Table = function (start, table, finish) - if table then - table.start = start - table.finish = finish - 1 - else - table = { - type = 'table', - start = start, - finish = finish - 1, - } - end + Table = function (start, ...) + local table = { + type = 'table', + start = start, + ..., + } + local max = #table + local finish = table[max] + table.finish = finish - 1 + table[max] = nil return table end, - TableFields = function (...) - if ... == '' then - return nil - else - return { - type = 'table', - ..., - } - end - end, NewField = function (key, value) return { type = 'pair', @@ -452,11 +456,11 @@ local defs = { end, -- 捕获错误 - UnknownSymbol = function (start, symbol, finish) + UnknownSymbol = function (start, symbol) pushError { type = 'UNKNOWN_SYMBOL', start = start, - finish = finish - 1, + finish = start + #symbol - 1, info = { symbol = symbol, } @@ -475,6 +479,19 @@ local defs = { [1] = '' } end, + DirtyExp = function (pos) + pushError { + type = 'MISS_EXP', + start = pos, + finish = pos, + } + return { + type = 'name', + start = pos, + finish = pos, + [1] = '' + } + end, MissExponent = function (start, finish) pushError { type = 'MISS_EXPONENT', @@ -529,6 +546,26 @@ local defs = { } } end, + MissBR = function (pos) + pushError { + type = 'MISS_SYMBOL', + start = pos, + finish = pos, + info = { + symbol = ']', + } + } + end, + MissPR = function (pos) + pushError { + type = 'MISS_SYMBOL', + start = pos, + finish = pos, + info = { + symbol = ')', + } + } + end, ErrEsc = function (pos) pushError { type = 'ERR_ESC', @@ -543,6 +580,26 @@ local defs = { finish = math.max(pos + #str - 1, pos), } end, + MissAssign = function (pos) + pushError { + type = 'MISS_SYMBOL', + start = pos, + finish = pos, + info = { + symbol = '=', + } + } + end, + MissTableSep = function (pos) + pushError { + type = 'MISS_SYMBOL', + start = pos, + finish = pos, + info = { + symbol = ',' + } + } + end, } return function (self, lua, mode) diff --git a/server/src/parser/grammar.lua b/server/src/parser/grammar.lua index a13fb465..051ab446 100644 --- a/server/src/parser/grammar.lua +++ b/server/src/parser/grammar.lua @@ -54,6 +54,7 @@ defs.en = '\n' defs.er = '\r' defs.et = '\t' defs.ev = '\v' +defs['nil'] = m.Cp() / function () return nil end defs.NotReserved = function (_, _, str) if RESERVED[str] then return false @@ -94,6 +95,7 @@ grammar 'Common' [[ Word <- [a-zA-Z0-9_] Cut <- !Word X16 <- [a-fA-F0-9] +Rest <- (!%nl .)* AND <- Sp {'and'} Cut BREAK <- Sp 'break' Cut @@ -134,12 +136,11 @@ EChar <- 'a' -> ea / ('z' (%nl / %s)*) -> '' / ('x' {X16 X16}) -> Char16 / ([0-9] [0-9]? [0-9]?) -> Char10 - / ('u{' {} {X16*} '}') - -> CharUtf8 + / ('u{' {} {Word*} '}') -> CharUtf8 -- 错误处理 / 'x' {} -> MissEscX / 'u' !'{' {} -> MissTL - / 'u{' X16* !'}' {} -> MissTR + / 'u{' Word* !'}' {} -> MissTR / {} -> ErrEsc Comp <- Sp {CompList} @@ -188,6 +189,11 @@ ASSIGN <- Sp '=' Nothing <- {} -> Nothing TOCLOSE <- Sp '*toclose' + +DirtyAssign <- ASSIGN / {} -> MissAssign +DirtyBR <- BR / {} -> MissBR +DirtyTR <- TR / {} -> MissTR +DirtyPR <- TR / {} -> MissPR ]] grammar 'Nil' [[ @@ -219,7 +225,7 @@ grammar 'Number' [[ Number <- Sp ({} {NumberDef} {}) -> Number ErrNumber? NumberDef <- Number16 / Number10 -ErrNumber <- ({} {([0-9a-zA-Z] / '.')+} {}) +ErrNumber <- ({} {([0-9a-zA-Z] / '.')+}) -> UnknownSymbol Number10 <- Float10 Float10Exp? @@ -249,7 +255,7 @@ DirtyName <- {} -> DirtyName ]] grammar 'Exp' [[ -Exp <- ExpOr +Exp <- Sp ExpOr ExpOr <- (ExpAnd (OR ExpAnd)*) -> Binary ExpAnd <- (ExpCompare (AND ExpCompare)*) -> Binary ExpCompare <- (ExpBor (Comp ExpBor)*) -> Binary @@ -284,7 +290,8 @@ Suffix <- DOT MustName / Sp ({} BL DirtyExp (BR / Sp) {}) -> Index / Sp ({} PL ExpList (PR / Sp) {}) -> Call -DirtyExp <- Exp / DirtyName +DirtyExp <- Exp + / {} -> DirtyExp ExpList <- (COMMA DirtyExp)+ -> List / (Exp (COMMA DirtyExp)*)? @@ -306,14 +313,28 @@ AfterArg <- DOTS / MustName -Table <- Sp ({} TL TableFields TR? {}) +Table <- ({} TL TR {}) + -> Table + / ({} TL TableFields DirtyTR {}) -> Table -TableFields <- TableSep (TableSep? TableField)+ TableSep? - / (TableField (TableSep? TableField)* TableSep?)? - -> TableFields + / ({} TL DirtyTR {}) + -> Table +TableFields <- TableField + (TableAfterF)* + LastTableSep? + / DirtyExp + (TableAfterF)+ + LastTableSep? +TableAfterF <- TableSep !TR TableField + / DirtyInTable TableSep <- COMMA / SEMICOLON -TableField <- NewIndex / NewField / Exp -NewIndex <- (BL DirtyExp BR ASSIGN DirtyExp) + / {} + -> MissTableSep +LastTableSep<- COMMA / SEMICOLON +TableField <- DirtyInTable? (NewIndex / NewField / Exp) +DirtyInTable<- Sp ({} {(!TR !COMMA !SEMICOLON !Word !BL !PL !TL !DOTS .)+}) + -> UnknownSymbol +NewIndex <- (BL DirtyExp DirtyBR DirtyAssign DirtyExp) -> NewIndex NewField <- (MustName ASSIGN DirtyExp) -> NewField @@ -356,7 +377,7 @@ CrtAction <- SEMICOLON / Set / Call / Exp -UnkAction <- ({} {. (!Sps !CrtAction .)*} {}) +UnkAction <- ({} {. (!Sps !CrtAction .)*}) -> UnknownSymbol SimpleList <- (Simple (COMMA Simple)*) |