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 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
|
» Code template file for the Delphi Compiler Generator (DCG), special version for
» a syntax highlighter.
» target language is Delphi
» Note: - the character "" is considered as DCG comment, all text follwing this char
» will not appear in the target,
» - identifiers enclosed in "" and "" are considered as macros which will be
» replaced by corresponding strings defined in the global definition section
» of the according lexer template (*.l file)
unit UCESQLHighlighter;
// This file is machine-generated. Do not modify it manually.
interface
uses
Classes, Graphics, UCEHighlighter;
const
CR = #13;
LF = #10;
// User defined start states and tokens.
%%
type
TUCESQLHighlighter = class(TUCEHighlighter)
private
FCurrentToken: Integer; // type of current token
FInput: PChar; // input string to be tokenized
FCurrentPos: Cardinal; // current lexer position in FInput
FTokenPos: Cardinal; // current token position in FInput
FLookahead: Char; // next char after current position
FCurrentState,
FStartState,
FLineState: Cardinal; // lexer states
FLastChar: Char; // last matched char (#0 if no match)
FPositions: array of Cardinal; // for each rule the last marked position, zeroed when
// rule has already been considered (allocated once)
FMatchStack: array of Cardinal; // stack containing matched rules (growing dynamically)
FMatchCount: Integer; // contains current number of matches
FRejected: Boolean; // current match rejected?
FDoStop: Boolean; // token or eof found?
FEOL: Boolean;
FSystemVariableAttributes,
FUserVariableAttributes,
FCommentWithCOmmandAttributes,
FEmbeddedCommandAttributes,
FStringAttributes,
FNumberAttributes,
FKeyAttributes,
FSymbolAttributes,
FCommentAttributes,
FIdentifierAttributes,
FSpaceAttributes: THighlightAttributes;
procedure RuleToToken(Rule: Integer);
function IsKeyword: Boolean;
function GetNextChar: Char;
function MatchRule(var Rule: Integer): Boolean;
function DoDefault: Boolean;
function GetCurrentChar: Char;
procedure SetAttribute(const Index: Integer; const Value: THighlightAttributes);
procedure HighlightChange(Sender: TObject);
protected
procedure Reset; virtual;
procedure SetMatch(I: Integer);
procedure SetState(Value: Cardinal); virtual;
procedure SetToken(Token: Integer);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function EOL: Boolean; override;
function GetToken: string; override;
function GetTokenInfo: TTokenData; override;
procedure Next; override;
procedure Reject;
function GetAttribute(Index: Integer): THighlightAttributes; override;
function GetAttributeCount: Integer; override;
function GetIdentChars: TIdentChars; override;
function GetLanguageName: string; override;
function GetRange: Integer; override;
function HasMoreTokens: Boolean; override;
procedure ResetRange; override;
procedure SetRange(Value: Integer); override;
procedure SetLine(const NewValue: string); override;
property CurrentChar: Char read GetCurrentChar;
property NextChar: Char read GetNextChar;
property Lookahead: Char read FLookahead;
property State: Cardinal read FCurrentState write SetState; // read to get current lexer state
// write to set start state for next run
property TokenPosition: Cardinal read FTokenPos;
published
property CommentAttributes: THighlightAttributes index 1 read FCommentAttributes write SetAttribute;
property CommentWithCommandAttributes: THighlightAttributes index 8 read FCommentWithCommandAttributes write SetAttribute;
property EmbeddedCommandAttributes: THighlightAttributes index 7 read FEmbeddedCommandAttributes write SetAttribute;
property IdentifierAttributes: THighlightAttributes index 2 read FIdentifierAttributes write SetAttribute;
property KeyAttributes: THighlightAttributes index 3 read FKeyAttributes write SetAttribute;
property NumberAttributes: THighlightAttributes index 4 read FNumberAttributes write SetAttribute;
property SpaceAttributes: THighlightAttributes index 5 read FSpaceAttributes write SetAttribute;
property StringAttributes: THighlightAttributes index 6 read FStringAttributes write SetAttribute;
property SymbolAttributes: THighlightAttributes index 7 read FSymbolAttributes write SetAttribute;
property SystemVariableAttributes: THighlightAttributes index 9 read FSystemVariableAttributes write SetAttribute;
property UserVariableAttributes: THighlightAttributes index 10 read FuserVariableAttributes write SetAttribute;
end;
//----------------------------------------------------------------------------------------------------------------------
implementation
uses
SysUtils;
// DFA table
%%
//----------------------------------------------------------------------------------------------------------------------
constructor TUCESQLHighlighter.Create(AOwner: TComponent);
begin
inherited;
// allocate positions array (length is highest possible mark position)
SetLength(FPositions, MarksHigh[StateCount - 1] + 1);
FCommentAttributes := THighlightAttributes.Create('comment');
FCommentAttributes.Style := [fsItalic];
FIdentifierAttributes := THighlightAttributes.Create('identifier');
FKeyAttributes := THighlightAttributes.Create('reserved word');
FKeyAttributes.Style := [fsBold];
FNumberAttributes := THighlightAttributes.Create('number');
FSpaceAttributes := THighlightAttributes.Create('space');
FStringAttributes := THighlightAttributes.Create('String');
FSymbolAttributes := THighlightAttributes.Create('symbol');
FCommentWithCommandAttributes := THighlightAttributes.Create('comment with command');
FEmbeddedCommandAttributes := THighlightAttributes.Create('embedded command');
FSystemVariableAttributes := THighlightAttributes.Create('system variable');
FUserVariableAttributes := THighlightAttributes.Create('user variable');
FCommentAttributes.Onchange := HighlightChange;
FIdentifierAttributes.Onchange := HighlightChange;
FKeyAttributes.Onchange := HighlightChange;
FNumberAttributes.Onchange := HighlightChange;
FSpaceAttributes.Onchange := HighlightChange;
FStringAttributes.Onchange := HighlightChange;
FSymbolAttributes.Onchange := HighlightChange;
FCommentWithCommandAttributes.Onchange := HighlightChange;
FEmbeddedCommandAttributes.Onchange := HighlightChange;
FSystemVariableAttributes.Onchange := HighlightChange;
FUserVariableAttributes.Onchange := HighlightChange;
DefaultFilter := 'SQL script files (*.sql)|*.sql';
end;
//----------------------------------------------------------------------------------------------------------------------
destructor TUCESQLHighlighter.Destroy;
begin
FPositions := nil;
FMatchStack := nil;
FCommentAttributes.Free;
FIdentifierAttributes.Free;
FKeyAttributes.Free;
FNumberAttributes.Free;
FSpaceAttributes.Free;
FStringAttributes.Free;
FSymbolAttributes.Free;
FCommentWithCommandAttributes.Free;
FEmbeddedCommandAttributes.Free;
FSystemVariableAttributes.Free;
FUserVariableAttributes.Free;
inherited;
end;
//----------------------------------------------------------------------------------------------------------------------
function TUCESQLHighlighter.IsKeyword: Boolean;
// checks whether the current token is a keyword,
// if so then the current token type is set accordingly
var
Word: string;
begin
SetString(Word, FInput + FTokenPos, FCurrentPos - FTokenPos);
FCurrentToken := GetKeywordToken(Word);
if FCurrentToken > 0 then
begin
Result := True;
FDoStop := True;
end
else
Result := False;
end;
//----------------------------------------------------------------------------------------------------------------------
function TUCESQLHighlighter.EOL: Boolean;
begin
Result := (FCurrentToken = -1) or FEOL;
end;
//----------------------------------------------------------------------------------------------------------------------
function TUCESQLHighlighter.GetToken: string;
var
Len: Cardinal;
begin
Len := FCurrentPos - FTokenPos;
SetString(Result, FInput + FTokenPos, Len);
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TUCESQLHighlighter.Reset;
begin
FTokenPos := 0;
FCurrentPos := 0;
FCurrentToken := 0;
FLineState := 1;
FLastChar := #0;
FMatchCount := 0;
FEOL := False;
ResetRange;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TUCESQLHighlighter.SetToken(Token: Integer);
begin
FCurrentToken := Token;
FDoStop := True;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TUCESQLHighlighter.RuleToToken(Rule: Integer);
begin
case Rule of
%%
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function TUCESQLHighlighter.MatchRule(var Rule: Integer): Boolean;
// finds the last match and the corresponding marked position and adjusts
// the matched string accordingly;
// returns:
// - True if a rule has been matched, False otherwise
// - Rule: the number of the matched rule
begin
FRejected := False;
while (FMatchCount > 0) and (FPositions[FMatchStack[FMatchCount - 1]] = 0) do
Dec(FMatchCount);
if FMatchCount > 0 then
begin
Rule := FMatchStack[FMatchCount - 1];
FCurrentPos := FTokenPos + FPositions[Rule];
FPositions[Rule] := 0;
if (FCurrentPos - FTokenPos) > 0 then
FLastChar := FInput[FCurrentPos]
else
FLastChar := #0;
Result := True;
end
else
begin
// return position to starting point
FCurrentPos := FTokenPos;
FLastChar := #0;
Result := False;
end
end;
//----------------------------------------------------------------------------------------------------------------------
function TUCESQLHighlighter.DoDefault: Boolean;
begin
FRejected := False;
if NextChar <> #0 then
Result := True
else
begin
FLineState := 1;
Result := False;
end;
if not FEOL then
FLastChar := FInput[FCurrentPos];
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TUCESQLHighlighter.Next;
// scans for the next token and determines its kind (FCurrentToken and FTokenPos
// are set appropriately)
var
I: Integer;
Len: Cardinal;
Matched: Boolean;
begin
FDoStop := False;
repeat
// initialize run
FTokenPos := FCurrentPos;
if FLastChar <> #0 then
if FLastChar = LF then
FLineState := 1
else
FLineState := 0;
FCurrentState := FStartState + FLineState;
repeat // character scan loop
// mark positions and matches
Len := FCurrentPos - FTokenPos;
for I := MarksLow[FCurrentState] to MarksHigh[FCurrentState] do
FPositions[MarkPositionTable[I]] := Len;
for I := MatchesHigh[FCurrentState] downto MatchesLow[FCurrentState] do
SetMatch(MatchTable[I]);
if TransitionsLow[FCurrentState] > TransitionsHigh[FCurrentState] then
Break; // dead state, do action
// determine action
I := TransitionsLow[FCurrentState];
while (I <= TransitionsHigh[FCurrentState]) and not (FInput[FCurrentPos] in TransitionTable[I].CharClass) do
Inc(I);
if I > TransitionsHigh[FCurrentState] then
Break; // no transition on current char in this state
// get next character
NextChar;
// switch to new state
FCurrentState := TransitionTable[I].NextState;
until False;
repeat
Matched := MatchRule(I);
if Matched then
RuleToToken(I)
else
Break;
// FRejected can be set in RuleToToken by calling Reject
until Matched and not FRejected;
if not (Matched or DoDefault) then
begin
FStartState := Normal;
FLineState := 1;
FLastChar := #0;
SetToken(0);
end;
until FDoStop;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TUCESQLHighlighter.SetLine(const NewValue: string);
begin
FInput := PChar(NewValue);
Reset;
end;
//----------------------------------------------------------------------------------------------------------------------
function TUCESQLHighlighter.GetNextChar: Char;
begin
if not FEOL and (FInput[FCurrentPos] = #0) then
FEOL := True;
Inc(FCurrentPos);
if not FEOL then
begin
FLookahead := FInput[FCurrentPos + 1];
Result := FInput[FCurrentPos];
end
else
Result := #0;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TUCESQLHighlighter.SetState(Value: Cardinal);
begin
FStartState := Value;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TUCESQLHighlighter.SetMatch(I: Integer);
begin
if FMatchCount >= Length(FMatchStack) then
SetLength(FMatchStack, FMatchCount + 1);
FMatchStack[FMatchCount] := I;
Inc(FMatchCount);
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TUCESQLHighlighter.Reject;
begin
FRejected := True;
end;
//----------------------------------------------------------------------------------------------------------------------
function TUCESQLHighlighter.GetTokenInfo: TTokenData;
// Returns token string, token position relative to entire input string, and token length in one call.
begin
FillChar(Result, SizeOf(Result), 0);
with Result do
begin
if FEOL then
begin
Token := StrEnd(FInput);
Length := Token - FInput - Integer(FTokenPos);
end
else
begin
Token := FInput + FTokenPos;
Length := FCurrentPos - FTokenPos;
end;
TokenType := FCurrentToken;
Position := FTokenPos;
case FCurrentToken of
SLCOMMENT,
MLCOMMENT:
begin
Background := ColorToRGB(FCommentAttributes.Background);
Foreground := ColorToRGB(FCommentAttributes.Foreground);
Style := FCommentAttributes.Style;
end;
IDENTIFIER:
begin
Background := ColorToRGB(FIdentifierAttributes.Background);
Foreground := ColorToRGB(FIdentifierAttributes.Foreground);
Style := FIdentifierAttributes.Style;
end;
KEYWORD:
begin
Background := ColorToRGB(FKeyAttributes.Background);
Foreground := ColorToRGB(FKeyAttributes.Foreground);
Style := FKeyAttributes.Style;
end;
FLOATNUMBER,
INTEGERNUMBER:
begin
Background := ColorToRGB(FNumberAttributes.Background);
Foreground := ColorToRGB(FNumberAttributes.Foreground);
Style := FNumberAttributes.Style;
end;
WHITESPACE:
begin
Background := ColorToRGB(FSpaceAttributes.Background);
Foreground := ColorToRGB(FSpaceAttributes.Foreground);
Style := FSpaceAttributes.Style;
end;
STRINGCONSTANT:
begin
Background := ColorToRGB(FStringAttributes.Background);
Foreground := ColorToRGB(FStringAttributes.Foreground);
Style := FStringAttributes.Style;
end;
SYMBOL:
begin
Background := ColorToRGB(FSymbolAttributes.Background);
Foreground := ColorToRGB(FSymbolAttributes.Foreground);
Style := FSymbolAttributes.Style;
end;
COMMENT_WITH_COMMAND:
begin
Background := ColorToRGB(FCommentWithCommandAttributes.Background);
Foreground := ColorToRGB(FCommentWithCommandAttributes.Foreground);
Style := FEmbeddedCommandAttributes.Style;
end;
EMBEDDED_COMMAND:
begin
Background := ColorToRGB(FEmbeddedCommandAttributes.Background);
Foreground := ColorToRGB(FEmbeddedCommandAttributes.Foreground);
Style := FEmbeddedCommandAttributes.Style;
end;
SYSTEM_VARIABLE:
begin
Background := ColorToRGB(FSystemVariableAttributes.Background);
Foreground := ColorToRGB(FSystemVariableAttributes.Foreground);
Style := FSystemVariableAttributes.Style;
end;
USER_VARIABLE:
begin
Background := ColorToRGB(FUserVariableAttributes.Background);
Foreground := ColorToRGB(FUserVariableAttributes.Foreground);
Style := FUserVariableAttributes.Style;
end;
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function TUCESQLHighlighter.GetCurrentChar: Char;
begin
Result := FInput[FCurrentPos];
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TUCESQLHighlighter.HighlightChange(Sender: TObject);
begin
WindowList.Invalidate;
end;
//----------------------------------------------------------------------------------------------------------------------
function TUCESQLHighlighter.GetIdentChars: TIdentChars;
begin
Result := ['_', '0'..'9', 'a'..'z', 'A'..'Z'];
end;
//----------------------------------------------------------------------------------------------------------------------
function TUCESQLHighlighter.GetAttributeCount: Integer;
begin
Result := 7;
end;
//----------------------------------------------------------------------------------------------------------------------
function TUCESQLHighlighter.GetAttribute(Index: Integer): THighlightAttributes;
begin
case Index of
0:
Result := FCommentAttributes;
1:
Result := FIdentifierAttributes;
2:
Result := FNumberAttributes;
3:
Result := FKeyAttributes;
4:
Result := FSpaceAttributes;
5:
Result := FStringAttributes;
6:
Result := FSymbolAttributes;
7:
Result := FEmbeddedCommandAttributes;
8:
Result := FCommentWithCommandAttributes;
9:
Result := FSystemVariableAttributes;
10:
Result := FUserVariableAttributes;
else
Result := nil;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function TUCESQLHighlighter.GetLanguageName: string;
begin
Result := 'SQL';
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TUCESQLHighlighter.SetAttribute(const Index: Integer; const Value: THighlightAttributes);
begin
case Index of
0:
FCommentAttributes.Assign(Value);
1:
FIdentifierAttributes.Assign(Value);
2:
FNumberAttributes.Assign(Value);
3:
FKeyAttributes.Assign(Value);
4:
FSpaceAttributes.Assign(Value);
5:
FStringAttributes.Assign(Value);
6:
FSymbolAttributes.Assign(Value);
7:
FEmbeddedCommandAttributes.Assign(Value);
8:
FCommentWithCommandAttributes.Assign(Value);
9:
FSystemVariableAttributes.Assign(Value);
10:
FUserVariableAttributes.Assign(Value);
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function TUCESQLHighlighter.GetRange: Integer;
begin
Result := FStartState;
end;
//----------------------------------------------------------------------------------------------------------------------
function TUCESQLHighlighter.HasMoreTokens: Boolean;
begin
Result := FCurrentToken > -1;
end;
//--------------------------------------------------------------------------------
procedure TUCESQLHighlighter.SetRange(Value: Integer);
begin
FStartState := Value;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TUCESQLHighlighter.ResetRange;
begin
FStartState := Normal;
FCurrentState := Normal;
end;
//----------------------------------------------------------------------------------------------------------------------
end.
|