syngenunit.pas

来自「一个mwEdit控件原码,比mwCuuEdit0.92a功能先进.」· PAS 代码 · 共 1,810 行 · 第 1/5 页

PAS
1,810
字号
      CboUnknownTokenAttr.ItemIndex := CboUnknownTokenAttr.Items.IndexOf
        (ReadString(FFileName, 'UnknownTokenAttr', 'Identifier'));
      ListBoxFields.Items.CommaText := ReadString(FFileName, 'Fields', '');
    finally
      Free;
    end;
    StrDispose(UserName);
    CboLangNameChange(Self);    
  end
  else
    Result := False;
end;

procedure TFrmMain.FormCreate(Sender: TObject);
var i: Integer;
begin
  for i := FrmMain.ComponentCount - 1 downto 0 do
    if FrmMain.Components[i] is TComboBox then
      if TComboBox(FrmMain.Components[i]).Parent = GrpAttrNames then
      begin
        TComboBox(FrmMain.Components[i]).Items.Clear;
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrAsm');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrAsmComment');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrAsmKey');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrASP');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrAssembler');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrBlock');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrBrackets');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrCharacter');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrClass');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrComment');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrCondition');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrDir');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrDirective');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrDocumentation');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrEmbedSQL');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrEmbedText');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrEscapeAmpersand');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrForm');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrFunction');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrIcon');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrIdentifier');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrIllegalChar');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrIndirect');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrInvalidSymbol');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrInternalFunction');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrKey');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrLabel');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrMacro');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrMarker');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrMessage');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrMiscellaneous');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrNull');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrNumber');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrOperator');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrPragma');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrPreprocessor');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrQualifier');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrRegister');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrReservedWord');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrRpl');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrRplKey');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrRplComment');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrSASM');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrSASMComment');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrSASMKey');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrSecondReservedWord');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrSection');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrSpace');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrSpecialVariable');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrString');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrSymbol');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrSyntaxError');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrSystem');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrSystemValue');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrText');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrUnknownWord');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrUser');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrUserFunction');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrValue');
        TComboBox(FrmMain.Components[i]).Items.Add('SYNS_AttrVariable');
      end;
  PageControl.ActivePage := PageControl.Pages[0];
  Stream := TMemoryStream.Create;
  Lex := TGenLex.Create;
  KeyList := TList.Create;
  SetList := TList.Create;
  EnclosedList := TList.Create;
  SampleSourceList := TStringList.Create;
  IdentList := TStringList.Create;
  // read ini file
  IniFile := Copy(ExtractFileName(Application.ExeName), 0,
             Length(ExtractFileName(Application.ExeName)) -
             Length(ExtractFileExt(Application.ExeName))) + '.ini';
  with TRegIniFile.Create(IniFile) do
  try
    OpenDialog.InitialDir := ReadString('General', 'OpenDir',
                             ExtractFilePath(Application.ExeName));
  finally
    Free;
  end;
  
  if PerformFileOpen then
  begin
    MakeHashTable;
    MakeSensitiveHashTable;
  end
  else
    Halt;
end;

procedure TFrmMain.ClearAll;
var
  I: Integer;
begin
// Clear the contents of KeyList 
  for I := 0 to (KeyList.Count - 1) do
    TObject(KeyList[I]).Free;
  KeyList.Clear;
// Clear the contents of SetList
  for I := 0 to (SetList.Count - 1) do
    TObject(SetList[I]).Free;
  SetList.Clear;
// Clear the contents of EnclosedList
  for I := 0 to (EnclosedList.Count - 1) do
    TObject(EnclosedList[I]).Free;
  EnclosedList.Clear;
// Clear the contents of IdentList
  for I := 0 to (IdentList.Count - 1) do
  begin
    if Assigned(IdentList.Objects[I]) then
      TObject(IdentList.Objects[I]).Free;
  end;
  IdentList.Clear;
// Clear the contents of SampleSourceList
  SampleSourceList.Clear;
end;

procedure TFrmMain.FormDestroy(Sender: TObject);
begin
  ClearAll;
  Lex.Free;
  Stream.Free;
  IdentList.Free;
  KeyList.Free;
  SetList.Free;
  EnclosedList.Free;
end;

procedure TFrmMain.BtnStartClick(Sender: TObject);
begin
  ClearAll;
  
  Screen.Cursor := crHourGlass;
  Stream.Clear;
  Stream.LoadFromFile(OpenDialog.FileName);
  Lex.Origin := Stream.Memory;
  Lex.Tokenize;

  while Lex.RunId <> IDIdentifier do Lex.Next;
  LexName := Lex.RunToken;

  Lex.Next;
  while Lex.RunId <> IDIdentifier do Lex.Next;
  IdentPre := Lex.RunToken;

  OutFileCreate(OpenDialog.FileName);
  try
    while not (Lex.RunId in [IdSensitive, IdIdentStart]) do
      Lex.Next;

    if Lex.RunId = IdSensitive then
      Sensitivity := True
    else
      Sensitivity := False;
    Lex.Next;

    while Lex.RunId <> IDCharSet do Lex.Next;
    IdentContent := Lex.RunToken;
    Lex.Next;

    while Lex.RunId <> IDNull do
    begin
      case Lex.RunId of
        IDCharSet      : IdentStart := Lex.RunToken;
        IDKeys         : FillKeyList;
        IDTokenTypes   : FillTokenTypeList;
        IDChars        : ParseCharSets;
        IDEnclosedBy   : ParseEnclosedBy;
        IDSampleSource : ParseSampleSource;
      end;
      Lex.Next;
    end;
    
    if (KeyList.Count = 0) then
      raise Exception.Create('You should specify at least 1 keyword!');
    if (IdentList.Count = 0) then
      raise Exception.Create('You should specify at least 1 token type');
      
    WriteRest;
    while (Lex.RunId <> IdNull) do
    begin
      Lex.Next;
    end;
  finally
    Screen.Cursor := crDefault;
    CloseFile(OutFile);
  end;
  MessageDlg(LexName + ' created on ' + DateTimeToStr(Now), mtInformation, [mbOk], 0);
end;

procedure TFrmMain.FillKeyList;
var
  aLexKey: TLexKeys;
  aString: String;
  aTokenType: String;
begin
  Lex.Next;

  aTokenType := '';
  while Lex.RunId <> IdCRLF do
  begin
    if not (Lex.RunId in [IdSpace, IdBraceOpen]) then
      aTokenType := aTokenType + Lex.RunToken;
    Lex.Next;
  end;

  if (aTokenType = '') then
    aTokenType := 'Key';

  while Lex.RunId <> IdStop do
  begin
    while Lex.RunId in [IdSpace, IdBraceOpen, IdCRLF] do Lex.Next;
    if Lex.RunId <> IdStop then
    begin
      aString:= '';
      while not (Lex.RunId in [IdSpace, IdBraceOpen, IdCRLF]) do
      begin
        aString:= aString + Lex.RunToken;
        Lex.Next;
      end;
      aLexKey := TLexKeys.Create;
      aLexKey.TokenType := aTokenType;
      aLexKey.KeyName := aString;
      if Sensitivity then
        aLexKey.Key := SensKeyHash(aLexKey.KeyName)
      else
        aLexKey.Key := KeyHash(aLexKey.KeyName);
      KeyList.Add(aLexKey);
    end
    else
      Break;
    Lex.Next;
  end;
  KeyList.Sort(CompareKeys);
end;

procedure TFrmMain.FillTokenTypeList;
var
  i: Integer;
  List: TStringList;
  sIdent: String;
  sLine: String;
  DefAttri: TLexDefaultAttri;
begin
  Lex.Next;
  IdentList.Add(IdentPre + 'Unknown');
  IdentList.Add(IdentPre + 'Null');
  while (Lex.RunId <> IdStop) do
  begin
    while Lex.RunId in [IdSpace, IdBraceOpen, IdCRLF, IDUnknown] do
      Lex.Next;
    if (Lex.RunId <> IdStop) then
    begin
      sIdent := IdentPre + Lex.RunToken;
      if not IsValidIdent(sIdent) then
        raise Exception.Create('Invalid identifier for token type: ' + sIdent);
        
      if (IdentList.IndexOf(sIdent) < 0) then
        IdentList.Add(sIdent);
      Lex.Next;

      sLine := '';
      while (Lex.RunId = IdSpace) do
        Lex.Next;
      while not (Lex.RunId in [IdStop, IdCRLF]) do
      begin                                 { is there more data on this line? }
        sLine := sLine + Lex.RunToken;
        Lex.Next;
      end;
      
      if (sLine <> '') then        { The Msg file specifies default attributes }
      begin
        List := TStringList.Create;
        try
          while (sLine <> '') do
          begin
            i := Pos('|', sLine);
            if (i > 0) then
            begin
              List.Add(Copy(sLine, 1, i - 1));
              Delete(sLine, 1, i);
            end
            else
            begin
              List.Add(sLine);
              sLine := '';
            end;
          end;

          i := IdentList.IndexOf(sIdent);
          if (i >= 0) then
          begin
            DefAttri := TLexDefaultAttri.Create;
            DefAttri.Style := List.Values['Style'];
            DefAttri.Foreground := List.Values['Foreground'];
            DefAttri.Background := List.Values['Background'];
            IdentList.Objects[i] := DefAttri;
          end;
        finally
          List.Free;
        end;
      end;
    end
    else
      Break;
  end;
end;

procedure TFrmMain.OutFileCreate(InName: String);
var
  OutName, UName: String;
  sysTime: TSystemTime;
  ISODate: string;
begin
  OutName := ExtractFileName(InName);
  Delete(OutName, Length(OutName) - 3, 4);
  Uname := OutName;
  OutName := OutName + '.pas';
  AssignFile(OutFile, OutName);
  rewrite(OutFile);
  GetSystemTime(sysTime);
  ISODate := Format('%.4d-%.2d-%.2d', [sysTime.wYear, sysTime.wMonth, sysTime.wDay]);
  if ChkGPLHeader.Checked then
  begin
    Writeln(OutFile, '{-------------------------------------------------------------------------------');
    Writeln(OutFile, 'The contents of this file are subject to the Mozilla Public License');
    Writeln(OutFile, 'Version 1.1 (the "License"); you may not use this file except in compliance');
    Writeln(OutFile, 'with the License. You may obtain a copy of the License at');
    Writeln(OutFile, 'http://www.mozilla.org/MPL/');
    Writeln(OutFile);
    Writeln(OutFile, 'Software distributed under the License is distributed on an "AS IS" basis,');
    Writeln(OutFile, 'WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for');
    Writeln(OutFile, 'the specific language governing rights and limitations under the License.');
    Writeln(OutFile);
    Writeln(OutFile, 'Code template generated with SynGen.');
    Writeln(OutFile, 'The original code is: ' + OutName + ', released ' + ISODate + '.');
    Writeln(OutFile, 'Description: ' + EditDescription.Text);
    Writeln(OutFile, 'The initial author of this file is ' + EditAuthor.Text + '.');
    Writeln(OutFile, 'Copyright (c) ' + Format('%d', [sysTime.wYear]) + ', all rights reserved.');
    Writeln(OutFile);
    Writeln(OutFile, 'Contributors to the SynEdit and mwEdit projects are listed in the');

⌨️ 快捷键说明

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