summaryrefslogtreecommitdiff
path: root/server/src
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2018-12-30 19:19:59 +0800
committer最萌小汐 <sumneko@hotmail.com>2018-12-30 19:19:59 +0800
commitdfbdcbd3f01602307f49f1af098899264d0162e7 (patch)
tree423be756ec7460bd3fee58a101d92c9690da5788 /server/src
parentfadb6538eaadf65764f33a2fd3e14bb640b3af27 (diff)
downloadlua-language-server-dfbdcbd3f01602307f49f1af098899264d0162e7.zip
更新语法解析
Diffstat (limited to 'server/src')
-rw-r--r--server/src/parser/ast.lua105
-rw-r--r--server/src/parser/grammar.lua47
2 files changed, 115 insertions, 37 deletions
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)*)