forked from geany/geany
/
vhdl.c
299 lines (274 loc) · 6.27 KB
/
vhdl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* $Id: vhdl.c,v 1.0 2005/11/05
*
* Copyright (c) 2005, Klaus Dannecker
*
* This source code is released for free distribution under the terms of the
* GNU General Public License.
*
* This module contains functions for generating tags for the Vhdl HDL
* (Hardware Description Language).
*
*/
/*
* INCLUDE FILES
*/
#include "general.h" /* must always come first */
#include <string.h>
#include <setjmp.h>
#include "keyword.h"
#include "parse.h"
#include "read.h"
#include "vstring.h"
/*
* DATA DECLARATIONS
*/
typedef enum eException { ExceptionNone, ExceptionEOF } exception_t;
typedef enum {
K_UNDEFINED = -1,
K_CONSTANT,
K_TYPE,
K_VARIABLE,
K_ATRIBUTE,
K_SIGNAL,
K_FUNCTION,
K_PROCEDURE,
K_COMPONENT,
K_PACKAGE,
K_PROCESS,
K_ENTITY,
K_ARCHITECTURE,
K_PORT,
K_ALIAS
} vhdlKind;
typedef struct {
const char *keyword;
vhdlKind kind;
} keywordAssoc;
/*
* DATA DEFINITIONS
*/
static int Ungetc;
static int Lang_vhdl;
static jmp_buf Exception;
static vString* Name=NULL;
static vString* Lastname=NULL;
static vString* Keyword=NULL;
static vString* TagName=NULL;
static kindOption VhdlKinds [] = {
{ TRUE, 'c', "variable", "constants" },
{ TRUE, 't', "typedef", "types" },
{ TRUE, 'v', "variable", "variables" },
{ TRUE, 'a', "atribute", "atributes" },
{ TRUE, 's', "variable", "signals" },
{ TRUE, 'f', "function", "functions" },
{ TRUE, 'p', "function", "procedure" },
{ TRUE, 'k', "member", "components" },
{ TRUE, 'l', "namespace", "packages" },
{ TRUE, 'm', "member", "process" },
{ TRUE, 'n', "class", "entity" },
{ TRUE, 'o', "struct", "architecture" },
{ TRUE, 'u', "port", "ports" },
{ TRUE, 'v', "typedef", "alias" }
};
static keywordAssoc VhdlKeywordTable [] = {
{ "constant", K_CONSTANT },
{ "variable", K_VARIABLE },
{ "type", K_TYPE },
{ "subtype", K_TYPE },
{ "signal", K_SIGNAL },
{ "function", K_FUNCTION },
{ "procedure", K_PROCEDURE },
{ "component", K_COMPONENT },
{ "package", K_PACKAGE },
{ "process", K_PROCESS },
{ "entity", K_ENTITY },
{ "architecture", K_ARCHITECTURE },
{ "inout", K_PORT },
{ "in", K_PORT },
{ "out", K_PORT },
{ "alias", K_ALIAS }
};
/*
* FUNCTION DEFINITIONS
*/
static void initialize (const langType language)
{
size_t i;
const size_t count = sizeof (VhdlKeywordTable) /
sizeof (VhdlKeywordTable [0]);
Lang_vhdl = language;
for (i = 0 ; i < count ; ++i)
{
const keywordAssoc* const p = &VhdlKeywordTable [i];
addKeyword (p->keyword, language, (int) p->kind);
}
}
static void vUngetc (int c)
{
Assert (Ungetc == '\0');
Ungetc = c;
}
static int vGetc (void)
{
int c;
if (Ungetc == '\0')
c = fileGetc ();
else
{
c = Ungetc;
Ungetc = '\0';
}
if (c == '-')
{
int c2 = fileGetc ();
if (c2 == EOF)
longjmp (Exception, (int) ExceptionEOF);
else if (c2 == '-') /* strip comment until end-of-line */
{
do
c = fileGetc ();
while (c != '\n' && c != EOF);
}
else
Ungetc = c2;
}
if (c == EOF)
longjmp (Exception, (int) ExceptionEOF);
return c;
}
static boolean isIdentifierCharacter (const int c)
{
return (boolean)(isalnum (c) || c == '_' || c == '`');
}
static int skipWhite (int c)
{
while (c==' ')
c = vGetc ();
return c;
}
static boolean readIdentifier (vString *const name, int c)
{
vStringClear (name);
if (isIdentifierCharacter (c))
{
while (isIdentifierCharacter (c))
{
vStringPut (name, c);
c = vGetc ();
}
vUngetc (c);
vStringTerminate (name);
}
return (boolean)(name->length > 0);
}
static void tagNameList (const vhdlKind kind, int c)
{
Assert (isIdentifierCharacter (c));
if (isIdentifierCharacter (c))
{
readIdentifier (TagName, c);
makeSimpleTag (TagName, VhdlKinds, kind);
vUngetc (c);
}
}
static void findTag (vString *const name)
{
int c = '\0';
vhdlKind kind;
vStringCopyToLower (Keyword, name);
kind = (vhdlKind)lookupKeyword (vStringValue (Keyword), Lang_vhdl);
if (kind == K_UNDEFINED)
{
c = skipWhite (vGetc ());
vStringCopyS(Lastname,vStringValue(name));
if (c == ':')
{
c = skipWhite (vGetc ());
if (isIdentifierCharacter (c))
{
readIdentifier (name, c);
vStringCopyToLower (Keyword, name);
lookupKeyword (vStringValue (Keyword), Lang_vhdl);
kind = (vhdlKind)lookupKeyword (vStringValue (Keyword), Lang_vhdl);
if (kind == K_PROCESS || kind == K_PORT)
{
makeSimpleTag (Lastname, VhdlKinds, kind);
}
}
} else {
vUngetc (c);
}
}
else
{
if (kind == K_SIGNAL) {
while (c!=':') {
c = skipWhite (vGetc ());
if (c==',')
c = vGetc ();
if (isIdentifierCharacter (c))
tagNameList (kind, c);
else
break;
c = vGetc ();
}
}
else if (kind == K_PROCESS) {
vStringCopyS(TagName,"unnamed");
makeSimpleTag (TagName, VhdlKinds, kind);
} else {
c = skipWhite (vGetc ());
if (c=='\"')
c = vGetc ();
if (isIdentifierCharacter (c))
tagNameList (kind, c);
}
}
}
static void findVhdlTags (void)
{
volatile boolean newStatement = TRUE;
volatile int c = '\0';
exception_t exception = (exception_t) setjmp (Exception);
Name = vStringNew ();
Lastname = vStringNew ();
Keyword = vStringNew ();
TagName = vStringNew ();
if (exception == ExceptionNone) while (c != EOF)
{
c = vGetc ();
switch (c)
{
case ';':
case '\n':
newStatement = TRUE;
break;
case ' ':
case '\t':
break;
default:
if (newStatement && readIdentifier (Name, c)) {
findTag (Name);
}
newStatement = FALSE;
break;
}
}
vStringDelete (Name);
vStringDelete (Lastname);
vStringDelete (Keyword);
vStringDelete (TagName);
}
extern parserDefinition* VhdlParser (void)
{
static const char *const extensions [] = { "vhdl", "vhd", NULL };
parserDefinition* def = parserNew ("Vhdl");
def->kinds = VhdlKinds;
def->kindCount = KIND_COUNT (VhdlKinds);
def->extensions = extensions;
def->parser = findVhdlTags;
def->initialize = initialize;
return def;
}
/* vi:set tabstop=8 shiftwidth=4: */