Skip to content

Commit 9b33baa

Browse files
committed
add classes for each parser.object type
1 parent 6ba0c93 commit 9b33baa

File tree

11 files changed

+335
-53
lines changed

11 files changed

+335
-53
lines changed

script/parser/compile.lua

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,7 @@ local function resolveLable(label, obj)
816816
end
817817
end
818818

819+
---@param gotos parser.object.goto[]
819820
local function resolveGoTo(gotos)
820821
for i = 1, #gotos do
821822
local action = gotos[i]
@@ -848,6 +849,7 @@ local function popChunk()
848849
Chunk[#Chunk] = nil
849850
end
850851

852+
---@return parser.object.nil?
851853
local function parseNil()
852854
if Tokens[Index + 1] ~= 'nil' then
853855
return nil
@@ -861,6 +863,7 @@ local function parseNil()
861863
}
862864
end
863865

866+
---@return parser.object.boolean?
864867
local function parseBoolean()
865868
local word = Tokens[Index+1]
866869
if word ~= 'true'
@@ -1342,6 +1345,7 @@ local function dropNumberTail(offset, integer)
13421345
return finish + 1
13431346
end
13441347

1348+
---@return (parser.object.number|parser.object.integer)?
13451349
local function parseNumber()
13461350
local offset = Tokens[Index]
13471351
if not offset then
@@ -1379,6 +1383,7 @@ local function parseNumber()
13791383
if neg then
13801384
number = - number
13811385
end
1386+
--- @type parser.object.number|parser.object.integer
13821387
local result = {
13831388
type = integer and 'integer' or 'number',
13841389
start = startPos,
@@ -1412,6 +1417,7 @@ local function isKeyWord(word, nextToken)
14121417
return false
14131418
end
14141419

1420+
---@return parser.object.name?
14151421
local function parseName(asAction)
14161422
local word = peekWord()
14171423
if not word then
@@ -1552,11 +1558,13 @@ local function parseExpList(mini)
15521558
return list
15531559
end
15541560

1561+
---@return parser.object.index
15551562
local function parseIndex()
15561563
local start = getPosition(Tokens[Index], 'left')
15571564
Index = Index + 2
15581565
skipSpace()
15591566
local exp = parseExp()
1567+
--- @type parser.object.index
15601568
local index = {
15611569
type = 'index',
15621570
start = start,
@@ -1578,13 +1586,18 @@ local function parseIndex()
15781586
return index
15791587
end
15801588

1589+
---@return parser.object.table
15811590
local function parseTable()
1591+
local start = getPosition(Tokens[Index], 'left')
1592+
local finish = getPosition(Tokens[Index], 'right')
1593+
---@type parser.object.table
15821594
local tbl = {
15831595
type = 'table',
1584-
start = getPosition(Tokens[Index], 'left'),
1585-
finish = getPosition(Tokens[Index], 'right'),
1596+
start = start,
1597+
finish = finish,
1598+
bstart = finish,
1599+
bfinish = finish,
15861600
}
1587-
tbl.bstart = tbl.finish
15881601
Index = Index + 2
15891602
local index = 0
15901603
local tindex = 0
@@ -1624,6 +1637,7 @@ local function parseTable()
16241637
wantSep = true
16251638
skipSpace()
16261639
local fvalue = parseExp()
1640+
---@type parser.object.tablefield
16271641
local tfield = {
16281642
type = 'tablefield',
16291643
start = name.start,
@@ -1634,8 +1648,9 @@ local function parseTable()
16341648
field = name,
16351649
value = fvalue,
16361650
}
1637-
name.type = 'field'
1638-
name.parent = tfield
1651+
local field = name --[[@as parser.object.field]]
1652+
field.type = 'field'
1653+
field.parent = tfield
16391654
if fvalue then
16401655
fvalue.parent = tfield
16411656
else
@@ -1667,6 +1682,7 @@ local function parseTable()
16671682
end
16681683
index = index + 1
16691684
tindex = tindex + 1
1685+
---@type parser.object.tableexp
16701686
local texp = {
16711687
type = 'tableexp',
16721688
start = exp.start,
@@ -1689,7 +1705,7 @@ local function parseTable()
16891705
}
16901706
end
16911707
wantSep = true
1692-
local tindex = parseIndex()
1708+
local tindex = parseIndex() --[[@as parser.object.tableindex]]
16931709
skipSpace()
16941710
tindex.type = 'tableindex'
16951711
tindex.node = tbl
@@ -1803,7 +1819,12 @@ local function parseSimple(node, funcName)
18031819
}
18041820
Index = Index + 2
18051821
skipSpace()
1806-
local field = parseName(true)
1822+
local field = parseName(true) --[[@as parser.object.field?]]
1823+
if field then
1824+
field.type = 'field'
1825+
end
1826+
1827+
---@type parser.object.getfield
18071828
local getfield = {
18081829
type = 'getfield',
18091830
start = node.start,
@@ -1814,7 +1835,6 @@ local function parseSimple(node, funcName)
18141835
}
18151836
if field then
18161837
field.parent = getfield
1817-
field.type = 'field'
18181838
if currentName then
18191839
if node.type == 'getlocal'
18201840
or node.type == 'getglobal'
@@ -1843,7 +1863,8 @@ local function parseSimple(node, funcName)
18431863
}
18441864
Index = Index + 2
18451865
skipSpace()
1846-
local method = parseName(true)
1866+
local method = parseName(true) --[[@as parser.object.method?]]
1867+
---@type parser.object.getmethod
18471868
local getmethod = {
18481869
type = 'getmethod',
18491870
start = node.start,
@@ -1904,12 +1925,14 @@ local function parseSimple(node, funcName)
19041925
break
19051926
end
19061927
local tbl = parseTable()
1928+
---@type parser.object.call
19071929
local call = {
19081930
type = 'call',
19091931
start = node.start,
19101932
finish = tbl.finish,
19111933
node = node,
19121934
}
1935+
---@type parser.object.callargs
19131936
local args = {
19141937
type = 'callargs',
19151938
start = tbl.start,
@@ -1970,7 +1993,7 @@ local function parseSimple(node, funcName)
19701993
node.parent = call
19711994
node = call
19721995
else
1973-
local index = parseIndex()
1996+
local index = parseIndex() --[[@as parser.object.getindex]]
19741997
local bstart = index.start
19751998
index.type = 'getindex'
19761999
index.start = node.start
@@ -2279,6 +2302,7 @@ local function parseParams(params, isLambda)
22792302
return params
22802303
end
22812304

2305+
---@return parser.object.function
22822306
local function parseFunction(isLocal, isAction)
22832307
local funcLeft = getPosition(Tokens[Index], 'left')
22842308
local funcRight = getPosition(Tokens[Index] + 7, 'right')
@@ -2292,6 +2316,7 @@ local function parseFunction(isLocal, isAction)
22922316
[2] = funcRight,
22932317
},
22942318
}
2319+
---@cast func parser.object.function
22952320
Index = Index + 2
22962321
skipSpace(true)
22972322
local hasLeftParen = Tokens[Index + 1] == '('
@@ -2463,6 +2488,7 @@ local function parseLambda(isDoublePipe)
24632488

24642489
if child then
24652490
-- create dummy return
2491+
---@type parser.object.return
24662492
local rtn = {
24672493
type = 'return',
24682494
start = child.start,
@@ -2741,6 +2767,7 @@ function parseExp(asAction, level)
27412767
missExp()
27422768
end
27432769
end
2770+
---@type parser.object.binary
27442771
local bin = {
27452772
type = 'binary',
27462773
start = exp.start,
@@ -3199,7 +3226,11 @@ local function parseLabel()
31993226
local left = getPosition(Tokens[Index], 'left')
32003227
Index = Index + 2
32013228
skipSpace()
3202-
local label = parseName()
3229+
local label = parseName() --[[@as parser.object.label]]
3230+
if label then
3231+
label.type = 'label'
3232+
end
3233+
32033234
skipSpace()
32043235

32053236
if not label then
@@ -3218,7 +3249,6 @@ local function parseLabel()
32183249
return nil
32193250
end
32203251

3221-
label.type = 'label'
32223252
pushActionIntoCurrentChunk(label)
32233253

32243254
local block = guide.getBlock(label)
@@ -3262,20 +3292,23 @@ local function parseLabel()
32623292
return label
32633293
end
32643294

3295+
---@return parser.object.goto?
32653296
local function parseGoTo()
32663297
local start = getPosition(Tokens[Index], 'left')
32673298
Index = Index + 2
32683299
skipSpace()
32693300

3270-
local action = parseName()
3301+
local action = parseName() --[[@as parser.object.goto]]
3302+
if action then
3303+
action.type = 'goto'
3304+
action.keyStart = start
3305+
end
3306+
32713307
if not action then
32723308
missName()
32733309
return nil
32743310
end
32753311

3276-
action.type = 'goto'
3277-
action.keyStart = start
3278-
32793312
for i = #Chunk, 1, -1 do
32803313
local chunk = Chunk[i]
32813314
if chunk.type == 'function'
@@ -3305,6 +3338,7 @@ local function parseIfBlock(parent)
33053338
local ifLeft = getPosition(Tokens[Index], 'left')
33063339
local ifRight = getPosition(Tokens[Index] + 1, 'right')
33073340
Index = Index + 2
3341+
---@type parser.object.ifblock
33083342
local ifblock = {
33093343
type = 'ifblock',
33103344
parent = parent,
@@ -3712,6 +3746,7 @@ local function parseFor()
37123746
end
37133747

37143748
local function parseWhile()
3749+
---@type parser.object.while
37153750
local action = {
37163751
type = 'while',
37173752
start = getPosition(Tokens[Index], 'left'),
@@ -3840,6 +3875,7 @@ local function parseRepeat()
38403875
return action
38413876
end
38423877

3878+
---@return parser.object.break
38433879
local function parseBreak()
38443880
local returnLeft = getPosition(Tokens[Index], 'left')
38453881
local returnRight = getPosition(Tokens[Index] + #Tokens[Index + 1] - 1, 'right')
@@ -3952,7 +3988,7 @@ function parseAction()
39523988
exp.parent = name
39533989
if name.type == 'setlocal' then
39543990
local loc = name.node
3955-
if loc.attrs then
3991+
if loc and loc.attrs then
39563992
pushError {
39573993
type = 'SET_CONST',
39583994
start = name.start,

0 commit comments

Comments
 (0)