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