nhasmsyn.pas

来自「本人买的<<VC++项目开发实例>>源代码配套光盘.」· PAS 代码 · 共 1,494 行 · 第 1/3 页

PAS
1,494
字号
{+--------------------------------------------------------------------------+
 |                 x86 Assembly Language Syntax Parser v0.57                |
 +--------------------------------------------------------------------------+
 |
 | Description:   An x86 Assembly Language syntax parser for use
 |                with mwCustomEdit. Supports all x86 Op codes, plus Intel MMX
 |                and AMD 3D Now Op codes.
 |
 +--------------------------------------------------------------------------+
 | Unit:          nhAsmSyn
 | Created:       07/99
 | Last change:   1999-10-27
 | Author:        Nick Hoddinott (nickh@conceptdelta.com)
 | Copyright      (c) 1999 All rights reserved
 |                (Large!) Portions Copyright Martin Waldenburg
 | Version:       0.57
 | Status:        Public Domain
 | DISCLAIMER:    This is provided as is, expressly without a warranty of any kind.
 |                You use it at your own risc.
 |
 | Thanks to:     Martin Waldenburg, Dave Muir, Hideo Koiso
 +--------------------------------------------------------------------------+
 |Know problems:
 |
 +--------------------------------------------------------------------------+
 |Revision History:
 |
 | 0.56:    * Adapted to the new mwEdit version 0.86
 | 0.55:    * Removed high level language keywords for time being.
 |            Added support for # and // comments. These are suppported by
 |            some assemblers.
 | 0.50:    * Initial version.
 +--------------------------------------------------------------------------+}

unit nhAsmSyn;

interface

uses
  SysUtils, Windows, Messages, Classes, Controls, Graphics, Registry, mwHighlighter;

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

  TRangeState = (rsANil, rsUnKnown);

  TProcTableProc = procedure of Object;
  TIdentFuncTableFunc = function: TtkTokenKind of Object;

type
  TnhAsmSyn = class(TmwCustomHighLighter)
  private
    fRange: TRangeState;
    fLine: PChar;
    fLineNumber: Integer;
    fProcTable: array[#0..#255] of TProcTableProc;
    Run: LongInt;
    fStringLen: Integer;
    fToIdent: PChar;
    fTokenPos: Integer;
    FTokenID: TtkTokenKind;

    {Syntax Highlight Attributes}
    fCommentAttri: TmwHighLightAttributes;
    fIdentifierAttri: TmwHighLightAttributes;
    fKeyAttri: TmwHighLightAttributes;
    fNumberAttri: TmwHighLightAttributes;
    fSpaceAttri: TmwHighLightAttributes;
    fStringAttri: TmwHighLightAttributes;
    fSymbolAttri: TmwHighLightAttributes;

    fIdentFuncTable: array[0..121] of TIdentFuncTableFunc;
    function KeyHash(ToHash: PChar): Integer;
    function KeyComp(const aKey: String): Boolean;

    function Func6:TtkTokenKind;
    function Func8:TtkTokenKind;
    function Func9:TtkTokenKind;
    function Func11:TtkTokenKind;
    function Func12:TtkTokenKind;
    function Func13:TtkTokenKind;
    function Func15:TtkTokenKind;
    function Func16:TtkTokenKind;
    function Func17:TtkTokenKind;
    function Func18:TtkTokenKind;
    function Func19:TtkTokenKind;
    function Func22:TtkTokenKind;
    function Func23:TtkTokenKind;
    function Func24:TtkTokenKind;
    function Func25:TtkTokenKind;
    function Func26:TtkTokenKind;
    function Func27:TtkTokenKind;
    function Func28:TtkTokenKind;
    function Func29:TtkTokenKind;
    function Func30:TtkTokenKind;
    function Func31:TtkTokenKind;
    function Func32: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 Func44:TtkTokenKind;
    function Func45:TtkTokenKind;
    function Func46:TtkTokenKind;
    function Func47:TtkTokenKind;
    function Func48:TtkTokenKind;
    function Func49:TtkTokenKind;
    function Func50:TtkTokenKind;
    function Func51:TtkTokenKind;
    function Func52:TtkTokenKind;
    function Func53:TtkTokenKind;
    function Func54:TtkTokenKind;
    function Func55:TtkTokenKind;
    function Func56:TtkTokenKind;
    function Func57:TtkTokenKind;
    function Func58:TtkTokenKind;
    function Func59:TtkTokenKind;
    function Func60:TtkTokenKind;
    function Func61:TtkTokenKind;
    function Func62:TtkTokenKind;
    function Func63:TtkTokenKind;
    function Func64:TtkTokenKind;
    function Func65:TtkTokenKind;
    function Func66:TtkTokenKind;
    function Func67:TtkTokenKind;
    function Func68:TtkTokenKind;
    function Func69:TtkTokenKind;
    function Func70:TtkTokenKind;
    function Func71:TtkTokenKind;
    function Func72:TtkTokenKind;
    function Func73:TtkTokenKind;
    function Func74:TtkTokenKind;
    function Func75:TtkTokenKind;
    function Func76:TtkTokenKind;
    function Func77:TtkTokenKind;
    function Func78:TtkTokenKind;
    function Func79:TtkTokenKind;
    function Func80:TtkTokenKind;
    function Func81:TtkTokenKind;
    function Func82:TtkTokenKind;
    function Func83:TtkTokenKind;
    function Func84:TtkTokenKind;
    function Func85:TtkTokenKind;
    function Func86:TtkTokenKind;
    function Func87:TtkTokenKind;
    function Func88:TtkTokenKind;
    function Func90:TtkTokenKind;
    function Func92:TtkTokenKind;
    function Func93:TtkTokenKind;
    function Func94:TtkTokenKind;
    function Func96:TtkTokenKind;
    function Func97:TtkTokenKind;
    function Func98:TtkTokenKind;
    function Func100:TtkTokenKind;
    function Func101:TtkTokenKind;
    function Func104:TtkTokenKind;
    function Func105:TtkTokenKind;
    function Func110:TtkTokenKind;
    function Func111:TtkTokenKind;
    function Func114:TtkTokenKind;
    function Func116:TtkTokenKind;
    function Func118:TtkTokenKind;
    function Func120:TtkTokenKind;
    function Func121:TtkTokenKind;

    procedure AmpersandProc;
    procedure ApostropheProc;
    procedure BraceCloseProc;
    procedure BraceOpenProc;
    procedure CRProc;
    procedure ColonProc;
    procedure CommaProc;
    procedure DateProc;
    procedure EqualProc;
    procedure ExponentiationProc;
    procedure GreaterProc;
    procedure IdentProc;
    procedure LFProc;
    procedure LowerProc;
    procedure MinusProc;
    procedure NullProc;
    procedure NumberProc;
    procedure PlusProc;
    procedure PointProc;
    procedure RoundCloseProc;
    procedure RoundOpenProc;
    procedure SemiColonProc;
    procedure SlashProc;
    procedure SpaceProc;
    procedure StarProc;
    procedure StringProc;
    procedure UnknownProc;
    function AltFunc: TtkTokenKind;
    procedure InitIdent;
    function IdentKind(MayBe: PChar): TtkTokenKind;
    procedure MakeMethodTables;
  protected
    {General Stuff}
    function GetIdentChars: TIdentChars; override;
    function GetLanguageName: string; override;
    function GetCapability: THighlighterCapability; override;
  public
    constructor Create(AOwner: TComponent); 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: TmwHighLightAttributes; override;
    function GetTokenKind: integer; override;
    function GetTokenPos: Integer; override;
    procedure Next; override;
    procedure SetRange(Value: Pointer); override;
    procedure ReSetRange; override;

    property IdentChars;
    property LanguageName;
    property Capability;
  published

    property CommentAttri: TmwHighLightAttributes read fCommentAttri write fCommentAttri;
    property IdentifierAttri: TmwHighLightAttributes read fIdentifierAttri write fIdentifierAttri;
    property KeyAttri: TmwHighLightAttributes read fKeyAttri write fKeyAttri;
    property NumberAttri: TmwHighLightAttributes read fNumberAttri write fNumberAttri;
    property SpaceAttri: TmwHighLightAttributes read fSpaceAttri write fSpaceAttri;
    property StringAttri: TmwHighLightAttributes read fStringAttri write fStringAttri;
    property SymbolAttri: TmwHighLightAttributes read fSymbolAttri write fSymbolAttri;
  end;

procedure Register;

implementation

uses mwLocalStr;

procedure Register;
begin
  RegisterComponents(MWS_HighlightersPage, [TnhAsmSyn]);
end;

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 := UpperCase(I)[1];
    Case I in['_', 'a'..'z', 'A'..'Z'] of
      True: mHashTable[I] := Ord(J) - 64
    else mHashTable[I] := 0;
    end;
  end;
end;

procedure TnhAsmSyn.InitIdent;
var
  I: Integer;
begin
  for I := 0 to 121 do
    Case I of
      6: fIdentFuncTable[I] := Func6;
      8: fIdentFuncTable[I] := Func8;
      9: fIdentFuncTable[I] := Func9;
      11: fIdentFuncTable[I] := Func11;
      12: fIdentFuncTable[I] := Func12;
      13: fIdentFuncTable[I] := Func13;
      15: fIdentFuncTable[I] := Func15;
      16: fIdentFuncTable[I] := Func16;
      17: fIdentFuncTable[I] := Func17;
      18: fIdentFuncTable[I] := Func18;
      19: fIdentFuncTable[I] := Func19;
      22: fIdentFuncTable[I] := Func22;
      23: fIdentFuncTable[I] := Func23;
      24: fIdentFuncTable[I] := Func24;
      25: fIdentFuncTable[I] := Func25;
      26: fIdentFuncTable[I] := Func26;
      27: fIdentFuncTable[I] := Func27;
      28: fIdentFuncTable[I] := Func28;
      29: fIdentFuncTable[I] := Func29;
      30: fIdentFuncTable[I] := Func30;
      31: fIdentFuncTable[I] := Func31;
      32: fIdentFuncTable[I] := Func32;
      33: fIdentFuncTable[I] := Func33;
      34: fIdentFuncTable[I] := Func34;
      35: fIdentFuncTable[I] := Func35;
      36: fIdentFuncTable[I] := Func36;
      37: fIdentFuncTable[I] := Func37;
      38: fIdentFuncTable[I] := Func38;
      39: fIdentFuncTable[I] := Func39;
      40: fIdentFuncTable[I] := Func40;
      41: fIdentFuncTable[I] := Func41;
      42: fIdentFuncTable[I] := Func42;
      43: fIdentFuncTable[I] := Func43;
      44: fIdentFuncTable[I] := Func44;
      45: fIdentFuncTable[I] := Func45;
      46: fIdentFuncTable[I] := Func46;
      47: fIdentFuncTable[I] := Func47;
      48: fIdentFuncTable[I] := Func48;
      49: fIdentFuncTable[I] := Func49;
      50: fIdentFuncTable[I] := Func50;
      51: fIdentFuncTable[I] := Func51;
      52: fIdentFuncTable[I] := Func52;
      53: fIdentFuncTable[I] := Func53;
      54: fIdentFuncTable[I] := Func54;
      55: fIdentFuncTable[I] := Func55;
      56: fIdentFuncTable[I] := Func56;
      57: fIdentFuncTable[I] := Func57;
      58: fIdentFuncTable[I] := Func58;
      59: fIdentFuncTable[I] := Func59;
      60: fIdentFuncTable[I] := Func60;
      61: fIdentFuncTable[I] := Func61;
      62: fIdentFuncTable[I] := Func62;
      63: fIdentFuncTable[I] := Func63;
      64: fIdentFuncTable[I] := Func64;
      65: fIdentFuncTable[I] := Func65;
      66: fIdentFuncTable[I] := Func66;
      67: fIdentFuncTable[I] := Func67;
      68: fIdentFuncTable[I] := Func68;
      69: fIdentFuncTable[I] := Func69;
      70: fIdentFuncTable[I] := Func70;
      71: fIdentFuncTable[I] := Func71;
      72: fIdentFuncTable[I] := Func72;
      73: fIdentFuncTable[I] := Func73;
      74: fIdentFuncTable[I] := Func74;
      75: fIdentFuncTable[I] := Func75;
      76: fIdentFuncTable[I] := Func76;
      77: fIdentFuncTable[I] := Func77;
      78: fIdentFuncTable[I] := Func78;
      79: fIdentFuncTable[I] := Func79;
      80: fIdentFuncTable[I] := Func80;
      81: fIdentFuncTable[I] := Func81;
      82: fIdentFuncTable[I] := Func82;
      83: fIdentFuncTable[I] := Func83;
      84: fIdentFuncTable[I] := Func84;
      85: fIdentFuncTable[I] := Func85;
      86: fIdentFuncTable[I] := Func86;
      87: fIdentFuncTable[I] := Func87;
      88: fIdentFuncTable[I] := Func88;
      90: fIdentFuncTable[I] := Func90;
      92: fIdentFuncTable[I] := Func92;
      93: fIdentFuncTable[I] := Func93;
      94: fIdentFuncTable[I] := Func94;
      96: fIdentFuncTable[I] := Func96;
      97: fIdentFuncTable[I] := Func97;
      98: fIdentFuncTable[I] := Func98;
      100: fIdentFuncTable[I] := Func100;
      101: fIdentFuncTable[I] := Func101;
      104: fIdentFuncTable[I] := Func104;
      105: fIdentFuncTable[I] := Func105;
      110: fIdentFuncTable[I] := Func110;
      111: fIdentFuncTable[I] := Func111;
      114: fIdentFuncTable[I] := Func114;
      116: fIdentFuncTable[I] := Func116;
      118: fIdentFuncTable[I] := Func118;
      120: fIdentFuncTable[I] := Func120;
      121: fIdentFuncTable[I] := Func121;
    else fIdentFuncTable[I] := AltFunc;
    end;
end;

function TnhAsmSyn.KeyHash(ToHash: PChar): Integer;
begin
  Result := 0;
  while ToHash^ in ['_', '0'..'9', 'a'..'z', 'A'..'Z'] do
  begin
    inc(Result, mHashTable[ToHash^]);
    inc(ToHash);
  end;
  fStringLen := ToHash - fToIdent;
end; { KeyHash }

function TnhAsmSyn.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; { KeyComp }

{----------Generated by mwSynGen ----------}

function TnhAsmSyn.Func6: TtkTokenKind;
begin
  if KeyComp('aad') then Result := tkKey else Result := tkIdentifier;
end;

function TnhAsmSyn.Func8: TtkTokenKind;
begin
  if KeyComp('adc') then Result := tkKey else Result := tkIdentifier;
end;

function TnhAsmSyn.Func9: TtkTokenKind;
begin
  if KeyComp('add') then Result := tkKey else Result := tkIdentifier;
end;

function TnhAsmSyn.Func11: TtkTokenKind;
begin
  if KeyComp('ja') then Result := tkKey else Result := tkIdentifier;
end;

function TnhAsmSyn.Func12: TtkTokenKind;
begin
  if KeyComp('jb') then Result := tkKey else Result := tkIdentifier;
end;

function TnhAsmSyn.Func13: TtkTokenKind;
begin
  if KeyComp('jc') then Result := tkKey else Result := tkIdentifier;
end;

function TnhAsmSyn.Func15: TtkTokenKind;
begin
  if KeyComp('je') then Result := tkKey else
    if KeyComp('aam') then Result := tkKey else
      if KeyComp('fadd') then Result := tkKey else Result := tkIdentifier;
end;

function TnhAsmSyn.Func16: TtkTokenKind;
begin
  if KeyComp('jae') then Result := tkKey else Result := tkIdentifier;
end;

function TnhAsmSyn.Func17: TtkTokenKind;
begin
  if KeyComp('jg') then Result := tkKey else
    if KeyComp('jbe') then Result := tkKey else Result := tkIdentifier;
end;

function TnhAsmSyn.Func18: TtkTokenKind;
begin
  if KeyComp('lea') then Result := tkKey else Result := tkIdentifier;
end;

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

function TnhAsmSyn.Func22: TtkTokenKind;
begin
  if KeyComp('fld') then Result := tkKey else
    if KeyComp('jl') then Result := tkKey else
      if KeyComp('jge') then Result := tkKey else
        if KeyComp('fld1') then Result := tkKey else Result := tkIdentifier;
end;

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

function TnhAsmSyn.Func24: TtkTokenKind;
begin
  if KeyComp('fbld') then Result := tkKey else
    if KeyComp('cdq') then Result := tkKey else
      if KeyComp('fiadd') then Result := tkKey else Result := tkIdentifier;
end;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?