📄 synhighlighterjscript.pas
字号:
'0'..'9': fProcTable[I] := NumberProc;
'|': fProcTable[I] := OrSymbolProc;
'+': fProcTable[I] := PlusProc;
'.': fProcTable[I] := PointProc;
'/': fProcTable[I] := SlashProc;
#1..#9, #11, #12, #14..#32: fProcTable[I] := SpaceProc;
'*': fProcTable[I] := StarProc;
'"', #39: fProcTable[I] := StringProc;
'~', '{', '}', ',', '(', ')', '[', ']', '<', '>', ':', '?', ';', '!', '=':
fProcTable[I] := SymbolProc;
else
fProcTable[I] := UnknownProc;
end;
end;
constructor TSynJScriptSyn.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
fCommentAttri := TSynHighlighterAttributes.Create(SYNS_AttrComment);
fCommentAttri.Style := [fsItalic];
AddAttribute(fCommentAttri);
fIdentifierAttri := TSynHighlighterAttributes.Create(SYNS_AttrIdentifier);
AddAttribute(fIdentifierAttri);
fKeyAttri := TSynHighlighterAttributes.Create(SYNS_AttrReservedWord);
fKeyAttri.Style := [fsBold];
AddAttribute(fKeyAttri);
fNonReservedKeyAttri := TSynHighlighterAttributes.Create(SYNS_AttrNonReservedKeyword);
AddAttribute(fNonReservedKeyAttri);
fEventAttri := TSynHighlighterAttributes.Create(SYNS_AttrEvent);
AddAttribute(fEventAttri);
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);
AddAttribute(fSymbolAttri);
SetAttributesOnChange(DefHighlightChange);
InitIdent;
MakeMethodTables;
fDefaultFilter := SYNS_FilterJScript;
fRange := rsUnknown;
end;
procedure TSynJScriptSyn.SetLine(NewValue: String; LineNumber: Integer);
begin
fLine := PChar(NewValue);
Run := 0;
fLineNumber := LineNumber;
Next;
end;
procedure TSynJScriptSyn.AndSymbolProc;
begin
fTokenID := tkSymbol;
inc(Run);
if fLine[Run] in ['=', '&'] then inc(Run);
end;
procedure TSynJScriptSyn.CommentProc;
begin
if fLine[Run] = #0 then
fTokenID := tkNull
else begin
fTokenID := tkComment;
repeat
if (fLine[Run] = '*') and (fLine[Run + 1] = '/') then begin
fRange := rsUnKnown;
inc(Run, 2);
break;
end;
inc(Run);
until fLine[Run] in [#0, #10, #13];
end;
end;
procedure TSynJScriptSyn.CRProc;
begin
fTokenID := tkSpace;
inc(Run);
if fLine[Run] = #10 then inc(Run);
end;
procedure TSynJScriptSyn.IdentProc;
begin
fTokenID := IdentKind((fLine + Run));
inc(Run, fStringLen);
while Identifiers[fLine[Run]] do inc(Run);
end;
procedure TSynJScriptSyn.LFProc;
begin
fTokenID := tkSpace;
inc(Run);
end;
procedure TSynJScriptSyn.MinusProc;
begin
fTokenID := tkSymbol;
inc(Run);
if fLine[Run] in ['=', '-', '>'] then inc(Run);
end;
procedure TSynJScriptSyn.ModSymbolProc;
begin
fTokenID := tkSymbol;
inc(Run);
if fLine[Run] = '=' then inc(Run);
end;
procedure TSynJScriptSyn.NullProc;
begin
fTokenID := tkNull;
end;
procedure TSynJScriptSyn.NumberProc;
var
idx1: Integer; // token[1]
isHex: Boolean;
begin
fTokenID := tkNumber;
isHex := False;
idx1 := Run;
Inc(Run);
while FLine[Run] in ['0'..'9', '.', 'a'..'f', 'A'..'F', 'x', 'X'] do
begin
case FLine[Run] of
'.':
if FLine[Succ(Run)] = '.' then
Break;
'a'..'f', 'A'..'F':
if not isHex then
Break;
'x', 'X':
begin
if (FLine[idx1] <> '0') or (Run > Succ(idx1)) then
Break;
if not (FLine[Succ(Run)] in ['0'..'9', 'a'..'f', 'A'..'F']) then
Break;
isHex := True;
end;
end;
Inc(Run);
end;
end;
procedure TSynJScriptSyn.OrSymbolProc;
begin
fTokenID := tkSymbol;
inc(Run);
if fLine[Run] in ['=', '|'] then inc(Run);
end;
procedure TSynJScriptSyn.PlusProc;
begin
fTokenID := tkSymbol;
inc(Run);
if fLine[Run] in ['=', '+'] then inc(Run);
end;
procedure TSynJScriptSyn.PointProc;
begin
fTokenID := tkSymbol;
inc(Run);
if (fLine[Run] = '.') and (fLine[Run + 1] = '.') then inc(Run, 2);
end;
procedure TSynJScriptSyn.SlashProc;
begin
Inc(Run);
case fLine[Run] of
'/': begin
fTokenID := tkComment;
repeat
Inc(Run);
until fLine[Run] in [#0, #10, #13];
end;
'*': begin
fTokenID := tkComment;
fRange := rsAnsi;
repeat
Inc(Run);
if (fLine[Run] = '*') and (fLine[Run + 1] = '/') then begin
fRange := rsUnKnown;
Inc(Run, 2);
break;
end;
until fLine[Run] in [#0, #10, #13];
end;
'=': begin
Inc(Run);
fTokenID := tkSymbol;
end;
else
fTokenID := tkSymbol;
end;
end;
procedure TSynJScriptSyn.SpaceProc;
begin
inc(Run);
fTokenID := tkSpace;
while FLine[Run] in [#1..#9, #11, #12, #14..#32] do inc(Run);
end;
procedure TSynJScriptSyn.StarProc;
begin
fTokenID := tkSymbol;
inc(Run);
if fLine[Run] = '=' then inc(Run);
end;
procedure TSynJScriptSyn.StringProc;
var
l_strChar : String;
begin
fTokenID := tkString;
l_strChar := FLine[Run]; // We could have '"' or #39
if (FLine[Run + 1] = l_strChar) and (FLine[Run + 2] = l_strChar) then inc(Run, 2);
repeat
case FLine[Run] of
#0, #10, #13: break;
end;
inc(Run);
until (FLine[Run] = l_strChar) and (FLine[Pred(Run)] <> '\');
if FLine[Run] <> #0 then
Inc(Run);
end;
procedure TSynJScriptSyn.SymbolProc;
begin
inc(Run);
fTokenId := tkSymbol;
end;
procedure TSynJScriptSyn.UnknownProc;
begin
{$IFDEF SYN_MBCSSUPPORT}
if FLine[Run] in LeadBytes then
Inc(Run, 2)
else
{$ENDIF}
inc(Run);
fTokenID := tkUnknown;
end;
procedure TSynJScriptSyn.Next;
begin
fTokenPos := Run;
if fRange = rsANSI then
CommentProc
else
fProcTable[fLine[Run]];
end;
function TSynJScriptSyn.GetDefaultAttribute(Index: integer): TSynHighlighterAttributes;
begin
case Index of
SYN_ATTR_COMMENT: Result := fCommentAttri;
SYN_ATTR_IDENTIFIER: Result := fIdentifierAttri;
SYN_ATTR_KEYWORD: Result := fKeyAttri;
SYN_ATTR_STRING: Result := fStringAttri;
SYN_ATTR_WHITESPACE: Result := fSpaceAttri;
SYN_ATTR_SYMBOL: Result := fSymbolAttri;
else
Result := nil;
end;
end;
function TSynJScriptSyn.GetEol: Boolean;
begin
Result := fTokenID = tkNull;
end;
function TSynJScriptSyn.GetRange: Pointer;
begin
Result := Pointer(fRange);
end;
function TSynJScriptSyn.GetToken: String;
var
Len: LongInt;
begin
Len := Run - fTokenPos;
SetString(Result, (FLine + fTokenPos), Len);
end;
function TSynJScriptSyn.GetTokenID: TtkTokenKind;
begin
Result := fTokenId;
end;
function TSynJScriptSyn.GetTokenAttribute: TSynHighlighterAttributes;
begin
case GetTokenID of
tkComment: Result := fCommentAttri;
tkIdentifier: Result := fIdentifierAttri;
tkKey: Result := fKeyAttri;
tkNonReservedKey: Result := fNonReservedKeyAttri;
tkEvent: Result := fEventAttri;
tkNumber: Result := fNumberAttri;
tkSpace: Result := fSpaceAttri;
tkString: Result := fStringAttri;
tkSymbol: Result := fSymbolAttri;
tkUnknown: Result := fIdentifierAttri;
else Result := nil;
end;
end;
function TSynJScriptSyn.GetTokenKind: integer;
begin
Result := Ord(fTokenId);
end;
function TSynJScriptSyn.GetTokenPos: Integer;
begin
Result := fTokenPos;
end;
procedure TSynJScriptSyn.ResetRange;
begin
fRange := rsUnknown;
end;
procedure TSynJScriptSyn.SetRange(Value: Pointer);
begin
fRange := TRangeState(Value);
end;
function TSynJScriptSyn.GetIdentChars: TSynIdentChars;
begin
Result := TSynValidStringChars;
end;
function TSynJScriptSyn.IsFilterStored: Boolean;
begin
Result := fDefaultFilter <> SYNS_FilterJScript;
end;
class function TSynJScriptSyn.GetLanguageName: string;
begin
Result := SYNS_LangJScript;
end;
function TSynJScriptSyn.GetSampleSource: String;
begin
Result := '// Syntax highlighting'#13#10+
'function printNumber()'#13#10+
'{'#13#10+
' var number = 1234;'#13#10+
' var x;'#13#10+
' document.write("The number is " + number);'#13#10+
' for (var i = 0; i <= number; i++)'#13#10+
' {'#13#10+
' x++;'#13#10+
' x--;'#13#10+
' x += 1.0;'#13#10+
' }'#13#10+
' i += @; // illegal character'#13#10+
'}'#13#10+
'body.onLoad = printNumber;';
end;
initialization
MakeIdentTable;
{$IFNDEF SYN_CPPB_1}
RegisterPlaceableHighlighter(TSynJScriptSyn);
{$ENDIF}
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -