// Converted by Nyitrai Péter for VB
// and adapted for TI Extended Basic & SXB by Stefan Bauch in 2021
{-------------------------------------------------------------------------------
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: SynHighlighterVB.pas, released 2000-04-20.
The Original Code is based on the wbADSP21xxSyn.pas file from the
mwEdit component suite by Martin Waldenburg and other developers, the Initial
Author of this file is Max Horváth.
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: SynHighlighterVB.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 a Visual Basic highlighter for SynEdit)
@author(Max Horváth <TheProfessor@gmx.de>, converted to SynEdit by David Muir <david@loanhead45.freeserve.co.uk>)
@created(5 December 1999, converted to SynEdit April 21, 2000)
@lastmod(2000-06-23)
The SynHighlighterVB unit provides SynEdit with a Visual Basic (.bas) highlighter.
}
unit uSynHighlighterXB;


{$IF defined(WINDOWS)}
  { $I C:\lazarus\components\synedit\SynEdit.inc}
{$ELSEIF defined(DARWIN)}
  {$I /Applications/Lazarus/components/synedit/SynEdit.inc}
{$ELSEIF defined(UNIX)}
  {$I /usr/share/lazarus/4.6.0/components/synedit/SynEdit.inc}
{$ENDIF}


interface

uses
  SysUtils, Classes,
  LCLIntf, LCLType,
  Controls, Graphics,
  SynEditTypes, SynEditHighlighter, SynEditStrConst;

type
  TtkTokenKind = (tkComment, tkIdentifier, tkKey, tkNull, tkNumber, tkSpace,
    tkString, tkSymbol, tkUnknown);

  TProcTableProc = procedure of object;

  PIdentFuncTableFunc = ^TIdentFuncTableFunc;
  TIdentFuncTableFunc = function: TtkTokenKind of object;

type
  TSynXBSyn = class(TSynCustomHighlighter)
  private
    fLine: PChar;
    fLineNumber: Integer;
    fProcTable: array[#0..#255] of TProcTableProc;
    Run: LongInt;
    fStringLen: Integer;
    fToIdent: PChar;
    fTokenPos: Integer;
    asmline,asmcomment:Integer;
    FTokenID: TtkTokenKind;
    fIdentFuncTable: array[0..133] of TIdentFuncTableFunc;
    fCommentAttri: TSynHighlighterAttributes;
    fIdentifierAttri: TSynHighlighterAttributes;
    fKeyAttri: TSynHighlighterAttributes;
    fNumberAttri: TSynHighlighterAttributes;
    fSpaceAttri: TSynHighlighterAttributes;
    fStringAttri: TSynHighlighterAttributes;
    fSymbolAttri: TSynHighlighterAttributes;
    function KeyHash(ToHash: PChar): Integer;
    function KeyComp(const aKey: String): Boolean;
    function Func15: TtkTokenKind;
    function Func19: TtkTokenKind;
    function Func21: TtkTokenKind;
    function Func22: TtkTokenKind;
    function Func23: TtkTokenKind;
    function Func25: TtkTokenKind;
    function Func26: TtkTokenKind;
    function Func27: TtkTokenKind;
    function Func28: TtkTokenKind;
    function Func29: TtkTokenKind;
    function Func31: TtkTokenKind;
    function Func33: TtkTokenKind;
    function Func34: TtkTokenKind;
    function Func35: TtkTokenKind;
    function Func36: TtkTokenKind;
    function Func37: TtkTokenKind;
    function Func38: TtkTokenKind;
    function Func39: TtkTokenKind;
    function Func40: TtkTokenKind;
    function Func41: TtkTokenKind;
    function Func42: TtkTokenKind;
    function Func43: TtkTokenKind;
    function Func45: TtkTokenKind;
    function Func46: TtkTokenKind;
    function Func47: TtkTokenKind;
    function Func48: TtkTokenKind;
    function Func49: TtkTokenKind;
    function Func50: TtkTokenKind;
    function Func51: TtkTokenKind;
    function Func53: TtkTokenKind;
    function Func54: TtkTokenKind;
    function Func56: TtkTokenKind;
    function Func57: TtkTokenKind;
    function Func59: TtkTokenKind;
    function Func60: TtkTokenKind;
    function Func63: TtkTokenKind;
    function Func64: TtkTokenKind;
    function Func65: TtkTokenKind;
    function Func67: TtkTokenKind;
    function Func70: TtkTokenKind;
    function Func71: TtkTokenKind;
    function Func72: TtkTokenKind;
    function Func74: TtkTokenKind;
    function Func76: TtkTokenKind;
    function Func77: TtkTokenKind;
    function Func80: TtkTokenKind;
    function Func82: TtkTokenKind;
    function Func83: TtkTokenKind;
    function Func86: TtkTokenKind;
    function Func89: TtkTokenKind;
    function Func92: TtkTokenKind;
    function Func93: TtkTokenKind;
    function Func96: TtkTokenKind;
    function Func100: TtkTokenKind;
    function Func105: TtkTokenKind;
    function Func106: TtkTokenKind;
    function Func113: TtkTokenKind;
    function Func123: TtkTokenKind;
    procedure SymbolProc;
    procedure ApostropheProc;
    procedure CRProc;
    procedure DateProc;
    procedure GreaterProc;
    procedure IdentProc;
    procedure LFProc;
    procedure LowerProc;
    procedure NullProc;
    procedure NumberProc;
    procedure SpaceProc;
    procedure StringProc;
    procedure UnknownProc;
    function AltFunc: TtkTokenKind;
    procedure InitIdent;
    function IdentKind(MayBe: PChar): TtkTokenKind;
    procedure MakeMethodTables;
  protected
    function GetIdentChars: TSynIdentChars; override;
    function GetSampleSource: String; override;
    function IsFilterStored: Boolean; override;
  public
    class function GetLanguageName: string; override;
  public
    constructor Create(AOwner: TComponent); override;
    function GetDefaultAttribute(Index: integer): TSynHighlighterAttributes;
      override;
    function GetEol: Boolean; 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;

  published
    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 SpaceAttri: TSynHighlighterAttributes read fSpaceAttri
      write fSpaceAttri;
    property StringAttri: TSynHighlighterAttributes read fStringAttri
      write fStringAttri;
    property SymbolAttri: TSynHighlighterAttributes read fSymbolAttri
      write fSymbolAttri;
  end;

implementation

const asmkeywords ='A,AB,ABS,AI,C,CB,CI,DEC,DECT,DIV,INC,INCT,LI,MOV,MOVB,MPY,NEG,NOP,S,SB,ANDI,CLR,COC,CZC,INV,ORI,SETO,SOC,SOCB,SZC,SZCB,XOR,B,BL,BLWP,JEQ,JGT,JH,JHE,JL,JLE,JLT,JMP,JNC,JNE,JNO,JOC,JOP,RTWP,XOP,SLA,SRA,SRC,SRL,LDCR,SBO,SBZ,TB,STCR,LIMI,LWPI,STST,STWP,SWPB,X,DATA,BYTE,TEXT,BSS,BES,EVEN,RT,DEF,REF,AORG,RORG,END,COPY,BREAK,';

var
  Identifiers: array[#0..#255] of ByteBool;
  mHashTable: array[#0..#255] of Integer;

procedure MakeIdentTable;
var
  I, J: Char;
begin
  for I := #0 to #255 do
  begin
    Case I of
      '_', '0'..'9', 'a'..'z', 'A'..'Z': Identifiers[I] := True;
    else Identifiers[I] := False;
    end;
    J := UpCase(I);
    Case I in ['_', 'A'..'Z', 'a'..'z'] of
      True: mHashTable[I] := Ord(J) - 64
    else mHashTable[I] := 0;
    end;
  end;
end;

procedure TSynXBSyn.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[21] := @Func21;
  fIdentFuncTable[22] := @Func22;
  fIdentFuncTable[23] := @Func23;
  fIdentFuncTable[25] := @Func25;
  fIdentFuncTable[26] := @Func26;
  fIdentFuncTable[27] := @Func27;
  fIdentFuncTable[28] := @Func28;
  fIdentFuncTable[29] := @Func29;
  fIdentFuncTable[31] := @Func31;
  fIdentFuncTable[33] := @Func33;
  fIdentFuncTable[34] := @Func34;
  fIdentFuncTable[35] := @Func35;
  fIdentFuncTable[36] := @Func36;
  fIdentFuncTable[37] := @Func37;
  fIdentFuncTable[38] := @Func38;
  fIdentFuncTable[39] := @Func39;
  fIdentFuncTable[40] := @Func40;
  fIdentFuncTable[41] := @Func41;
  fIdentFuncTable[42] := @Func42;
  fIdentFuncTable[43] := @Func43;
  fIdentFuncTable[45] := @Func45;
  fIdentFuncTable[46] := @Func46;
  fIdentFuncTable[47] := @Func47;
  fIdentFuncTable[48] := @Func48;
  fIdentFuncTable[49] := @Func49;
  fIdentFuncTable[50] := @Func50;
  fIdentFuncTable[51] := @Func51;
  fIdentFuncTable[53] := @Func53;
  fIdentFuncTable[54] := @Func54;
  fIdentFuncTable[56] := @Func56;
  fIdentFuncTable[57] := @Func57;
  fIdentFuncTable[59] := @Func59;
  fIdentFuncTable[60] := @Func60;
  fIdentFuncTable[63] := @Func63;
  fIdentFuncTable[64] := @Func64;
  fIdentFuncTable[65] := @Func65;
  fIdentFuncTable[67] := @Func67;
  fIdentFuncTable[70] := @Func70;
  fIdentFuncTable[71] := @Func71;
  fIdentFuncTable[72] := @Func72;
  fIdentFuncTable[74] := @Func74;
  fIdentFuncTable[76] := @Func76;
  fIdentFuncTable[77] := @Func77;
  fIdentFuncTable[80] := @Func80;
  fIdentFuncTable[82] := @Func82;
  fIdentFuncTable[83] := @Func83;
  fIdentFuncTable[86] := @Func86;
  fIdentFuncTable[89] := @Func89;
  fIdentFuncTable[92] := @Func92;
  fIdentFuncTable[93] := @Func93;
  fIdentFuncTable[96] := @Func96;
  fIdentFuncTable[100] := @Func100;
  fIdentFuncTable[105] := @Func105;
  fIdentFuncTable[106] := @Func106;
  fIdentFuncTable[113] := @Func113;
  fIdentFuncTable[123] := @Func123;
end;

function TSynXBSyn.KeyHash(ToHash: PChar): Integer;
begin
  if tohash='movb' then
       Result := 0;  // Breakpoint for hash value reading
  Result := 0;
  while ToHash^ in ['_', '0'..'9', 'a'..'z', 'A'..'Z'] do
  begin
    inc(Result, mHashTable[ToHash^]);
    inc(ToHash);
  end;
  fStringLen := ToHash - fToIdent;
end;

function TSynXBSyn.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 mHashTable[Temp^] <> mHashTable[aKey[i]] then
      begin
        Result := False;
        break;
      end;
      inc(Temp);
    end;
  end else Result := False;
end;

function TSynXBSyn.Func15: TtkTokenKind;
begin
  if KeyComp('def') then Result := tkKey else
    if KeyComp('if') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func19: TtkTokenKind;
begin
  if KeyComp('and') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func21: TtkTokenKind;
begin
  if KeyComp('of') then Result := tkKey else
    if KeyComp('at') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func22: TtkTokenKind;
begin
  if KeyComp('go') then Result := tkKey else
    if KeyComp('abs') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func23: TtkTokenKind;
begin
  if KeyComp('in') then Result := tkKey else
    if KeyComp('end') then Result := tkKey else
      if KeyComp('asc') then Result := tkKey else
        if KeyComp('tab') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func25: TtkTokenKind;
begin
  if KeyComp('pi') then Result := tkKey else
    if KeyComp('all') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func26: TtkTokenKind;
begin
  if KeyComp('data') then Result := tkKey else
    if KeyComp('eof') then Result := tkKey else
      if KeyComp('rec') then Result := tkKey else
        if KeyComp('dim') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func27: TtkTokenKind;
begin
  if KeyComp('base') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func28: TtkTokenKind;
begin
  if KeyComp('read') then Result := tkKey else
    if KeyComp('beep') then Result := tkKey else
      if KeyComp('call') then Result := tkKey else
        if KeyComp('case') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func29: TtkTokenKind;
begin
  if KeyComp('on') then Result := tkKey else
    if KeyComp('chr') then Result := tkKey else Result := tkIdentifier;
end;


function TSynXBSyn.Func31: TtkTokenKind;
begin
  if KeyComp('seg') then Result := tkKey else
    if KeyComp('len') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func33: TtkTokenKind;
begin
  if KeyComp('or') then Result := tkKey else
    if KeyComp('asm') then Result := tkKey else
      Result := tkIdentifier;
end;

function TSynXBSyn.Func34: TtkTokenKind;
begin
  if KeyComp('log') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func35: TtkTokenKind;
begin
  if KeyComp('val') then Result := tkKey else
    if KeyComp('tan') then Result := tkKey else
      if KeyComp('to') then Result := tkKey else
        if KeyComp('image') then Result := tkKey else
          if KeyComp('atn') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func36: TtkTokenKind;
begin
  if KeyComp('min') then Result := tkKey else
    if KeyComp('rnd') then Result := tkKey else
      if KeyComp('rem') then
      begin
        ApostropheProc;
        fStringLen:=0;
        Result := tkComment;
      end
      else
        Result := tkIdentifier;
end;

function TSynXBSyn.Func37: TtkTokenKind;
begin
  if KeyComp('cos') then Result := tkKey else
    if KeyComp('begin') then Result := tkKey else
      if KeyComp('let') then Result := tkKey else
        if KeyComp('break') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func38: TtkTokenKind;
begin
  if KeyComp('alpha') then Result := tkKey else
    if KeyComp('max') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func39: TtkTokenKind;
begin
  if KeyComp('for') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func40: TtkTokenKind;
begin
  if KeyComp('sgn') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func41: TtkTokenKind;
begin
  if KeyComp('err') then Result := tkKey else
    if KeyComp('else') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func42: TtkTokenKind;
begin
  if KeyComp('sub') then Result := tkKey else
    if KeyComp('sin') then Result := tkKey else
      if KeyComp('new') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func43: TtkTokenKind;
begin
  if KeyComp('int') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func45: TtkTokenKind;
begin
  if KeyComp('exp') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func46: TtkTokenKind;
begin
  if KeyComp('wend') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func47: TtkTokenKind;
begin
  if KeyComp('trace') then Result := tkKey else
    if KeyComp('then') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func48: TtkTokenKind;
begin
  if KeyComp('fixed') then Result := tkKey else
    if KeyComp('accept') then Result := tkKey else
      if KeyComp('erase') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func49: TtkTokenKind;
begin
  if KeyComp('digit') then Result := tkKey else
    if KeyComp('not') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func50: TtkTokenKind;
begin
  if KeyComp('pos') then Result := tkKey else
    if KeyComp('open') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func51: TtkTokenKind;
begin
  if KeyComp('endcase') then Result := tkKey else
    if KeyComp('delete') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func53: TtkTokenKind;
begin
  if KeyComp('run') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func54: TtkTokenKind;
begin
  if KeyComp('sqr') then Result := tkKey else
    if KeyComp('rpt') then Result := tkKey else
      if KeyComp('close') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func56: TtkTokenKind;
begin
  if KeyComp('append') then Result := tkKey else
    if KeyComp('asmend') then Result := tkKey else
       Result := tkIdentifier;
end;

function TSynXBSyn.Func57: TtkTokenKind;
begin
  if KeyComp('str') then Result := tkKey else
    if KeyComp('xor') then Result := tkKey else
      if KeyComp('goto') then Result := tkKey else
        if KeyComp('while') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func59: TtkTokenKind;
begin
  if KeyComp('size') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func60: TtkTokenKind;
begin
  if KeyComp('with') then Result := tkKey else
    if KeyComp('step') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func63: TtkTokenKind;
begin
  if KeyComp('next') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func64: TtkTokenKind;
begin
  if KeyComp('gosub') then Result := tkKey else
    if KeyComp('width') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func65: TtkTokenKind;
begin
  if KeyComp('subend') then Result := tkKey else
    if KeyComp('repeat') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func67: TtkTokenKind;
begin
  if KeyComp('update') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func70: TtkTokenKind;
begin
  if KeyComp('stop') then Result := tkKey else
    if KeyComp('variable') then Result := tkKey else
      if KeyComp('using') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func71: TtkTokenKind;
begin
  if KeyComp('const') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func72: TtkTokenKind;
begin
  if KeyComp('unbreak') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func74: TtkTokenKind;
begin
  if KeyComp('validate') then Result := tkKey else
    if KeyComp('error') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func76: TtkTokenKind;
begin
  if KeyComp('until') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func77: TtkTokenKind;
begin
  if KeyComp('print') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func80: TtkTokenKind;
begin
  if KeyComp('input') then Result := tkKey else
    if KeyComp('endwhile') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func82: TtkTokenKind;
begin
  if KeyComp('untrace') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func83: TtkTokenKind;
begin
  if KeyComp('numeric') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func86: TtkTokenKind;
begin
  if KeyComp('warning') then Result := tkKey else
    if KeyComp('display') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func89: TtkTokenKind;
begin
  if KeyComp('option') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func92: TtkTokenKind;
begin
  if KeyComp('relative') then Result := tkKey else
    if KeyComp('linput') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func93: TtkTokenKind;
begin
  if KeyComp('internal') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func96: TtkTokenKind;
begin
  if KeyComp('return') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func100: TtkTokenKind;
begin
  if KeyComp('restore') then Result := tkKey else
    if KeyComp('subexit') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func105: TtkTokenKind;
begin
  if KeyComp('randomize') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func106: TtkTokenKind;
begin
  if KeyComp('permanent') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func113: TtkTokenKind;
begin
  if KeyComp('output') then Result := tkKey else Result := tkIdentifier;
end;

function TSynXBSyn.Func123: TtkTokenKind;
begin
  if KeyComp('sequential') then Result := tkKey else Result := tkIdentifier;
end;


function TSynXBSyn.AltFunc: TtkTokenKind;
begin
  Result := tkIdentifier;
end;

function TSynXBSyn.IdentKind(MayBe: PChar): TtkTokenKind;
var
  HashKey,i: Integer;
  s : string;
begin
  // Check for ASM keywords based on position
  if (run=7) and (fline[6]=' ') then begin
    i:=0;
    while (run+i<length(fline)) and (fline[run+i]<>' ') do inc(i);
    s:=uppercase(copy(fline,run+1,i));
    if pos(s+',',asmkeywords)>0 then begin
      result:=tkKey;
      fStringLen:=length(s);
      asmline   :=fLineNumber;
      i:=run+fStringLen;
      if (s='RT') or (s='EVEN') or (s='RTWP') or (s='END') or (s='BREAK') then asmcomment:=i+1 else
      begin
        // look for the start of the comment part
        while (i<length(fline)) and (FLine[i] in [#1..#9, #11, #12, #14..#32]) do inc(i);
        while (i<length(fline)) and not (FLine[i] in [#1..#9, #11, #12, #14..#32]) do inc(i);
        asmcomment:=i;
      end;
      exit;
    end;
  end;
  if (fLineNumber=asmline) and (run>=asmcomment) then begin
    result:=tkComment;
    fStringLen  := length(fLine)-run;
    exit
  end;
  if (fLineNumber<>asmline) then asmline:=-1;
  fToIdent := MayBe;
  HashKey := KeyHash(MayBe);
  if HashKey < 134 then
    Result := fIdentFuncTable[HashKey]()
  else
    Result := tkIdentifier; 
end;

procedure TSynXBSyn.MakeMethodTables;
var
  I: Char;
begin
  for I := #0 to #255 do
    case I of
      '&': fProcTable[I] := @SymbolProc;
      #39: fProcTable[I] := @ApostropheProc;
      #33: fProcTable[I] := @ApostropheProc; // SXB ! as comment
      '}': fProcTable[I] := @SymbolProc;
      '{': fProcTable[I] := @SymbolProc;
      #13: fProcTable[I] := @CRProc;
      ':': fProcTable[I] := @SymbolProc;
      ',': fProcTable[I] := @SymbolProc;
      '#': fProcTable[I] := @SymbolProc; // was     '#': fProcTable[I] := @DateProc;
      '=': fProcTable[I] := @SymbolProc;
      '^': fProcTable[I] := @SymbolProc;
      '>': fProcTable[I] := @GreaterProc;
      'A'..'Z', 'a'..'z', '_': fProcTable[I] := @IdentProc;
      #10: fProcTable[I] := @LFProc;
      '<': fProcTable[I] := @LowerProc;
      '-': fProcTable[I] := @SymbolProc;
      #0: fProcTable[I] := @NullProc;
      '0'..'9': fProcTable[I] := @NumberProc;
      '+': fProcTable[I] := @SymbolProc;
      '.': fProcTable[I] := @SymbolProc;
      ')': fProcTable[I] := @SymbolProc;
      '(': fProcTable[I] := @SymbolProc;
      ';': fProcTable[I] := @SymbolProc;
      '/': fProcTable[I] := @ApostropheProc; // was   '/': fProcTable[I] := @SymbolProc;
      '\': fProcTable[I] := @SymbolProc; // integer division
       #1..#9, #11, #12, #14..#32: fProcTable[I] := @SpaceProc;
      '*': fProcTable[I] := @SymbolProc;
      #34: fProcTable[I] := @StringProc;
      else fProcTable[I] := @UnknownProc;
    end;
end;

constructor TSynXBSyn.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  fCommentAttri := TSynHighlighterAttributes.Create(@SYNS_AttrComment, SYNS_XML_AttrComment);
  fCommentAttri.Style:= [fsItalic];
  AddAttribute(fCommentAttri);
  fIdentifierAttri := TSynHighlighterAttributes.Create(@SYNS_AttrIdentifier, SYNS_XML_AttrIdentifier);
  AddAttribute(fIdentifierAttri);
  fKeyAttri := TSynHighlighterAttributes.Create(@SYNS_AttrReservedWord, SYNS_XML_AttrReservedWord);
  fKeyAttri.Style:= [fsBold];
  AddAttribute(fKeyAttri);
  fNumberAttri := TSynHighlighterAttributes.Create(@SYNS_AttrNumber, SYNS_XML_AttrNumber);
  AddAttribute(fNumberAttri);
  fSpaceAttri := TSynHighlighterAttributes.Create(@SYNS_AttrSpace, SYNS_XML_AttrSpace);
  AddAttribute(fSpaceAttri);
  fStringAttri := TSynHighlighterAttributes.Create(@SYNS_AttrString, SYNS_XML_AttrString);
  AddAttribute(fStringAttri);
  fSymbolAttri := TSynHighlighterAttributes.Create(@SYNS_AttrSymbol, SYNS_XML_AttrSymbol);
  AddAttribute(fSymbolAttri);
  SetAttributesOnChange(@DefHighlightChange);
  InitIdent;
  MakeMethodTables;
  fDefaultFilter := SYNS_FilterVisualBASIC;
  asmline := -1;
  asmcomment:=255;
end;

procedure TSynXBSyn.SetLine(const NewValue: String; LineNumber: Integer);
begin
  inherited;
  fLine := PChar(NewValue);
  Run := 0;
  fLineNumber := LineNumber;
  Next;
end;

procedure TSynXBSyn.SymbolProc;
begin
  // embedded assembler comment
  if (run=0) and (fLine[0]='*') then begin
      fTokenId := tkComment;
      run:=length(fLine)
  end else begin
    inc(Run);
    fTokenId := tkSymbol;
  end;
end;

procedure TSynXBSyn.ApostropheProc;
begin      // in SXB a single slash is a Symbol, a double Slash starts a comment
  if (fLine[run]='/') and (length(fLine)>run) and (fLine[run+1]<>'/') then SymbolProc else
  begin
    fTokenID := tkComment;
    repeat
      Inc(Run);
    until fLine[Run] in [#0, #10, #13];
  end;
end;


procedure TSynXBSyn.CRProc;
begin
  fTokenID := tkSpace;
  Inc(Run);
  if fLine[Run] = #10 then Inc(Run);
end;

procedure TSynXBSyn.DateProc;
begin
  fTokenID := tkString;
  repeat
    case FLine[Run] of
      #0, #10, #13: break;
    end;
    inc(Run);
  until FLine[Run] = '#';
  if FLine[Run] <> #0 then inc(Run);
end;

procedure TSynXBSyn.GreaterProc;
begin
  fTokenID := tkSymbol;
  Inc(Run);
  if fLine[Run] = '=' then Inc(Run);
end;

procedure TSynXBSyn.IdentProc;
begin
  fTokenID := IdentKind((fLine + Run));
  inc(Run, fStringLen);
  while Identifiers[fLine[Run]] do inc(Run);
end;

procedure TSynXBSyn.LFProc;
begin
  fTokenID := tkSpace;
  inc(Run);
end;

procedure TSynXBSyn.LowerProc;
begin
  fTokenID := tkSymbol;
  Inc(Run);
  if fLine[Run] in ['=', '>'] then Inc(Run);
end;

procedure TSynXBSyn.NullProc;
begin
  fTokenID := tkNull;
end;

procedure TSynXBSyn.NumberProc;
begin
  inc(Run);
  fTokenID := tkNumber;
  while FLine[Run] in ['0'..'9'] do inc(Run);
  if (FLine[Run]='.') and not(fLine[Run+1]='.')  then begin
    inc(Run);
    while FLine[Run] in ['0'..'9'] do inc(Run);
  end;
  if (FLine[Run]='e') or (fLine[Run]='E')  then begin
    inc(Run);
    if (FLine[Run]='+') or (fLine[Run]='-')  then inc(Run);
    while FLine[Run] in ['0'..'9'] do inc(Run);
  end;
end;

procedure TSynXBSyn.SpaceProc;
begin
  inc(Run);
  fTokenID := tkSpace;
  while FLine[Run] in [#1..#9, #11, #12, #14..#32] do inc(Run);
end;

procedure TSynXBSyn.StringProc;
begin
  fTokenID := tkString;
  if (FLine[Run + 1] = #34) and (FLine[Run + 2] = #34) then inc(Run, 2);
  repeat
    case FLine[Run] of
      #0, #10, #13: break;
    end;
    inc(Run);
  until FLine[Run] = #34;
  if FLine[Run] <> #0 then inc(Run);
end;

procedure TSynXBSyn.UnknownProc;
begin
{$IFDEF SYN_MBCSSUPPORT}
  if FLine[Run] in LeadBytes then
    Inc(Run, 2)
  else
{$ENDIF}
  inc(Run);
  while (fLine[Run] in [#128..#191]) OR // continued utf8 subcode
   ((fLine[Run]<>#0) and (fProcTable[fLine[Run]] = @UnknownProc)) do inc(Run);
  fTokenID := tkUnknown;
end;

procedure TSynXBSyn.Next;
begin
  fTokenPos := Run;
  fProcTable[fLine[Run]]();
end;

function TSynXBSyn.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;
    SYN_ATTR_NUMBER: Result := fNumberAttri;
  else
    Result := nil;
  end;
end;

function TSynXBSyn.GetEol: Boolean;
begin
  Result := fTokenID = tkNull;
end;

function TSynXBSyn.GetToken: String;
var
  Len: LongInt;
begin
  Result := '';
  Len := Run - fTokenPos;
  SetString(Result, (FLine + fTokenPos), Len);
end;

procedure TSynXBSyn.GetTokenEx(out TokenStart: PChar;
  out TokenLength: integer);
begin
  TokenLength:=Run-fTokenPos;
  TokenStart:=FLine + fTokenPos;
end;

function TSynXBSyn.GetTokenID: TtkTokenKind;
begin
  Result := fTokenId;
end;

function TSynXBSyn.GetTokenAttribute: TSynHighlighterAttributes;
begin
  case GetTokenID of
    tkComment: Result := fCommentAttri;
    tkIdentifier: Result := fIdentifierAttri;
    tkKey: Result := fKeyAttri;
    tkNumber: Result := fNumberAttri;
    tkSpace: Result := fSpaceAttri;
    tkString: Result := fStringAttri;
    tkSymbol: Result := fSymbolAttri;
    tkUnknown: Result := fIdentifierAttri;
    else Result := nil;
  end;
end;

function TSynXBSyn.GetTokenKind: integer;
begin
  Result := Ord(fTokenId);
end;

function TSynXBSyn.GetTokenPos: Integer;
begin
  Result := fTokenPos;
end;

function TSynXBSyn.GetIdentChars: TSynIdentChars;
begin
  Result := TSynValidStringChars;
end;

function TSynXBSyn.IsFilterStored: Boolean;
begin
  Result := fDefaultFilter <> SYNS_FilterVisualBASIC;
end;

class function TSynXBSyn.GetLanguageName: string;
begin
  Result := SYNS_LangVisualBASIC;
end;

function TSynXBSyn.GetSampleSource: String;
begin
  Result := ''' Syntax highlighting'#13#10+
            'Function PrintNumber'#13#10+
            '  Dim Number'#13#10+
            '  Dim X'#13#10+
            ''#13#10+
            '  Number = 123456'#13#10+
            '  Response.Write "The number is " & number'#13#10+
            ''#13#10+
            '  For I = 0 To Number'#13#10+
            '    X = X + &h4c'#13#10+
            '    X = X - &o8'#13#10+
            '    X = X + 1.0'#13#10+
            '  Next'#13#10+
            ''#13#10+
            '  I = I + @;  '' illegal character'#13#10+
            'End Function';
end;

initialization
  MakeIdentTable;
  RegisterPlaceableHighlighter(TSynXBSyn);
  RegisterComponents('SynEdit', [TSynXBSyn]);
end.
