Debian Codegraph @

scintilla/lexilla/lexers/LexVerilog.cxx

raw
1 // Scintilla source code edit control
2 /** @file LexVerilog.cxx
3 ** Lexer for Verilog.
4 ** Written by Avi Yegudin, based on C++ lexer by Neil Hodgson
5 **/
6 // Copyright 1998-2002 by Neil Hodgson <[email protected]>
7 // The License.txt file describes the conditions under which this software may be distributed.
8
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdio.h>
12 #include <stdarg.h>
13 #include <assert.h>
14 #include <ctype.h>
15
16 #include <string>
17 #include <string_view>
18 #include <vector>
19 #include <map>
20 #include <algorithm>
21 #include <functional>
22
23 #include "ILexer.h"
24 #include "Scintilla.h"
25 #include "SciLexer.h"
26
27 #include "WordList.h"
28 #include "LexAccessor.h"
29 #include "Accessor.h"
30 #include "StyleContext.h"
31 #include "CharacterSet.h"
32 #include "LexerModule.h"
33
34 #include "OptionSet.h"
35 #include "SubStyles.h"
36 #include "DefaultLexer.h"
37
38 using namespace Scintilla;
39 using namespace Lexilla;
40
41 namespace {
42 // Use an unnamed namespace to protect the functions and classes from name conflicts
43
44 struct PPDefinition {
45 Sci_Position line;
46 std::string key;
47 std::string value;
48 bool isUndef;
49 std::string arguments;
50 PPDefinition(Sci_Position line_, const std::string &key_, const std::string &value_, bool isUndef_ = false, const std::string &arguments_ = "") :
51 line(line_), key(key_), value(value_), isUndef(isUndef_), arguments(arguments_) {
52 }
53 };
54
55 class LinePPState {
56 int state;
57 int ifTaken;
58 int level;
59 bool ValidLevel() const {
60 return level >= 0 && level < 32;
61 }
62 int maskLevel() const {
63 if (level >= 0) {
64 return 1 << level;
65 } else {
66 return 1;
67 }
68 }
69 public:
70 LinePPState() : state(0), ifTaken(0), level(-1) {
71 }
72 bool IsInactive() const {
73 return state != 0;
74 }
75 bool CurrentIfTaken() const {
76 return (ifTaken & maskLevel()) != 0;
77 }
78 void StartSection(bool on) {
79 level++;
80 if (ValidLevel()) {
81 if (on) {
82 state &= ~maskLevel();
83 ifTaken |= maskLevel();
84 } else {
85 state |= maskLevel();
86 ifTaken &= ~maskLevel();
87 }
88 }
89 }
90 void EndSection() {
91 if (ValidLevel()) {
92 state &= ~maskLevel();
93 ifTaken &= ~maskLevel();
94 }
95 level--;
96 }
97 void InvertCurrentLevel() {
98 if (ValidLevel()) {
99 state ^= maskLevel();
100 ifTaken |= maskLevel();
101 }
102 }
103 };
104
105 // Hold the preprocessor state for each line seen.
106 // Currently one entry per line but could become sparse with just one entry per preprocessor line.
107 class PPStates {
108 std::vector<LinePPState> vlls;
109 public:
110 LinePPState ForLine(Sci_Position line) const {
111 if ((line > 0) && (vlls.size() > static_cast<size_t>(line))) {
112 return vlls[line];
113 } else {
114 return LinePPState();
115 }
116 }
117 void Add(Sci_Position line, LinePPState lls) {
118 vlls.resize(line+1);
119 vlls[line] = lls;
120 }
121 };
122
123 // Options used for LexerVerilog
124 struct OptionsVerilog {
125 bool foldComment;
126 bool foldPreprocessor;
127 bool foldPreprocessorElse;
128 bool foldCompact;
129 bool foldAtElse;
130 bool foldAtModule;
131 bool trackPreprocessor;
132 bool updatePreprocessor;
133 bool portStyling;
134 bool allUppercaseDocKeyword;
135 OptionsVerilog() {
136 foldComment = false;
137 foldPreprocessor = false;
138 foldPreprocessorElse = false;
139 foldCompact = false;
140 foldAtElse = false;
141 foldAtModule = false;
142 // for backwards compatibility, preprocessor functionality is disabled by default
143 trackPreprocessor = false;
144 updatePreprocessor = false;
145 // for backwards compatibility, treat input/output/inout as regular keywords
146 portStyling = false;
147 // for backwards compatibility, don't treat all uppercase identifiers as documentation keywords
148 allUppercaseDocKeyword = false;
149 }
150 };
151
152 struct OptionSetVerilog : public OptionSet<OptionsVerilog> {
153 OptionSetVerilog() {
154 DefineProperty("fold.comment", &OptionsVerilog::foldComment,
155 "This option enables folding multi-line comments when using the Verilog lexer.");
156 DefineProperty("fold.preprocessor", &OptionsVerilog::foldPreprocessor,
157 "This option enables folding preprocessor directives when using the Verilog lexer.");
158 DefineProperty("fold.compact", &OptionsVerilog::foldCompact);
159 DefineProperty("fold.at.else", &OptionsVerilog::foldAtElse,
160 "This option enables folding on the else line of an if statement.");
161 DefineProperty("fold.verilog.flags", &OptionsVerilog::foldAtModule,
162 "This option enables folding module definitions. Typically source files "
163 "contain only one module definition so this option is somewhat useless.");
164 DefineProperty("lexer.verilog.track.preprocessor", &OptionsVerilog::trackPreprocessor,
165 "Set to 1 to interpret `if/`else/`endif to grey out code that is not active.");
166 DefineProperty("lexer.verilog.update.preprocessor", &OptionsVerilog::updatePreprocessor,
167 "Set to 1 to update preprocessor definitions when `define, `undef, or `undefineall found.");
168 DefineProperty("lexer.verilog.portstyling", &OptionsVerilog::portStyling,
169 "Set to 1 to style input, output, and inout ports differently from regular keywords.");
170 DefineProperty("lexer.verilog.allupperkeywords", &OptionsVerilog::allUppercaseDocKeyword,
171 "Set to 1 to style identifiers that are all uppercase as documentation keyword.");
172 DefineProperty("lexer.verilog.fold.preprocessor.else", &OptionsVerilog::foldPreprocessorElse,
173 "This option enables folding on `else and `elsif preprocessor directives.");
174 }
175 };
176
177 const char styleSubable[] = {0};
178
179 }
180
181 class LexerVerilog : public DefaultLexer {
182 CharacterSet setWord;
183 WordList keywords;
184 WordList keywords2;
185 WordList keywords3;
186 WordList keywords4;
187 WordList keywords5;
188 WordList ppDefinitions;
189 PPStates vlls;
190 std::vector<PPDefinition> ppDefineHistory;
191 struct SymbolValue {
192 std::string value;
193 std::string arguments;
194 SymbolValue(const std::string &value_="", const std::string &arguments_="") : value(value_), arguments(arguments_) {
195 }
196 SymbolValue &operator = (const std::string &value_) {
197 value = value_;
198 arguments.clear();
199 return *this;
200 }
201 bool IsMacro() const {
202 return !arguments.empty();
203 }
204 };
205 typedef std::map<std::string, SymbolValue> SymbolTable;
206 SymbolTable preprocessorDefinitionsStart;
207 OptionsVerilog options;
208 OptionSetVerilog osVerilog;
209 enum { activeFlag = 0x40 };
210 SubStyles subStyles;
211
212 // states at end of line (EOL) during fold operations:
213 // foldExternFlag: EOL while parsing an extern function/task declaration terminated by ';'
214 // foldWaitDisableFlag: EOL while parsing wait or disable statement, terminated by "fork" or '('
215 // typdefFlag: EOL while parsing typedef statement, terminated by ';'
216 enum {foldExternFlag = 0x01, foldWaitDisableFlag = 0x02, typedefFlag = 0x04, protectedFlag = 0x08};
217 // map using line number as key to store fold state information
218 std::map<Sci_Position, int> foldState;
219
220 public:
221 LexerVerilog() :
222 DefaultLexer("verilog", SCLEX_VERILOG),
223 setWord(CharacterSet::setAlphaNum, "._", 0x80, true),
224 subStyles(styleSubable, 0x80, 0x40, activeFlag) {
225 }
226 virtual ~LexerVerilog() {}
227 int SCI_METHOD Version() const override {
228 return lvRelease5;
229 }
230 void SCI_METHOD Release() override {
231 delete this;
232 }
233 const char* SCI_METHOD PropertyNames() override {
234 return osVerilog.PropertyNames();
235 }
236 int SCI_METHOD PropertyType(const char* name) override {
237 return osVerilog.PropertyType(name);
238 }
239 const char* SCI_METHOD DescribeProperty(const char* name) override {
240 return osVerilog.DescribeProperty(name);
241 }
242 Sci_Position SCI_METHOD PropertySet(const char* key, const char* val) override {
243 return osVerilog.PropertySet(&options, key, val);
244 }
245 const char * SCI_METHOD PropertyGet(const char *key) override {
246 return osVerilog.PropertyGet(key);
247 }
248 const char* SCI_METHOD DescribeWordListSets() override {
249 return osVerilog.DescribeWordListSets();
250 }
251 Sci_Position SCI_METHOD WordListSet(int n, const char* wl) override;
252 void SCI_METHOD Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override;
253 void SCI_METHOD Fold(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess) override;
254 void* SCI_METHOD PrivateCall(int, void*) override {
255 return 0;
256 }
257 int SCI_METHOD LineEndTypesSupported() override {
258 return SC_LINE_END_TYPE_UNICODE;
259 }
260 int SCI_METHOD AllocateSubStyles(int styleBase, int numberStyles) override {
261 return subStyles.Allocate(styleBase, numberStyles);
262 }
263 int SCI_METHOD SubStylesStart(int styleBase) override {
264 return subStyles.Start(styleBase);
265 }
266 int SCI_METHOD SubStylesLength(int styleBase) override {
267 return subStyles.Length(styleBase);
268 }
269 int SCI_METHOD StyleFromSubStyle(int subStyle) override {
270 int styleBase = subStyles.BaseStyle(MaskActive(subStyle));
271 int active = subStyle & activeFlag;
272 return styleBase | active;
273 }
274 int SCI_METHOD PrimaryStyleFromStyle(int style) override {
275 return MaskActive(style);
276 }
277 void SCI_METHOD FreeSubStyles() override {
278 subStyles.Free();
279 }
280 void SCI_METHOD SetIdentifiers(int style, const char *identifiers) override {
281 subStyles.SetIdentifiers(style, identifiers);
282 }
283 int SCI_METHOD DistanceToSecondaryStyles() override {
284 return activeFlag;
285 }
286 const char * SCI_METHOD GetSubStyleBases() override {
287 return styleSubable;
288 }
289 static ILexer5* LexerFactoryVerilog() {
290 return new LexerVerilog();
291 }
292 static int MaskActive(int style) {
293 return style & ~activeFlag;
294 }
295 std::vector<std::string> Tokenize(const std::string &expr) const;
296 };
297
298 Sci_Position SCI_METHOD LexerVerilog::WordListSet(int n, const char *wl) {
299 WordList *wordListN = 0;
300 switch (n) {
301 case 0:
302 wordListN = &keywords;
303 break;
304 case 1:
305 wordListN = &keywords2;
306 break;
307 case 2:
308 wordListN = &keywords3;
309 break;
310 case 3:
311 wordListN = &keywords4;
312 break;
313 case 4:
314 wordListN = &keywords5;
315 break;
316 case 5:
317 wordListN = &ppDefinitions;
318 break;
319 }
320 Sci_Position firstModification = -1;
321 if (wordListN) {
322 WordList wlNew;
323 wlNew.Set(wl);
324 if (*wordListN != wlNew) {
325 wordListN->Set(wl);
326 firstModification = 0;
327 if (n == 5) {
328 // Rebuild preprocessorDefinitions
329 preprocessorDefinitionsStart.clear();
330 for (int nDefinition = 0; nDefinition < ppDefinitions.Length(); nDefinition++) {
331 const char *cpDefinition = ppDefinitions.WordAt(nDefinition);
332 const char *cpEquals = strchr(cpDefinition, '=');
333 if (cpEquals) {
334 std::string name(cpDefinition, cpEquals - cpDefinition);
335 std::string val(cpEquals+1);
336 size_t bracket = name.find('(');
337 size_t bracketEnd = name.find(')');
338 if ((bracket != std::string::npos) && (bracketEnd != std::string::npos)) {
339 // Macro
340 std::string args = name.substr(bracket + 1, bracketEnd - bracket - 1);
341 name = name.substr(0, bracket);
342 preprocessorDefinitionsStart[name] = SymbolValue(val, args);
343 } else {
344 preprocessorDefinitionsStart[name] = val;
345 }
346 } else {
347 std::string name(cpDefinition);
348 std::string val("1");
349 preprocessorDefinitionsStart[name] = val;
350 }
351 }
352 }
353 }
354 }
355 return firstModification;
356 }
357
358 static inline bool IsAWordChar(const int ch) {
359 return (ch < 0x80) && (isalnum(ch) || ch == '_' || ch == '\''|| ch == '$');
360 }
361
362 static inline bool IsAWordStart(const int ch) {
363 return (ch < 0x80) && (isalnum(ch) || ch == '_' || ch == '$');
364 }
365
366 static inline bool AllUpperCase(const char *a) {
367 while (*a) {
368 if (*a >= 'a' && *a <= 'z') return false;
369 a++;
370 }
371 return true;
372 }
373
374 // Functor used to truncate history
375 struct After {
376 Sci_Position line;
377 explicit After(Sci_Position line_) : line(line_) {}
378 bool operator()(PPDefinition &p) const {
379 return p.line > line;
380 }
381 };
382
383 static std::string GetRestOfLine(LexAccessor &styler, Sci_Position start, bool allowSpace) {
384 std::string restOfLine;
385 Sci_Position i =0;
386 char ch = styler.SafeGetCharAt(start, '\n');
387 Sci_Position endLine = styler.LineEnd(styler.GetLine(start));
388 while (((start+i) < endLine) && (ch != '\r')) {
389 char chNext = styler.SafeGetCharAt(start + i + 1, '\n');
390 if (ch == '/' && (chNext == '/' || chNext == '*'))
391 break;
392 if (allowSpace || (ch != ' '))
393 restOfLine += ch;
394 i++;
395 ch = chNext;
396 }
397 return restOfLine;
398 }
399
400 static bool IsSpaceOrTab(int ch) {
401 return ch == ' ' || ch == '\t';
402 }
403
404 void SCI_METHOD LexerVerilog::Lex(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess)
405 {
406 LexAccessor styler(pAccess);
407
408 const int kwOther=0, kwDot=0x100, kwInput=0x200, kwOutput=0x300, kwInout=0x400, kwProtected=0x800;
409 int lineState = kwOther;
410 bool continuationLine = false;
411
412 Sci_Position curLine = styler.GetLine(startPos);
413 if (curLine > 0) lineState = styler.GetLineState(curLine - 1);
414
415 // Do not leak onto next line
416 if (initStyle == SCE_V_STRINGEOL)
417 initStyle = SCE_V_DEFAULT;
418
419 if ((MaskActive(initStyle) == SCE_V_PREPROCESSOR) ||
420 (MaskActive(initStyle) == SCE_V_COMMENTLINE) ||
421 (MaskActive(initStyle) == SCE_V_COMMENTLINEBANG)) {
422 // Set continuationLine if last character of previous line is '\'
423 if (curLine > 0) {
424 Sci_Position endLinePrevious = styler.LineEnd(curLine - 1);
425 if (endLinePrevious > 0) {
426 continuationLine = styler.SafeGetCharAt(endLinePrevious-1) == '\\';
427 }
428 }
429 }
430
431 StyleContext sc(startPos, length, initStyle, styler);
432 LinePPState preproc = vlls.ForLine(curLine);
433
434 bool definitionsChanged = false;
435
436 // Truncate ppDefineHistory before current line
437
438 if (!options.updatePreprocessor)
439 ppDefineHistory.clear();
440
441 std::vector<PPDefinition>::iterator itInvalid = std::find_if(ppDefineHistory.begin(), ppDefineHistory.end(), After(curLine-1));
442 if (itInvalid != ppDefineHistory.end()) {
443 ppDefineHistory.erase(itInvalid, ppDefineHistory.end());
444 definitionsChanged = true;
445 }
446
447 SymbolTable preprocessorDefinitions = preprocessorDefinitionsStart;
448 for (std::vector<PPDefinition>::iterator itDef = ppDefineHistory.begin(); itDef != ppDefineHistory.end(); ++itDef) {
449 if (itDef->isUndef)
450 preprocessorDefinitions.erase(itDef->key);
451 else
452 preprocessorDefinitions[itDef->key] = SymbolValue(itDef->value, itDef->arguments);
453 }
454
455 int activitySet = preproc.IsInactive() ? activeFlag : 0;
456 Sci_Position lineEndNext = styler.LineEnd(curLine);
457 bool isEscapedId = false; // true when parsing an escaped Identifier
458 bool isProtected = (lineState&kwProtected) != 0; // true when parsing a protected region
459
460 for (; sc.More(); sc.Forward()) {
461 if (sc.atLineStart) {
462 if (sc.state == SCE_V_STRING) {
463 // Prevent SCE_V_STRINGEOL from leaking back to previous line
464 sc.SetState(SCE_V_STRING);
465 }
466 if ((MaskActive(sc.state) == SCE_V_PREPROCESSOR) && (!continuationLine)) {
467 sc.SetState(SCE_V_DEFAULT|activitySet);
468 }
469 if (preproc.IsInactive()) {
470 activitySet = activeFlag;
471 sc.SetState(sc.state | activitySet);
472 }
473 }
474
475 if (sc.MatchLineEnd()) {
476 curLine++;
477 lineEndNext = styler.LineEnd(curLine);
478 vlls.Add(curLine, preproc);
479 // Update the line state, so it can be seen by next line
480 styler.SetLineState(curLine, lineState);
481 isEscapedId = false; // EOL terminates an escaped Identifier
482 }
483
484 // Handle line continuation generically.
485 if (sc.ch == '\\') {
486 if (static_cast<Sci_Position>((sc.currentPos+1)) >= lineEndNext) {
487 curLine++;
488 lineEndNext = styler.LineEnd(curLine);
489 vlls.Add(curLine, preproc);
490 // Update the line state, so it can be seen by next line
491 styler.SetLineState(curLine, lineState);
492 sc.Forward();
493 if (sc.ch == '\r' && sc.chNext == '\n') {
494 // Even in UTF-8, \r and \n are separate
495 sc.Forward();
496 }
497 continuationLine = true;
498 sc.Forward();
499 continue;
500 }
501 }
502
503 // for comment keyword
504 if (MaskActive(sc.state) == SCE_V_COMMENT_WORD && !IsAWordChar(sc.ch)) {
505 char s[100];
506 int state = lineState & 0xff;
507 sc.GetCurrent(s, sizeof(s));
508 if (keywords5.InList(s)) {
509 sc.ChangeState(SCE_V_COMMENT_WORD|activitySet);
510 } else {
511 sc.ChangeState(state|activitySet);
512 }
513 sc.SetState(state|activitySet);
514 }
515
516 const bool atLineEndBeforeSwitch = sc.MatchLineEnd();
517
518 // Determine if the current state should terminate.
519 switch (MaskActive(sc.state)) {
520 case SCE_V_OPERATOR:
521 sc.SetState(SCE_V_DEFAULT|activitySet);
522 break;
523 case SCE_V_NUMBER:
524 if (!(IsAWordChar(sc.ch) || (sc.ch == '?'))) {
525 sc.SetState(SCE_V_DEFAULT|activitySet);
526 }
527 break;
528 case SCE_V_IDENTIFIER:
529 if (!isEscapedId &&(!IsAWordChar(sc.ch) || (sc.ch == '.'))) {
530 char s[100];
531 lineState &= 0xff00;
532 sc.GetCurrent(s, sizeof(s));
533 if (options.portStyling && (strcmp(s, "input") == 0)) {
534 lineState = kwInput;
535 sc.ChangeState(SCE_V_INPUT|activitySet);
536 } else if (options.portStyling && (strcmp(s, "output") == 0)) {
537 lineState = kwOutput;
538 sc.ChangeState(SCE_V_OUTPUT|activitySet);
539 } else if (options.portStyling && (strcmp(s, "inout") == 0)) {
540 lineState = kwInout;
541 sc.ChangeState(SCE_V_INOUT|activitySet);
542 } else if (lineState == kwInput) {
543 sc.ChangeState(SCE_V_INPUT|activitySet);
544 } else if (lineState == kwOutput) {
545 sc.ChangeState(SCE_V_OUTPUT|activitySet);
546 } else if (lineState == kwInout) {
547 sc.ChangeState(SCE_V_INOUT|activitySet);
548 } else if (lineState == kwDot) {
549 lineState = kwOther;
550 if (options.portStyling)
551 sc.ChangeState(SCE_V_PORT_CONNECT|activitySet);
552 } else if (keywords.InList(s)) {
553 sc.ChangeState(SCE_V_WORD|activitySet);
554 } else if (keywords2.InList(s)) {
555 sc.ChangeState(SCE_V_WORD2|activitySet);
556 } else if (keywords3.InList(s)) {
557 sc.ChangeState(SCE_V_WORD3|activitySet);
558 } else if (keywords4.InList(s)) {
559 sc.ChangeState(SCE_V_USER|activitySet);
560 } else if (options.allUppercaseDocKeyword && AllUpperCase(s)) {
561 sc.ChangeState(SCE_V_USER|activitySet);
562 }
563 sc.SetState(SCE_V_DEFAULT|activitySet);
564 }
565 break;
566 case SCE_V_PREPROCESSOR:
567 if (!IsAWordChar(sc.ch) || sc.MatchLineEnd()) {
568 sc.SetState(SCE_V_DEFAULT|activitySet);
569 }
570 break;
571 case SCE_V_COMMENT:
572 if (sc.Match('*', '/')) {
573 sc.Forward();
574 sc.ForwardSetState(SCE_V_DEFAULT|activitySet);
575 } else if (IsAWordStart(sc.ch)) {
576 lineState = sc.state | (lineState & 0xff00);
577 sc.SetState(SCE_V_COMMENT_WORD|activitySet);
578 }
579 break;
580 case SCE_V_COMMENTLINE:
581 case SCE_V_COMMENTLINEBANG:
582 if (sc.atLineStart) {
583 sc.SetState(SCE_V_DEFAULT|activitySet);
584 } else if (IsAWordStart(sc.ch)) {
585 lineState = sc.state | (lineState & 0xff00);
586 sc.SetState(SCE_V_COMMENT_WORD|activitySet);
587 }
588 break;
589 case SCE_V_STRING:
590 if (sc.ch == '\\') {
591 if (sc.chNext == '\"' || sc.chNext == '\'' || sc.chNext == '\\') {
592 sc.Forward();
593 }
594 } else if (sc.ch == '\"') {
595 sc.ForwardSetState(SCE_V_DEFAULT|activitySet);
596 } else if (sc.MatchLineEnd()) {
597 sc.ChangeState(SCE_V_STRINGEOL|activitySet);
598 if (sc.Match('\r', '\n'))
599 sc.Forward();
600 sc.ForwardSetState(SCE_V_DEFAULT|activitySet);
601 }
602 break;
603 }
604
605 if (sc.MatchLineEnd() && !atLineEndBeforeSwitch) {
606 // State exit processing consumed characters up to end of line.
607 curLine++;
608 lineEndNext = styler.LineEnd(curLine);
609 vlls.Add(curLine, preproc);
610 // Update the line state, so it can be seen by next line
611 styler.SetLineState(curLine, lineState);
612 isEscapedId = false; // EOL terminates an escaped Identifier
613 }
614
615 // Determine if a new state should be entered.
616 if (MaskActive(sc.state) == SCE_V_DEFAULT) {
617 if (sc.ch == '`') {
618 sc.SetState(SCE_V_PREPROCESSOR|activitySet);
619 // Skip whitespace between ` and preprocessor word
620 do {
621 sc.Forward();
622 } while ((sc.ch == ' ' || sc.ch == '\t') && sc.More());
623 if (sc.MatchLineEnd()) {
624 sc.SetState(SCE_V_DEFAULT|activitySet);
625 styler.SetLineState(curLine, lineState);
626 } else {
627 if (sc.Match("protected")) {
628 isProtected = true;
629 lineState |= kwProtected;
630 styler.SetLineState(curLine, lineState);
631 } else if (sc.Match("endprotected")) {
632 isProtected = false;
633 lineState &= ~kwProtected;
634 styler.SetLineState(curLine, lineState);
635 } else if (!isProtected && options.trackPreprocessor) {
636 if (sc.Match("ifdef") || sc.Match("ifndef")) {
637 bool isIfDef = sc.Match("ifdef");
638 int i = isIfDef ? 5 : 6;
639 std::string restOfLine = GetRestOfLine(styler, sc.currentPos + i + 1, false);
640 bool foundDef = preprocessorDefinitions.find(restOfLine) != preprocessorDefinitions.end();
641 preproc.StartSection(isIfDef == foundDef);
642 } else if (sc.Match("else")) {
643 if (!preproc.CurrentIfTaken()) {
644 preproc.InvertCurrentLevel();
645 activitySet = preproc.IsInactive() ? activeFlag : 0;
646 if (!activitySet) {
647 sc.ChangeState(SCE_V_PREPROCESSOR|activitySet);
648 }
649 } else if (!preproc.IsInactive()) {
650 preproc.InvertCurrentLevel();
651 activitySet = preproc.IsInactive() ? activeFlag : 0;
652 if (!activitySet) {
653 sc.ChangeState(SCE_V_PREPROCESSOR|activitySet);
654 }
655 }
656 } else if (sc.Match("elsif")) {
657 // Ensure only one chosen out of `if .. `elsif .. `elsif .. `else .. `endif
658 if (!preproc.CurrentIfTaken()) {
659 // Similar to `ifdef
660 std::string restOfLine = GetRestOfLine(styler, sc.currentPos + 6, true);
661 bool ifGood = preprocessorDefinitions.find(restOfLine) != preprocessorDefinitions.end();
662 if (ifGood) {
663 preproc.InvertCurrentLevel();
664 activitySet = preproc.IsInactive() ? activeFlag : 0;
665 if (!activitySet)
666 sc.ChangeState(SCE_V_PREPROCESSOR|activitySet);
667 }
668 } else if (!preproc.IsInactive()) {
669 preproc.InvertCurrentLevel();
670 activitySet = preproc.IsInactive() ? activeFlag : 0;
671 if (!activitySet)
672 sc.ChangeState(SCE_V_PREPROCESSOR|activitySet);
673 }
674 } else if (sc.Match("endif")) {
675 preproc.EndSection();
676 activitySet = preproc.IsInactive() ? activeFlag : 0;
677 sc.ChangeState(SCE_V_PREPROCESSOR|activitySet);
678 } else if (sc.Match("define")) {
679 if (options.updatePreprocessor && !preproc.IsInactive()) {
680 std::string restOfLine = GetRestOfLine(styler, sc.currentPos + 6, true);
681 size_t startName = 0;
682 while ((startName < restOfLine.length()) && IsSpaceOrTab(restOfLine[startName]))
683 startName++;
684 size_t endName = startName;
685 while ((endName < restOfLine.length()) && setWord.Contains(static_cast<unsigned char>(restOfLine[endName])))
686 endName++;
687 std::string key = restOfLine.substr(startName, endName-startName);
688 if ((endName < restOfLine.length()) && (restOfLine.at(endName) == '(')) {
689 // Macro
690 size_t endArgs = endName;
691 while ((endArgs < restOfLine.length()) && (restOfLine[endArgs] != ')'))
692 endArgs++;
693 std::string args = restOfLine.substr(endName + 1, endArgs - endName - 1);
694 size_t startValue = endArgs+1;
695 while ((startValue < restOfLine.length()) && IsSpaceOrTab(restOfLine[startValue]))
696 startValue++;
697 std::string value;
698 if (startValue < restOfLine.length())
699 value = restOfLine.substr(startValue);
700 preprocessorDefinitions[key] = SymbolValue(value, args);
701 ppDefineHistory.push_back(PPDefinition(curLine, key, value, false, args));
702 definitionsChanged = true;
703 } else {
704 // Value
705 size_t startValue = endName;
706 while ((startValue < restOfLine.length()) && IsSpaceOrTab(restOfLine[startValue]))
707 startValue++;
708 std::string value = restOfLine.substr(startValue);
709 preprocessorDefinitions[key] = value;
710 ppDefineHistory.push_back(PPDefinition(curLine, key, value));
711 definitionsChanged = true;
712 }
713 }
714 } else if (sc.Match("undefineall")) {
715 if (options.updatePreprocessor && !preproc.IsInactive()) {
716 // remove all preprocessor definitions
717 std::map<std::string, SymbolValue>::iterator itDef;
718 for(itDef = preprocessorDefinitions.begin(); itDef != preprocessorDefinitions.end(); ++itDef) {
719 ppDefineHistory.push_back(PPDefinition(curLine, itDef->first, "", true));
720 }
721 preprocessorDefinitions.clear();
722 definitionsChanged = true;
723 }
724 } else if (sc.Match("undef")) {
725 if (options.updatePreprocessor && !preproc.IsInactive()) {
726 std::string restOfLine = GetRestOfLine(styler, sc.currentPos + 5, true);
727 std::vector<std::string> tokens = Tokenize(restOfLine);
728 std::string key;
729 if (tokens.size() >= 1) {
730 key = tokens[0];
731 preprocessorDefinitions.erase(key);
732 ppDefineHistory.push_back(PPDefinition(curLine, key, "", true));
733 definitionsChanged = true;
734 }
735 }
736 }
737 }
738 }
739 } else if (!isProtected) {
740 if (IsADigit(sc.ch) || (sc.ch == '\'') || (sc.ch == '.' && IsADigit(sc.chNext))) {
741 sc.SetState(SCE_V_NUMBER|activitySet);
742 } else if (IsAWordStart(sc.ch)) {
743 sc.SetState(SCE_V_IDENTIFIER|activitySet);
744 } else if (sc.Match('/', '*')) {
745 sc.SetState(SCE_V_COMMENT|activitySet);
746 sc.Forward(); // Eat the * so it isn't used for the end of the comment
747 } else if (sc.Match('/', '/')) {
748 if (sc.Match("//!")) // Nice to have a different comment style
749 sc.SetState(SCE_V_COMMENTLINEBANG|activitySet);
750 else
751 sc.SetState(SCE_V_COMMENTLINE|activitySet);
752 } else if (sc.ch == '\"') {
753 sc.SetState(SCE_V_STRING|activitySet);
754 } else if (sc.ch == '\\') {
755 // escaped identifier, everything is ok up to whitespace
756 isEscapedId = true;
757 sc.SetState(SCE_V_IDENTIFIER|activitySet);
758 } else if (isoperator(static_cast<char>(sc.ch)) || sc.ch == '@' || sc.ch == '#') {
759 sc.SetState(SCE_V_OPERATOR|activitySet);
760 if (sc.ch == '.') lineState = kwDot;
761 if (sc.ch == ';') lineState = kwOther;
762 }
763 }
764 }
765 if (isEscapedId && isspacechar(sc.ch)) {
766 isEscapedId = false;
767 }
768 }
769 if (definitionsChanged) {
770 styler.ChangeLexerState(startPos, startPos + length);
771 }
772 sc.Complete();
773 }
774
775 static bool IsStreamCommentStyle(int style) {
776 return style == SCE_V_COMMENT;
777 }
778
779 static bool IsCommentLine(Sci_Position line, LexAccessor &styler) {
780 Sci_Position pos = styler.LineStart(line);
781 Sci_Position eolPos = styler.LineStart(line + 1) - 1;
782 for (Sci_Position i = pos; i < eolPos; i++) {
783 char ch = styler[i];
784 char chNext = styler.SafeGetCharAt(i + 1);
785 int style = styler.StyleAt(i);
786 if (ch == '/' && chNext == '/' &&
787 (style == SCE_V_COMMENTLINE || style == SCE_V_COMMENTLINEBANG)) {
788 return true;
789 } else if (!IsASpaceOrTab(ch)) {
790 return false;
791 }
792 }
793 return false;
794 }
795
796 // Store both the current line's fold level and the next lines in the
797 // level store to make it easy to pick up with each increment
798 // and to make it possible to fiddle the current level for "} else {".
799 void SCI_METHOD LexerVerilog::Fold(Sci_PositionU startPos, Sci_Position length, int initStyle, IDocument *pAccess)
800 {
801 LexAccessor styler(pAccess);
802 bool foldAtBrace = 1;
803 bool foldAtParenthese = 1;
804
805 Sci_Position lineCurrent = styler.GetLine(startPos);
806 // Move back one line to be compatible with LexerModule::Fold behavior, fixes problem with foldComment behavior
807 if (lineCurrent > 0) {
808 lineCurrent--;
809 Sci_Position newStartPos = styler.LineStart(lineCurrent);
810 length += startPos - newStartPos;
811 startPos = newStartPos;
812 initStyle = 0;
813 if (startPos > 0) {
814 initStyle = styler.StyleAt(startPos - 1);
815 }
816 }
817 Sci_PositionU endPos = startPos + length;
818 int visibleChars = 0;
819 int levelCurrent = SC_FOLDLEVELBASE;
820 if (lineCurrent > 0)
821 levelCurrent = styler.LevelAt(lineCurrent-1) >> 16;
822 int levelMinCurrent = levelCurrent;
823 int levelNext = levelCurrent;
824 char chNext = styler[startPos];
825 int styleNext = MaskActive(styler.StyleAt(startPos));
826 int style = MaskActive(initStyle);
827
828 // restore fold state (if it exists) for prior line
829 int stateCurrent = 0;
830 std::map<Sci_Position,int>::iterator foldStateIterator = foldState.find(lineCurrent-1);
831 if (foldStateIterator != foldState.end()) {
832 stateCurrent = foldStateIterator->second;
833 }
834
835 // remove all foldState entries after lineCurrent-1
836 foldStateIterator = foldState.upper_bound(lineCurrent-1);
837 if (foldStateIterator != foldState.end()) {
838 foldState.erase(foldStateIterator, foldState.end());
839 }
840
841 for (Sci_PositionU i = startPos; i < endPos; i++) {
842 char ch = chNext;
843 chNext = styler.SafeGetCharAt(i + 1);
844 int stylePrev = style;
845 style = styleNext;
846 styleNext = MaskActive(styler.StyleAt(i + 1));
847 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
848 if (!(stateCurrent & protectedFlag)) {
849 if (options.foldComment && IsStreamCommentStyle(style)) {
850 if (!IsStreamCommentStyle(stylePrev)) {
851 levelNext++;
852 } else if (!IsStreamCommentStyle(styleNext) && !atEOL) {
853 // Comments don't end at end of line and the next character may be unstyled.
854 levelNext--;
855 }
856 }
857 if (options.foldComment && atEOL && IsCommentLine(lineCurrent, styler))
858 {
859 if (!IsCommentLine(lineCurrent - 1, styler)
860 && IsCommentLine(lineCurrent + 1, styler))
861 levelNext++;
862 else if (IsCommentLine(lineCurrent - 1, styler)
863 && !IsCommentLine(lineCurrent+1, styler))
864 levelNext--;
865 }
866 if (options.foldComment && (style == SCE_V_COMMENTLINE)) {
867 if ((ch == '/') && (chNext == '/')) {
868 char chNext2 = styler.SafeGetCharAt(i + 2);
869 if (chNext2 == '{') {
870 levelNext++;
871 } else if (chNext2 == '}') {
872 levelNext--;
873 }
874 }
875 }
876 }
877 if (ch == '`') {
878 Sci_PositionU j = i + 1;
879 while ((j < endPos) && IsASpaceOrTab(styler.SafeGetCharAt(j))) {
880 j++;
881 }
882 if (styler.Match(j, "protected")) {
883 stateCurrent |= protectedFlag;
884 levelNext++;
885 } else if (styler.Match(j, "endprotected")) {
886 stateCurrent &= ~protectedFlag;
887 levelNext--;
888 } else if (!(stateCurrent & protectedFlag) && options.foldPreprocessor && (style == SCE_V_PREPROCESSOR)) {
889 if (styler.Match(j, "if")) {
890 if (options.foldPreprocessorElse) {
891 // Measure the minimum before a begin to allow
892 // folding on "end else begin"
893 if (levelMinCurrent > levelNext) {
894 levelMinCurrent = levelNext;
895 }
896 }
897 levelNext++;
898 } else if (options.foldPreprocessorElse && styler.Match(j, "else")) {
899 levelNext--;
900 if (levelMinCurrent > levelNext) {
901 levelMinCurrent = levelNext;
902 }
903 levelNext++;
904 } else if (options.foldPreprocessorElse && styler.Match(j, "elsif")) {
905 levelNext--;
906 // Measure the minimum before a begin to allow
907 // folding on "end else begin"
908 if (levelMinCurrent > levelNext) {
909 levelMinCurrent = levelNext;
910 }
911 levelNext++;
912 } else if (styler.Match(j, "endif")) {
913 levelNext--;
914 }
915 }
916 }
917 if (style == SCE_V_OPERATOR) {
918 if (foldAtParenthese) {
919 if (ch == '(') {
920 levelNext++;
921 } else if (ch == ')') {
922 levelNext--;
923 }
924 }
925 // semicolons terminate external declarations
926 if (ch == ';') {
927 // extern and pure virtual declarations terminated by semicolon
928 if (stateCurrent & foldExternFlag) {
929 levelNext--;
930 stateCurrent &= ~foldExternFlag;
931 }
932 // wait and disable statements terminated by semicolon
933 if (stateCurrent & foldWaitDisableFlag) {
934 stateCurrent &= ~foldWaitDisableFlag;
935 }
936 // typedef statements terminated by semicolon
937 if (stateCurrent & typedefFlag) {
938 stateCurrent &= ~typedefFlag;
939 }
940 }
941 // wait and disable statements containing '(' will not contain "fork" keyword, special processing is not needed
942 if (ch == '(') {
943 if (stateCurrent & foldWaitDisableFlag) {
944 stateCurrent &= ~foldWaitDisableFlag;
945 }
946 }
947 }
948 if (style == SCE_V_OPERATOR) {
949 if (foldAtBrace) {
950 if (ch == '{') {
951 levelNext++;
952 } else if (ch == '}') {
953 levelNext--;
954 }
955 }
956 }
957 if (style == SCE_V_WORD && stylePrev != SCE_V_WORD) {
958 Sci_PositionU j = i;
959 if (styler.Match(j, "case") ||
960 styler.Match(j, "casex") ||
961 styler.Match(j, "casez") ||
962 styler.Match(j, "covergroup") ||
963 styler.Match(j, "function") ||
964 styler.Match(j, "generate") ||
965 styler.Match(j, "interface") ||
966 styler.Match(j, "package") ||
967 styler.Match(j, "primitive") ||
968 styler.Match(j, "program") ||
969 styler.Match(j, "sequence") ||
970 styler.Match(j, "specify") ||
971 styler.Match(j, "table") ||
972 styler.Match(j, "task") ||
973 (styler.Match(j, "module") && options.foldAtModule)) {
974 levelNext++;
975 } else if (styler.Match(j, "begin")) {
976 // Measure the minimum before a begin to allow
977 // folding on "end else begin"
978 if (levelMinCurrent > levelNext) {
979 levelMinCurrent = levelNext;
980 }
981 levelNext++;
982 } else if (styler.Match(j, "class")) {
983 // class does not introduce a block when used in a typedef statement
984 if (!(stateCurrent & typedefFlag))
985 levelNext++;
986 } else if (styler.Match(j, "fork")) {
987 // fork does not introduce a block when used in a wait or disable statement
988 if (stateCurrent & foldWaitDisableFlag) {
989 stateCurrent &= ~foldWaitDisableFlag;
990 } else
991 levelNext++;
992 } else if (styler.Match(j, "endcase") ||
993 styler.Match(j, "endclass") ||
994 styler.Match(j, "endfunction") ||
995 styler.Match(j, "endgenerate") ||
996 styler.Match(j, "endgroup") ||
997 styler.Match(j, "endinterface") ||
998 styler.Match(j, "endpackage") ||
999 styler.Match(j, "endprimitive") ||
1000 styler.Match(j, "endprogram") ||
1001 styler.Match(j, "endsequence") ||
1002 styler.Match(j, "endspecify") ||
1003 styler.Match(j, "endtable") ||
1004 styler.Match(j, "endtask") ||
1005 styler.Match(j, "join") ||
1006 styler.Match(j, "join_any") ||
1007 styler.Match(j, "join_none") ||
1008 (styler.Match(j, "endmodule") && options.foldAtModule) ||
1009 (styler.Match(j, "end") && !IsAWordChar(styler.SafeGetCharAt(j + 3)))) {
1010 levelNext--;
1011 } else if (styler.Match(j, "extern") ||
1012 styler.Match(j, "pure")) {
1013 // extern and pure virtual functions/tasks are terminated by ';' not endfunction/endtask
1014 stateCurrent |= foldExternFlag;
1015 } else if (styler.Match(j, "disable") ||
1016 styler.Match(j, "wait")) {
1017 // fork does not introduce a block when used in a wait or disable statement
1018 stateCurrent |= foldWaitDisableFlag;
1019 } else if (styler.Match(j, "typedef")) {
1020 stateCurrent |= typedefFlag;
1021 }
1022 }
1023 if (atEOL) {
1024 int levelUse = levelCurrent;
1025 if (options.foldAtElse||options.foldPreprocessorElse) {
1026 levelUse = levelMinCurrent;
1027 }
1028 int lev = levelUse | levelNext << 16;
1029 if (visibleChars == 0 && options.foldCompact)
1030 lev |= SC_FOLDLEVELWHITEFLAG;
1031 if (levelUse < levelNext)
1032 lev |= SC_FOLDLEVELHEADERFLAG;
1033 if (stateCurrent) {
1034 foldState[lineCurrent] = stateCurrent;
1035 }
1036 if (lev != styler.LevelAt(lineCurrent)) {
1037 styler.SetLevel(lineCurrent, lev);
1038 }
1039 lineCurrent++;
1040 levelCurrent = levelNext;
1041 levelMinCurrent = levelCurrent;
1042 visibleChars = 0;
1043 }
1044 if (!isspacechar(ch))
1045 visibleChars++;
1046 }
1047 }
1048
1049 std::vector<std::string> LexerVerilog::Tokenize(const std::string &expr) const {
1050 // Break into tokens
1051 std::vector<std::string> tokens;
1052 const char *cp = expr.c_str();
1053 while (*cp) {
1054 std::string word;
1055 if (setWord.Contains(static_cast<unsigned char>(*cp))) {
1056 // Identifiers and numbers
1057 while (setWord.Contains(static_cast<unsigned char>(*cp))) {
1058 word += *cp;
1059 cp++;
1060 }
1061 } else if (IsSpaceOrTab(*cp)) {
1062 while (IsSpaceOrTab(*cp)) {
1063 cp++;
1064 }
1065 continue;
1066 } else {
1067 // Should handle strings, characters, and comments here
1068 word += *cp;
1069 cp++;
1070 }
1071 tokens.push_back(word);
1072 }
1073 return tokens;
1074 }
1075
1076 static const char * const verilogWordLists[] = {
1077 "Primary keywords and identifiers",
1078 "Secondary keywords and identifiers",
1079 "System Tasks",
1080 "User defined tasks and identifiers",
1081 "Documentation comment keywords",
1082 "Preprocessor definitions",
1083 0,
1084 };
1085
1086 extern const LexerModule lmVerilog(SCLEX_VERILOG, LexerVerilog::LexerFactoryVerilog, "verilog", verilogWordLists);