]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/LexVHDL.cxx
Interface fixes for Phoenix
[wxWidgets.git] / src / stc / scintilla / src / LexVHDL.cxx
1 // Scintilla source code edit control
2 /** @file LexVHDL.cxx
3 ** Lexer for VHDL
4 ** Written by Phil Reid,
5 ** Based on:
6 ** - The Verilog Lexer by Avi Yegudin
7 ** - The Fortran Lexer by Chuan-jian Shen
8 ** - The C++ lexer by Neil Hodgson
9 **/
10 // Copyright 1998-2002 by Neil Hodgson <neilh@scintilla.org>
11 // The License.txt file describes the conditions under which this software may be distributed.
12
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <stdio.h>
17 #include <stdarg.h>
18
19 #include "Platform.h"
20
21 #include "PropSet.h"
22 #include "Accessor.h"
23 #include "StyleContext.h"
24 #include "KeyWords.h"
25 #include "Scintilla.h"
26 #include "SciLexer.h"
27
28 #ifdef SCI_NAMESPACE
29 using namespace Scintilla;
30 #endif
31
32 static void ColouriseVHDLDoc(
33 unsigned int startPos,
34 int length,
35 int initStyle,
36 WordList *keywordlists[],
37 Accessor &styler);
38
39
40 /***************************************/
41 static inline bool IsAWordChar(const int ch) {
42 return (ch < 0x80) && (isalnum(ch) || ch == '.' || ch == '_' );
43 }
44
45 /***************************************/
46 static inline bool IsAWordStart(const int ch) {
47 return (ch < 0x80) && (isalnum(ch) || ch == '_');
48 }
49
50 /***************************************/
51 inline bool IsABlank(unsigned int ch) {
52 return (ch == ' ') || (ch == 0x09) || (ch == 0x0b) ;
53 }
54
55 /***************************************/
56 static void ColouriseVHDLDoc(
57 unsigned int startPos,
58 int length,
59 int initStyle,
60 WordList *keywordlists[],
61 Accessor &styler)
62 {
63 WordList &Keywords = *keywordlists[0];
64 WordList &Operators = *keywordlists[1];
65 WordList &Attributes = *keywordlists[2];
66 WordList &Functions = *keywordlists[3];
67 WordList &Packages = *keywordlists[4];
68 WordList &Types = *keywordlists[5];
69 WordList &User = *keywordlists[6];
70
71 StyleContext sc(startPos, length, initStyle, styler);
72
73 for (; sc.More(); sc.Forward())
74 {
75
76 // Determine if the current state should terminate.
77 if (sc.state == SCE_VHDL_OPERATOR) {
78 sc.SetState(SCE_VHDL_DEFAULT);
79 } else if (sc.state == SCE_VHDL_NUMBER) {
80 if (!IsAWordChar(sc.ch) && (sc.ch != '#')) {
81 sc.SetState(SCE_VHDL_DEFAULT);
82 }
83 } else if (sc.state == SCE_VHDL_IDENTIFIER) {
84 if (!IsAWordChar(sc.ch) || (sc.ch == '.')) {
85 char s[100];
86 sc.GetCurrentLowered(s, sizeof(s));
87 if (Keywords.InList(s)) {
88 sc.ChangeState(SCE_VHDL_KEYWORD);
89 } else if (Operators.InList(s)) {
90 sc.ChangeState(SCE_VHDL_STDOPERATOR);
91 } else if (Attributes.InList(s)) {
92 sc.ChangeState(SCE_VHDL_ATTRIBUTE);
93 } else if (Functions.InList(s)) {
94 sc.ChangeState(SCE_VHDL_STDFUNCTION);
95 } else if (Packages.InList(s)) {
96 sc.ChangeState(SCE_VHDL_STDPACKAGE);
97 } else if (Types.InList(s)) {
98 sc.ChangeState(SCE_VHDL_STDTYPE);
99 } else if (User.InList(s)) {
100 sc.ChangeState(SCE_VHDL_USERWORD);
101 }
102 sc.SetState(SCE_VHDL_DEFAULT);
103 }
104 } else if (sc.state == SCE_VHDL_COMMENT || sc.state == SCE_V_COMMENTLINEBANG) {
105 if (sc.atLineEnd) {
106 sc.SetState(SCE_VHDL_DEFAULT);
107 }
108 } else if (sc.state == SCE_VHDL_STRING) {
109 if (sc.ch == '\\') {
110 if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') {
111 sc.Forward();
112 }
113 } else if (sc.ch == '\"') {
114 sc.ForwardSetState(SCE_VHDL_DEFAULT);
115 } else if (sc.atLineEnd) {
116 sc.ChangeState(SCE_V_STRINGEOL);
117 sc.ForwardSetState(SCE_VHDL_DEFAULT);
118 }
119 }
120
121 // Determine if a new state should be entered.
122 if (sc.state == SCE_VHDL_DEFAULT) {
123 if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
124 sc.SetState(SCE_VHDL_NUMBER);
125 } else if (IsAWordStart(sc.ch)) {
126 sc.SetState(SCE_VHDL_IDENTIFIER);
127 } else if (sc.Match('-', '-')) {
128 sc.SetState(SCE_VHDL_COMMENT);
129 sc.Forward();
130 } else if (sc.Match('-', '-')) {
131 if (sc.Match("--!")) // Nice to have a different comment style
132 sc.SetState(SCE_VHDL_COMMENTLINEBANG);
133 else
134 sc.SetState(SCE_VHDL_COMMENT);
135 } else if (sc.ch == '\"') {
136 sc.SetState(SCE_VHDL_STRING);
137 } else if (isoperator(static_cast<char>(sc.ch))) {
138 sc.SetState(SCE_VHDL_OPERATOR);
139 }
140 }
141 }
142 sc.Complete();
143 }
144 //=============================================================================
145 static bool IsCommentLine(int line, Accessor &styler) {
146 int pos = styler.LineStart(line);
147 int eol_pos = styler.LineStart(line + 1) - 1;
148 for (int i = pos; i < eol_pos; i++) {
149 char ch = styler[i];
150 char chNext = styler[i+1];
151 if ((ch == '-') && (chNext == '-'))
152 return true;
153 else if (ch != ' ' && ch != '\t')
154 return false;
155 }
156 return false;
157 }
158
159 //=============================================================================
160 // Folding the code
161 static void FoldNoBoxVHDLDoc(
162 unsigned int startPos,
163 int length,
164 int initStyle,
165 Accessor &styler)
166 {
167 // Decided it would be smarter to have the lexer have all keywords included. Therefore I
168 // don't check if the style for the keywords that I use to adjust the levels.
169 char words[] =
170 "architecture begin case component else elsif end entity generate loop package process record then "
171 "procedure function when";
172 WordList keywords;
173 keywords.Set(words);
174
175 bool foldComment = styler.GetPropertyInt("fold.comment", 1) != 0;
176 bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
177 bool foldAtElse = styler.GetPropertyInt("fold.at.else", 1) != 0;
178 bool foldAtBegin = styler.GetPropertyInt("fold.at.Begin", 1) != 0;
179 bool foldAtParenthese = styler.GetPropertyInt("fold.at.Parenthese", 1) != 0;
180 //bool foldAtWhen = styler.GetPropertyInt("fold.at.When", 1) != 0; //< fold at when in case statements
181
182 int visibleChars = 0;
183 unsigned int endPos = startPos + length;
184
185 int lineCurrent = styler.GetLine(startPos);
186 int levelCurrent = SC_FOLDLEVELBASE;
187 if(lineCurrent > 0)
188 levelCurrent = styler.LevelAt(lineCurrent-1) >> 16;
189 //int levelMinCurrent = levelCurrent;
190 int levelMinCurrentElse = levelCurrent; //< Used for folding at 'else'
191 int levelMinCurrentBegin = levelCurrent; //< Used for folding at 'begin'
192 int levelNext = levelCurrent;
193
194 /***************************************/
195 int lastStart = 0;
196 char prevWord[32] = "";
197
198 /***************************************/
199 // Find prev word
200 // The logic for going up or down a level depends on a the previous keyword
201 // This code could be cleaned up.
202 int end = 0;
203 unsigned int j;
204 for(j = startPos; j>0; j--)
205 {
206 char ch = styler.SafeGetCharAt(j);
207 char chPrev = styler.SafeGetCharAt(j-1);
208 int style = styler.StyleAt(j);
209 int stylePrev = styler.StyleAt(j-1);
210 if ((stylePrev != SCE_VHDL_COMMENT) && (stylePrev != SCE_VHDL_STRING))
211 {
212 if(IsAWordChar(chPrev) && !IsAWordChar(ch))
213 {
214 end = j-1;
215 }
216 }
217 if ((style != SCE_VHDL_COMMENT) && (style != SCE_VHDL_STRING))
218 {
219 if(!IsAWordChar(chPrev) && IsAWordStart(ch) && (end != 0))
220 {
221 char s[32];
222 unsigned int k;
223 for(k=0; (k<31 ) && (k<end-j+1 ); k++) {
224 s[k] = static_cast<char>(tolower(styler[j+k]));
225 }
226 s[k] = '\0';
227
228 if(keywords.InList(s)) {
229 strcpy(prevWord, s);
230 break;
231 }
232 }
233 }
234 }
235 for(j=j+strlen(prevWord); j<endPos; j++)
236 {
237 char ch = styler.SafeGetCharAt(j);
238 int style = styler.StyleAt(j);
239 if ((style != SCE_VHDL_COMMENT) && (style != SCE_VHDL_STRING))
240 {
241 if((ch == ';') && (strcmp(prevWord, "end") == 0))
242 {
243 strcpy(prevWord, ";");
244 }
245 }
246 }
247
248 char chNext = styler[startPos];
249 char chPrev = '\0';
250 char chNextNonBlank;
251 int styleNext = styler.StyleAt(startPos);
252 int style = initStyle;
253 //Platform::DebugPrintf("Line[%04d] Prev[%20s] ************************* Level[%x]\n", lineCurrent+1, prevWord, levelCurrent);
254
255 /***************************************/
256 for (unsigned int i = startPos; i < endPos; i++)
257 {
258 char ch = chNext;
259 chNext = styler.SafeGetCharAt(i + 1);
260 chPrev = styler.SafeGetCharAt(i - 1);
261 chNextNonBlank = chNext;
262 unsigned int j = i+1;
263 while(IsABlank(chNextNonBlank) && j<endPos)
264 {
265 j ++ ;
266 chNextNonBlank = styler.SafeGetCharAt(j);
267 }
268 style = styleNext;
269 styleNext = styler.StyleAt(i + 1);
270 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
271
272 if (foldComment && atEOL && IsCommentLine(lineCurrent, styler))
273 {
274 if(!IsCommentLine(lineCurrent-1, styler) && IsCommentLine(lineCurrent+1, styler))
275 {
276 levelNext++;
277 }
278 else if(IsCommentLine(lineCurrent-1, styler) && !IsCommentLine(lineCurrent+1, styler))
279 {
280 levelNext--;
281 }
282 }
283
284 if ((style == SCE_VHDL_OPERATOR) && foldAtParenthese)
285 {
286 if(ch == '(') {
287 levelNext++;
288 } else if (ch == ')') {
289 levelNext--;
290 }
291 }
292
293 if ((style != SCE_VHDL_COMMENT) && (style != SCE_VHDL_STRING))
294 {
295 if((ch == ';') && (strcmp(prevWord, "end") == 0))
296 {
297 strcpy(prevWord, ";");
298 }
299
300 if(!IsAWordChar(chPrev) && IsAWordStart(ch))
301 {
302 lastStart = i;
303 }
304
305 if(iswordchar(ch) && !iswordchar(chNext)) {
306 char s[32];
307 unsigned int k;
308 for(k=0; (k<31 ) && (k<i-lastStart+1 ); k++) {
309 s[k] = static_cast<char>(tolower(styler[lastStart+k]));
310 }
311 s[k] = '\0';
312
313 if(keywords.InList(s))
314 {
315 if (
316 strcmp(s, "architecture") == 0 ||
317 strcmp(s, "case") == 0 ||
318 strcmp(s, "component") == 0 ||
319 strcmp(s, "entity") == 0 ||
320 strcmp(s, "generate") == 0 ||
321 strcmp(s, "loop") == 0 ||
322 strcmp(s, "package") ==0 ||
323 strcmp(s, "process") == 0 ||
324 strcmp(s, "record") == 0 ||
325 strcmp(s, "then") == 0)
326 {
327 if (strcmp(prevWord, "end") != 0)
328 {
329 if (levelMinCurrentElse > levelNext) {
330 levelMinCurrentElse = levelNext;
331 }
332 levelNext++;
333 }
334 } else if (
335 strcmp(s, "procedure") == 0 ||
336 strcmp(s, "function") == 0)
337 {
338 if (strcmp(prevWord, "end") != 0) // check for "end procedure" etc.
339 { // This code checks to see if the procedure / function is a definition within a "package"
340 // rather than the actual code in the body.
341 int BracketLevel = 0;
342 for(int j=i+1; j<styler.Length(); j++)
343 {
344 int LocalStyle = styler.StyleAt(j);
345 char LocalCh = styler.SafeGetCharAt(j);
346 if(LocalCh == '(') BracketLevel++;
347 if(LocalCh == ')') BracketLevel--;
348 if(
349 (BracketLevel == 0) &&
350 (LocalStyle != SCE_VHDL_COMMENT) &&
351 (LocalStyle != SCE_VHDL_STRING) &&
352 !iswordchar(styler.SafeGetCharAt(j-1)) &&
353 styler.Match(j, "is") &&
354 !iswordchar(styler.SafeGetCharAt(j+2)))
355 {
356 if (levelMinCurrentElse > levelNext) {
357 levelMinCurrentElse = levelNext;
358 }
359 levelNext++;
360 break;
361 }
362 if((BracketLevel == 0) && (LocalCh == ';'))
363 {
364 break;
365 }
366 }
367 }
368
369 } else if (strcmp(s, "end") == 0) {
370 levelNext--;
371 } else if(strcmp(s, "elsif") == 0) { // elsif is followed by then so folding occurs correctly
372 levelNext--;
373 } else if (strcmp(s, "else") == 0) {
374 if(strcmp(prevWord, "when") != 0) // ignore a <= x when y else z;
375 {
376 levelMinCurrentElse = levelNext - 1; // VHDL else is all on its own so just dec. the min level
377 }
378 } else if(
379 ((strcmp(s, "begin") == 0) && (strcmp(prevWord, "architecture") == 0)) ||
380 ((strcmp(s, "begin") == 0) && (strcmp(prevWord, "function") == 0)) ||
381 ((strcmp(s, "begin") == 0) && (strcmp(prevWord, "procedure") == 0)))
382 {
383 levelMinCurrentBegin = levelNext - 1;
384 }
385 //Platform::DebugPrintf("Line[%04d] Prev[%20s] Cur[%20s] Level[%x]\n", lineCurrent+1, prevWord, s, levelCurrent);
386 strcpy(prevWord, s);
387 }
388 }
389 }
390 if (atEOL) {
391 int levelUse = levelCurrent;
392
393 if (foldAtElse && (levelMinCurrentElse < levelUse)) {
394 levelUse = levelMinCurrentElse;
395 }
396 if (foldAtBegin && (levelMinCurrentBegin < levelUse)) {
397 levelUse = levelMinCurrentBegin;
398 }
399 int lev = levelUse | levelNext << 16;
400 if (visibleChars == 0 && foldCompact)
401 lev |= SC_FOLDLEVELWHITEFLAG;
402
403 if (levelUse < levelNext)
404 lev |= SC_FOLDLEVELHEADERFLAG;
405 if (lev != styler.LevelAt(lineCurrent)) {
406 styler.SetLevel(lineCurrent, lev);
407 }
408 //Platform::DebugPrintf("Line[%04d] ---------------------------------------------------- Level[%x]\n", lineCurrent+1, levelCurrent);
409 lineCurrent++;
410 levelCurrent = levelNext;
411 //levelMinCurrent = levelCurrent;
412 levelMinCurrentElse = levelCurrent;
413 levelMinCurrentBegin = levelCurrent;
414 visibleChars = 0;
415 }
416 /***************************************/
417 if (!isspacechar(ch)) visibleChars++;
418 }
419
420 /***************************************/
421 // Platform::DebugPrintf("Line[%04d] ---------------------------------------------------- Level[%x]\n", lineCurrent+1, levelCurrent);
422 }
423
424 //=============================================================================
425 static void FoldVHDLDoc(unsigned int startPos, int length, int initStyle, WordList *[],
426 Accessor &styler) {
427 FoldNoBoxVHDLDoc(startPos, length, initStyle, styler);
428 }
429
430 //=============================================================================
431 static const char * const VHDLWordLists[] = {
432 "Keywords",
433 "Operators",
434 "Attributes",
435 "Standard Functions",
436 "Standard Packages",
437 "Standard Types",
438 "User Words",
439 0,
440 };
441
442
443 LexerModule lmVHDL(SCLEX_VHDL, ColouriseVHDLDoc, "vhdl", FoldVHDLDoc, VHDLWordLists);
444
445
446 // Keyword:
447 // access after alias all architecture array assert attribute begin block body buffer bus case component
448 // configuration constant disconnect downto else elsif end entity exit file for function generate generic
449 // group guarded if impure in inertial inout is label library linkage literal loop map new next null of
450 // on open others out package port postponed procedure process pure range record register reject report
451 // return select severity shared signal subtype then to transport type unaffected units until use variable
452 // wait when while with
453 //
454 // Operators:
455 // abs and mod nand nor not or rem rol ror sla sll sra srl xnor xor
456 //
457 // Attributes:
458 // left right low high ascending image value pos val succ pred leftof rightof base range reverse_range
459 // length delayed stable quiet transaction event active last_event last_active last_value driving
460 // driving_value simple_name path_name instance_name
461 //
462 // Std Functions:
463 // now readline read writeline write endfile resolved to_bit to_bitvector to_stdulogic to_stdlogicvector
464 // to_stdulogicvector to_x01 to_x01z to_UX01 rising_edge falling_edge is_x shift_left shift_right rotate_left
465 // rotate_right resize to_integer to_unsigned to_signed std_match to_01
466 //
467 // Std Packages:
468 // std ieee work standard textio std_logic_1164 std_logic_arith std_logic_misc std_logic_signed
469 // std_logic_textio std_logic_unsigned numeric_bit numeric_std math_complex math_real vital_primitives
470 // vital_timing
471 //
472 // Std Types:
473 // boolean bit character severity_level integer real time delay_length natural positive string bit_vector
474 // file_open_kind file_open_status line text side width std_ulogic std_ulogic_vector std_logic
475 // std_logic_vector X01 X01Z UX01 UX01Z unsigned signed
476 //
477