Handle template expressions that may use the << or >> operators
[arduino-ctags.git] / basic.c
bloba117afaad2f159142f6941cd4ecf45cc015445b1
1 /*
2 * $Id:$
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.
16 * INCLUDE FILES
18 #include "general.h" /* must always come first */
20 #include <string.h>
22 #include "options.h"
23 #include "parse.h"
24 #include "read.h"
25 #include "routines.h"
26 #include "vstring.h"
29 * DATA DEFINITIONS
31 typedef enum {
32 K_CONST,
33 K_FUNCTION,
34 K_LABEL,
35 K_TYPE,
36 K_VARIABLE,
37 K_ENUM
38 } BasicKind;
40 typedef struct {
41 char const *token;
42 BasicKind kind;
43 int skip;
44 } KeyWord;
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},
60 {"type", K_TYPE, 0},
61 {NULL, 0, 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},
71 {NULL, 0, 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},
85 {"type", K_TYPE, 0},
86 {"enum", K_ENUM, 0},
87 {NULL, 0, 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))
98 pos++;
99 vStringClear (name);
100 for (; *pos && !isspace (*pos) && *pos != '(' && *pos != ','; pos++)
101 vStringPut (name, *pos);
102 vStringTerminate (name);
103 return pos;
106 /* Match a keyword starting at p (case insensitive). */
107 static int match_keyword (const char *p, KeyWord const *kw)
109 vString *name;
110 size_t i;
111 int j;
112 for (i = 0; i < strlen (kw->token); i++)
114 if (tolower (p[i]) != kw->token[i])
115 return 0;
117 name = vStringNew ();
118 p += i;
119 for (j = 0; j < 1 + kw->skip; j++)
121 p = extract_name (p, name);
123 makeSimpleTag (name, BasicKinds, kw->kind);
124 vStringDelete (name);
125 return 1;
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))
133 end--;
134 if (*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)
146 if (*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)
157 const char *line;
158 const char *extension = fileExtension (vStringValue (File.name));
159 KeyWord *keywords;
161 if (strcmp (extension, "bb") == 0)
162 keywords = blitzbasic_keywords;
163 else if (strcmp (extension, "pb") == 0)
164 keywords = purebasic_keywords;
165 else
166 keywords = freebasic_keywords;
168 while ((line = (const char *) fileReadLine ()) != NULL)
170 const char *p = line;
171 KeyWord const *kw;
173 while (isspace (*p))
174 p++;
176 /* Empty line? */
177 if (!*p)
178 continue;
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;
184 /* Is it a label? */
185 if (strcmp (extension, "bb") == 0)
186 match_dot_label (p);
187 else
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;
200 return def;
203 /* vi:set tabstop=4 shiftwidth=4: */