📄 synhighlightersml.pas
字号:
{-------------------------------------------------------------------------------
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: SynHighlighterSML.pas, released 2000-04-17.
The Original Code is based on the dmMLSyn.pas file from the
mwEdit component suite by Martin Waldenburg and other developers, the Initial
Author of this file is David H. Muir.
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: SynHighlighterSml.pas,v 1.15 2005/01/28 16:53:25 maelh 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 SynEdit with a Standard ML syntax highlighter, with extra options for the standard Basis library.)
@author(David H Muir <dhm@dmsoftware.co.uk>)
@created(1999)
@lastmod(2000-06-23)
The SynHighlighterSML.pas unit provides SynEdit text control with a Standard ML highlighter. Many formatting attributes can
be specified, and there is an option to include extra keywords and operators only found in the Basis library, this option can
be disabled for backwards compatibility with older ML compilers that do not have support for the Basis Library.
}
{$IFNDEF QSYNHIGHLIGHTERSML}
unit SynHighlighterSml;
{$ENDIF}
{$I SynEdit.inc}
interface
uses
{$IFDEF SYN_CLX}
QGraphics,
QSynEditTypes,
QSynEditHighlighter,
{$ELSE}
Graphics,
Registry,
SynEditTypes,
SynEditHighlighter,
{$ENDIF}
SysUtils,
Classes;
Type
TtkTokenKind = (tkCharacter, tkComment, tkIdentifier, tkKey, tkNull, tkNumber,
tkOperator, tkSpace, tkString, tkSymbol, tkSyntaxError, tkUnknown);
TProcTableProc = procedure of object;
TRangeState = (rsUnknown, rsComment, rsMultilineString);
PIdentFuncTableFunc = ^TIdentFuncTableFunc;
TIdentFuncTableFunc = function: TtkTokenKind of object;
type
TSynSMLSyn = class(TSynCustomHighlighter)
private
fBasis: Boolean;
fLine: PChar;
fLineNumber: Integer;
fProcTable: array[#0..#255] of TProcTableProc;
fRange: TRangeState;
Run: LongInt;
fStringLen: Integer;
fToIdent: PChar;
fTokenPos: Integer;
FTokenID: TtkTokenKind;
fIdentFuncTable: array[0..145] of TIdentFuncTableFunc;
fCharacterAttri: TSynHighlighterAttributes;
fCommentAttri: TSynHighlighterAttributes;
fIdentifierAttri: TSynHighlighterAttributes;
fKeyAttri: TSynHighlighterAttributes;
fNumberAttri: TSynHighlighterAttributes;
fOperatorAttri: TSynHighlighterAttributes;
fSpaceAttri: TSynHighlighterAttributes;
fStringAttri: TSynHighlighterAttributes;
fSymbolAttri: TSynHighlighterAttributes;
fSyntaxErrorAttri: TSynHighlighterAttributes;
function IsValidMLCharacter: Boolean;
function KeyHash(ToHash: PChar): Integer;
function KeyComp(const aKey: String): Boolean;
function Func15: TtkTokenKind;
function Func19: TtkTokenKind;
function Func20: TtkTokenKind;
function Func21: TtkTokenKind;
function Func23: TtkTokenKind;
function Func26: TtkTokenKind;
function Func28: TtkTokenKind;
function Func31: TtkTokenKind;
function Func35: TtkTokenKind;
function Func37: TtkTokenKind;
function Func41: TtkTokenKind;
function Func43: TtkTokenKind;
function Func44: TtkTokenKind;
function Func47: TtkTokenKind;
function Func50: TtkTokenKind;
function Func52: TtkTokenKind;
function Func57: TtkTokenKind;
function Func59: TtkTokenKind;
function Func60: TtkTokenKind;
function Func62: TtkTokenKind;
function Func66: TtkTokenKind;
function Func68: TtkTokenKind;
function Func74: TtkTokenKind;
function Func76: TtkTokenKind;
function Func80: TtkTokenKind;
function Func82: TtkTokenKind;
function Func88: TtkTokenKind;
function Func92: TtkTokenKind;
function Func97: TtkTokenKind;
function Func101: TtkTokenKind;
function Func111: TtkTokenKind;
function Func114: TtkTokenKind;
function Func126: TtkTokenKind;
function Func145: TtkTokenKind;
procedure CRProc;
procedure CharacterProc;
procedure ColonProc;
procedure CommentProc;
procedure IdentProc;
procedure LFProc;
procedure NullProc;
procedure NumberProc;
procedure OperatorProc;
procedure RoundBracketOpenProc;
procedure SpaceProc;
procedure StringProc;
procedure SymbolProc;
procedure UnknownProc;
procedure BasisOpProc;
function AltFunc: TtkTokenKind;
procedure InitIdent;
function IdentKind(MayBe: PChar): TtkTokenKind;
procedure MakeMethodTables;
procedure StringEndProc;
procedure PoundProc;
protected
function GetIdentChars: TSynIdentChars; override;
function GetSampleSource: string; override;
function IsFilterStored: Boolean; override;
public
class function GetLanguageName: string; override;
function GetRange: Pointer; override;
procedure ResetRange; override;
procedure SetRange(Value: Pointer); override;
public
constructor Create(AOwner: TComponent); override;
function GetDefaultAttribute(Index: integer): TSynHighlighterAttributes;
override;
function GetEol: Boolean; override;
function GetTokenID: TtkTokenKind;
procedure SetLine(NewValue: String; LineNumber: Integer); override;
function GetToken: String; override;
function GetTokenAttribute: TSynHighlighterAttributes; override;
function GetTokenKind: integer; override;
function GetTokenPos: Integer; override;
procedure Next; override;
published
property CharacterAttri: TSynHighlighterAttributes read fCharacterAttri
write fCharacterAttri;
property CommentAttri: TSynHighlighterAttributes read fCommentAttri
write fCommentAttri;
property IdentifierAttri: TSynHighlighterAttributes read fIdentifierAttri
write fIdentifierAttri;
property KeyAttri: TSynHighlighterAttributes read fKeyAttri write fKeyAttri;
property NumberAttri: TSynHighlighterAttributes read fNumberAttri
write fNumberAttri;
property OperatorAttri: TSynHighlighterAttributes read fOperatorAttri
write fOperatorAttri;
property SpaceAttri: TSynHighlighterAttributes read fSpaceAttri
write fSpaceAttri;
property StringAttri: TSynHighlighterAttributes read fStringAttri
write fStringAttri;
property SymbolAttri: TSynHighlighterAttributes read fSymbolAttri
write fSymbolAttri;
property SyntaxErrorAttri: TSynHighlighterAttributes read fSyntaxErrorAttri
write fSyntaxErrorAttri;
property Basis: Boolean read FBasis write FBasis default True;
end;
implementation
uses
{$IFDEF SYN_CLX}
QSynEditStrConst;
{$ELSE}
SynEditStrConst;
{$ENDIF}
const
Identifiers = [#39, '_', '0'..'9', 'a'..'z', 'A'..'Z'];
var
mHashTable: array[#0..#255] of Integer;
procedure MakeIdentTable;
var
I: Char;
begin
for I := #0 to #255 do
begin
if I in ['_', 'A'..'Z', 'a'..'z'] then
mHashTable[I] := Ord(UpCase(I)) - 64
else
mHashTable[I] := 0;
end;
end;
function TSynSMLSyn.IsValidMLCharacter: Boolean;
var
ASCIIStr: string;
ASCIICode, Error: Integer;
begin
Result := False;
if (fLine[Run] = '"') then
if (Run > 2) and (fLine[Run - 1] <> '\') and (fLine[Run - 2] = '"') then
Result := True
else if (Run > 3) and (fLine[Run - 1] = '\') and (fLine[Run - 2] = '\')
and (fLine[Run - 3] = '"') then
Result := True
else if (Run > 3) and (fLine[Run - 1] in ['a', 'b', 'n', 'r', 't']) and
(fLine[Run - 2] = '\') and (fLine[Run - 3] = '"') then
Result := True
else if (Run > 5) and (fLine[Run - 4] = '\') and (fLine[Run - 5] = '"') then
begin
ASCIIStr := copy(fLine, Run - 2, 3);
Val(ASCIIStr, ASCIICode, Error);
if (Error = 0) and (ASCIICode >= 0) and (ASCIICode <= 255) then
Result := True
end
end;
procedure TSynSMLSyn.InitIdent;
var
I: Integer;
pF: PIdentFuncTableFunc;
begin
pF := PIdentFuncTableFunc(@fIdentFuncTable);
for I := Low(fIdentFuncTable) to High(fIdentFuncTable) do begin
pF^ := AltFunc;
Inc(pF);
end;
fIdentFuncTable[15] := Func15;
fIdentFuncTable[19] := Func19;
fIdentFuncTable[20] := Func20;
fIdentFuncTable[21] := Func21;
fIdentFuncTable[23] := Func23;
fIdentFuncTable[26] := Func26;
fIdentFuncTable[28] := Func28;
fIdentFuncTable[31] := Func31;
fIdentFuncTable[35] := Func35;
fIdentFuncTable[37] := Func37;
fIdentFuncTable[41] := Func41;
fIdentFuncTable[43] := Func43;
fIdentFuncTable[44] := Func44;
fIdentFuncTable[47] := Func47;
fIdentFuncTable[50] := Func50;
fIdentFuncTable[52] := Func52;
fIdentFuncTable[57] := Func57;
fIdentFuncTable[59] := Func59;
fIdentFuncTable[60] := Func60;
fIdentFuncTable[62] := Func62;
fIdentFuncTable[66] := Func66;
fIdentFuncTable[68] := Func68;
fIdentFuncTable[74] := Func74;
fIdentFuncTable[76] := Func76;
fIdentFuncTable[80] := Func80;
fIdentFuncTable[82] := Func82;
fIdentFuncTable[88] := Func88;
fIdentFuncTable[92] := Func92;
fIdentFuncTable[97] := Func97;
fIdentFuncTable[101] := Func101;
fIdentFuncTable[111] := Func111;
fIdentFuncTable[114] := Func114;
fIdentFuncTable[126] := Func126;
fIdentFuncTable[145] := Func145;
end;
function TSynSMLSyn.KeyHash(ToHash: PChar): Integer;
begin
Result := 0;
while ToHash^ in Identifiers do
begin
inc(Result, mHashTable[ToHash^]);
inc(ToHash);
end;
fStringLen := ToHash - fToIdent;
end;
function TSynSMLSyn.KeyComp(const aKey: String): Boolean;
var
I: Integer;
Temp: PChar;
begin
Temp := fToIdent;
if Length(aKey) = fStringLen then
begin
Result := True;
for i := 1 to fStringLen do
begin
if Temp^ <> aKey[i] then
begin
Result := False;
break;
end;
inc(Temp);
end;
end else Result := False;
end;
function TSynSMLSyn.Func15: TtkTokenKind;
begin
if KeyComp('if') then Result := tkKey else Result := tkIdentifier;
end;
function TSynSMLSyn.Func19: TtkTokenKind;
begin
if KeyComp('do') then Result := tkKey else
if KeyComp('and') then Result := tkKey else Result := tkIdentifier;
end;
function TSynSMLSyn.Func20: TtkTokenKind;
begin
if KeyComp('as') then Result := tkKey else
if KeyComp('fn') then Result := tkKey else Result := tkIdentifier;
end;
function TSynSMLSyn.Func21: TtkTokenKind;
begin
if KeyComp('of') then Result := tkKey else Result := tkIdentifier;
end;
function TSynSMLSyn.Func23: TtkTokenKind;
begin
if KeyComp('in') then Result := tkKey else
if KeyComp('end') then Result := tkKey else Result := tkIdentifier;
end;
function TSynSMLSyn.Func26: TtkTokenKind;
begin
if KeyComp('rec') then Result := tkKey else Result := tkIdentifier;
end;
function TSynSMLSyn.Func28: TtkTokenKind;
begin
if KeyComp('case') then Result := tkKey else Result := tkIdentifier;
end;
function TSynSMLSyn.Func31: TtkTokenKind;
begin
if KeyComp('op') then Result := tkKey else Result := tkIdentifier;
end;
function TSynSMLSyn.Func35: TtkTokenKind;
begin
if KeyComp('val') then Result := tkKey else
if KeyComp('sig') then Result := tkKey else Result := tkIdentifier;
end;
function TSynSMLSyn.Func37: TtkTokenKind;
begin
if KeyComp('let') then Result := tkKey else Result := tkIdentifier;
end;
function TSynSMLSyn.Func41: TtkTokenKind;
begin
if KeyComp('fun') then Result := tkKey else
if KeyComp('else') then Result := tkKey else Result := tkIdentifier;
end;
function TSynSMLSyn.Func43: TtkTokenKind;
begin
if KeyComp('local') then Result := tkKey else Result := tkIdentifier;
end;
function TSynSMLSyn.Func44: TtkTokenKind;
begin
if KeyComp('handle') then Result := tkKey else Result := tkIdentifier;
end;
function TSynSMLSyn.Func47: TtkTokenKind;
begin
if KeyComp('then') then Result := tkKey else Result := tkIdentifier;
end;
function TSynSMLSyn.Func50: TtkTokenKind;
begin
if KeyComp('open') then Result := tkKey else Result := tkIdentifier;
end;
function TSynSMLSyn.Func52: TtkTokenKind;
begin
if KeyComp('raise') then Result := tkKey else Result := tkIdentifier;
end;
function TSynSMLSyn.Func57: TtkTokenKind;
begin
if KeyComp('while') then Result := tkKey else Result := tkIdentifier;
end;
function TSynSMLSyn.Func59: TtkTokenKind;
begin
if KeyComp('where') then Result := tkKey else Result := tkIdentifier;
end;
function TSynSMLSyn.Func60: TtkTokenKind;
begin
if KeyComp('with') then Result := tkKey else Result := tkIdentifier;
end;
function TSynSMLSyn.Func62: TtkTokenKind;
begin
if KeyComp('infix') then Result := tkKey else Result := tkIdentifier;
end;
function TSynSMLSyn.Func66: TtkTokenKind;
begin
if KeyComp('andalso') then Result := tkKey else
if KeyComp('type') then Result := tkKey else Result := tkIdentifier;
end;
function TSynSMLSyn.Func68: TtkTokenKind;
begin
if KeyComp('include') then Result := tkKey else Result := tkIdentifier;
end;
function TSynSMLSyn.Func74: TtkTokenKind;
begin
if KeyComp('orelse') then Result := tkKey else Result := tkIdentifier;
end;
function TSynSMLSyn.Func76: TtkTokenKind;
begin
if KeyComp('sharing') then Result := tkKey else Result := tkIdentifier;
end;
function TSynSMLSyn.Func80: TtkTokenKind;
begin
if KeyComp('infixr') then Result := tkKey else Result := tkIdentifier;
end;
function TSynSMLSyn.Func82: TtkTokenKind;
begin
if KeyComp('nonfix') then Result := tkKey else Result := tkIdentifier;
end;
function TSynSMLSyn.Func88: TtkTokenKind;
begin
if KeyComp('abstype') then Result := tkKey else
if KeyComp('eqtype') then Result := tkKey else Result := tkIdentifier;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -