~ubuntu-branches/ubuntu/raring/geany/raring-proposed

« back to all changes in this revision

Viewing changes to tagmanager/vhdl.c

  • Committer: Bazaar Package Importer
  • Author(s): Damián Viano
  • Date: 2008-05-02 11:37:45 UTC
  • mfrom: (1.2.1 upstream) (9 hardy)
  • mto: (3.2.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: james.westby@ubuntu.com-20080502113745-xzp4g6dmovrpoj17
Tags: 0.14-1
New upstream release (Closes: #478126)

Show diffs side-by-side

added added

removed removed

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