diff options
Diffstat (limited to 'server/src/parser')
-rw-r--r-- | server/src/parser/ast.lua | 40 | ||||
-rw-r--r-- | server/src/parser/grammar.lua | 35 |
2 files changed, 45 insertions, 30 deletions
diff --git a/server/src/parser/ast.lua b/server/src/parser/ast.lua index c419e50a..341a5519 100644 --- a/server/src/parser/ast.lua +++ b/server/src/parser/ast.lua @@ -4,6 +4,7 @@ local utf8_char = utf8.char local Errs local function pushError(err) + err.level = 'error' Errs[#Errs+1] = err end @@ -74,20 +75,6 @@ local defs = { [1] = str, } end, - DirtyName = function (pos) - pushError { - type = 'MISS_NAME', - start = pos, - finish = pos, - level = 'error', - } - return { - type = 'name', - start = pos, - finish = pos, - [1] = '' - } - end, Simple = function (first, ...) if ... then local obj = { @@ -423,6 +410,31 @@ local defs = { end return {...} end, + + -- 捕获错误 + UnknownSymbol = function (start, symbol, finish) + pushError { + type = 'UNKNOWN_SYMBOL', + start = start, + finish = finish - 1, + info = { + symbol = symbol, + } + } + end, + DirtyName = function (pos) + pushError { + type = 'MISS_NAME', + start = pos, + finish = pos, + } + return { + type = 'name', + start = pos, + finish = pos, + [1] = '' + } + end, } return function (self, lua, mode) diff --git a/server/src/parser/grammar.lua b/server/src/parser/grammar.lua index 7afea5e7..562dab12 100644 --- a/server/src/parser/grammar.lua +++ b/server/src/parser/grammar.lua @@ -78,7 +78,6 @@ local function errorpos(pos, err) type = 'UNKNOWN', start = pos, err = err, - level = 'error', } end @@ -91,6 +90,7 @@ ShortComment <- (!%nl .)* grammar 'Sp' [[ Sp <- (Comment / %nl / %s)* +Sps <- (Comment / %nl / %s)+ ]] grammar 'Common' [[ @@ -299,13 +299,13 @@ NewField <- (MustName ASSIGN DirtyExp) Function <- Sp ({} FunctionBody {}) -> Function FunctionBody<- FUNCTION FuncName PL ArgList PR - Action* + (!END Action)* END? / FUNCTION FuncName PL ArgList PR? - Action* + (!END Action)* END? / FUNCTION FuncName Nothing - Action* + (!END Action)* END? FuncName <- (Name? (FuncSuffix)*) -> Simple @@ -317,7 +317,8 @@ Action <- !END . ]] grammar 'Action' [[ -Action <- SEMICOLON +Action <- Sp (CrtAction / UnkAction) +CrtAction <- SEMICOLON / Do / Break / Return @@ -333,13 +334,15 @@ Action <- SEMICOLON / Set / Call / Exp +UnkAction <- ({} {. (!Sps !CrtAction .)*} {}) + -> UnknownSymbol SimpleList <- (Simple (COMMA Simple)*) -> List Do <- Sp ({} DO DoBody END? {}) -> Do -DoBody <- Action* +DoBody <- (!END Action)* -> DoBody Break <- BREAK @@ -362,19 +365,19 @@ IfBody <- IfHead (ElsePart -> ElseBlock)? END? IfPart <- IF Exp THEN - {} (!ELSEIF !ELSE Action)* {} + {} (!ELSEIF !ELSE !END Action)* {} / IF DirtyExp THEN - {} (!ELSEIF !ELSE Action)* {} + {} (!ELSEIF !ELSE !END Action)* {} / IF DirtyExp {} {} ElseIfPart <- ELSEIF Exp THEN - {} (!ELSE !ELSEIF Action)* {} + {} (!ELSE !ELSEIF !END Action)* {} / ELSEIF DirtyExp THEN - {} (!ELSE !ELSEIF Action)* {} + {} (!ELSE !ELSEIF !END Action)* {} / ELSEIF DirtyExp {} {} ElsePart <- ELSE - {} Action* {} + {} (!END Action)* {} For <- Loop / In / FOR @@ -382,7 +385,7 @@ For <- Loop / In Loop <- Sp ({} LoopBody {}) -> Loop LoopBody <- FOR LoopStart LoopFinish LoopStep DO? - Action* + (!END Action)* END? LoopStart <- MustName ASSIGN DirtyExp LoopFinish <- COMMA? Exp @@ -394,19 +397,19 @@ LoopStep <- COMMA DirtyExp In <- Sp ({} InBody {}) -> In InBody <- FOR NameList IN? ExpList DO? - Action* + (!END Action)* END? While <- Sp ({} WhileBody {}) -> While WhileBody <- WHILE Exp DO - Action* + (!END Action)* END? Repeat <- Sp ({} RepeatBody {}) -> Repeat RepeatBody <- REPEAT - Action* + (!UNTIL Action)* UNTIL Exp Local <- (LOCAL TOCLOSE? NameList (ASSIGN ExpList)?) @@ -422,7 +425,7 @@ LocalFunction ]] grammar 'Lua' [[ -Lua <- (Sp Action)* -> Lua Sp +Lua <- Action* -> Lua Sp ]] return function (lua, mode, parser_) |