diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-12-29 18:12:15 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-12-29 18:12:15 +0800 |
commit | 94b099f3b8f93c975f949a22fd2b4a85033160dd (patch) | |
tree | 07df26933477b44fa2814609d619689a83348b43 /server/src | |
parent | 2092b61ac8a42f1dea21c99c0e95bd4e3c446214 (diff) | |
download | lua-language-server-94b099f3b8f93c975f949a22fd2b4a85033160dd.zip |
支持部分错误
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/parser/ast.lua | 118 | ||||
-rw-r--r-- | server/src/parser/grammar.lua | 61 |
2 files changed, 155 insertions, 24 deletions
diff --git a/server/src/parser/ast.lua b/server/src/parser/ast.lua index 341a5519..04a625c1 100644 --- a/server/src/parser/ast.lua +++ b/server/src/parser/ast.lua @@ -40,6 +40,19 @@ local defs = { [1] = str, } end, + LongString = function (beforeEq, afterEq, str, missPos) + if missPos then + pushError { + type = 'MISS_SYMBOL', + start = missPos, + finish = missPos, + info = { + symbol = ']' .. ('='):rep(afterEq-beforeEq) .. ']' + } + } + end + return str + end, Char10 = function (char) char = tonumber(char) if not char or char < 0 or char > 255 then @@ -51,13 +64,40 @@ local defs = { Char16 = function (char) return string_char(tonumber(char, 16)) end, - CharUtf8 = function (char) - char = tonumber(char, 16) - if not char or char < 0 or char > 0x10ffff then - -- TODO 记录错误 + CharUtf8 = function (pos, char) + if #char == 0 then + pushError { + type = 'UTF8_SMALL', + start = pos-3, + finish = pos, + } + return '' + end + if #char > 6 then + pushError { + type = 'UTF8_LARGE', + start = pos-3, + finish = pos+#char, + } + return '' + end + local v = tonumber(char, 16) + if not v then + return '' + end + if v < 0 or v > 0x10ffff then + pushError { + type = 'UTF8_MAX', + start = pos-3, + finish = pos+#char, + info = { + min = '000000', + max = '10ffff', + } + } return '' end - return utf8_char(char) + return utf8_char(v) end, Number = function (start, number, finish) return { @@ -435,6 +475,74 @@ local defs = { [1] = '' } end, + MissExponent = function (start, finish) + pushError { + type = 'MISS_EXPONENT', + start = start, + finish = finish - 1, + } + end, + MissQuote1 = function (pos) + pushError { + type = 'MISS_SYMBOL', + start = pos, + finish = pos, + info = { + symbol = '"' + } + } + end, + MissQuote2 = function (pos) + pushError { + type = 'MISS_SYMBOL', + start = pos, + finish = pos, + info = { + symbol = "'" + } + } + end, + MissEscX = function (pos) + pushError { + type = 'MISS_ESC_X', + start = pos-2, + finish = pos+1, + } + end, + MissTL = function (pos) + pushError { + type = 'MISS_SYMBOL', + start = pos, + finish = pos, + info = { + symbol = '{', + } + } + end, + MissTR = function (pos) + pushError { + type = 'MISS_SYMBOL', + start = pos, + finish = pos, + info = { + symbol = '}', + } + } + end, + ErrEsc = function (pos) + pushError { + type = 'ERR_ESC', + start = pos-1, + finish = pos, + } + end, + MustX16 = function (pos, str) + pushError { + type = 'MUST_X16', + start = pos, + finish = math.max(pos + #str - 1, pos), + } + end, } return function (self, lua, mode) diff --git a/server/src/parser/grammar.lua b/server/src/parser/grammar.lua index 562dab12..a13fb465 100644 --- a/server/src/parser/grammar.lua +++ b/server/src/parser/grammar.lua @@ -61,9 +61,6 @@ defs.NotReserved = function (_, _, str) return true, str end -defs.first = function (first, ...) - return first -end local eof = re.compile '!. / %{SYNTAX_ERROR}' local function grammar(tag) @@ -94,7 +91,8 @@ Sps <- (Comment / %nl / %s)+ ]] grammar 'Common' [[ -Cut <- ![a-zA-Z0-9_] +Word <- [a-zA-Z0-9_] +Cut <- !Word X16 <- [a-fA-F0-9] AND <- Sp {'and'} Cut @@ -133,10 +131,16 @@ EChar <- 'a' -> ea / '"' / "'" / %nl - / ('z' (%nl / %s)*) -> '' - / ('x' {X16 X16}) -> Char16 - / ([0-9] [0-9]? [0-9]?) -> Char10 - / ('u{' {X16^+1^-6} '}')-> CharUtf8 + / ('z' (%nl / %s)*) -> '' + / ('x' {X16 X16}) -> Char16 + / ([0-9] [0-9]? [0-9]?) -> Char10 + / ('u{' {} {X16*} '}') + -> CharUtf8 + -- 错误处理 + / 'x' {} -> MissEscX + / 'u' !'{' {} -> MissTL + / 'u{' X16* !'}' {} -> MissTR + / {} -> ErrEsc Comp <- Sp {CompList} CompList <- '<=' @@ -198,23 +202,41 @@ Boolean <- Sp ({} -> True) TRUE grammar 'String' [[ String <- Sp ({} StringDef {}) -> String -StringDef <- '"' {~(Esc / !%nl !'"' .)*~} -> first (%nl / '"'?) - / "'" {~(Esc / !%nl !"'" .)*~} -> first (%nl / "'"?) - / '[' {:eq: '='* :} '[' {(!StringClose .)*} -> first StringClose +StringDef <- '"' + {~(Esc / !%nl !'"' .)*~} -> 1 + ('"' / {} -> MissQuote1) + / "'" + {~(Esc / !%nl !"'" .)*~} -> 1 + ("'" / {} -> MissQuote2) + / ('[' {} {:eq: '='* :} {} '[' + {(!StringClose .)*} -> 1 + (StringClose / {})) + -> LongString StringClose <- ']' =eq ']' ]] grammar 'Number' [[ Number <- Sp ({} {NumberDef} {}) -> Number + ErrNumber? NumberDef <- Number16 / Number10 +ErrNumber <- ({} {([0-9a-zA-Z] / '.')+} {}) + -> UnknownSymbol -Number10 <- Integer10 Float10 -Integer10 <- '0' / [1-9] [0-9]* -Float10 <- ('.' [0-9]*)? ([eE] [+-]? [1-9]? [0-9]*)? - -Number16 <- Integer16 Float16 -Integer16 <- '0' [xX] X16* -Float16 <- ('.' X16*)? ([pP] [+-]? [1-9]? [0-9]*)? +Number10 <- Float10 Float10Exp? + / Integer10 Float10? Float10Exp? +Integer10 <- '0' / [1-9] [0-9]* '.'? [0-9]* +Float10 <- '.' [0-9]+ +Float10Exp <- [eE] [+-]? [1-9] [0-9]* + / ({} [eE] [+-]? {}) -> MissExponent + +Number16 <- '0' [xX] Float16 Float16Exp? + / '0' [xX] Integer16 Float16? Float16Exp? +Integer16 <- X16+ '.'? X16* + / ({} {Word*}) -> MustX16 +Float16 <- '.' X16+ + / '.' ({} {Word*}) -> MustX16 +Float16Exp <- [pP] [+-]? [1-9] [0-9]* + / ({} [pP] [+-]? {}) -> MissExponent ]] grammar 'Name' [[ @@ -425,7 +447,8 @@ LocalFunction ]] grammar 'Lua' [[ -Lua <- Action* -> Lua Sp +Lua <- Head? Action* -> Lua Sp +Head <- '#' (!%nl .)* ]] return function (lua, mode, parser_) |