lazarus/components/synedit/synhighlighterini.pas

473 lines
13 KiB
ObjectPascal

{-------------------------------------------------------------------------------
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: SynHighlighterIni.pas, released 2000-04-21.
The Original Code is based on the izIniSyn.pas file from the
mwEdit component suite by Martin Waldenburg and other developers, the Initial
Author of this file is Igor P. Zenkov.
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: SynHighlighterIni.pas,v 1.7 2001/11/09 07:46:17 plpolak Exp $
You may retrieve the latest version of this file at the SynEdit home page,
located at http://SynEdit.SourceForge.net
Known Issues:
-------------------------------------------------------------------------------}
{
@abstract(Provides an Ini-files highlighter for SynEdit)
@author(Igor P. Zenkov, converted to SynEdit by Bruno Mikkelsen <btm@scientist.com>)
@created(1999-11-02, converted to SynEdit 2000-04-21)
@lastmod(2000-04-21)
The SynHighlighterIni unit provides SynEdit with an Ini-files highlighter.
Thanks to Primoz Gabrijelcic, Martin Waldenburg and Michael Hieke.
}
unit SynHighlighterIni;
{$I SynEdit.inc}
interface
uses
Classes,
Graphics,
SynEditTypes, SynEditHighlighter, SynEditStrConst, LazEditTextAttributes, LazEditHighlighter;
type
TtkTokenKind = (tkComment, tkText, tkSection, tkKey, tkNull, tkNumber,
tkSpace, tkString, tkSymbol, tkUnknown);
TProcTableProc = procedure of object;
type
TSynIniCommentType = (ictSemicolon, ictHash); // TODO ictSemicolonMidLine, ictHashMidline
TSynIniCommentTypes = set of TSynIniCommentType;
{ TSynIniSyn }
TSynIniSyn = class(TSynCustomHighlighter)
private type
TSynPasAttribute = (
attribComment,
attribText,
attribSection,
attribKey,
attribNumber,
attribSpace,
attribString,
attribSymbol
);
private
FCommentTypes: TSynIniCommentTypes;
fProcTable: array[#0..#255] of TProcTableProc;
Run: LongInt;
fTokenPos: Integer;
FTokenID: TtkTokenKind;
fCommentAttri: TSynHighlighterAttributes;
fTextAttri: TSynHighlighterAttributes;
fSectionAttri: TSynHighlighterAttributes;
fKeyAttri: TSynHighlighterAttributes;
fNumberAttri: TSynHighlighterAttributes;
fSpaceAttri: TSynHighlighterAttributes;
fStringAttri: TSynHighlighterAttributes;
fSymbolAttri: TSynHighlighterAttributes;
procedure SetAttribute(AnIndex: TSynPasAttribute; AValue: TSynHighlighterAttributes);
procedure SetCommentTypes(AValue: TSynIniCommentTypes);
procedure SectionOpenProc;
procedure KeyProc;
procedure CRProc;
procedure EqualProc;
procedure TextProc;
procedure LFProc;
procedure NullProc;
procedure NumberProc;
procedure CommentProc;
procedure SpaceProc;
procedure StringProc; // ""
procedure StringProc1; // ''
procedure MakeMethodTables;
protected
{General Stuff}
function GetIdentChars: TSynIdentChars; override;
function GetSampleSource: String; override;
public
class function GetLanguageName: string; override;
public
constructor Create(AOwner: TComponent); override;
function GetTokenClassAttribute(ATkClass: TLazEditTokenClass;
ATkDetails: TLazEditTokenDetails = []): TLazEditTextAttribute; override;
function GetEol: Boolean; override;
function GetTokenID: TtkTokenKind;
procedure InitForScanningLine; override;
function GetToken: String; override;
procedure GetTokenEx(out TokenStart: PChar; out TokenLength: integer); override;
function GetTokenAttribute: TLazEditTextAttribute; override;
function GetTokenKind: integer; override;
function GetTokenPos: Integer; override;
procedure Next; override;
published
property CommentTypes: TSynIniCommentTypes read FCommentTypes write SetCommentTypes default [ictSemicolon];
property CommentAttri: TSynHighlighterAttributes index attribComment read fCommentAttri write SetAttribute;
property TextAttri : TSynHighlighterAttributes index attribText read fTextAttri write SetAttribute;
property SectionAttri: TSynHighlighterAttributes index attribSection read fSectionAttri write SetAttribute;
property KeyAttri : TSynHighlighterAttributes index attribKey read fKeyAttri write SetAttribute;
property NumberAttri : TSynHighlighterAttributes index attribNumber read fNumberAttri write SetAttribute;
property SpaceAttri : TSynHighlighterAttributes index attribSpace read fSpaceAttri write SetAttribute;
property StringAttri : TSynHighlighterAttributes index attribString read fStringAttri write SetAttribute;
property SymbolAttri : TSynHighlighterAttributes index attribSymbol read fSymbolAttri write SetAttribute;
end;
implementation
procedure TSynIniSyn.MakeMethodTables;
var
i: Char;
begin
for i := #0 to #255 do
case i of
#0 : fProcTable[i] := @NullProc;
#10 {LF}: fProcTable[i] := @LFProc;
#13 {CR}: fProcTable[i] := @CRProc;
#34 {"} : fProcTable[i] := @StringProc;
#39 {'} : fProcTable[i] := @StringProc1;
'0'..'9': fProcTable[i] := @NumberProc;
#59 {;} : begin
if ictSemicolon in FCommentTypes then
fProcTable[i] := @CommentProc
else
fProcTable[i] := @TextProc;
end;
'#': begin
if ictHash in FCommentTypes then
fProcTable[i] := @CommentProc
else
fProcTable[i] := @TextProc;
end;
#61 {=} : fProcTable[i] := @EqualProc;
#91 {[} : fProcTable[i] := @SectionOpenProc;
#1..#9, #11, #12, #14..#32: fProcTable[i] := @SpaceProc;
else
fProcTable[i] := @TextProc;
end;
end;
constructor TSynIniSyn.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
fCommentAttri := TSynHighlighterAttributes.Create(@SYNS_AttrComment);
fCommentAttri.Style := [fsItalic];
fCommentAttri.Foreground := clGreen;
AddAttribute(fCommentAttri);
fTextAttri := TSynHighlighterAttributes.Create(@SYNS_AttrText);
AddAttribute(fTextAttri);
fSectionAttri := TSynHighlighterAttributes.Create(@SYNS_AttrSection);
fSectionAttri.Style := [fsBold];
AddAttribute(fSectionAttri);
fKeyAttri := TSynHighlighterAttributes.Create(@SYNS_AttrKey);
fKeyAttri.Foreground := clBlue;
AddAttribute(fKeyAttri);
fNumberAttri := TSynHighlighterAttributes.Create(@SYNS_AttrNumber);
AddAttribute(fNumberAttri);
fSpaceAttri := TSynHighlighterAttributes.Create(@SYNS_AttrSpace);
AddAttribute(fSpaceAttri);
fStringAttri := TSynHighlighterAttributes.Create(@SYNS_AttrString);
AddAttribute(fStringAttri);
fSymbolAttri := TSynHighlighterAttributes.Create(@SYNS_AttrSymbol);
fSymbolAttri.Foreground := clRed;
AddAttribute(fSymbolAttri);
SetAttributesOnChange(@DefHighlightChange);
fDefaultFilter := SYNS_FilterINI;
FCommentTypes := [ictSemicolon];
MakeMethodTables;
end; { Create }
procedure TSynIniSyn.InitForScanningLine;
begin
inherited;
Run := 0;
Next;
end;
procedure TSynIniSyn.SectionOpenProc;
begin
// if it is not column 0 mark as tkText and get out of here
if Run > 0 then
begin
fTokenID := tkText;
inc(Run);
Exit;
end;
// this is column 0 ok it is a Section
fTokenID := tkSection;
inc(Run);
while LinePtr[Run] <> #0 do
case LinePtr[Run] of
']': begin inc(Run); break end;
#10: break;
#13: break;
else inc(Run);
end;
end;
procedure TSynIniSyn.CRProc;
begin
fTokenID := tkSpace;
Case LinePtr[Run + 1] of
#10: inc(Run, 2);
else inc(Run);
end;
end;
procedure TSynIniSyn.EqualProc;
begin
inc(Run);
fTokenID := tkSymbol;
end;
procedure TSynIniSyn.SetCommentTypes(AValue: TSynIniCommentTypes);
begin
if FCommentTypes = AValue then Exit;
FCommentTypes := AValue;
MakeMethodTables;
RequestFullRescan;
end;
procedure TSynIniSyn.SetAttribute(AnIndex: TSynPasAttribute; AValue: TSynHighlighterAttributes);
begin
case AnIndex of
attribComment: FCommentAttri.Assign(AValue);
attribText: FTextAttri.Assign(AValue);
attribSection: FSectionAttri.Assign(AValue);
attribKey: FKeyAttri.Assign(AValue);
attribNumber: FNumberAttri.Assign(AValue);
attribSpace: FSpaceAttri.Assign(AValue);
attribString: FStringAttri.Assign(AValue);
attribSymbol: FSymbolAttri.Assign(AValue);
end;
end;
procedure TSynIniSyn.KeyProc;
begin
fTokenID := tkKey;
inc(Run);
while LinePtr[Run] <> #0 do
case LinePtr[Run] of
'=': break;
#10: break;
#13: break;
else inc(Run);
end;
end;
procedure TSynIniSyn.TextProc;
begin
if Run = 0 then
KeyProc
else begin
inc(Run);
while (LinePtr[Run] in [#128..#191]) OR // continued utf8 subcode
((LinePtr[Run]<>#0) and (fProcTable[LinePtr[Run]] = @TextProc)) do inc(Run);
fTokenID := tkText;
//fTokenID := tkText;
//inc(Run);
end;
end;
procedure TSynIniSyn.LFProc;
begin
fTokenID := tkSpace;
inc(Run);
end;
procedure TSynIniSyn.NullProc;
begin
fTokenID := tkNull;
end;
procedure TSynIniSyn.NumberProc;
begin
if Run = 0 then
KeyProc
else begin
inc(Run);
fTokenID := tkNumber;
while LinePtr[Run] in ['0'..'9', '.', 'e', 'E'] do inc(Run);
if LinePtr[Run] in ['a'..'z','A'..'Z'] then TextProc;
end;
end;
// ;
procedure TSynIniSyn.CommentProc;
begin
// if it is not column 0 mark as tkText and get out of here
if Run > 0 then
begin
fTokenID := tkText;
inc(Run);
Exit;
end;
// this is column 0 ok it is a comment
fTokenID := tkComment;
inc(Run);
while LinePtr[Run] <> #0 do
case LinePtr[Run] of
#10: break;
#13: break;
else inc(Run);
end;
end;
procedure TSynIniSyn.SpaceProc;
begin
inc(Run);
fTokenID := tkSpace;
while LinePtr[Run] in [#1..#9, #11, #12, #14..#32] do inc(Run);
end;
// ""
procedure TSynIniSyn.StringProc;
begin
fTokenID := tkString;
if (LinePtr[Run + 1] = #34) and (LinePtr[Run + 2] = #34) then inc(Run, 2);
repeat
case LinePtr[Run] of
#0, #10, #13: break;
end;
inc(Run);
until LinePtr[Run] = #34;
if LinePtr[Run] <> #0 then inc(Run);
end;
// ''
procedure TSynIniSyn.StringProc1;
begin
fTokenID := tkString;
if (LinePtr[Run + 1] = #39) and (LinePtr[Run + 2] = #39) then inc(Run, 2);
repeat
case LinePtr[Run] of
#0, #10, #13: break;
end;
inc(Run);
until LinePtr[Run] = #39;
if LinePtr[Run] <> #0 then inc(Run);
end;
procedure TSynIniSyn.Next;
begin
fTokenPos := Run;
fProcTable[LinePtr[Run]];
end;
function TSynIniSyn.GetTokenClassAttribute(ATkClass: TLazEditTokenClass;
ATkDetails: TLazEditTokenDetails): TLazEditTextAttribute;
begin
case ATkClass of
tcComment: Result := fCommentAttri;
tcKeyword: Result := fKeyAttri;
tcString: Result := fStringAttri;
tcWhiteSpace: Result := fSpaceAttri;
tcSymbol: Result := fSymbolAttri;
tcNumber: Result := fNumberAttri;
tcHeader: Result := fSectionAttri;
else
Result := nil;
end;
end;
function TSynIniSyn.GetEol: Boolean;
begin
Result := fTokenId = tkNull;
end;
function TSynIniSyn.GetToken: String;
var
Len: LongInt;
begin
Len := Run - fTokenPos;
SetString(Result, (LinePtr + fTokenPos), Len);
end;
procedure TSynIniSyn.GetTokenEx(out TokenStart: PChar; out TokenLength: integer);
begin
TokenLength := Run - fTokenPos;
TokenStart := LinePtr + fTokenPos;
end;
function TSynIniSyn.GetTokenID: TtkTokenKind;
begin
Result := fTokenId;
end;
function TSynIniSyn.GetTokenAttribute: TLazEditTextAttribute;
begin
case fTokenID of
tkComment: Result := fCommentAttri;
tkText : Result := fTextAttri;
tkSection: Result := fSectionAttri;
tkKey : Result := fKeyAttri;
tkNumber : Result := fNumberAttri;
tkSpace : Result := fSpaceAttri;
tkString : Result := fStringAttri;
tkSymbol : Result := fSymbolAttri;
tkUnknown: Result := fTextAttri;
else Result := nil;
end;
end;
function TSynIniSyn.GetTokenKind: integer;
begin
Result := Ord(fTokenId);
end;
function TSynIniSyn.GetTokenPos: Integer;
begin
Result := fTokenPos;
end;
function TSynIniSyn.GetIdentChars: TSynIdentChars;
begin
Result := TSynValidStringChars;
end;
class function TSynIniSyn.GetLanguageName: string;
begin
Result := SYNS_LangINI;
end;
function TSynIniSyn.GetSampleSource: String;
begin
Result := '; Syntax highlighting'#13#10+
'[Section]'#13#10+
'Key=value'#13#10+
'String="Arial"'#13#10+
'Number=123456';
end;
initialization
RegisterPlaceableHighlighter(TSynIniSyn);
end.