diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-07-23 10:12:20 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-07-23 10:12:20 +0800 |
commit | dc7e31883cdb2c8f452d1f73ca06c2af33c393b4 (patch) | |
tree | 2f28a939c0eb248e1fe15dce5a7e312b47ef00ca /server/src/parser | |
parent | a98f8cf8381e5037c282fd3562a41ea6760167bf (diff) | |
download | lua-language-server-dc7e31883cdb2c8f452d1f73ca06c2af33c393b4.zip |
更新语法解析
Diffstat (limited to 'server/src/parser')
-rw-r--r-- | server/src/parser/ast.lua | 167 | ||||
-rw-r--r-- | server/src/parser/grammar.lua | 28 |
2 files changed, 176 insertions, 19 deletions
diff --git a/server/src/parser/ast.lua b/server/src/parser/ast.lua index 80f82c82..8a1c7024 100644 --- a/server/src/parser/ast.lua +++ b/server/src/parser/ast.lua @@ -252,18 +252,66 @@ local Defs = { [1] = false, } end, - LongComment = function (beforeEq, afterEq, missPos) + LongComment = function (beforeEq, afterEq, str, missPos) if missPos then + local endSymbol = ']' .. ('='):rep(afterEq-beforeEq) .. ']' + local s, _, w = str:find('(%][%=]*%])[%c%s]*$') + if s then + pushError { + type = 'ERR_LCOMMENT_END', + start = missPos - #str + s - 1, + finish = missPos - #str + s + #w - 2, + info = { + symbol = endSymbol, + }, + fix = { + title = 'FIX_LCOMMENT_END', + { + start = missPos - #str + s - 1, + finish = missPos - #str + s + #w - 2, + text = endSymbol, + } + }, + } + end pushError { type = 'MISS_SYMBOL', start = missPos, finish = missPos, info = { - symbol = ']' .. ('='):rep(afterEq-beforeEq) .. ']' - } + symbol = endSymbol, + }, + fix = { + title = 'ADD_LCOMMENT_END', + { + start = missPos, + finish = missPos, + text = endSymbol, + } + }, } end end, + CLongComment = function (start1, finish1, start2, finish2) + pushError { + type = 'ERR_C_LONG_COMMENT', + start = start1, + finish = finish2 - 1, + fix = { + title = 'FIX_C_LONG_COMMENT', + { + start = start1, + finish = finish1 - 1, + text = '--[[', + }, + { + start = start2, + finish = finish2 - 1, + text = '--]]' + }, + } + } + end, CCommentPrefix = function (start, finish) pushError { type = 'ERR_COMMENT_PREFIX', @@ -273,7 +321,7 @@ local Defs = { title = 'FIX_COMMENT_PREFIX', { start = start, - finish = finish, + finish = finish - 1, text = '--', }, } @@ -291,13 +339,41 @@ local Defs = { end, LongString = function (beforeEq, afterEq, str, missPos) if missPos then + local endSymbol = ']' .. ('='):rep(afterEq-beforeEq) .. ']' + local s, _, w = str:find('(%][%=]*%])[%c%s]*$') + if s then + pushError { + type = 'ERR_LSTRING_END', + start = missPos - #str + s - 1, + finish = missPos - #str + s + #w - 2, + info = { + symbol = endSymbol, + }, + fix = { + title = 'FIX_LSTRING_END', + { + start = missPos - #str + s - 1, + finish = missPos - #str + s + #w - 2, + text = endSymbol, + } + }, + } + end pushError { type = 'MISS_SYMBOL', start = missPos, finish = missPos, info = { - symbol = ']' .. ('='):rep(afterEq-beforeEq) .. ']' - } + symbol = endSymbol, + }, + fix = { + title = 'ADD_LSTRING_END', + { + start = missPos, + finish = missPos, + text = endSymbol, + } + }, } end return '[' .. ('='):rep(afterEq-beforeEq) .. '[', str @@ -1693,7 +1769,84 @@ local Defs = { symbol = '>' } } - end + end, + ErrAssign = function (start, finish) + pushError { + type = 'ERR_ASSIGN_AS_EQ', + start = start, + finish = finish - 1, + fix = { + title = 'FIX_ASSIGN_AS_EQ', + { + start = start, + finish = finish - 1, + text = '=', + } + } + } + end, + ErrEQ = function (start, finish) + pushError { + type = 'ERR_EQ_AS_ASSIGN', + start = start, + finish = finish - 1, + fix = { + title = 'FIX_EQ_AS_ASSIGN', + { + start = start, + finish = finish - 1, + text = '==', + } + } + } + return '==' + end, + ErrUEQ = function (start, finish) + pushError { + type = 'ERR_UEQ', + start = start, + finish = finish - 1, + fix = { + title = 'FIX_UEQ', + { + start = start, + finish = finish - 1, + text = '~=', + } + } + } + return '==' + end, + ErrThen = function (start, finish) + pushError { + type = 'ERR_THEN_AS_DO', + start = start, + finish = finish - 1, + fix = { + title = 'FIX_THEN_AS_DO', + { + start = start, + finish = finish - 1, + text = 'then', + } + } + } + end, + ErrDo = function (start, finish) + pushError { + type = 'ERR_DO_AS_THEN', + start = start, + finish = finish - 1, + fix = { + title = 'ERR_DO_AS_THEN', + { + start = start, + finish = finish - 1, + text = 'do', + } + } + } + end, } return function (self, lua, mode, version) diff --git a/server/src/parser/grammar.lua b/server/src/parser/grammar.lua index 6e7a028e..36734174 100644 --- a/server/src/parser/grammar.lua +++ b/server/src/parser/grammar.lua @@ -92,11 +92,17 @@ local function errorpos(pos, err) end grammar 'Comment' [[ -Comment <- '--' (LongComment / ShortComment) -LongComment <- ('[' {} {:eq: '='* :} {} '[' - (!CommentClose .)* +Comment <- LongComment / '--' ShortComment +LongComment <- ('--[' {} {:eq: '='* :} {} '[' + {(!CommentClose .)*} (CommentClose / {})) -> LongComment + / ( + {} '/*' {} + (!'*/' .)* + {} '*/' {} + ) + -> CLongComment CommentClose <- ']' =eq ']' ShortComment <- (!%nl .)* ]] @@ -115,6 +121,7 @@ Rest <- (!%nl .)* AND <- Sp {'and'} Cut BREAK <- Sp 'break' Cut DO <- Sp 'do' Cut + / Sp ({} 'then' Cut {}) -> ErrDo ELSE <- Sp 'else' Cut ELSEIF <- Sp 'elseif' Cut END <- Sp 'end' Cut @@ -131,6 +138,7 @@ OR <- Sp {'or'} Cut REPEAT <- Sp 'repeat' Cut RETURN <- Sp 'return' Cut THEN <- Sp 'then' Cut + / Sp ({} 'do' Cut {}) -> ErrThen TRUE <- Sp 'true' Cut UNTIL <- Sp 'until' Cut WHILE <- Sp 'while' Cut @@ -158,13 +166,6 @@ EChar <- 'a' -> ea / 'u{' Word* !'}' {} -> MissTR / {} -> ErrEsc -Comp <- Sp {CompList} -CompList <- '<=' - / '>=' - / '<' - / '>' - / '~=' - / '==' BOR <- Sp {'|'} BXOR <- Sp {'~'} !'=' BAND <- Sp {'&'} @@ -190,6 +191,8 @@ POWER <- Sp {'^'} BinaryOp <- Sp {} {'or'} Cut / Sp {} {'and'} Cut / Sp {} {'<=' / '>=' / '<'!'<' / '>'!'>' / '~=' / '=='} + / Sp {} ({} '=' {}) -> ErrEQ + / Sp {} ({} '!=' {}) -> ErrUEQ / Sp {} {'|'} / Sp {} {'~'} / Sp {} {'&'} @@ -212,7 +215,8 @@ DOTS <- Sp ({} '...') -> DOTS DOT <- Sp ({} '.' !'.') -> DOT COLON <- Sp ({} ':' !':') -> COLON LABEL <- Sp '::' -ASSIGN <- Sp '=' +ASSIGN <- Sp ({} '==' {}) -> ErrAssign + / Sp '=' Nothing <- {} -> Nothing @@ -400,7 +404,7 @@ Semicolon <- SEMICOLON SimpleList <- (Simple (COMMA Simple)*) -> List -Do <- Sp ({} DO DoBody NeedEnd {}) +Do <- Sp ({} 'do' DoBody NeedEnd {}) -> Do DoBody <- (Emmy / !END Action)* -> DoBody |