Skip to content

Commit 6507b8a

Browse files
authored
Merge branch 'master' into bugfix/cli_default_severity
2 parents 917bdfe + b313323 commit 6507b8a

File tree

4 files changed

+98
-3
lines changed

4 files changed

+98
-3
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<!-- Add all new changes here. They will be moved under a version at release -->
55
* `NEW` Add postfix snippet for `unpack`
66
* `FIX` `diagnostics.severity` defaulting to "Warning" when run using `--check` [#2730](https://github.com/LuaLS/lua-language-server/issues/2730)
7+
* `NEW` Add support for lambda style functions, `|paramList| expr` is syntactic sugar for `function(paramList) return expr end`
78

89
## 3.9.3
910
`2024-6-11`

doc/en-us/config.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,6 +1829,7 @@ Array<string>
18291829
* ``"!"``
18301830
* ``"!="``
18311831
* ``"continue"``
1832+
* ``"|lambda|"``
18321833

18331834
## default
18341835

doc/pt-br/config.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,6 +1829,7 @@ Array<string>
18291829
* ``"!"``
18301830
* ``"!="``
18311831
* ``"continue"``
1832+
* ``"|lambda|"``
18321833

18331834
## default
18341835

script/parser/compile.lua

Lines changed: 95 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2188,13 +2188,14 @@ local function parseActions()
21882188
end
21892189
end
21902190

2191-
local function parseParams(params)
2191+
local function parseParams(params, isLambda)
21922192
local lastSep
21932193
local hasDots
2194+
local endToken = isLambda and '|' or ')'
21942195
while true do
21952196
skipSpace()
21962197
local token = Tokens[Index + 1]
2197-
if not token or token == ')' then
2198+
if not token or token == endToken then
21982199
if lastSep then
21992200
missName()
22002201
end
@@ -2269,7 +2270,7 @@ local function parseParams(params)
22692270
Index = Index + 2
22702271
goto CONTINUE
22712272
end
2272-
skipUnknownSymbol '%,%)%.'
2273+
skipUnknownSymbol ('%,%' .. endToken .. '%.')
22732274
::CONTINUE::
22742275
end
22752276
return params
@@ -2393,6 +2394,91 @@ local function parseFunction(isLocal, isAction)
23932394
return func
23942395
end
23952396

2397+
local function parseLambda(isDoublePipe)
2398+
local lambdaLeft = getPosition(Tokens[Index], 'left')
2399+
local lambdaRight = getPosition(Tokens[Index], 'right')
2400+
local lambda = {
2401+
type = 'function',
2402+
start = lambdaLeft,
2403+
finish = lambdaRight,
2404+
bstart = lambdaRight,
2405+
keyword = {
2406+
[1] = lambdaLeft,
2407+
[2] = lambdaRight,
2408+
},
2409+
hasReturn = true
2410+
}
2411+
Index = Index + 2
2412+
local pipeLeft = getPosition(Tokens[Index], 'left')
2413+
local pipeRight = getPosition(Tokens[Index], 'right')
2414+
skipSpace(true)
2415+
local params
2416+
local LastLocalCount = LocalCount
2417+
-- if nonstandardSymbol for '||' is true it is possible for token to be || when there are no params
2418+
if isDoublePipe then
2419+
params = {
2420+
start = pipeLeft,
2421+
finish = pipeRight,
2422+
parent = lambda,
2423+
type = 'funcargs'
2424+
}
2425+
else
2426+
-- fake chunk to store locals
2427+
pushChunk(lambda)
2428+
LocalCount = 0
2429+
params = parseParams({}, true)
2430+
params.type = 'funcargs'
2431+
params.start = pipeLeft
2432+
params.finish = lastRightPosition()
2433+
params.parent = lambda
2434+
lambda.args = params
2435+
skipSpace()
2436+
if Tokens[Index + 1] == '|' then
2437+
pipeRight = getPosition(Tokens[Index], 'right')
2438+
lambda.finish = pipeRight
2439+
lambda.bstart = pipeRight
2440+
if params then
2441+
params.finish = pipeRight
2442+
end
2443+
Index = Index + 2
2444+
skipSpace()
2445+
else
2446+
lambda.finish = lastRightPosition()
2447+
lambda.bstart = lambda.finish
2448+
if params then
2449+
params.finish = lambda.finish
2450+
end
2451+
missSymbol '|'
2452+
end
2453+
end
2454+
local child = parseExp()
2455+
2456+
2457+
-- don't want popChunk logic here as this is not a real chunk
2458+
Chunk[#Chunk] = nil
2459+
2460+
if child then
2461+
-- create dummy return
2462+
local rtn = {
2463+
type = 'return',
2464+
start = child.start,
2465+
finish = child.finish,
2466+
parent = lambda,
2467+
[1] = child}
2468+
child.parent = rtn
2469+
lambda[1] = rtn
2470+
lambda.returns = {rtn}
2471+
lambda.finish = child.finish
2472+
lambda.keyword[3] = child.finish
2473+
lambda.keyword[4] = child.finish
2474+
else
2475+
lambda.finish = lastRightPosition()
2476+
missExp()
2477+
end
2478+
LocalCount = LastLocalCount
2479+
return lambda
2480+
end
2481+
23962482
local function checkNeedParen(source)
23972483
local token = Tokens[Index + 1]
23982484
if token ~= '.'
@@ -2485,6 +2571,12 @@ local function parseExpUnit()
24852571
return parseFunction()
24862572
end
24872573

2574+
-- FIXME: Use something other than nonstandardSymbol to check for lambda support
2575+
if State.options.nonstandardSymbol['|lambda|'] and (token == '|'
2576+
or token == '||') then
2577+
return parseLambda(token == '||')
2578+
end
2579+
24882580
local node = parseName()
24892581
if node then
24902582
local nameNode = resolveName(node)

0 commit comments

Comments
 (0)