LexVerilog.cpp 35 KB

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