Skip to content

Commit

Permalink
update parser
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Apr 29, 2021
1 parent 2ffcab3 commit 6c4f440
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 57 deletions.
1 change: 1 addition & 0 deletions locale/en-us/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ PARSER_UNEXPECT_SYMBOL = 'Unexpected symbol `{symbol}` .'
PARSER_UNKNOWN_TAG = 'Unknown attribute.'
PARSER_MULTI_TAG = 'Dose not support multi attributes.'
PARSER_UNEXPECT_LFUNC_NAME = 'Local function can only use identifiers as name.'
PARSER_UNEXPECT_EFUNC_NAME = 'Function as expression cannot be named.'
PARSER_ERR_LCOMMENT_END = 'Multi-line annotations should be closed by `{symbol}` .'
PARSER_ERR_C_LONG_COMMENT = 'Lua should use `--[[ ]]` for multi-line annotations.'
PARSER_ERR_LSTRING_END = 'Long string should be closed by `{symbol}` .'
Expand Down
1 change: 1 addition & 0 deletions locale/zh-cn/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ PARSER_UNEXPECT_SYMBOL = '未知的符号 `{symbol}` 。'
PARSER_UNKNOWN_TAG = '不支持的属性。'
PARSER_MULTI_TAG = '只能设置一个属性。'
PARSER_UNEXPECT_LFUNC_NAME = '局部函数只能使用标识符作为名称。'
PARSER_UNEXPECT_EFUNC_NAME = '函数作为表达式时不能命名。'
PARSER_ERR_LCOMMENT_END = '应使用`{symbol}`来关闭多行注释。'
PARSER_ERR_C_LONG_COMMENT = 'Lua应使用`--[[ ]]`来进行多行注释。'
PARSER_ERR_LSTRING_END = '应使用`{symbol}`来关闭长字符串。'
Expand Down
70 changes: 36 additions & 34 deletions script/parser/ast.lua
Original file line number Diff line number Diff line change
Expand Up @@ -983,19 +983,7 @@ local Defs = {
finish = start,
}
end,
Function = function (functionStart, functionFinish, args, actions, endStart, endFinish)
actions.type = 'function'
actions.start = functionStart
actions.finish = endFinish - 1
actions.args = args
actions.keyword= {
functionStart, functionFinish - 1,
endStart, endFinish - 1,
}
checkMissEnd(functionStart)
return actions
end,
NamedFunction = function (functionStart, functionFinish, name, args, actions, endStart, endFinish)
Function = function (functionStart, functionFinish, name, args, actions, endStart, endFinish)
actions.type = 'function'
actions.start = functionStart
actions.finish = endFinish - 1
Expand All @@ -1006,7 +994,7 @@ local Defs = {
}
checkMissEnd(functionStart)
if not name then
return
return actions
end
if name.type == 'getname' then
name.type = 'setname'
Expand All @@ -1030,35 +1018,49 @@ local Defs = {
name.vstart = functionStart
return name
end,
LocalFunction = function (start, functionStart, functionFinish, name, args, actions, endStart, endFinish)
actions.type = 'function'
actions.start = start
actions.finish = endFinish - 1
actions.args = args
actions.keyword= {
functionStart, functionFinish - 1,
endStart, endFinish - 1,
}
checkMissEnd(start)

if not name then
return
LocalFunction = function (start, name)
if name.type == 'function' then
PushError {
type = 'MISS_NAME',
start = name.keyword[2] + 1,
finish = name.keyword[2] + 1,
}
return name
end

if name.type ~= 'getname' then
if name.type ~= 'setname' then
PushError {
type = 'UNEXPECT_LFUNC_NAME',
start = name.start,
finish = name.finish,
}
return
return name
end

local loc = createLocal(name, name.start, actions)
local loc = createLocal(name, name.start, name.value)
loc.localfunction = true
loc.vstart = functionStart

return loc
loc.vstart = name.value.start
return name
end,
NamedFunction = function (name)
if name.type == 'function' then
PushError {
type = 'MISS_NAME',
start = name.keyword[2] + 1,
finish = name.keyword[2] + 1,
}
end
return name
end,
ExpFunction = function (func)
if func.type ~= 'function' then
PushError {
type = 'UNEXPECT_EFUNC_NAME',
start = func.start,
finish = func.finish,
}
return func.value
end
return func
end,
Table = function (start, tbl, finish)
tbl.type = 'table'
Expand Down
43 changes: 20 additions & 23 deletions script/parser/grammar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -319,15 +319,15 @@ ExpUnit <- Nil
/ Number
/ Dots
/ Table
/ Function
/ ExpFunction
/ Simple
Simple <- {| Prefix (Sp Suffix)* |}
-> Simple
Prefix <- Sp ({} PL DirtyExp DirtyPR {})
-> Paren
/ Single
Single <- Name
Single <- !FUNCTION Name
-> Single
Suffix <- SuffixWithoutCall
/ ({} PL SuffixCall DirtyPR {})
Expand Down Expand Up @@ -377,21 +377,28 @@ NewIndex <- Sp ({} Index NeedAssign DirtyExp {})
NewField <- Sp ({} MustName ASSIGN DirtyExp {})
-> NewField
ExpFunction <- Function
-> ExpFunction
Function <- FunctionBody
-> Function
FunctionBody
<- FUNCTION FuncName FuncArgs
{| (!END Action)* |}
NeedEnd
/ FUNCTION FuncName FuncArgsMiss
{| %nil |}
NeedEnd
FuncName <- !END {| Single (Sp SuffixWithoutCall)* |}
-> Simple
/ %nil
FuncArgs <- Sp ({} PL {| FuncArg+ |} DirtyPR {})
-> FuncArgs
/ PL DirtyPR %nil
FuncArgsMiss<- {} -> MissPL DirtyPR %nil
FuncArg <- DOTS
/ Name
/ COMMA
FunctionBody<- FUNCTION FuncArgs
{| (!END Action)* |}
NeedEnd
/ FUNCTION FuncArgsMiss
{| %nil |}
NeedEnd
-- 纯占位,修改了 `relabel.lua` 使重复定义不抛错
Action <- !END .
Expand Down Expand Up @@ -515,26 +522,16 @@ LocalNameList
LocalName <- (MustName LocalAttr?)
-> LocalName
NamedFunction
<- Function
-> NamedFunction
Call <- Simple
-> SimpleCall
LocalFunction
<- Sp ({} LOCAL FunctionNamedBody)
<- Sp ({} LOCAL Function)
-> LocalFunction
NamedFunction
<- FunctionNamedBody
-> NamedFunction
FunctionNamedBody
<- FUNCTION FuncName FuncArgs
{| (!END Action)* |}
NeedEnd
/ FUNCTION FuncName FuncArgsMiss
{| %nil |}
NeedEnd
FuncName <- {| Single (Sp SuffixWithoutCall)* |}
-> Simple
/ {} -> MissName %nil
]]

grammar 'Lua' [[
Expand Down

0 comments on commit 6c4f440

Please sign in to comment.