2 * $Id: vhdl.c,v 1.0 2005/11/05
4 * Copyright (c) 2005, Klaus Dannecker
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 the Vhdl HDL
10 * (Hardware Description Language).
17 #include "general.h" /* must always come first */
32 typedef enum eException
{ ExceptionNone
, ExceptionEOF
} exception_t
;
58 static jmp_buf Exception
;
59 static vString
* Name
=NULL
;
60 static vString
* Lastname
=NULL
;
61 static vString
* Keyword
=NULL
;
62 static vString
* TagName
=NULL
;
64 static kindOption VhdlKinds
[] = {
65 { true, 'c', "variable", "constants" },
66 { true, 't', "typedef", "types" },
67 { true, 'v', "variable", "variables" },
68 { true, 'a', "attribute", "attributes" },
69 { true, 's', "variable", "signals" },
70 { true, 'f', "function", "functions" },
71 { true, 'p', "function", "procedure" },
72 { true, 'k', "member", "components" },
73 { true, 'l', "namespace", "packages" },
74 { true, 'm', "member", "process" },
75 { true, 'n', "class", "entity" },
76 { true, 'o', "struct", "architecture" },
77 { true, 'u', "port", "ports" },
78 { true, 'b', "member", "blocks" },
79 { true, 'A', "typedef", "alias" }
82 static keywordTable VhdlKeywordTable
[] = {
83 { "constant", K_CONSTANT
},
84 { "variable", K_VARIABLE
},
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
},
104 * FUNCTION DEFINITIONS
107 static void initialize (const langType language
)
109 Lang_vhdl
= language
;
112 static void vUngetc (int c
)
114 Assert (Ungetc
== '\0');
118 static int vGetc (void)
122 c
= getcFromInputFile ();
130 int c2
= getcFromInputFile ();
132 longjmp (Exception
, (int) ExceptionEOF
);
133 else if (c2
== '-') /* strip comment until end-of-line */
136 c
= getcFromInputFile ();
137 while (c
!= '\n' && c
!= EOF
);
143 longjmp (Exception
, (int) ExceptionEOF
);
147 static bool isIdentifierCharacter (const int c
)
149 return (bool)(isalnum (c
) || c
== '_' || c
== '`');
152 static int skipWhite (int c
)
159 static bool readIdentifier (vString
*const name
, int c
)
162 if (isIdentifierCharacter (c
))
164 while (isIdentifierCharacter (c
))
166 vStringPut (name
, c
);
171 return (bool)(name
->length
> 0);
174 static void tagNameList (const vhdlKind kind
, int c
)
176 Assert (isIdentifierCharacter (c
));
177 if (isIdentifierCharacter (c
))
179 readIdentifier (TagName
, c
);
180 makeSimpleTag (TagName
, VhdlKinds
, kind
);
184 static void findTag (vString
*const name
)
188 vStringCopyToLower (Keyword
, name
);
189 kind
= (vhdlKind
)lookupKeyword (vStringValue (Keyword
), Lang_vhdl
);
190 if (kind
== K_UNDEFINED
)
192 c
= skipWhite (vGetc ());
193 vStringCopyS(Lastname
,vStringValue(name
));
196 c
= skipWhite (vGetc ());
197 if (isIdentifierCharacter (c
))
199 readIdentifier (name
, c
);
200 vStringCopyToLower (Keyword
, name
);
201 lookupKeyword (vStringValue (Keyword
), Lang_vhdl
);
202 kind
= (vhdlKind
)lookupKeyword (vStringValue (Keyword
), Lang_vhdl
);
203 if (kind
== K_PROCESS
|| kind
== K_BLOCK
|| kind
== K_PORT
)
205 makeSimpleTag (Lastname
, VhdlKinds
, kind
);
214 if (kind
== K_SIGNAL
) {
216 c
= skipWhite (vGetc ());
219 if (isIdentifierCharacter (c
))
220 tagNameList (kind
, c
);
226 else if (kind
== K_PROCESS
|| kind
== K_BLOCK
) {
227 vStringCopyS(TagName
,"unnamed");
228 makeSimpleTag (TagName
, VhdlKinds
, kind
);
230 c
= skipWhite (vGetc ());
233 if (isIdentifierCharacter (c
))
234 tagNameList (kind
, c
);
239 static void findVhdlTags (void)
241 volatile bool newStatement
= true;
242 volatile int c
= '\0';
243 exception_t exception
= (exception_t
) setjmp (Exception
);
244 Name
= vStringNew ();
245 Lastname
= vStringNew ();
246 Keyword
= vStringNew ();
247 TagName
= vStringNew ();
249 if (exception
== ExceptionNone
) while (c
!= EOF
)
264 if (newStatement
&& readIdentifier (Name
, c
)) {
267 newStatement
= false;
271 vStringDelete (Name
);
272 vStringDelete (Lastname
);
273 vStringDelete (Keyword
);
274 vStringDelete (TagName
);
277 extern parserDefinition
* VhdlParser (void)
279 static const char *const extensions
[] = { "vhdl", "vhd", NULL
};
280 parserDefinition
* def
= parserNew ("Vhdl");
281 def
->kinds
= VhdlKinds
;
282 def
->kindCount
= ARRAY_SIZE (VhdlKinds
);
283 def
->extensions
= extensions
;
284 def
->parser
= findVhdlTags
;
285 def
->initialize
= initialize
;
286 def
->keywordTable
= VhdlKeywordTable
;
287 def
->keywordCount
= ARRAY_SIZE (VhdlKeywordTable
);