Skip to content

Commit 9f9a097

Browse files
committed
Lua: fill scope fields in tags of function kinds
With the last commit for the Lua parser, "lua: don't include module in function names", the lua parser degraded in the following aspects. test = {} test.f = function() pirint "hello" end The original parser can represent the relation between "test" and "f"; the "f" is extracted as "test.f". With the last commit, the parser cannot represent the relation between "test" and "f". To recover the degradation, this change fills the scope fields as following: f iput.lua /^test.f = function()$/;" kind:function scope:unknown:test test.f input.lua /^test.f = function()$/;" kind:function scope:unknown:test extras:qualified "test.f" is tagged only when --extras=+q is given. Far from perfect but better than nothing. TODO: tagging "test". Signed-off-by: Masatake YAMATO <[email protected]>
1 parent bf8189b commit 9f9a097

File tree

9 files changed

+154
-7
lines changed

9 files changed

+154
-7
lines changed

Tmain/list-roles.d/stdout-expected.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Kconfig k/kconfig source on kconfig file loaded
5151
LdScript i/inputSection discarded on discarded when linking
5252
LdScript i/inputSection mapped on mapped to output section
5353
LdScript s/symbol entrypoint on entry points
54+
Lua X/unknown referenced off referenced somehow
5455
M4 I/macrofile included on included macro
5556
M4 I/macrofile sincluded on silently included macro
5657
M4 d/macro undef on undefined
@@ -142,6 +143,7 @@ Kconfig k/kconfig source on kconfig file loaded
142143
LdScript i/inputSection discarded on discarded when linking
143144
LdScript i/inputSection mapped on mapped to output section
144145
LdScript s/symbol entrypoint on entry points
146+
Lua X/unknown referenced off referenced somehow
145147
M4 I/macrofile included on included macro
146148
M4 I/macrofile sincluded on silently included macro
147149
M4 d/macro undef on undefined
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a crash test.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--extras=+q
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
a = {}
2+
a..b = {}
3+
a..b..c = function()
4+
print "hello"
5+
end
6+
a..b..c()
7+
8+
ax = {}
9+
ax..by = {}
10+
ax..by..cz = function()
11+
print "hello"
12+
end
13+
ax..by..cz()
14+
15+
16+
.aX = function()
17+
print "hello"
18+
end
19+
20+
.aX.bY = function()
21+
print "hello"
22+
end
23+
24+
.aX.bY( = function()
25+
print "hello"
26+
end
27+
28+
.(aX.bY = function()
29+
print "hello"
30+
end
31+
32+
..aX.bY = function()
33+
print "hello"
34+
end
35+
36+
.aX.bY.cZ. = function()
37+
print "hello"
38+
end
39+
40+
..aX.bY.cZ. = function()
41+
print "hello"
42+
end
43+
44+
.aX.bY.cZ.. = function()
45+
print "hello"
46+
end
47+
48+
.aX.b(Y.cZ.. = function()
49+
print "hello"
50+
end
51+
52+
.aX.(bY.cZ.. = function()
53+
print "hello"
54+
end
55+
56+
.aX(.bY.cZ.. = function()
57+
print "hello"
58+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--sort=no
2+
--extras=+q
3+
--fields=-+E
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
c input.lua /^a.b.c = function()$/;" f unknown:a.b
2+
a.b.c input.lua /^a.b.c = function()$/;" f unknown:a.b extras:qualified
3+
cz input.lua /^ax.by.cz = function()$/;" f unknown:ax.by
4+
ax.by.cz input.lua /^ax.by.cz = function()$/;" f unknown:ax.by extras:qualified
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
a = {}
2+
a.b = {}
3+
a.b.c = function()
4+
print "hello"
5+
end
6+
a.b.c()
7+
8+
ax = {}
9+
ax.by = {}
10+
ax.by.cz = function()
11+
print "hello"
12+
end
13+
ax.by.cz()
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
i_123 input.lua /^ test.i_123 = function (x)$/;" f
2-
me_12a input.lua /^function test.me_12a(one, two)$/;" f
3-
me_12b input.lua /^function test.me_12b (one, two)$/;" f
1+
i_123 input.lua /^ test.i_123 = function (x)$/;" f unknown:test
2+
me_12a input.lua /^function test.me_12a(one, two)$/;" f unknown:test
3+
me_12b input.lua /^function test.me_12b (one, two)$/;" f unknown:test

parsers/lua.c

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <string.h>
1616

1717
#include "debug.h"
18+
#include "entry.h"
1819
#include "parse.h"
1920
#include "read.h"
2021
#include "routines.h"
@@ -24,11 +25,24 @@
2425
* DATA DEFINITIONS
2526
*/
2627
typedef enum {
27-
K_FUNCTION
28+
K_FUNCTION,
29+
K_UNKNOWN,
2830
} luaKind;
2931

32+
typedef enum {
33+
LUA_UNKNOWN_REFERENCED,
34+
} luaUnknownRole;
35+
36+
static roleDefinition LuaUnknownRoles [] = {
37+
{ false, "referenced", "referenced somehow" },
38+
};
39+
3040
static kindDefinition LuaKinds [] = {
31-
{ true, 'f', "function", "functions" }
41+
{ true, 'f', "function", "functions" },
42+
43+
/* `unknown' is a kind just for making FQ tag for functions. */
44+
{ false, 'X', "unknown", "unknown language object",
45+
.referenceOnly = true, ATTACH_ROLES(LuaUnknownRoles) },
3246
};
3347

3448
/*
@@ -63,6 +77,18 @@ static bool isLuaIdentifier (char c)
6377
return (bool) !(isspace(c) || c == '(' || c == ')' || c == '=' || c == '.' || c == ':');
6478
}
6579

80+
static void set_scope (int child, int parent)
81+
{
82+
if (parent == CORK_NIL || child == CORK_NIL)
83+
return;
84+
85+
tagEntryInfo *e = getEntryInCorkQueue (child);
86+
if (!e)
87+
return;
88+
89+
e->extensionFields.scopeIndex = parent;
90+
}
91+
6692
static void extract_next_token (const char *begin, const char *end_sentinel, vString *name)
6793
{
6894
if (begin == NULL || end_sentinel == NULL)
@@ -90,10 +116,16 @@ static void extract_next_token (const char *begin, const char *end_sentinel, vSt
90116

91117
Assert (begin <= end);
92118

119+
int lastCorkIndx = CORK_NIL;
93120
for (const char *c = begin; c <= end; ++c)
94121
{
95122
if (*c == '.' || *c == ':')
96123
{
124+
int r = makeSimpleRefTag(name,
125+
K_UNKNOWN, LUA_UNKNOWN_REFERENCED);
126+
set_scope(r, lastCorkIndx);
127+
lastCorkIndx = r;
128+
97129
/* Do not include module names in function name */
98130
vStringClear (name);
99131
}
@@ -108,7 +140,8 @@ static void extract_next_token (const char *begin, const char *end_sentinel, vSt
108140
}
109141
}
110142

111-
makeSimpleTag (name, K_FUNCTION);
143+
int d = makeSimpleTag (name, K_FUNCTION);
144+
set_scope(d, lastCorkIndx);
112145
vStringClear (name);
113146
}
114147

@@ -133,12 +166,42 @@ static void extract_prev_token (const char *end, const char *begin_sentinel, vSt
133166
while (begin_sentinel <= begin && isLuaIdentifier (*begin))
134167
begin--;
135168

169+
int targetCorkIndex = CORK_NIL;
136170
if (end - begin)
137171
{
138172
vStringNCatS (name, begin + 1, end - begin);
139-
makeSimpleTag (name, K_FUNCTION);
173+
targetCorkIndex = makeSimpleTag (name, K_FUNCTION);
140174
vStringClear (name);
141175
}
176+
177+
if (targetCorkIndex == CORK_NIL || begin_sentinel == begin)
178+
return;
179+
180+
/* Fill the scope field of the function. */
181+
end = begin;
182+
while (begin_sentinel <= (begin + 1))
183+
{
184+
bool on_boundary = false;
185+
if (begin < begin_sentinel || !isLuaIdentifier (*begin))
186+
{
187+
if (end - begin)
188+
{
189+
vStringNCatS (name, begin + 1, end - begin);
190+
int r = makeSimpleRefTag (name,
191+
K_UNKNOWN, LUA_UNKNOWN_REFERENCED);
192+
set_scope (targetCorkIndex, r);
193+
targetCorkIndex = r;
194+
vStringClear (name);
195+
}
196+
if (begin_sentinel <= begin && ! (*begin == ':' || *begin == '.'))
197+
break;
198+
on_boundary = true;
199+
}
200+
begin--;
201+
202+
if(on_boundary)
203+
end = begin;
204+
}
142205
}
143206

144207
static void findLuaTags (void)
@@ -188,5 +251,7 @@ extern parserDefinition* LuaParser (void)
188251
def->kindCount = ARRAY_SIZE (LuaKinds);
189252
def->extensions = extensions;
190253
def->parser = findLuaTags;
254+
def->useCork = CORK_QUEUE;
255+
def->requestAutomaticFQTag = true;
191256
return def;
192257
}

0 commit comments

Comments
 (0)