4 * Copyright (c) 2000-2006, Darren Hiebert, Elias Pschernig
6 * This source code is released for free distribution under the terms of the
7 * GNU General Public License.
9 * This module contains functions for generating tags for BlitzBasic
10 * (BlitzMax), PureBasic and FreeBasic language files. For now, this is kept
11 * quite simple - but feel free to ask for more things added any time -
12 * patches are of course most welcome.
18 #include "general.h" /* must always come first */
43 static kindOption BasicKinds
[] = {
44 {TRUE
, 'c', "macro", "constants"},
45 {TRUE
, 'f', "function", "functions"},
46 {TRUE
, 'l', "namespace", "labels"},
47 {TRUE
, 't', "struct", "types"},
48 {TRUE
, 'v', "variable", "variables"},
49 {TRUE
, 'g', "externvar", "enumerations"}
52 static KeyWord freebasic_keywords
[] = {
53 {"dim", K_VARIABLE
}, /* must always be the first */
54 {"common", K_VARIABLE
}, /* must always be the second */
55 {"const", K_CONST
}, /* must always be the third */
56 {"function", K_FUNCTION
},
58 {"property", K_FUNCTION
},
59 {"constructor", K_FUNCTION
},
60 {"destructor", K_FUNCTION
},
61 {"private sub", K_FUNCTION
},
62 {"public sub", K_FUNCTION
},
63 {"private function", K_FUNCTION
},
64 {"public function", K_FUNCTION
},
71 * FUNCTION DEFINITIONS
74 /* Match the name of a dim or const starting at pos. */
75 static int extract_dim (char const *pos
, vString
* name
, BasicKind kind
)
77 const char *old_pos
= pos
;
78 while (isspace (*pos
))
81 /* create tags only if there is some space between the keyword and the identifier */
87 if (strncasecmp (pos
, "shared", 6) == 0)
88 pos
+= 6; /* skip keyword "shared" */
90 while (isspace (*pos
))
93 /* capture "dim as String str" */
94 if (strncasecmp (pos
, "as", 2) == 0)
96 pos
+= 2; /* skip keyword "as" */
98 while (isspace (*pos
))
100 while (!isspace (*pos
)) /* skip next part which is a type */
102 while (isspace (*pos
))
104 /* now we are at the name */
106 /* capture "dim as foo ptr bar" */
107 if (strncasecmp (pos
, "ptr", 3) == 0 && isspace(*(pos
+4)))
109 pos
+= 3; /* skip keyword "ptr" */
110 while (isspace (*pos
))
113 /* capture "dim as string * 4096 chunk" */
114 if (strncmp (pos
, "*", 1) == 0)
116 pos
+= 1; /* skip "*" */
117 while (isspace (*pos
) || isdigit(*pos
) || ispunct(*pos
))
121 for (; *pos
&& !isspace (*pos
) && *pos
!= '(' && *pos
!= ',' && *pos
!= '='; pos
++)
122 vStringPut (name
, *pos
);
123 vStringTerminate (name
);
124 makeSimpleTag (name
, BasicKinds
, kind
);
126 /* if the line contains a ',', we have multiple declarations */
127 while (*pos
&& strchr (pos
, ','))
129 /* skip all we don't need(e.g. "..., new_array(5), " we skip "(5)") */
130 while (*pos
!= ',' && *pos
!= '\'')
134 return 0; /* break if we are in a comment */
136 while (isspace (*pos
) || *pos
== ',')
140 return 0; /* break if we are in a comment */
143 for (; *pos
&& !isspace (*pos
) && *pos
!= '(' && *pos
!= ',' && *pos
!= '='; pos
++)
144 vStringPut (name
, *pos
);
145 vStringTerminate (name
);
146 makeSimpleTag (name
, BasicKinds
, kind
);
149 vStringDelete (name
);
153 /* Match the name of a tag (function, variable, type, ...) starting at pos. */
154 static char const *extract_name (char const *pos
, vString
* name
)
156 while (isspace (*pos
))
159 for (; *pos
&& !isspace (*pos
) && *pos
!= '(' && *pos
!= ',' && *pos
!= '='; pos
++)
160 vStringPut (name
, *pos
);
161 vStringTerminate (name
);
165 /* Match a keyword starting at p (case insensitive). */
166 static int match_keyword (const char *p
, KeyWord
const *kw
)
172 for (i
= 0; i
< strlen (kw
->token
); i
++)
174 if (tolower (p
[i
]) != kw
->token
[i
])
177 name
= vStringNew ();
179 if (kw
== &freebasic_keywords
[0] ||
180 kw
== &freebasic_keywords
[1] ||
181 kw
== &freebasic_keywords
[2])
182 return extract_dim (p
, name
, kw
->kind
); /* extract_dim adds the found tag(s) */
188 /* create tags only if there is some space between the keyword and the identifier */
191 vStringDelete (name
);
195 for (j
= 0; j
< 1; j
++)
197 p
= extract_name (p
, name
);
199 makeSimpleTag (name
, BasicKinds
, kw
->kind
);
200 vStringDelete (name
);
205 /* Match a "label:" style label. */
206 static void match_colon_label (char const *p
)
208 char const *end
= p
+ strlen (p
) - 1;
209 while (isspace (*end
))
213 vString
*name
= vStringNew ();
214 vStringNCatS (name
, p
, end
- p
);
215 makeSimpleTag (name
, BasicKinds
, K_LABEL
);
216 vStringDelete (name
);
220 static void findBasicTags (void)
225 keywords
= freebasic_keywords
;
227 while ((line
= (const char *) fileReadLine ()) != NULL
)
229 const char *p
= line
;
235 /* Empty line or comment? */
236 if (!*p
|| *p
== '\'')
239 /* In Basic, keywords always are at the start of the line. */
240 for (kw
= keywords
; kw
->token
; kw
++)
241 if (match_keyword (p
, kw
)) break;
244 match_colon_label (p
);
248 parserDefinition
*FreeBasicParser (void)
250 static char const *extensions
[] = { "bas", "bi", "bb", "pb", NULL
};
251 parserDefinition
*def
= parserNew ("FreeBasic");
252 def
->kinds
= BasicKinds
;
253 def
->kindCount
= KIND_COUNT (BasicKinds
);
254 def
->extensions
= extensions
;
255 def
->parser
= findBasicTags
;
259 /* vi:set tabstop=4 shiftwidth=4: */