summaryrefslogtreecommitdiff
path: root/script/parser
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-05-08 16:20:30 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-05-08 16:20:30 +0800
commitb5dd953912738cac05d8292eb080c6860f18d418 (patch)
treeeba01883c6989f7b3c814cfba755246d22869431 /script/parser
parent5e0c1dfa6beaf4431d7fae3e392aab7e7da34213 (diff)
parentf3bf7d8fcf18f8fb5b07e17236356012732ee46a (diff)
downloadlua-language-server-b5dd953912738cac05d8292eb080c6860f18d418.zip
Merge remote-tracking branch 'origin/master' into 2.0.0
Diffstat (limited to 'script/parser')
-rw-r--r--script/parser/ast.lua85
-rw-r--r--script/parser/grammar.lua49
-rw-r--r--script/parser/luadoc.lua18
3 files changed, 82 insertions, 70 deletions
diff --git a/script/parser/ast.lua b/script/parser/ast.lua
index 0a188da4..4f27d37d 100644
--- a/script/parser/ast.lua
+++ b/script/parser/ast.lua
@@ -9,7 +9,12 @@ local tableSort = table.sort
_ENV = nil
-local State
+local DefaultState = {
+ lua = '',
+ options = {},
+}
+
+local State = DefaultState
local PushError
local PushDiag
local PushComment
@@ -277,7 +282,7 @@ local Defs = {
type = 'comment.long',
start = start,
finish = finish - 1,
- text = '',
+ text = str,
}
if not close then
local endSymbol = ']' .. ('='):rep(afterEq-beforeEq) .. ']'
@@ -318,7 +323,7 @@ local Defs = {
}
end
end,
- CLongComment = function (start1, finish1, start2, finish2)
+ CLongComment = function (start1, finish1, str, start2, finish2)
if State.options.nonstandardSymbol and State.options.nonstandardSymbol['/**/'] then
else
PushError {
@@ -344,7 +349,7 @@ local Defs = {
type = 'comment.clong',
start = start1,
finish = finish2 - 1,
- text = '',
+ text = str,
}
end,
CCommentPrefix = function (start, finish, commentFinish)
@@ -983,19 +988,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
@@ -1006,7 +999,7 @@ local Defs = {
}
checkMissEnd(functionStart)
if not name then
- return
+ return actions
end
if name.type == 'getname' then
name.type = 'setname'
@@ -1030,35 +1023,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'
@@ -1923,7 +1930,7 @@ local function init(state)
end
local function close()
- State = nil
+ State = DefaultState
PushError = function (...) end
PushDiag = function (...) end
PushComment = function (...) end
diff --git a/script/parser/grammar.lua b/script/parser/grammar.lua
index 53d174f3..01756c2a 100644
--- a/script/parser/grammar.lua
+++ b/script/parser/grammar.lua
@@ -88,13 +88,13 @@ end
grammar 'Comment' [[
Comment <- LongComment
/ '--' ShortComment
-LongComment <- ({} '--[' {} {:eq: '='* :} {} '['
+LongComment <- ({} '--[' {} {:eq: '='* :} {} '[' %nl?
{(!CommentClose .)*}
((CommentClose / %nil) {}))
-> LongComment
/ (
- {} '/*' {}
- (!'*/' .)*
+ {} '/*' {} %nl?
+ {(!'*/' .)*}
{} '*/' {}
)
-> CLongComment
@@ -319,7 +319,7 @@ ExpUnit <- Nil
/ Number
/ Dots
/ Table
- / Function
+ / ExpFunction
/ Simple
Simple <- {| Prefix (Sp Suffix)* |}
@@ -327,7 +327,7 @@ Simple <- {| Prefix (Sp Suffix)* |}
Prefix <- Sp ({} PL DirtyExp DirtyPR {})
-> Paren
/ Single
-Single <- Name
+Single <- !FUNCTION Name
-> Single
Suffix <- SuffixWithoutCall
/ ({} PL SuffixCall DirtyPR {})
@@ -377,8 +377,21 @@ 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
@@ -386,12 +399,6 @@ FuncArgsMiss<- {} -> MissPL DirtyPR %nil
FuncArg <- DOTS
/ Name
/ COMMA
-FunctionBody<- FUNCTION FuncArgs
- {| (!END Action)* |}
- NeedEnd
- / FUNCTION FuncArgsMiss
- {| %nil |}
- NeedEnd
-- 纯占位,修改了 `relabel.lua` 使重复定义不抛错
Action <- !END .
@@ -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' [[
diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua
index af5071b3..0edf5371 100644
--- a/script/parser/luadoc.lua
+++ b/script/parser/luadoc.lua
@@ -2,6 +2,7 @@ local m = require 'lpeglabel'
local re = require 'parser.relabel'
local lines = require 'parser.lines'
local guide = require 'parser.guide'
+local grammar = require 'parser.grammar'
local TokenTypes, TokenStarts, TokenFinishs, TokenContents
local Ci, Offset, pushError, Ct, NextComment, Lines
@@ -990,16 +991,23 @@ local function convertTokens()
end
local function trimTailComment(text)
+ local comment = text
if text:sub(1, 1) == '@' then
- return text:sub(2)
+ comment = text:sub(2)
end
if text:sub(1, 1) == '#' then
- return text:sub(2)
+ comment = text:sub(2)
end
if text:sub(1, 2) == '--' then
- return text:sub(3)
+ comment = text:sub(3)
end
- return text
+ if comment:find '^%s*[\'"[]' then
+ local result = grammar(nil, comment:gsub('^%s+', ''), 'string')
+ if result then
+ comment = result[1][1]
+ end
+ end
+ return comment
end
local function buildLuaDoc(comment)
@@ -1177,7 +1185,7 @@ local function bindDoc(sources, lns, binded)
end
bindGeneric(binded)
local row = guide.positionOf(lns, lastDoc.finish)
- local cstart, cfinish = guide.lineRange(lns, row)
+ local cstart, cfinish = guide.lineRange(lns, row)
local nstart, nfinish = guide.lineRange(lns, row + 1)
bindDocsBetween(sources, binded, bindSources, cstart, cfinish)
if #bindSources == 0 then