1 // Scintilla source code edit control
4 ** Written by Phil Reid,
6 ** - The Verilog Lexer by Avi Yegudin
7 ** - The Fortran Lexer by Chuan-jian Shen
8 ** - The C++ lexer by Neil Hodgson
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.
21 #include "Scintilla.h"
25 #include "LexAccessor.h"
27 #include "StyleContext.h"
28 #include "CharacterSet.h"
29 #include "LexerModule.h"
32 using namespace Scintilla
;
35 static void ColouriseVHDLDoc(
36 unsigned int startPos
,
39 WordList
*keywordlists
[],
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
,
63 WordList
*keywordlists
[],
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
);
76 for (; sc
.More(); sc
.Forward())
79 // Determine if the current state should terminate.
80 if (sc
.state
== SCE_VHDL_OPERATOR
) {
81 sc
.SetState(SCE_VHDL_DEFAULT
);
82 } else if (sc
.state
== SCE_VHDL_NUMBER
) {
83 if (!IsAWordChar(sc
.ch
) && (sc
.ch
!= '#')) {
84 sc
.SetState(SCE_VHDL_DEFAULT
);
86 } else if (sc
.state
== SCE_VHDL_IDENTIFIER
) {
87 if (!IsAWordChar(sc
.ch
) || (sc
.ch
== '.')) {
89 sc
.GetCurrentLowered(s
, sizeof(s
));
90 if (Keywords
.InList(s
)) {
91 sc
.ChangeState(SCE_VHDL_KEYWORD
);
92 } else if (Operators
.InList(s
)) {
93 sc
.ChangeState(SCE_VHDL_STDOPERATOR
);
94 } else if (Attributes
.InList(s
)) {
95 sc
.ChangeState(SCE_VHDL_ATTRIBUTE
);
96 } else if (Functions
.InList(s
)) {
97 sc
.ChangeState(SCE_VHDL_STDFUNCTION
);
98 } else if (Packages
.InList(s
)) {
99 sc
.ChangeState(SCE_VHDL_STDPACKAGE
);
100 } else if (Types
.InList(s
)) {
101 sc
.ChangeState(SCE_VHDL_STDTYPE
);
102 } else if (User
.InList(s
)) {
103 sc
.ChangeState(SCE_VHDL_USERWORD
);
105 sc
.SetState(SCE_VHDL_DEFAULT
);
107 } else if (sc
.state
== SCE_VHDL_COMMENT
|| sc
.state
== SCE_VHDL_COMMENTLINEBANG
) {
109 sc
.SetState(SCE_VHDL_DEFAULT
);
111 } else if (sc
.state
== SCE_VHDL_STRING
) {
113 if (sc
.chNext
== '\"' || sc
.chNext
== '\'' || sc
.chNext
== '\\') {
116 } else if (sc
.ch
== '\"') {
117 sc
.ForwardSetState(SCE_VHDL_DEFAULT
);
118 } else if (sc
.atLineEnd
) {
119 sc
.ChangeState(SCE_VHDL_STRINGEOL
);
120 sc
.ForwardSetState(SCE_VHDL_DEFAULT
);
122 } else if (sc
.state
== SCE_VHDL_BLOCK_COMMENT
){
123 if(sc
.ch
== '*' && sc
.chNext
== '/'){
125 sc
.ForwardSetState(SCE_VHDL_DEFAULT
);
129 // Determine if a new state should be entered.
130 if (sc
.state
== SCE_VHDL_DEFAULT
) {
131 if (IsADigit(sc
.ch
) || (sc
.ch
== '.' && IsADigit(sc
.chNext
))) {
132 sc
.SetState(SCE_VHDL_NUMBER
);
133 } else if (IsAWordStart(sc
.ch
)) {
134 sc
.SetState(SCE_VHDL_IDENTIFIER
);
135 } else if (sc
.Match('-', '-')) {
136 if (sc
.Match("--!")) // Nice to have a different comment style
137 sc
.SetState(SCE_VHDL_COMMENTLINEBANG
);
139 sc
.SetState(SCE_VHDL_COMMENT
);
140 } else if (sc
.Match('/', '*')){
141 sc
.SetState(SCE_VHDL_BLOCK_COMMENT
);
142 } else if (sc
.ch
== '\"') {
143 sc
.SetState(SCE_VHDL_STRING
);
144 } else if (isoperator(static_cast<char>(sc
.ch
))) {
145 sc
.SetState(SCE_VHDL_OPERATOR
);
151 //=============================================================================
152 static bool IsCommentLine(int line
, Accessor
&styler
) {
153 int pos
= styler
.LineStart(line
);
154 int eol_pos
= styler
.LineStart(line
+ 1) - 1;
155 for (int i
= pos
; i
< eol_pos
; i
++) {
157 char chNext
= styler
[i
+1];
158 if ((ch
== '-') && (chNext
== '-'))
160 else if (ch
!= ' ' && ch
!= '\t')
165 static bool IsCommentBlockStart(int line
, Accessor
&styler
)
167 int pos
= styler
.LineStart(line
);
168 int eol_pos
= styler
.LineStart(line
+ 1) - 1;
169 for (int i
= pos
; i
< eol_pos
; i
++) {
171 char chNext
= styler
[i
+1];
172 char style
= styler
.StyleAt(i
);
173 if ((style
== SCE_VHDL_BLOCK_COMMENT
) && (ch
== '/') && (chNext
== '*'))
179 static bool IsCommentBlockEnd(int line
, Accessor
&styler
)
181 int pos
= styler
.LineStart(line
);
182 int eol_pos
= styler
.LineStart(line
+ 1) - 1;
184 for (int i
= pos
; i
< eol_pos
; i
++) {
186 char chNext
= styler
[i
+1];
187 char style
= styler
.StyleAt(i
);
188 if ((style
== SCE_VHDL_BLOCK_COMMENT
) && (ch
== '*') && (chNext
== '/'))
194 static bool IsCommentStyle(char style
)
196 return style
== SCE_VHDL_BLOCK_COMMENT
|| style
== SCE_VHDL_COMMENT
|| style
== SCE_VHDL_COMMENTLINEBANG
;
199 //=============================================================================
201 static void FoldNoBoxVHDLDoc(
202 unsigned int startPos
,
207 // Decided it would be smarter to have the lexer have all keywords included. Therefore I
208 // don't check if the style for the keywords that I use to adjust the levels.
210 "architecture begin case component else elsif end entity generate loop package process record then "
211 "procedure function when";
215 bool foldComment
= styler
.GetPropertyInt("fold.comment", 1) != 0;
216 bool foldCompact
= styler
.GetPropertyInt("fold.compact", 1) != 0;
217 bool foldAtElse
= styler
.GetPropertyInt("fold.at.else", 1) != 0;
218 bool foldAtBegin
= styler
.GetPropertyInt("fold.at.Begin", 1) != 0;
219 bool foldAtParenthese
= styler
.GetPropertyInt("fold.at.Parenthese", 1) != 0;
220 //bool foldAtWhen = styler.GetPropertyInt("fold.at.When", 1) != 0; //< fold at when in case statements
222 int visibleChars
= 0;
223 unsigned int endPos
= startPos
+ length
;
225 int lineCurrent
= styler
.GetLine(startPos
);
226 int levelCurrent
= SC_FOLDLEVELBASE
;
228 levelCurrent
= styler
.LevelAt(lineCurrent
-1) >> 16;
229 //int levelMinCurrent = levelCurrent;
230 int levelMinCurrentElse
= levelCurrent
; //< Used for folding at 'else'
231 int levelMinCurrentBegin
= levelCurrent
; //< Used for folding at 'begin'
232 int levelNext
= levelCurrent
;
234 /***************************************/
236 char prevWord
[32] = "";
238 /***************************************/
240 // The logic for going up or down a level depends on a the previous keyword
241 // This code could be cleaned up.
244 for(j
= startPos
; j
>0; j
--)
246 char ch
= styler
.SafeGetCharAt(j
);
247 char chPrev
= styler
.SafeGetCharAt(j
-1);
248 int style
= styler
.StyleAt(j
);
249 int stylePrev
= styler
.StyleAt(j
-1);
250 if ((!IsCommentStyle(style
)) && (stylePrev
!= SCE_VHDL_STRING
))
252 if(IsAWordChar(chPrev
) && !IsAWordChar(ch
))
257 if ((!IsCommentStyle(style
)) && (style
!= SCE_VHDL_STRING
))
259 if(!IsAWordChar(chPrev
) && IsAWordStart(ch
) && (end
!= 0))
263 for(k
=0; (k
<31 ) && (k
<end
-j
+1 ); k
++) {
264 s
[k
] = static_cast<char>(tolower(styler
[j
+k
]));
268 if(keywords
.InList(s
)) {
275 for(j
=j
+static_cast<unsigned int>(strlen(prevWord
)); j
<endPos
; j
++)
277 char ch
= styler
.SafeGetCharAt(j
);
278 int style
= styler
.StyleAt(j
);
279 if ((!IsCommentStyle(style
)) && (style
!= SCE_VHDL_STRING
))
281 if((ch
== ';') && (strcmp(prevWord
, "end") == 0))
283 strcpy(prevWord
, ";");
288 char chNext
= styler
[startPos
];
291 int styleNext
= styler
.StyleAt(startPos
);
292 //Platform::DebugPrintf("Line[%04d] Prev[%20s] ************************* Level[%x]\n", lineCurrent+1, prevWord, levelCurrent);
294 /***************************************/
295 for (unsigned int i
= startPos
; i
< endPos
; i
++)
298 chNext
= styler
.SafeGetCharAt(i
+ 1);
299 chPrev
= styler
.SafeGetCharAt(i
- 1);
300 chNextNonBlank
= chNext
;
301 unsigned int j
= i
+1;
302 while(IsABlank(chNextNonBlank
) && j
<endPos
)
305 chNextNonBlank
= styler
.SafeGetCharAt(j
);
307 int style
= styleNext
;
308 styleNext
= styler
.StyleAt(i
+ 1);
309 bool atEOL
= (ch
== '\r' && chNext
!= '\n') || (ch
== '\n');
311 if (foldComment
&& atEOL
)
313 if(IsCommentLine(lineCurrent
, styler
))
315 if(!IsCommentLine(lineCurrent
-1, styler
) && IsCommentLine(lineCurrent
+1, styler
))
319 else if(IsCommentLine(lineCurrent
-1, styler
) && !IsCommentLine(lineCurrent
+1, styler
))
326 if (IsCommentBlockStart(lineCurrent
, styler
) && !IsCommentBlockEnd(lineCurrent
, styler
))
330 else if (IsCommentBlockEnd(lineCurrent
, styler
) && !IsCommentBlockStart(lineCurrent
, styler
))
337 if ((style
== SCE_VHDL_OPERATOR
) && foldAtParenthese
)
341 } else if (ch
== ')') {
346 if ((!IsCommentStyle(style
)) && (style
!= SCE_VHDL_STRING
))
348 if((ch
== ';') && (strcmp(prevWord
, "end") == 0))
350 strcpy(prevWord
, ";");
353 if(!IsAWordChar(chPrev
) && IsAWordStart(ch
))
358 if(IsAWordChar(ch
) && !IsAWordChar(chNext
)) {
361 for(k
=0; (k
<31 ) && (k
<i
-lastStart
+1 ); k
++) {
362 s
[k
] = static_cast<char>(tolower(styler
[lastStart
+k
]));
366 if(keywords
.InList(s
))
369 strcmp(s
, "architecture") == 0 ||
370 strcmp(s
, "case") == 0 ||
371 strcmp(s
, "generate") == 0 ||
372 strcmp(s
, "loop") == 0 ||
373 strcmp(s
, "package") ==0 ||
374 strcmp(s
, "process") == 0 ||
375 strcmp(s
, "record") == 0 ||
376 strcmp(s
, "then") == 0)
378 if (strcmp(prevWord
, "end") != 0)
380 if (levelMinCurrentElse
> levelNext
) {
381 levelMinCurrentElse
= levelNext
;
386 strcmp(s
, "component") == 0 ||
387 strcmp(s
, "entity") == 0 ||
388 strcmp(s
, "configuration") == 0 )
390 if (strcmp(prevWord
, "end") != 0)
391 { // check for instantiated unit by backward searching for the colon.
392 unsigned pos
= lastStart
-1;
393 char chAtPos
, styleAtPos
;
394 do{// skip white spaces
395 styleAtPos
= styler
.StyleAt(pos
);
396 chAtPos
= styler
.SafeGetCharAt(pos
--);
398 (chAtPos
== ' ' || chAtPos
== '\t' ||
399 chAtPos
== '\n' || chAtPos
== '\r' ||
400 IsCommentStyle(styleAtPos
)));
402 // check for a colon (':') before the instantiated units "entity", "component" or "configuration". Don't fold thereafter.
405 if (levelMinCurrentElse
> levelNext
) {
406 levelMinCurrentElse
= levelNext
;
412 strcmp(s
, "procedure") == 0 ||
413 strcmp(s
, "function") == 0)
415 if (strcmp(prevWord
, "end") != 0) // check for "end procedure" etc.
416 { // This code checks to see if the procedure / function is a definition within a "package"
417 // rather than the actual code in the body.
418 int BracketLevel
= 0;
419 for(int pos
=i
+1; pos
<styler
.Length(); pos
++)
421 int styleAtPos
= styler
.StyleAt(pos
);
422 char chAtPos
= styler
.SafeGetCharAt(pos
);
423 if(chAtPos
== '(') BracketLevel
++;
424 if(chAtPos
== ')') BracketLevel
--;
426 (BracketLevel
== 0) &&
427 (!IsCommentStyle(styleAtPos
)) &&
428 (styleAtPos
!= SCE_VHDL_STRING
) &&
429 !iswordchar(styler
.SafeGetCharAt(pos
-1)) &&
430 styler
.Match(pos
, "is") &&
431 !iswordchar(styler
.SafeGetCharAt(pos
+2)))
433 if (levelMinCurrentElse
> levelNext
) {
434 levelMinCurrentElse
= levelNext
;
439 if((BracketLevel
== 0) && (chAtPos
== ';'))
446 } else if (strcmp(s
, "end") == 0) {
448 } else if(strcmp(s
, "elsif") == 0) { // elsif is followed by then so folding occurs correctly
450 } else if (strcmp(s
, "else") == 0) {
451 if(strcmp(prevWord
, "when") != 0) // ignore a <= x when y else z;
453 levelMinCurrentElse
= levelNext
- 1; // VHDL else is all on its own so just dec. the min level
456 ((strcmp(s
, "begin") == 0) && (strcmp(prevWord
, "architecture") == 0)) ||
457 ((strcmp(s
, "begin") == 0) && (strcmp(prevWord
, "function") == 0)) ||
458 ((strcmp(s
, "begin") == 0) && (strcmp(prevWord
, "procedure") == 0)))
460 levelMinCurrentBegin
= levelNext
- 1;
462 //Platform::DebugPrintf("Line[%04d] Prev[%20s] Cur[%20s] Level[%x]\n", lineCurrent+1, prevWord, s, levelCurrent);
468 int levelUse
= levelCurrent
;
470 if (foldAtElse
&& (levelMinCurrentElse
< levelUse
)) {
471 levelUse
= levelMinCurrentElse
;
473 if (foldAtBegin
&& (levelMinCurrentBegin
< levelUse
)) {
474 levelUse
= levelMinCurrentBegin
;
476 int lev
= levelUse
| levelNext
<< 16;
477 if (visibleChars
== 0 && foldCompact
)
478 lev
|= SC_FOLDLEVELWHITEFLAG
;
480 if (levelUse
< levelNext
)
481 lev
|= SC_FOLDLEVELHEADERFLAG
;
482 if (lev
!= styler
.LevelAt(lineCurrent
)) {
483 styler
.SetLevel(lineCurrent
, lev
);
485 //Platform::DebugPrintf("Line[%04d] ---------------------------------------------------- Level[%x]\n", lineCurrent+1, levelCurrent);
487 levelCurrent
= levelNext
;
488 //levelMinCurrent = levelCurrent;
489 levelMinCurrentElse
= levelCurrent
;
490 levelMinCurrentBegin
= levelCurrent
;
493 /***************************************/
494 if (!isspacechar(ch
)) visibleChars
++;
497 /***************************************/
498 // Platform::DebugPrintf("Line[%04d] ---------------------------------------------------- Level[%x]\n", lineCurrent+1, levelCurrent);
501 //=============================================================================
502 static void FoldVHDLDoc(unsigned int startPos
, int length
, int initStyle
, WordList
*[],
504 FoldNoBoxVHDLDoc(startPos
, length
, initStyle
, styler
);
507 //=============================================================================
508 static const char * const VHDLWordLists
[] = {
512 "Standard Functions",
520 LexerModule
lmVHDL(SCLEX_VHDL
, ColouriseVHDLDoc
, "vhdl", FoldVHDLDoc
, VHDLWordLists
);
524 // access after alias all architecture array assert attribute begin block body buffer bus case component
525 // configuration constant disconnect downto else elsif end entity exit file for function generate generic
526 // group guarded if impure in inertial inout is label library linkage literal loop map new next null of
527 // on open others out package port postponed procedure process pure range record register reject report
528 // return select severity shared signal subtype then to transport type unaffected units until use variable
529 // wait when while with
532 // abs and mod nand nor not or rem rol ror sla sll sra srl xnor xor
535 // left right low high ascending image value pos val succ pred leftof rightof base range reverse_range
536 // length delayed stable quiet transaction event active last_event last_active last_value driving
537 // driving_value simple_name path_name instance_name
540 // now readline read writeline write endfile resolved to_bit to_bitvector to_stdulogic to_stdlogicvector
541 // to_stdulogicvector to_x01 to_x01z to_UX01 rising_edge falling_edge is_x shift_left shift_right rotate_left
542 // rotate_right resize to_integer to_unsigned to_signed std_match to_01
545 // std ieee work standard textio std_logic_1164 std_logic_arith std_logic_misc std_logic_signed
546 // std_logic_textio std_logic_unsigned numeric_bit numeric_std math_complex math_real vital_primitives
550 // boolean bit character severity_level integer real time delay_length natural positive string bit_vector
551 // file_open_kind file_open_status line text side width std_ulogic std_ulogic_vector std_logic
552 // std_logic_vector X01 X01Z UX01 UX01Z unsigned signed