Update Scintilla to version 3.5.7
[TortoiseGit.git] / ext / scintilla / lexers / LexVHDL.cxx
blob53e8377ad9a33bac6d181f9b89f8e32562f4ec1d
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.
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdio.h>
16 #include <stdarg.h>
17 #include <assert.h>
18 #include <ctype.h>
20 #include "ILexer.h"
21 #include "Scintilla.h"
22 #include "SciLexer.h"
24 #include "WordList.h"
25 #include "LexAccessor.h"
26 #include "Accessor.h"
27 #include "StyleContext.h"
28 #include "CharacterSet.h"
29 #include "LexerModule.h"
31 #ifdef SCI_NAMESPACE
32 using namespace Scintilla;
33 #endif
35 static void ColouriseVHDLDoc(
36 unsigned int startPos,
37 int length,
38 int initStyle,
39 WordList *keywordlists[],
40 Accessor &styler);
43 /***************************************/
44 static inline bool IsAWordChar(const int ch) {
45 return (ch < 0x80) && (isalnum(ch) || ch == '.' || ch == '_' );
48 /***************************************/
49 static inline bool IsAWordStart(const int ch) {
50 return (ch < 0x80) && (isalnum(ch) || ch == '_');
53 /***************************************/
54 static inline bool IsABlank(unsigned int ch) {
55 return (ch == ' ') || (ch == 0x09) || (ch == 0x0b) ;
58 /***************************************/
59 static void ColouriseVHDLDoc(
60 unsigned int startPos,
61 int length,
62 int initStyle,
63 WordList *keywordlists[],
64 Accessor &styler)
66 WordList &Keywords = *keywordlists[0];
67 WordList &Operators = *keywordlists[1];
68 WordList &Attributes = *keywordlists[2];
69 WordList &Functions = *keywordlists[3];
70 WordList &Packages = *keywordlists[4];
71 WordList &Types = *keywordlists[5];
72 WordList &User = *keywordlists[6];
74 StyleContext sc(startPos, length, initStyle, styler);
75 bool isExtendedId = false; // true when parsing an extended identifier
77 for (; sc.More(); sc.Forward())
80 // Determine if the current state should terminate.
81 if (sc.state == SCE_VHDL_OPERATOR) {
82 sc.SetState(SCE_VHDL_DEFAULT);
83 } else if (sc.state == SCE_VHDL_NUMBER) {
84 if (!IsAWordChar(sc.ch) && (sc.ch != '#')) {
85 sc.SetState(SCE_VHDL_DEFAULT);
87 } else if (sc.state == SCE_VHDL_IDENTIFIER) {
88 if (!isExtendedId && (!IsAWordChar(sc.ch) || (sc.ch == '.'))) {
89 char s[100];
90 sc.GetCurrentLowered(s, sizeof(s));
91 if (Keywords.InList(s)) {
92 sc.ChangeState(SCE_VHDL_KEYWORD);
93 } else if (Operators.InList(s)) {
94 sc.ChangeState(SCE_VHDL_STDOPERATOR);
95 } else if (Attributes.InList(s)) {
96 sc.ChangeState(SCE_VHDL_ATTRIBUTE);
97 } else if (Functions.InList(s)) {
98 sc.ChangeState(SCE_VHDL_STDFUNCTION);
99 } else if (Packages.InList(s)) {
100 sc.ChangeState(SCE_VHDL_STDPACKAGE);
101 } else if (Types.InList(s)) {
102 sc.ChangeState(SCE_VHDL_STDTYPE);
103 } else if (User.InList(s)) {
104 sc.ChangeState(SCE_VHDL_USERWORD);
106 sc.SetState(SCE_VHDL_DEFAULT);
107 } else if (isExtendedId && ((sc.ch == '\\') || sc.atLineEnd)) {
108 // extended identifiers are terminated by backslash, check for end of line in case we have invalid syntax
109 isExtendedId = false;
110 sc.ForwardSetState(SCE_VHDL_DEFAULT);
112 } else if (sc.state == SCE_VHDL_COMMENT || sc.state == SCE_VHDL_COMMENTLINEBANG) {
113 if (sc.atLineEnd) {
114 sc.SetState(SCE_VHDL_DEFAULT);
116 } else if (sc.state == SCE_VHDL_STRING) {
117 if (sc.ch == '\\') {
118 if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') {
119 sc.Forward();
121 } else if (sc.ch == '\"') {
122 sc.ForwardSetState(SCE_VHDL_DEFAULT);
123 } else if (sc.atLineEnd) {
124 sc.ChangeState(SCE_VHDL_STRINGEOL);
125 sc.ForwardSetState(SCE_VHDL_DEFAULT);
127 } else if (sc.state == SCE_VHDL_BLOCK_COMMENT){
128 if(sc.ch == '*' && sc.chNext == '/'){
129 sc.Forward();
130 sc.ForwardSetState(SCE_VHDL_DEFAULT);
134 // Determine if a new state should be entered.
135 if (sc.state == SCE_VHDL_DEFAULT) {
136 if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
137 sc.SetState(SCE_VHDL_NUMBER);
138 } else if (IsAWordStart(sc.ch)) {
139 sc.SetState(SCE_VHDL_IDENTIFIER);
140 } else if (sc.Match('-', '-')) {
141 if (sc.Match("--!")) // Nice to have a different comment style
142 sc.SetState(SCE_VHDL_COMMENTLINEBANG);
143 else
144 sc.SetState(SCE_VHDL_COMMENT);
145 } else if (sc.Match('/', '*')){
146 sc.SetState(SCE_VHDL_BLOCK_COMMENT);
147 } else if (sc.ch == '\"') {
148 sc.SetState(SCE_VHDL_STRING);
149 } else if (sc.ch == '\\') {
150 isExtendedId = true;
151 sc.SetState(SCE_VHDL_IDENTIFIER);
152 } else if (isoperator(static_cast<char>(sc.ch))) {
153 sc.SetState(SCE_VHDL_OPERATOR);
157 sc.Complete();
159 //=============================================================================
160 static bool IsCommentLine(int line, Accessor &styler) {
161 int pos = styler.LineStart(line);
162 int eol_pos = styler.LineStart(line + 1) - 1;
163 for (int i = pos; i < eol_pos; i++) {
164 char ch = styler[i];
165 char chNext = styler[i+1];
166 if ((ch == '-') && (chNext == '-'))
167 return true;
168 else if (ch != ' ' && ch != '\t')
169 return false;
171 return false;
173 static bool IsCommentBlockStart(int line, Accessor &styler)
175 int pos = styler.LineStart(line);
176 int eol_pos = styler.LineStart(line + 1) - 1;
177 for (int i = pos; i < eol_pos; i++) {
178 char ch = styler[i];
179 char chNext = styler[i+1];
180 char style = styler.StyleAt(i);
181 if ((style == SCE_VHDL_BLOCK_COMMENT) && (ch == '/') && (chNext == '*'))
182 return true;
184 return false;
187 static bool IsCommentBlockEnd(int line, Accessor &styler)
189 int pos = styler.LineStart(line);
190 int eol_pos = styler.LineStart(line + 1) - 1;
192 for (int i = pos; i < eol_pos; i++) {
193 char ch = styler[i];
194 char chNext = styler[i+1];
195 char style = styler.StyleAt(i);
196 if ((style == SCE_VHDL_BLOCK_COMMENT) && (ch == '*') && (chNext == '/'))
197 return true;
199 return false;
202 static bool IsCommentStyle(char style)
204 return style == SCE_VHDL_BLOCK_COMMENT || style == SCE_VHDL_COMMENT || style == SCE_VHDL_COMMENTLINEBANG;
207 //=============================================================================
208 // Folding the code
209 static void FoldNoBoxVHDLDoc(
210 unsigned int startPos,
211 int length,
212 int,
213 Accessor &styler)
215 // Decided it would be smarter to have the lexer have all keywords included. Therefore I
216 // don't check if the style for the keywords that I use to adjust the levels.
217 char words[] =
218 "architecture begin block case component else elsif end entity generate loop package process record then "
219 "procedure function when units";
220 WordList keywords;
221 keywords.Set(words);
223 bool foldComment = styler.GetPropertyInt("fold.comment", 1) != 0;
224 bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
225 bool foldAtElse = styler.GetPropertyInt("fold.at.else", 1) != 0;
226 bool foldAtBegin = styler.GetPropertyInt("fold.at.Begin", 1) != 0;
227 bool foldAtParenthese = styler.GetPropertyInt("fold.at.Parenthese", 1) != 0;
228 //bool foldAtWhen = styler.GetPropertyInt("fold.at.When", 1) != 0; //< fold at when in case statements
230 int visibleChars = 0;
231 unsigned int endPos = startPos + length;
233 int lineCurrent = styler.GetLine(startPos);
234 int levelCurrent = SC_FOLDLEVELBASE;
235 if(lineCurrent > 0)
236 levelCurrent = styler.LevelAt(lineCurrent-1) >> 16;
237 //int levelMinCurrent = levelCurrent;
238 int levelMinCurrentElse = levelCurrent; //< Used for folding at 'else'
239 int levelMinCurrentBegin = levelCurrent; //< Used for folding at 'begin'
240 int levelNext = levelCurrent;
242 /***************************************/
243 int lastStart = 0;
244 char prevWord[32] = "";
246 /***************************************/
247 // Find prev word
248 // The logic for going up or down a level depends on a the previous keyword
249 // This code could be cleaned up.
250 int end = 0;
251 unsigned int j;
252 for(j = startPos; j>0; j--)
254 char ch = styler.SafeGetCharAt(j);
255 char chPrev = styler.SafeGetCharAt(j-1);
256 int style = styler.StyleAt(j);
257 int stylePrev = styler.StyleAt(j-1);
258 if ((!IsCommentStyle(style)) && (stylePrev != SCE_VHDL_STRING))
260 if(IsAWordChar(chPrev) && !IsAWordChar(ch))
262 end = j-1;
265 if ((!IsCommentStyle(style)) && (style != SCE_VHDL_STRING))
267 if(!IsAWordChar(chPrev) && IsAWordStart(ch) && (end != 0))
269 char s[32];
270 unsigned int k;
271 for(k=0; (k<31 ) && (k<end-j+1 ); k++) {
272 s[k] = static_cast<char>(tolower(styler[j+k]));
274 s[k] = '\0';
276 if(keywords.InList(s)) {
277 strcpy(prevWord, s);
278 break;
283 for(j=j+static_cast<unsigned int>(strlen(prevWord)); j<endPos; j++)
285 char ch = styler.SafeGetCharAt(j);
286 int style = styler.StyleAt(j);
287 if ((!IsCommentStyle(style)) && (style != SCE_VHDL_STRING))
289 if((ch == ';') && (strcmp(prevWord, "end") == 0))
291 strcpy(prevWord, ";");
296 char chNext = styler[startPos];
297 char chPrev = '\0';
298 char chNextNonBlank;
299 int styleNext = styler.StyleAt(startPos);
300 //Platform::DebugPrintf("Line[%04d] Prev[%20s] ************************* Level[%x]\n", lineCurrent+1, prevWord, levelCurrent);
302 /***************************************/
303 for (unsigned int i = startPos; i < endPos; i++)
305 char ch = chNext;
306 chNext = styler.SafeGetCharAt(i + 1);
307 chPrev = styler.SafeGetCharAt(i - 1);
308 chNextNonBlank = chNext;
309 unsigned int j = i+1;
310 while(IsABlank(chNextNonBlank) && j<endPos)
312 j ++ ;
313 chNextNonBlank = styler.SafeGetCharAt(j);
315 int style = styleNext;
316 styleNext = styler.StyleAt(i + 1);
317 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
319 if (foldComment && atEOL)
321 if(IsCommentLine(lineCurrent, styler))
323 if(!IsCommentLine(lineCurrent-1, styler) && IsCommentLine(lineCurrent+1, styler))
325 levelNext++;
327 else if(IsCommentLine(lineCurrent-1, styler) && !IsCommentLine(lineCurrent+1, styler))
329 levelNext--;
332 else
334 if (IsCommentBlockStart(lineCurrent, styler) && !IsCommentBlockEnd(lineCurrent, styler))
336 levelNext++;
338 else if (IsCommentBlockEnd(lineCurrent, styler) && !IsCommentBlockStart(lineCurrent, styler))
340 levelNext--;
345 if ((style == SCE_VHDL_OPERATOR) && foldAtParenthese)
347 if(ch == '(') {
348 levelNext++;
349 } else if (ch == ')') {
350 levelNext--;
354 if ((!IsCommentStyle(style)) && (style != SCE_VHDL_STRING))
356 if((ch == ';') && (strcmp(prevWord, "end") == 0))
358 strcpy(prevWord, ";");
361 if(!IsAWordChar(chPrev) && IsAWordStart(ch))
363 lastStart = i;
366 if(IsAWordChar(ch) && !IsAWordChar(chNext)) {
367 char s[32];
368 unsigned int k;
369 for(k=0; (k<31 ) && (k<i-lastStart+1 ); k++) {
370 s[k] = static_cast<char>(tolower(styler[lastStart+k]));
372 s[k] = '\0';
374 if(keywords.InList(s))
376 if (
377 strcmp(s, "architecture") == 0 ||
378 strcmp(s, "case") == 0 ||
379 strcmp(s, "generate") == 0 ||
380 strcmp(s, "block") == 0 ||
381 strcmp(s, "loop") == 0 ||
382 strcmp(s, "package") ==0 ||
383 strcmp(s, "process") == 0 ||
384 strcmp(s, "record") == 0 ||
385 strcmp(s, "then") == 0 ||
386 strcmp(s, "units") == 0)
388 if (strcmp(prevWord, "end") != 0)
390 if (levelMinCurrentElse > levelNext) {
391 levelMinCurrentElse = levelNext;
393 levelNext++;
395 } else if (
396 strcmp(s, "component") == 0 ||
397 strcmp(s, "entity") == 0 ||
398 strcmp(s, "configuration") == 0 )
400 if (strcmp(prevWord, "end") != 0 && lastStart)
401 { // check for instantiated unit by backward searching for the colon.
402 unsigned pos = lastStart;
403 char chAtPos, styleAtPos;
404 do{// skip white spaces
405 pos--;
406 styleAtPos = styler.StyleAt(pos);
407 chAtPos = styler.SafeGetCharAt(pos);
408 }while(pos>0 &&
409 (chAtPos == ' ' || chAtPos == '\t' ||
410 chAtPos == '\n' || chAtPos == '\r' ||
411 IsCommentStyle(styleAtPos)));
413 // check for a colon (':') before the instantiated units "entity", "component" or "configuration". Don't fold thereafter.
414 if (chAtPos != ':')
416 if (levelMinCurrentElse > levelNext) {
417 levelMinCurrentElse = levelNext;
419 levelNext++;
422 } else if (
423 strcmp(s, "procedure") == 0 ||
424 strcmp(s, "function") == 0)
426 if (strcmp(prevWord, "end") != 0) // check for "end procedure" etc.
427 { // This code checks to see if the procedure / function is a definition within a "package"
428 // rather than the actual code in the body.
429 int BracketLevel = 0;
430 for(int pos=i+1; pos<styler.Length(); pos++)
432 int styleAtPos = styler.StyleAt(pos);
433 char chAtPos = styler.SafeGetCharAt(pos);
434 if(chAtPos == '(') BracketLevel++;
435 if(chAtPos == ')') BracketLevel--;
437 (BracketLevel == 0) &&
438 (!IsCommentStyle(styleAtPos)) &&
439 (styleAtPos != SCE_VHDL_STRING) &&
440 !iswordchar(styler.SafeGetCharAt(pos-1)) &&
441 (chAtPos|' ')=='i' && (styler.SafeGetCharAt(pos+1)|' ')=='s' &&
442 !iswordchar(styler.SafeGetCharAt(pos+2)))
444 if (levelMinCurrentElse > levelNext) {
445 levelMinCurrentElse = levelNext;
447 levelNext++;
448 break;
450 if((BracketLevel == 0) && (chAtPos == ';'))
452 break;
457 } else if (strcmp(s, "end") == 0) {
458 levelNext--;
459 } else if(strcmp(s, "elsif") == 0) { // elsif is followed by then so folding occurs correctly
460 levelNext--;
461 } else if (strcmp(s, "else") == 0) {
462 if(strcmp(prevWord, "when") != 0) // ignore a <= x when y else z;
464 levelMinCurrentElse = levelNext - 1; // VHDL else is all on its own so just dec. the min level
466 } else if(
467 ((strcmp(s, "begin") == 0) && (strcmp(prevWord, "architecture") == 0)) ||
468 ((strcmp(s, "begin") == 0) && (strcmp(prevWord, "function") == 0)) ||
469 ((strcmp(s, "begin") == 0) && (strcmp(prevWord, "procedure") == 0)))
471 levelMinCurrentBegin = levelNext - 1;
473 //Platform::DebugPrintf("Line[%04d] Prev[%20s] Cur[%20s] Level[%x]\n", lineCurrent+1, prevWord, s, levelCurrent);
474 strcpy(prevWord, s);
478 if (atEOL) {
479 int levelUse = levelCurrent;
481 if (foldAtElse && (levelMinCurrentElse < levelUse)) {
482 levelUse = levelMinCurrentElse;
484 if (foldAtBegin && (levelMinCurrentBegin < levelUse)) {
485 levelUse = levelMinCurrentBegin;
487 int lev = levelUse | levelNext << 16;
488 if (visibleChars == 0 && foldCompact)
489 lev |= SC_FOLDLEVELWHITEFLAG;
491 if (levelUse < levelNext)
492 lev |= SC_FOLDLEVELHEADERFLAG;
493 if (lev != styler.LevelAt(lineCurrent)) {
494 styler.SetLevel(lineCurrent, lev);
496 //Platform::DebugPrintf("Line[%04d] ---------------------------------------------------- Level[%x]\n", lineCurrent+1, levelCurrent);
497 lineCurrent++;
498 levelCurrent = levelNext;
499 //levelMinCurrent = levelCurrent;
500 levelMinCurrentElse = levelCurrent;
501 levelMinCurrentBegin = levelCurrent;
502 visibleChars = 0;
504 /***************************************/
505 if (!isspacechar(ch)) visibleChars++;
508 /***************************************/
509 // Platform::DebugPrintf("Line[%04d] ---------------------------------------------------- Level[%x]\n", lineCurrent+1, levelCurrent);
512 //=============================================================================
513 static void FoldVHDLDoc(unsigned int startPos, int length, int initStyle, WordList *[],
514 Accessor &styler) {
515 FoldNoBoxVHDLDoc(startPos, length, initStyle, styler);
518 //=============================================================================
519 static const char * const VHDLWordLists[] = {
520 "Keywords",
521 "Operators",
522 "Attributes",
523 "Standard Functions",
524 "Standard Packages",
525 "Standard Types",
526 "User Words",
531 LexerModule lmVHDL(SCLEX_VHDL, ColouriseVHDLDoc, "vhdl", FoldVHDLDoc, VHDLWordLists);
534 // Keyword:
535 // access after alias all architecture array assert attribute begin block body buffer bus case component
536 // configuration constant disconnect downto else elsif end entity exit file for function generate generic
537 // group guarded if impure in inertial inout is label library linkage literal loop map new next null of
538 // on open others out package port postponed procedure process pure range record register reject report
539 // return select severity shared signal subtype then to transport type unaffected units until use variable
540 // wait when while with
542 // Operators:
543 // abs and mod nand nor not or rem rol ror sla sll sra srl xnor xor
545 // Attributes:
546 // left right low high ascending image value pos val succ pred leftof rightof base range reverse_range
547 // length delayed stable quiet transaction event active last_event last_active last_value driving
548 // driving_value simple_name path_name instance_name
550 // Std Functions:
551 // now readline read writeline write endfile resolved to_bit to_bitvector to_stdulogic to_stdlogicvector
552 // to_stdulogicvector to_x01 to_x01z to_UX01 rising_edge falling_edge is_x shift_left shift_right rotate_left
553 // rotate_right resize to_integer to_unsigned to_signed std_match to_01
555 // Std Packages:
556 // std ieee work standard textio std_logic_1164 std_logic_arith std_logic_misc std_logic_signed
557 // std_logic_textio std_logic_unsigned numeric_bit numeric_std math_complex math_real vital_primitives
558 // vital_timing
560 // Std Types:
561 // boolean bit character severity_level integer real time delay_length natural positive string bit_vector
562 // file_open_kind file_open_status line text side width std_ulogic std_ulogic_vector std_logic
563 // std_logic_vector X01 X01Z UX01 UX01Z unsigned signed