Skip to content

Commit 25eb983

Browse files
committed
basic: add more advanced extraction of dim-like variables
1 parent 85ba81a commit 25eb983

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

Units/parser-basic.r/simple.bas.d/expected.tags

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
a input.bas /^Common a As Integer$/;" v
22
b input.bas /^Dim b As Integer$/;" v
3+
c input.bas /^dim as string c(20), d="a", e$/;" v
4+
d input.bas /^dim as string c(20), d="a", e$/;" v
5+
e input.bas /^dim as string c(20), d="a", e$/;" v
36
f input.bas /^Function f()$/;" f
7+
g input.bas /^dim shared as string g$/;" v
8+
h input.bas /^dim as string ptr h$/;" v
9+
i input.bas /^dim as string * 4096 i$/;" v
410
one input.bas /^Const one = 1$/;" c
511
start input.bas /^start:$/;" l
612
str input.bas /^DIM AS STRING str$/;" v

Units/parser-basic.r/simple.bas.d/input.bas

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Const one = 1
55
Common a As Integer
66
Dim b As Integer
77
DIM AS STRING str
8+
dim as string c(20), d="a", e
9+
dim shared as string g
10+
dim as string ptr h
11+
dim as string * 4096 i
812

913
Type test
1014
a As Integer

parsers/basic.c

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ static kindDefinition BasicKinds[] = {
5252
static KeyWord basic_keywords[] = {
5353
/* freebasic */
5454
{"const", K_CONST, 0},
55-
{"dim as", K_VARIABLE, 1},
5655
{"dim", K_VARIABLE, 0},
5756
{"common", K_VARIABLE, 0},
5857
{"function", K_FUNCTION, 0},
@@ -83,6 +82,74 @@ static KeyWord basic_keywords[] = {
8382
* FUNCTION DEFINITIONS
8483
*/
8584

85+
/* Match the name of a dim or const starting at pos. */
86+
static void extract_dim (char const *pos, BasicKind kind)
87+
{
88+
vString *name = vStringNew ();
89+
90+
if (strncasecmp (pos, "shared", 6) == 0)
91+
pos += 6; /* skip keyword "shared" */
92+
93+
while (isspace (*pos))
94+
pos++;
95+
96+
/* capture "dim as String str" */
97+
if (strncasecmp (pos, "as", 2) == 0)
98+
{
99+
pos += 2; /* skip keyword "as" */
100+
101+
while (isspace (*pos))
102+
pos++;
103+
while (!isspace (*pos) && *pos) /* skip next part which is a type */
104+
pos++;
105+
while (isspace (*pos))
106+
pos++;
107+
/* now we are at the name */
108+
}
109+
/* capture "dim as foo ptr bar" */
110+
if (strncasecmp (pos, "ptr", 3) == 0 && isspace(*(pos+3)))
111+
{
112+
pos += 3; /* skip keyword "ptr" */
113+
while (isspace (*pos))
114+
pos++;
115+
}
116+
/* capture "dim as string * 4096 chunk" */
117+
if (strncmp (pos, "*", 1) == 0)
118+
{
119+
pos += 1; /* skip "*" */
120+
while (isspace (*pos) || isdigit(*pos) || ispunct(*pos))
121+
pos++;
122+
}
123+
124+
for (; *pos && !isspace (*pos) && *pos != '(' && *pos != ',' && *pos != '='; pos++)
125+
vStringPut (name, *pos);
126+
makeSimpleTag (name, kind);
127+
128+
/* if the line contains a ',', we have multiple declarations */
129+
while (*pos && strchr (pos, ','))
130+
{
131+
/* skip all we don't need(e.g. "..., new_array(5), " we skip "(5)") */
132+
while (*pos != ',' && *pos != '\'' && *pos)
133+
pos++;
134+
135+
if (*pos == '\'')
136+
break; /* break if we are in a comment */
137+
138+
while (isspace (*pos) || *pos == ',')
139+
pos++;
140+
141+
if (*pos == '\'')
142+
break; /* break if we are in a comment */
143+
144+
vStringClear (name);
145+
for (; *pos && !isspace (*pos) && *pos != '(' && *pos != ',' && *pos != '='; pos++)
146+
vStringPut (name, *pos);
147+
makeSimpleTag (name, kind);
148+
}
149+
150+
vStringDelete (name);
151+
}
152+
86153
/* Match the name of a tag (function, variable, type, ...) starting at pos. */
87154
static char const *extract_name (char const *pos, vString * name)
88155
{
@@ -116,6 +183,12 @@ static int match_keyword (const char *p, KeyWord const *kw)
116183
if (old_p == p)
117184
return 0;
118185

186+
if (kw->kind == K_VARIABLE)
187+
{
188+
extract_dim (p, kw->kind); /* extract_dim adds the found tag(s) */
189+
return 1;
190+
}
191+
119192
name = vStringNew ();
120193
for (j = 0; j < 1 + kw->skip; j++)
121194
{

0 commit comments

Comments
 (0)