@@ -1087,99 +1087,6 @@ function m.getKeyType(obj)
10871087 return m .getKeyTypeOfLiteral (obj )
10881088end
10891089
1090- --- 测试 a 到 b 的路径(不经过函数,不考虑 goto),
1091- --- 每个路径是一个 block 。
1092- ---
1093- --- 如果 a 在 b 的前面,返回 `"before"` 加上 2个`list<block>`
1094- ---
1095- --- 如果 a 在 b 的后面,返回 `"after"` 加上 2个`list<block>`
1096- ---
1097- --- 否则返回 `false`
1098- ---
1099- --- 返回的2个 `list` 分别为基准block到达 a 与 b 的路径。
1100- --- @param a table
1101- --- @param b table
1102- --- @return string | false mode
1103- --- @return table ? pathA
1104- --- @return table ? pathB
1105- function m .getPath (a , b , sameFunction )
1106- --- 首先测试双方在同一个函数内
1107- if sameFunction and m .getParentFunction (a ) ~= m .getParentFunction (b ) then
1108- return false
1109- end
1110- local mode
1111- local objA
1112- local objB
1113- if a .finish < b .start then
1114- mode = ' before'
1115- objA = a
1116- objB = b
1117- elseif a .start > b .finish then
1118- mode = ' after'
1119- objA = b
1120- objB = a
1121- else
1122- return ' equal' , {}, {}
1123- end
1124- local pathA = {}
1125- local pathB = {}
1126- for _ = 1 , 1000 do
1127- objA = m .getParentBlock (objA )
1128- if objA then
1129- pathA [# pathA + 1 ] = objA
1130- if (not sameFunction and objA .type == ' function' ) or objA .type == ' main' then
1131- break
1132- end
1133- end
1134- end
1135- for _ = 1 , 1000 do
1136- objB = m .getParentBlock (objB )
1137- if objB then
1138- pathB [# pathB + 1 ] = objB
1139- if (not sameFunction and objB .type == ' function' ) or objB .type == ' main' then
1140- break
1141- end
1142- end
1143- end
1144- -- pathA: {1, 2, 3, 4, 5}
1145- -- pathB: {5, 6, 2, 3}
1146- local top = # pathB
1147- local start
1148- for i = # pathA , 1 , - 1 do
1149- local currentBlock = pathA [i ]
1150- if currentBlock == pathB [top ] then
1151- start = i
1152- break
1153- end
1154- end
1155- if not start then
1156- return false
1157- end
1158- -- pathA: { 1, 2, 3}
1159- -- pathB: {5, 6, 2, 3}
1160- local extra = 0
1161- local align = top - start
1162- for i = start , 1 , - 1 do
1163- local currentA = pathA [i ]
1164- local currentB = pathB [i + align ]
1165- if currentA ~= currentB then
1166- extra = i
1167- break
1168- end
1169- end
1170- -- pathA: {1}
1171- local resultA = {}
1172- for i = extra , 1 , - 1 do
1173- resultA [# resultA + 1 ] = pathA [i ]
1174- end
1175- -- pathB: {5, 6}
1176- local resultB = {}
1177- for i = extra + align , 1 , - 1 do
1178- resultB [# resultB + 1 ] = pathB [i ]
1179- end
1180- return mode , resultA , resultB
1181- end
1182-
11831090--- 是否是全局变量(包括 _G.XXX 形式)
11841091--- @param source parser.object
11851092--- @return boolean
@@ -1338,4 +1245,19 @@ function m.getFunctionSelfNode(func)
13381245 return nil
13391246end
13401247
1248+ --- @param source parser.object
1249+ --- @return parser.object ?
1250+ function m .getTopBlock (source )
1251+ for _ = 1 , 1000 do
1252+ local block = source .parent
1253+ if not m .isBlockType (block ) then
1254+ return nil
1255+ end
1256+ if block .type ~= ' do' then
1257+ return block
1258+ end
1259+ end
1260+ return nil
1261+ end
1262+
13411263return m
0 commit comments