📄 synhighlighterjava.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: SynHighlighterJava.pas, released 2000-04-10.
The Original Code is based on the DcjSynJava.pas file from the
mwEdit component suite by Martin Waldenburg and other developers, the Initial
Author of this file is Michael Trier.
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: SynHighlighterJava.pas,v 1.16 2004/02/26 17:58:28 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 a Java highlighter for SynEdit)
@author(Michael Trier)
@created(December 1998, converted to SynEdit 2000-04-10 by Michael Hieke)
@lastmod(2000-06-23)
The SynHighlighterJava unit provides SynEdit with a Java source (.java) highlighter.
}
{$IFNDEF QSYNHIGHLIGHTERJAVA}
unit SynHighlighterJava;
{$ENDIF}
{$I SynEdit.inc}
interface
uses
{$IFDEF SYN_CLX}
QGraphics,
QSynEditTypes,
QSynEditHighlighter,
{$ELSE}
Graphics,
SynEditTypes,
SynEditHighlighter,
{$ENDIF}
SysUtils, Classes;
type
TtkTokenKind = (tkComment, tkDocument, tkIdentifier, tkInvalid, tkKey,
tkNull, tkNumber, tkSpace, tkString, tkSymbol, tkUnknown);
TxtkTokenKind = (
xtkAdd, xtkAddAssign, xtkAnd, xtkAndAssign, xtkAssign, xtkBitComplement,
xtkBraceClose, xtkBraceOpen, xtkColon, xtkCondAnd, xtkCondOr, xtkDecrement,
xtkDivide, xtkDivideAssign, xtkGreaterThan, xtkGreaterThanEqual, xtkIncOr,
xtkIncOrAssign, xtkIncrement, xtkLessThan, xtkLessThanEqual,
xtkLogComplement, xtkLogEqual, xtkMultiply, xtkMultiplyAssign, xtkNotEqual,
xtkPoint, xtkQuestion, xtkRemainder, xtkRemainderAssign, xtkRoundClose,
xtkRoundOpen, xtkSemiColon, xtkShiftLeft, xtkShiftLeftAssign, xtkShiftRight,
xtkShiftRightAssign, xtkSquareClose, xtkSquareOpen, xtkSubtract,
xtkSubtractAssign, xtkUnsignShiftRight, xtkUnsignShiftRightAssign, xtkXor,
xtkXorAssign, xtkComma);
TRangeState = (rsANil, rsComment, rsDocument, rsUnknown);
TProcTableProc = procedure of Object;
TIdentFuncTableFunc = function: TtkTokenKind of Object;
TSynJavaSyn = class(TSynCustomHighlighter)
private
fRange: TRangeState;
fLine: PChar;
fProcTable: array[#0..#255] of TProcTableProc;
Run: LongInt;
FRoundCount: Integer;
FSquareCount: Integer;
fStringLen: Integer;
fToIdent: PChar;
fTokenPos: Integer;
FTokenID: TtkTokenKind;
FExtTokenID: TxtkTokenKind;
fEol: Boolean;
fIdentFuncTable: array[0..172] of TIdentFuncTableFunc;
fLineNumber: Integer;
fCommentAttri: TSynHighlighterAttributes;
fDocumentAttri: TSynHighlighterAttributes;
fIdentifierAttri: TSynHighlighterAttributes;
fInvalidAttri: TSynHighlighterAttributes;
fKeyAttri: TSynHighlighterAttributes;
fNumberAttri: TSynHighlighterAttributes;
fSpaceAttri: TSynHighlighterAttributes;
fStringAttri: TSynHighlighterAttributes;
fSymbolAttri: TSynHighlighterAttributes;
function KeyHash(ToHash: PChar): Integer;
function KeyComp(const aKey: String): Boolean;
function Func17: TtkTokenKind;
function Func21: TtkTokenKind;
function Func32: TtkTokenKind;
function Func34: TtkTokenKind;
function Func40: TtkTokenKind;
function Func42: TtkTokenKind;
function Func45: TtkTokenKind;
function Func46: TtkTokenKind;
function Func47: TtkTokenKind;
function Func48: TtkTokenKind;
function Func51: TtkTokenKind;
function Func52: TtkTokenKind;
function Func54: TtkTokenKind;
function Func56: TtkTokenKind;
function Func59: TtkTokenKind;
function Func60: TtkTokenKind;
function Func61: TtkTokenKind;
function Func62: TtkTokenKind;
function Func63: TtkTokenKind;
function Func65: TtkTokenKind;
function Func66: TtkTokenKind;
function Func68: TtkTokenKind;
function Func69: TtkTokenKind;
function Func71: TtkTokenKind;
function Func76: TtkTokenKind;
function Func77: TtkTokenKind;
function Func78: TtkTokenKind;
function Func84: TtkTokenKind;
function Func85: TtkTokenKind;
function Func86: TtkTokenKind;
function Func88: TtkTokenKind;
function Func89: TtkTokenKind;
function Func90: TtkTokenKind;
function Func92: TtkTokenKind;
function Func97: TtkTokenKind;
function Func98: TtkTokenKind;
function Func102: TtkTokenKind;
function Func104: TtkTokenKind;
function Func109: TtkTokenKind;
function Func115: TtkTokenKind;
function Func116: TtkTokenKind;
function Func119: TtkTokenKind;
function Func129: TtkTokenKind;
function Func136: TtkTokenKind;
function Func172: TtkTokenKind;
procedure CommentProc;
procedure AndSymbolProc;
procedure AsciiCharProc;
procedure AtSymbolProc;
procedure BraceCloseProc;
procedure BraceOpenProc;
procedure CRProc;
procedure ColonProc;
procedure CommaProc;
procedure EqualProc;
procedure GreaterProc;
procedure IdentProc;
procedure LFProc;
procedure LowerProc;
procedure MinusProc;
procedure MultiplyProc;
procedure NotSymbolProc;
procedure NullProc;
procedure NumberProc;
procedure OrSymbolProc;
procedure PlusProc;
procedure PointProc;
procedure PoundProc;
procedure QuestionProc;
procedure RemainderSymbolProc;
procedure RoundCloseProc;
procedure RoundOpenProc;
procedure SemiColonProc;
procedure SlashProc;
procedure SpaceProc;
procedure SquareCloseProc;
procedure SquareOpenProc;
procedure StringProc;
procedure TildeProc;
procedure XOrSymbolProc;
procedure UnknownProc;
function AltFunc: TtkTokenKind;
procedure InitIdent;
function IdentKind(MayBe: PChar): TtkTokenKind;
procedure MakeMethodTables;
protected
function GetIdentChars: TSynIdentChars; override;
function GetSampleSource: string; override;
function GetExtTokenID: TxtkTokenKind;
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(NewValue: String; LineNumber:Integer); override;
function GetToken: String; 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 ExtTokenID: TxtkTokenKind read GetExtTokenID;
published
property CommentAttri: TSynHighlighterAttributes read fCommentAttri
write fCommentAttri;
property DocumentAttri: TSynHighlighterAttributes read fDocumentAttri
write fDocumentAttri;
property IdentifierAttri: TSynHighlighterAttributes read fIdentifierAttri
write fIdentifierAttri;
property InvalidAttri: TSynHighlighterAttributes read fInvalidAttri
write fInvalidAttri;
property KeyAttri: TSynHighlighterAttributes read fKeyAttri write fKeyAttri;
property NumberAttri: TSynHighlighterAttributes read fNumberAttri
write fNumberAttri;
property SpaceAttri: TSynHighlighterAttributes read fSpaceAttri
write fSpaceAttri;
property StringAttri: TSynHighlighterAttributes read fStringAttri
write fStringAttri;
property SymbolAttri: TSynHighlighterAttributes read fSymbolAttri
write fSymbolAttri;
end;
implementation
uses
{$IFDEF SYN_CLX}
QSynEditStrConst;
{$ELSE}
SynEditStrConst;
{$ENDIF}
var
Identifiers: array[#0..#255] of ByteBool;
mHashTable: array[#0..#255] of Integer;
procedure MakeIdentTable;
var
I: Char;
begin
for I := #0 to #255 do
begin
// Java allows special characters in identifier names
Identifiers[I] := (I in ['_', '$', '0'..'9', 'a'..'z', 'A'..'Z']) or (I in TSynSpecialChars);
if (I in ['_', '$', 'a'..'z', 'A'..'Z']) or (I in TSynSpecialChars) then
begin
if (I > #64) and (I < #91) then
mHashTable[I] := Ord(I) - 64
else
if (I > #96) then
mHashTable[I] := Ord(I) - 95;
end
else
mHashTable[I] := 0;
end;
end;
procedure TSynJavaSyn.InitIdent;
var
I: Integer;
begin
for I := 0 to 172 do
Case I of
17: fIdentFuncTable[I] := Func17;
21: fIdentFuncTable[I] := Func21;
32: fIdentFuncTable[I] := Func32;
34: fIdentFuncTable[I] := Func34;
40: fIdentFuncTable[I] := Func40;
42: fIdentFuncTable[I] := Func42;
45: fIdentFuncTable[I] := Func45;
46: fIdentFuncTable[I] := Func46;
47: fIdentFuncTable[I] := Func47;
48: fIdentFuncTable[I] := Func48;
51: fIdentFuncTable[I] := Func51;
52: fIdentFuncTable[I] := Func52;
54: fIdentFuncTable[I] := Func54;
56: fIdentFuncTable[I] := Func56;
59: fIdentFuncTable[I] := Func59;
60: fIdentFuncTable[I] := Func60;
61: fIdentFuncTable[I] := Func61;
62: fIdentFuncTable[I] := Func62;
63: fIdentFuncTable[I] := Func63;
65: fIdentFuncTable[I] := Func65;
66: fIdentFuncTable[I] := Func66;
68: fIdentFuncTable[I] := Func68;
69: fIdentFuncTable[I] := Func69;
71: fIdentFuncTable[I] := Func71;
76: fIdentFuncTable[I] := Func76;
77: fIdentFuncTable[I] := Func77;
78: fIdentFuncTable[I] := Func78;
84: fIdentFuncTable[I] := Func84;
85: fIdentFuncTable[I] := Func85;
86: fIdentFuncTable[I] := Func86;
88: fIdentFuncTable[I] := Func88;
89: fIdentFuncTable[I] := Func89;
90: fIdentFuncTable[I] := Func90;
92: fIdentFuncTable[I] := Func92;
97: fIdentFuncTable[I] := Func97;
98: fIdentFuncTable[I] := Func98;
102: fIdentFuncTable[I] := Func102;
104: fIdentFuncTable[I] := Func104;
109: fIdentFuncTable[I] := Func109;
115: fIdentFuncTable[I] := Func115;
116: fIdentFuncTable[I] := Func116;
119: fIdentFuncTable[I] := Func119;
129: fIdentFuncTable[I] := Func129;
136: fIdentFuncTable[I] := Func136;
172: fIdentFuncTable[I] := Func172;
else fIdentFuncTable[I] := AltFunc;
end;
end;
function TSynJavaSyn.KeyHash(ToHash: PChar): Integer;
begin
Result := 0;
while (ToHash^ in ['_', '$', '0'..'9', 'a'..'z', 'A'..'Z']) or
(ToHash^ in TSynSpecialChars) do
begin
inc(Result, mHashTable[ToHash^]);
inc(ToHash);
end;
fStringLen := ToHash - fToIdent;
end; { KeyHash }
function TSynJavaSyn.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; { KeyComp }
function TSynJavaSyn.Func17: TtkTokenKind;
begin
if KeyComp('if') then Result := tkKey else Result := tkIdentifier;
end;
function TSynJavaSyn.Func21: TtkTokenKind;
begin
if KeyComp('do') then Result := tkKey else Result := tkIdentifier;
end;
function TSynJavaSyn.Func32: TtkTokenKind;
begin
if KeyComp('case') then Result := tkKey else Result := tkIdentifier;
end;
function TSynJavaSyn.Func34: TtkTokenKind;
begin
if KeyComp('char') then Result := tkKey else Result := tkIdentifier;
end;
function TSynJavaSyn.Func40: TtkTokenKind;
begin
if KeyComp('catch') then Result := tkKey else Result := tkIdentifier;
end;
function TSynJavaSyn.Func42: TtkTokenKind;
begin
if KeyComp('for') then Result := tkKey else
if KeyComp('break') then Result := tkKey else Result := tkIdentifier;
end;
function TSynJavaSyn.Func45: TtkTokenKind;
begin
if KeyComp('else') then Result := tkKey else
if KeyComp('new') then Result := tkKey else Result := tkIdentifier;
end;
function TSynJavaSyn.Func46: TtkTokenKind;
begin
if KeyComp('int') then Result := tkKey else Result := tkIdentifier;
end;
function TSynJavaSyn.Func47: TtkTokenKind;
begin
if KeyComp('final') then Result := tkKey else Result := tkIdentifier;
end;
function TSynJavaSyn.Func48: TtkTokenKind;
begin
if KeyComp('false') then Result := tkKey else Result := tkIdentifier;
end;
function TSynJavaSyn.Func51: TtkTokenKind;
begin
if KeyComp('package') then Result := tkKey else Result := tkIdentifier;
end;
function TSynJavaSyn.Func52: TtkTokenKind;
begin
if KeyComp('long') then Result := tkKey else Result := tkIdentifier;
end;
function TSynJavaSyn.Func54: TtkTokenKind;
begin
if KeyComp('void') then Result := tkKey else Result := tkIdentifier;
end;
function TSynJavaSyn.Func56: TtkTokenKind;
begin
if KeyComp('byte') then Result := tkKey else Result := tkIdentifier;
end;
function TSynJavaSyn.Func59: TtkTokenKind;
begin
if KeyComp('class') then Result := tkKey else
if KeyComp('float') then Result := tkKey else Result := tkIdentifier;
end;
function TSynJavaSyn.Func60: TtkTokenKind;
begin
if KeyComp('this') then Result := tkKey else Result := tkIdentifier;
end;
function TSynJavaSyn.Func61: TtkTokenKind;
begin
if KeyComp('goto') then Result := tkKey else Result := tkIdentifier;
end;
function TSynJavaSyn.Func62: TtkTokenKind;
begin
if KeyComp('while') then Result := tkKey else Result := tkIdentifier;
end;
function TSynJavaSyn.Func63: TtkTokenKind;
begin
if KeyComp('null') then Result := tkKey else Result := tkIdentifier;
end;
function TSynJavaSyn.Func65: TtkTokenKind;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -