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 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
|
{-------------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: SynHighlighterXML.pas, released 2000-11-20.
The Initial Author of this file is Jeff Rafter.
All Rights Reserved.
Contributors to the SynEdit and mwEdit projects are listed in the
Contributors.txt file.
Alternatively, the contents of this file may be used under the terms of the
GNU General Public License Version 2 or later (the "GPL"), in which case
the provisions of the GPL are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms
of the GPL and not to allow others to use your version of this file
under the MPL, indicate your decision by deleting the provisions above and
replace them with the notice and other provisions required by the GPL.
If you do not delete the provisions above, a recipient may use your version
of this file under either the MPL or the GPL.
$Id: synhighlighterxml.pas 50780 2015-12-13 20:38:06Z martin $
You may retrieve the latest version of this file at the SynEdit home page,
located at http://SynEdit.SourceForge.net
History:
-------------------------------------------------------------------------------
2000-11-30 Removed mHashTable and MakeIdentTable per Michael Hieke
Known Issues:
- Nothing is really constrained (properly) to valid name chars
- Entity Refs are not constrained to valid name chars
- Support for "Combining Chars and Extender Chars" in names are lacking
- The internal DTD is not parsed (and not handled correctly)
-------------------------------------------------------------------------------}
{
@abstract(Provides an XML highlighter for SynEdit)
@author(Jeff Rafter-- Phil 4:13, based on SynHighlighterHTML by Hideo Koiso)
@created(2000-11-17)
@lastmod(2001-03-12)
The SynHighlighterXML unit provides SynEdit with an XML highlighter.
}
unit SynHighlighterXML;
interface
{$I SynEdit.inc}
uses
Classes, Graphics, SynEditTypes, SynEditHighlighter,
SynEditHighlighterFoldBase, SynEditHighlighterXMLBase;
type
TtkTokenKind = (tkAposAttrValue, tkAposEntityRef, tkAttribute, tkCDATA,
tkComment, tkElement, tkEntityRef, tkEqual, tkNull, tkProcessingInstruction,
tkQuoteAttrValue, tkQuoteEntityRef, tkSpace, tkSymbol, tkText,
//
tknsAposAttrValue, tknsAposEntityRef, tknsAttribute, tknsEqual,
tknsQuoteAttrValue, tknsQuoteEntityRef,
//These are unused at the moment
tkDocType
{tkDocTypeAposAttrValue, tkDocTypeAposEntityRef, tkDocTypeAttribute,
tkDocTypeElement, tkDocTypeEqual tkDocTypeQuoteAttrValue,
tkDocTypeQuoteEntityRef}
);
TRangeState = (rsAposAttrValue, rsAPosEntityRef, rsAttribute, rsCDATA,
rsComment, rsElement, rsCloseElement, rsOpenElement, rsEntityRef, rsEqual, rsProcessingInstruction,
rsQuoteAttrValue, rsQuoteEntityRef, rsText,
//
rsnsAposAttrValue, rsnsAPosEntityRef, rsnsEqual, rsnsQuoteAttrValue,
rsnsQuoteEntityRef,
//These are unused at the moment
rsDocType, rsDocTypeSquareBraces //ek 2001-11-11
{rsDocTypeAposAttrValue, rsDocTypeAposEntityRef, rsDocTypeAttribute,
rsDocTypeElement, rsDocTypeEqual, rsDocTypeQuoteAttrValue,
rsDocTypeQuoteEntityRef}
);
TXmlCodeFoldBlockType = (
cfbtXmlNode, // <foo>...</node>
cfbtXmlComment, // <!-- -->
cfbtXmlCData, // <![CDATA[ ]]>
cfbtXmlDocType, // <!DOCTYPE
cfbtXmlProcess, // <?
// internal types / not configurable
cfbtXmlNone
);
type
TProcTableProc = procedure of object;
{ TSynXMLSyn }
TSynXMLSyn = class(TSynCustomXmlHighlighter)
private
fRange: TRangeState;
fLine: PChar;
Run: Longint;
fTokenPos: Integer;
fTokenID: TtkTokenKind;
fLineNumber: Integer;
fElementAttri: TSynHighlighterAttributes;
fSpaceAttri: TSynHighlighterAttributes;
fTextAttri: TSynHighlighterAttributes;
fEntityRefAttri: TSynHighlighterAttributes;
fProcessingInstructionAttri: TSynHighlighterAttributes;
fCDATAAttri: TSynHighlighterAttributes;
fCommentAttri: TSynHighlighterAttributes;
fDocTypeAttri: TSynHighlighterAttributes;
fAttributeAttri: TSynHighlighterAttributes;
fnsAttributeAttri: TSynHighlighterAttributes;
fAttributeValueAttri: TSynHighlighterAttributes;
fnsAttributeValueAttri: TSynHighlighterAttributes;
fSymbolAttri: TSynHighlighterAttributes;
fProcTable: array[#0..#255] of TProcTableProc;
FWantBracesParsed: Boolean;
procedure NullProc;
procedure CarriageReturnProc;
procedure LineFeedProc;
procedure SpaceProc;
procedure LessThanProc;
procedure GreaterThanProc;
procedure CommentProc;
procedure ProcessingInstructionProc;
procedure DocTypeProc;
procedure CDATAProc;
procedure TextProc;
procedure ElementProc;
procedure AttributeProc;
procedure QAttributeValueProc;
procedure AAttributeValueProc;
procedure EqualProc;
procedure IdentProc;
procedure MakeMethodTables;
function NextTokenIs(T: String): Boolean;
procedure EntityRefProc;
procedure QEntityRefProc;
procedure AEntityRefProc;
protected
function GetIdentChars: TSynIdentChars; override;
function GetSampleSource : String; override;
protected
// folding
procedure CreateRootCodeFoldBlock; override;
function StartXmlCodeFoldBlock(ABlockType: TXmlCodeFoldBlockType): TSynCustomCodeFoldBlock;
function StartXmlNodeCodeFoldBlock(ABlockType: TXmlCodeFoldBlockType;
OpenPos: Integer; AName: String): TSynCustomCodeFoldBlock;
procedure EndXmlNodeCodeFoldBlock(ClosePos: Integer = -1; AName: String = '');
function TopXmlCodeFoldBlockType(DownIndex: Integer = 0): TXmlCodeFoldBlockType;
function GetFoldConfigInstance(Index: Integer): TSynCustomFoldConfig; override;
function GetFoldConfigCount: Integer; override;
function GetFoldConfigInternalCount: Integer; override;
public
class function GetLanguageName: string; override;
public
constructor Create(AOwner: TComponent); override;
function GetDefaultAttribute(Index: integer): TSynHighlighterAttributes;
override;
function GetEol: Boolean; override;
function GetRange: Pointer; override;
function GetTokenID: TtkTokenKind;
procedure SetLine(const NewValue: string; LineNumber:Integer); override;
function GetToken: string; override;
procedure GetTokenEx(out TokenStart: PChar; out TokenLength: integer); override;
function GetTokenAttribute: TSynHighlighterAttributes; override;
function GetTokenKind: integer; override;
function GetTokenPos: Integer; override;
procedure Next; override;
procedure SetRange(Value: Pointer); override;
procedure ReSetRange; override;
property IdentChars;
published
property ElementAttri: TSynHighlighterAttributes read fElementAttri
write fElementAttri;
property AttributeAttri: TSynHighlighterAttributes read fAttributeAttri
write fAttributeAttri;
property NamespaceAttributeAttri: TSynHighlighterAttributes
read fnsAttributeAttri write fnsAttributeAttri;
property AttributeValueAttri: TSynHighlighterAttributes
read fAttributeValueAttri write fAttributeValueAttri;
property NamespaceAttributeValueAttri: TSynHighlighterAttributes
read fnsAttributeValueAttri write fnsAttributeValueAttri;
property TextAttri: TSynHighlighterAttributes read fTextAttri
write fTextAttri;
property CDATAAttri: TSynHighlighterAttributes read fCDATAAttri
write fCDATAAttri;
property EntityRefAttri: TSynHighlighterAttributes read fEntityRefAttri
write fEntityRefAttri;
property ProcessingInstructionAttri: TSynHighlighterAttributes
read fProcessingInstructionAttri write fProcessingInstructionAttri;
property CommentAttri: TSynHighlighterAttributes read fCommentAttri
write fCommentAttri;
property DocTypeAttri: TSynHighlighterAttributes read fDocTypeAttri
write fDocTypeAttri;
property SpaceAttri: TSynHighlighterAttributes read fSpaceAttri
write fSpaceAttri;
property SymbolAttri: TSynHighlighterAttributes read fSymbolAttri
write fSymbolAttri;
property WantBracesParsed : Boolean read FWantBracesParsed
write FWantBracesParsed default True;
end;
implementation
uses
SynEditStrConst;
const
NameChars : set of char = ['0'..'9', 'a'..'z', 'A'..'Z', '_', '.', ':', '-'];
constructor TSynXMLSyn.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
fElementAttri:= TSynHighlighterAttributes.Create(@SYNS_AttrElementName, SYNS_XML_AttrElementName);
fTextAttri:= TSynHighlighterAttributes.Create(@SYNS_AttrText, SYNS_XML_AttrText);
fSpaceAttri:= TSynHighlighterAttributes.Create(@SYNS_AttrWhitespace, SYNS_XML_AttrWhitespace);
fEntityRefAttri:= TSynHighlighterAttributes.Create(@SYNS_AttrEntityReference, SYNS_XML_AttrEntityReference);
fProcessingInstructionAttri:= TSynHighlighterAttributes.Create(@SYNS_AttrProcessingInstr, SYNS_XML_AttrProcessingInstr);
fCDATAAttri:= TSynHighlighterAttributes.Create(@SYNS_AttrCDATASection, SYNS_XML_AttrCDATASection);
fCommentAttri:= TSynHighlighterAttributes.Create(@SYNS_AttrComment, SYNS_XML_AttrComment);
fDocTypeAttri:= TSynHighlighterAttributes.Create(@SYNS_AttrDOCTYPESection, SYNS_XML_AttrDOCTYPESection);
fAttributeAttri:= TSynHighlighterAttributes.Create(@SYNS_AttrAttributeName, SYNS_XML_AttrAttributeName);
fnsAttributeAttri:= TSynHighlighterAttributes.Create(@SYNS_AttrNamespaceAttrName, SYNS_XML_AttrNamespaceAttrName);
fAttributeValueAttri:= TSynHighlighterAttributes.Create(@SYNS_AttrAttributeValue, SYNS_XML_AttrAttributeValue);
fnsAttributeValueAttri:= TSynHighlighterAttributes.Create(@SYNS_AttrNamespaceAttrValue, SYNS_XML_AttrNamespaceAttrValue);
fSymbolAttri:= TSynHighlighterAttributes.Create(@SYNS_AttrSymbol, SYNS_XML_AttrSymbol);
fElementAttri.Foreground:= clMaroon;
fElementAttri.Style:= [fsBold];
fDocTypeAttri.Foreground:= clblue;
fDocTypeAttri.Style:= [fsItalic];
fCDATAAttri.Foreground:= clOlive;
fCDATAAttri.Style:= [fsItalic];
fEntityRefAttri.Foreground:= clblue;
fEntityRefAttri.Style:= [fsbold];
fProcessingInstructionAttri.Foreground:= clblue;
fProcessingInstructionAttri.Style:= [];
fTextAttri.Foreground:= clBlack;
fTextAttri.Style:= [fsBold];
fAttributeAttri.Foreground:= clMaroon;
fAttributeAttri.Style:= [];
fnsAttributeAttri.Foreground:= clRed;
fnsAttributeAttri.Style:= [];
fAttributeValueAttri.Foreground:= clNavy;
fAttributeValueAttri.Style:= [fsBold];
fnsAttributeValueAttri.Foreground:= clRed;
fnsAttributeValueAttri.Style:= [fsBold];
fCommentAttri.Background:= clSilver;
fCommentAttri.Foreground:= clGray;
fCommentAttri.Style:= [fsbold, fsItalic];
fSymbolAttri.Foreground:= clblue;
fSymbolAttri.Style:= [];
AddAttribute(fSymbolAttri);
AddAttribute(fProcessingInstructionAttri);
AddAttribute(fDocTypeAttri);
AddAttribute(fCommentAttri);
AddAttribute(fElementAttri);
AddAttribute(fAttributeAttri);
AddAttribute(fnsAttributeAttri);
AddAttribute(fAttributeValueAttri);
AddAttribute(fnsAttributeValueAttri);
AddAttribute(fEntityRefAttri);
AddAttribute(fCDATAAttri);
AddAttribute(fSpaceAttri);
AddAttribute(fTextAttri);
SetAttributesOnChange(@DefHighlightChange);
MakeMethodTables;
fRange := rsText;
fDefaultFilter := SYNS_FilterXML;
end;
procedure TSynXMLSyn.MakeMethodTables;
var
i: Char;
begin
for i:= #0 To #255 do begin
case i of
#0:
begin
fProcTable[i] := @NullProc;
end;
#10:
begin
fProcTable[i] := @LineFeedProc;
end;
#13:
begin
fProcTable[i] := @CarriageReturnProc;
end;
#1..#9, #11, #12, #14..#32:
begin
fProcTable[i] := @SpaceProc;
end;
'<':
begin
fProcTable[i] := @LessThanProc;
end;
'>':
begin
fProcTable[i] := @GreaterThanProc;
end;
else
fProcTable[i] := @IdentProc;
end;
end;
end;
procedure TSynXMLSyn.SetLine(const NewValue: string;
LineNumber:Integer);
begin
inherited;
fLine := PChar(NewValue);
Run := 0;
fLineNumber := LineNumber;
Next;
end;
procedure TSynXMLSyn.NullProc;
begin
fTokenID := tkNull;
end;
procedure TSynXMLSyn.CarriageReturnProc;
begin
fTokenID := tkSpace;
Inc(Run);
if fLine[Run] = #10 then Inc(Run);
end;
procedure TSynXMLSyn.LineFeedProc;
begin
fTokenID := tkSpace;
Inc(Run);
end;
procedure TSynXMLSyn.SpaceProc;
begin
Inc(Run);
fTokenID := tkSpace;
while fLine[Run] <= #32 do begin
if fLine[Run] in [#0, #9, #10, #13] then break;
Inc(Run);
end;
end;
procedure TSynXMLSyn.LessThanProc;
begin
Inc(Run);
if (fLine[Run] = '/') then begin
Inc(Run);
fTokenID := tkSymbol;
fRange := rsCloseElement;
exit;
end;
if (fLine[Run] = '!') then
begin
if NextTokenIs('--') then begin
fTokenID := tkSymbol;
fRange := rsComment;
StartXmlCodeFoldBlock(cfbtXmlComment);
Inc(Run, 3);
end else if NextTokenIs('DOCTYPE') then begin
fTokenID := tkDocType;
fRange := rsDocType;
StartXmlCodeFoldBlock(cfbtXmlDocType);
Inc(Run, 7);
end else if NextTokenIs('[CDATA[') then begin
fTokenID := tkCDATA;
fRange := rsCDATA;
StartXmlCodeFoldBlock(cfbtXmlCData);
Inc(Run, 7);
end else begin
fTokenID := tkSymbol;
fRange := rsElement;
Inc(Run);
end;
end else if fLine[Run]= '?' then begin
fTokenID := tkProcessingInstruction;
fRange := rsProcessingInstruction;
StartXmlCodeFoldBlock(cfbtXmlProcess);
Inc(Run);
end else begin
fTokenID := tkSymbol;
fRange := rsOpenElement;
end;
end;
procedure TSynXMLSyn.GreaterThanProc;
begin
if (Run > 0) and (fLine[Run - 1] = '/') then
if TopXmlCodeFoldBlockType = cfbtXmlNode then
EndXmlNodeCodeFoldBlock;
fTokenId := tkSymbol;
fRange:= rsText;
Inc(Run);
end;
procedure TSynXMLSyn.CommentProc;
begin
if (fLine[Run] = '-') and (fLine[Run + 1] = '-') and
(fLine[Run + 2] = '>')
then begin
fTokenID := tkSymbol;
fRange:= rsText;
Inc(Run, 3);
if TopXmlCodeFoldBlockType = cfbtXmlComment then
EndXmlCodeFoldBlock;
Exit;
end;
fTokenID := tkComment;
if (fLine[Run] In [#0, #10, #13]) then begin
fProcTable[fLine[Run]]();
Exit;
end;
while not (fLine[Run] in [#0, #10, #13]) do begin
if (fLine[Run] = '-') and (fLine[Run + 1] = '-') and (fLine[Run + 2] = '>')
then begin
fRange := rsComment;
break;
end;
Inc(Run);
end;
end;
procedure TSynXMLSyn.ProcessingInstructionProc;
begin
fTokenID := tkProcessingInstruction;
if (fLine[Run] In [#0, #10, #13]) then begin
fProcTable[fLine[Run]]();
Exit;
end;
while not (fLine[Run] in [#0, #10, #13]) do begin
if (fLine[Run] = '>') and (fLine[Run - 1] = '?')
then begin
fRange := rsText;
Inc(Run);
if TopXmlCodeFoldBlockType = cfbtXmlProcess then
EndXmlCodeFoldBlock;
break;
end;
Inc(Run);
end;
end;
procedure TSynXMLSyn.DocTypeProc; //ek 2001-11-11
begin
fTokenID := tkDocType;
if (fLine[Run] In [#0, #10, #13]) then begin
fProcTable[fLine[Run]]();
Exit;
end;
case fRange of
rsDocType:
begin
while not (fLine[Run] in [#0, #10, #13]) do
begin
case fLine[Run] of
'[': begin
while True do
begin
inc(Run);
case fLine[Run] of
']':
begin
Inc(Run);
Exit;
end;
#0, #10, #13:
begin
fRange:=rsDocTypeSquareBraces;
Exit;
end;
end;
end;
end;
'>': begin
fRange := rsAttribute;
if TopXmlCodeFoldBlockType = cfbtXmlDocType then
EndXmlCodeFoldBlock;
Inc(Run);
Break;
end;
end;
inc(Run);
end;
end;
rsDocTypeSquareBraces:
begin
while not (fLine[Run] in [#0, #10, #13]) do
begin
if (fLine[Run]=']') then
begin
fRange := rsDocType;
Inc(Run);
Exit;
end;
inc(Run);
end;
end;
end;
end;
procedure TSynXMLSyn.CDATAProc;
begin
fTokenID := tkCDATA;
if (fLine[Run] In [#0, #10, #13]) then
begin
fProcTable[fLine[Run]]();
Exit;
end;
while not (fLine[Run] in [#0, #10, #13]) do
begin
if (Run >= 2) and (fLine[Run] = '>') and (fLine[Run - 1] = ']') and
(fLine[Run - 2] = ']')
then begin
fRange := rsText;
Inc(Run);
if TopXmlCodeFoldBlockType = cfbtXmlCData then
EndXmlCodeFoldBlock;
break;
end;
Inc(Run);
end;
end;
procedure TSynXMLSyn.ElementProc;
var
NameStart: LongInt;
begin
if fLine[Run] = '/' then
Inc(Run);
NameStart := Run;
while (fLine[Run] in NameChars) do Inc(Run);
if fRange = rsOpenElement then
StartXmlNodeCodeFoldBlock(cfbtXmlNode, NameStart, Copy(fLine, NameStart + 1, Run - NameStart));
if fRange = rsCloseElement then
EndXmlNodeCodeFoldBlock(NameStart, Copy(fLine, NameStart + 1, Run - NameStart)); // TODO: defer until ">" reached
fRange := rsAttribute;
fTokenID := tkElement;
end;
procedure TSynXMLSyn.AttributeProc;
begin
//Check if we are starting on a closing quote
if (fLine[Run] in [#34, #39]) then
begin
fTokenID := tkSymbol;
fRange := rsAttribute;
Inc(Run);
Exit;
end;
//Read the name
while (fLine[Run] in NameChars) do Inc(Run);
//Check if this is an xmlns: attribute
if (Pos('xmlns', GetToken) > 0) then begin
fTokenID := tknsAttribute;
fRange := rsnsEqual;
end else begin
fTokenID := tkAttribute;
fRange := rsEqual;
end;
end;
procedure TSynXMLSyn.EqualProc;
begin
if fRange = rsnsEqual then
fTokenID := tknsEqual
else
fTokenID := tkEqual;
while not (fLine[Run] in [#0, #10, #13]) do
begin
if (fLine[Run] = '/') then
begin
fTokenID := tkSymbol;
fRange := rsElement;
Inc(Run);
Exit;
end else if (fLine[Run] = #34) then
begin
if fRange = rsnsEqual then
fRange := rsnsQuoteAttrValue
else
fRange := rsQuoteAttrValue;
Inc(Run);
Exit;
end else if (fLine[Run] = #39) then
begin
if fRange = rsnsEqual then
fRange := rsnsAPosAttrValue
else
fRange := rsAPosAttrValue;
Inc(Run);
Exit;
end;
Inc(Run);
end;
end;
procedure TSynXMLSyn.QAttributeValueProc;
begin
if fRange = rsnsQuoteAttrValue then
fTokenID := tknsQuoteAttrValue
else
fTokenID := tkQuoteAttrValue;
while not (fLine[Run] in [#0, #10, #13, '&', #34]) do Inc(Run);
if fLine[Run] = '&' then
begin
if fRange = rsnsQuoteAttrValue then
fRange := rsnsQuoteEntityRef
else
fRange := rsQuoteEntityRef;
Exit;
end else if fLine[Run] <> #34 then
begin
Exit;
end;
fRange := rsAttribute;
end;
procedure TSynXMLSyn.AAttributeValueProc;
begin
if fRange = rsnsAPosAttrValue then
fTokenID := tknsAPosAttrValue
else
fTokenID := tkAPosAttrValue;
while not (fLine[Run] in [#0, #10, #13, '&', #39]) do Inc(Run);
if fLine[Run] = '&' then
begin
if fRange = rsnsAPosAttrValue then
fRange := rsnsAPosEntityRef
else
fRange := rsAPosEntityRef;
Exit;
end else if fLine[Run] <> #39 then
begin
Exit;
end;
fRange := rsAttribute;
end;
procedure TSynXMLSyn.TextProc;
const StopSet = [#0..#31, '<', '&'];
begin
if fLine[Run] in (StopSet - ['&']) then begin
fProcTable[fLine[Run]]();
exit;
end;
fTokenID := tkText;
while not (fLine[Run] in StopSet) do Inc(Run);
if (fLine[Run] = '&') then begin
fRange := rsEntityRef;
Exit;
end;
end;
procedure TSynXMLSyn.EntityRefProc;
begin
fTokenID := tkEntityRef;
fRange := rsEntityRef;
while not (fLine[Run] in [#0..#32, ';']) do Inc(Run);
if (fLine[Run] = ';') then Inc(Run);
fRange := rsText;
end;
procedure TSynXMLSyn.QEntityRefProc;
begin
if fRange = rsnsQuoteEntityRef then
fTokenID := tknsQuoteEntityRef
else
fTokenID := tkQuoteEntityRef;
while not (fLine[Run] in [#0..#32, ';']) do Inc(Run);
if (fLine[Run] = ';') then Inc(Run);
if fRange = rsnsQuoteEntityRef then
fRange := rsnsQuoteAttrValue
else
fRange := rsQuoteAttrValue;
end;
procedure TSynXMLSyn.AEntityRefProc;
begin
if fRange = rsnsAPosEntityRef then
fTokenID := tknsAPosEntityRef
else
fTokenID := tkAPosEntityRef;
while not (fLine[Run] in [#0..#32, ';']) do Inc(Run);
if (fLine[Run] = ';') then Inc(Run);
if fRange = rsnsAPosEntityRef then
fRange := rsnsAPosAttrValue
else
fRange := rsAPosAttrValue;
end;
function TSynXMLSyn.GetFoldConfigInstance(Index: Integer): TSynCustomFoldConfig;
begin
Result := inherited GetFoldConfigInstance(Index);
Result.Enabled := True;
if TXmlCodeFoldBlockType(Index) in [cfbtXmlNode] then begin
Result.SupportedModes := Result.SupportedModes + [fmMarkup];
Result.Modes := Result.Modes + [fmMarkup];
end;
end;
procedure TSynXMLSyn.IdentProc;
begin
case fRange of
rsElement, rsOpenElement, rsCloseElement:
begin
ElementProc();
end;
rsAttribute:
begin
AttributeProc();
end;
rsEqual, rsnsEqual:
begin
EqualProc();
end;
rsQuoteAttrValue, rsnsQuoteAttrValue:
begin
QAttributeValueProc();
end;
rsAposAttrValue, rsnsAPosAttrValue:
begin
AAttributeValueProc();
end;
rsQuoteEntityRef, rsnsQuoteEntityRef:
begin
QEntityRefProc();
end;
rsAposEntityRef, rsnsAPosEntityRef:
begin
AEntityRefProc();
end;
rsEntityRef:
begin
EntityRefProc();
end;
else ;
end;
end;
procedure TSynXMLSyn.Next;
begin
fTokenPos := Run;
case fRange of
rsText:
begin
TextProc();
end;
rsComment:
begin
CommentProc();
end;
rsProcessingInstruction:
begin
ProcessingInstructionProc();
end;
rsDocType, rsDocTypeSquareBraces: //ek 2001-11-11
begin
DocTypeProc();
end;
rsCDATA:
begin
CDATAProc();
end;
else
fProcTable[fLine[Run]]();
end;
end;
function TSynXMLSyn.NextTokenIs(T : String) : Boolean;
var I, Len : Integer;
begin
Result:= True;
Len:= Length(T);
for I:= 1 to Len do
if (fLine[Run + I] <> T[I]) then
begin
Result:= False;
Break;
end;
end;
function TSynXMLSyn.GetDefaultAttribute(
Index: integer): TSynHighlighterAttributes;
begin
case Index of
SYN_ATTR_COMMENT: Result := fCommentAttri;
SYN_ATTR_IDENTIFIER: Result := fAttributeAttri;
SYN_ATTR_KEYWORD: Result := fElementAttri;
SYN_ATTR_WHITESPACE: Result := fSpaceAttri;
SYN_ATTR_SYMBOL: Result := fSymbolAttri;
else
Result := nil;
end;
end;
function TSynXMLSyn.GetEol: Boolean;
begin
Result := fTokenId = tkNull;
end;
function TSynXMLSyn.GetToken: string;
var
len: Longint;
begin
Result := '';
Len := (Run - fTokenPos);
SetString(Result, (FLine + fTokenPos), len);
end;
procedure TSynXMLSyn.GetTokenEx(out TokenStart: PChar;
out TokenLength: integer);
begin
TokenLength:=Run-fTokenPos;
TokenStart:=FLine + fTokenPos;
end;
function TSynXMLSyn.GetTokenID: TtkTokenKind;
begin
Result := fTokenId;
end;
function TSynXMLSyn.GetTokenAttribute: TSynHighlighterAttributes;
begin
case fTokenID of
tkElement: Result:= fElementAttri;
tkAttribute: Result:= fAttributeAttri;
tknsAttribute: Result:= fnsAttributeAttri;
tkEqual: Result:= fSymbolAttri;
tknsEqual: Result:= fSymbolAttri;
tkQuoteAttrValue: Result:= fAttributeValueAttri;
tkAPosAttrValue: Result:= fAttributeValueAttri;
tknsQuoteAttrValue: Result:= fnsAttributeValueAttri;
tknsAPosAttrValue: Result:= fnsAttributeValueAttri;
tkText: Result:= fTextAttri;
tkCDATA: Result:= fCDATAAttri;
tkEntityRef: Result:= fEntityRefAttri;
tkQuoteEntityRef: Result:= fEntityRefAttri;
tkAposEntityRef: Result:= fEntityRefAttri;
tknsQuoteEntityRef: Result:= fEntityRefAttri;
tknsAposEntityRef: Result:= fEntityRefAttri;
tkProcessingInstruction: Result:= fProcessingInstructionAttri;
tkComment: Result:= fCommentAttri;
tkDocType: Result:= fDocTypeAttri;
tkSymbol: Result:= fSymbolAttri;
tkSpace: Result:= fSpaceAttri;
else
Result := nil;
end;
end;
function TSynXMLSyn.GetTokenKind: integer;
begin
Result := Ord(fTokenId);
end;
function TSynXMLSyn.GetTokenPos: Integer;
begin
Result := fTokenPos;
end;
function TSynXMLSyn.GetRange: Pointer;
begin
CodeFoldRange.RangeType:=Pointer(PtrUInt(Integer(fRange)));
Result := inherited;
end;
procedure TSynXMLSyn.SetRange(Value: Pointer);
begin
inherited;
fRange := TRangeState(Integer(PtrUInt(CodeFoldRange.RangeType)));
end;
procedure TSynXMLSyn.ReSetRange;
begin
inherited;
fRange:= rsText;
end;
function TSynXMLSyn.GetIdentChars: TSynIdentChars;
begin
Result := ['0'..'9', 'a'..'z', 'A'..'Z', '_', '.', '-'] + TSynSpecialChars;
end;
class function TSynXMLSyn.GetLanguageName: string;
begin
Result := SYNS_LangXML;
end;
function TSynXMLSyn.GetSampleSource: String;
begin
Result:= '<?xml version="1.0"?>'#13#10+
'<!DOCTYPE root ['#13#10+
' ]>'#13#10+
'<!-- Comment -->'#13#10+
'<root version="&test;">'#13#10+
' <![CDATA[ **CDATA section** ]]>'#13#10+
'</root>';
end;
procedure TSynXMLSyn.CreateRootCodeFoldBlock;
begin
inherited CreateRootCodeFoldBlock;
RootCodeFoldBlock.InitRootBlockType(Pointer(PtrInt(cfbtXmlNone)));
end;
function TSynXMLSyn.StartXmlCodeFoldBlock(ABlockType: TXmlCodeFoldBlockType): TSynCustomCodeFoldBlock;
begin
Result := inherited StartXmlCodeFoldBlock(ord(ABlockType));
end;
function TSynXMLSyn.StartXmlNodeCodeFoldBlock(ABlockType: TXmlCodeFoldBlockType;
OpenPos: Integer; AName: String): TSynCustomCodeFoldBlock;
begin
if not FFoldConfig[ord(cfbtXmlNode)].Enabled then exit(nil);
Result := inherited StartXmlNodeCodeFoldBlock(ord(ABlockType), OpenPos, AName);
end;
procedure TSynXMLSyn.EndXmlNodeCodeFoldBlock(ClosePos: Integer; AName: String);
begin
if not FFoldConfig[ord(cfbtXmlNode)].Enabled then exit;
inherited EndXmlNodeCodeFoldBlock(ClosePos, AName);
end;
function TSynXMLSyn.TopXmlCodeFoldBlockType(DownIndex: Integer): TXmlCodeFoldBlockType;
begin
Result := TXmlCodeFoldBlockType(PtrUInt(TopCodeFoldBlockType(DownIndex)));
end;
function TSynXMLSyn.GetFoldConfigCount: Integer;
begin
// excluded cfbtXmlNone
Result := ord(high(TXmlCodeFoldBlockType)) - ord(low(TXmlCodeFoldBlockType));
end;
function TSynXMLSyn.GetFoldConfigInternalCount: Integer;
begin
// excluded cfbtXmlNone;
Result := ord(high(TXmlCodeFoldBlockType)) - ord(low(TXmlCodeFoldBlockType)) + 1;
end;
initialization
RegisterPlaceableHighlighter(TSynXMLSyn);
end.
|