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 */
46 static kindOption BasicKinds
[] = {
47 {TRUE
, 'c', "constant", "constants"},
48 {TRUE
, 'f', "function", "functions"},
49 {TRUE
, 'l', "label", "labels"},
50 {TRUE
, 't', "type", "types"},
51 {TRUE
, 'v', "variable", "variables"},
52 {TRUE
, 'g', "enum", "enumerations"}
55 static KeyWord blitzbasic_keywords
[] = {
56 {"const", K_CONST
, 0},
57 {"global", K_VARIABLE
, 0},
58 {"dim", K_VARIABLE
, 0},
59 {"function", K_FUNCTION
, 0},
64 static KeyWord purebasic_keywords
[] = {
65 {"newlist", K_VARIABLE
, 0},
66 {"global", K_VARIABLE
, 0},
67 {"dim", K_VARIABLE
, 0},
68 {"procedure", K_FUNCTION
, 0},
69 {"interface", K_TYPE
, 0},
70 {"structure", K_TYPE
, 0},
74 static KeyWord freebasic_keywords
[] = {
75 {"const", K_CONST
, 0},
76 {"dim as", K_VARIABLE
, 1},
77 {"dim", K_VARIABLE
, 0},
78 {"common", K_VARIABLE
, 0},
79 {"function", K_FUNCTION
, 0},
80 {"sub", K_FUNCTION
, 0},
81 {"private sub", K_FUNCTION
, 0},
82 {"public sub", K_FUNCTION
, 0},
83 {"private function", K_FUNCTION
, 0},
84 {"public function", K_FUNCTION
, 0},
91 * FUNCTION DEFINITIONS
94 /* Match the name of a tag (function, variable, type, ...) starting at pos. */
95 static char const *extract_name (char const *pos
, vString
* name
)
97 while (isspace (*pos
))
100 for (; *pos
&& !isspace (*pos
) && *pos
!= '(' && *pos
!= ','; pos
++)
101 vStringPut (name
, *pos
);
102 vStringTerminate (name
);
106 /* Match a keyword starting at p (case insensitive). */
107 static int match_keyword (const char *p
, KeyWord
const *kw
)
112 for (i
= 0; i
< strlen (kw
->token
); i
++)
114 if (tolower (p
[i
]) != kw
->token
[i
])
117 name
= vStringNew ();
119 for (j
= 0; j
< 1 + kw
->skip
; j
++)
121 p
= extract_name (p
, name
);
123 makeSimpleTag (name
, BasicKinds
, kw
->kind
);
124 vStringDelete (name
);
128 /* Match a "label:" style label. */
129 static void match_colon_label (char const *p
)
131 char const *end
= p
+ strlen (p
) - 1;
132 while (isspace (*end
))
136 vString
*name
= vStringNew ();
137 vStringNCatS (name
, p
, end
- p
);
138 makeSimpleTag (name
, BasicKinds
, K_LABEL
);
139 vStringDelete (name
);
143 /* Match a ".label" style label. */
144 static void match_dot_label (char const *p
)
148 vString
*name
= vStringNew ();
149 extract_name (p
+ 1, name
);
150 makeSimpleTag (name
, BasicKinds
, K_LABEL
);
151 vStringDelete (name
);
155 static void findBasicTags (void)
158 const char *extension
= fileExtension (vStringValue (File
.name
));
161 if (strcmp (extension
, "bb") == 0)
162 keywords
= blitzbasic_keywords
;
163 else if (strcmp (extension
, "pb") == 0)
164 keywords
= purebasic_keywords
;
166 keywords
= freebasic_keywords
;
168 while ((line
= (const char *) fileReadLine ()) != NULL
)
170 const char *p
= line
;
180 /* In Basic, keywords always are at the start of the line. */
181 for (kw
= keywords
; kw
->token
; kw
++)
182 if (match_keyword (p
, kw
)) break;
185 if (strcmp (extension
, "bb") == 0)
188 match_colon_label (p
);
192 parserDefinition
*BasicParser (void)
194 static char const *extensions
[] = { "bas", "bi", "bb", "pb", NULL
};
195 parserDefinition
*def
= parserNew ("Basic");
196 def
->kinds
= BasicKinds
;
197 def
->kindCount
= KIND_COUNT (BasicKinds
);
198 def
->extensions
= extensions
;
199 def
->parser
= findBasicTags
;
203 /* vi:set tabstop=4 shiftwidth=4: */