📄 ubaseidents.pas
字号:
{ * * * *** * * * *** TPascalFile *** * * * *** * * * }
//Defines what type of comment the beginning of a line is.
TCommentType = (
ctNone, //no comment
ctCurly, //in a {}-comment
ctBraceStar); //in a (**)-comment
//An array for each line in a pascal file, to say if it starts/inherites a
//comment state.
TCommentArray = packed array of TCommentType;
//the parts of a pascal file
TFilePart = (
//for units the implementation part, for all other files the
//whole file
fpMain,
//the interface part of the unit
fpInterface);
//the uses/contains tokens in the file
//~see TUsedUnitList passes its data on to this type
TUsesClauses = array[TFilePart] of String;
//holds the uses/contains-clause, that means all used units
//the strings are the names and the objects of the lists are the files
//~see TUsesClauses inherits its data from the data in this type
TUsedUnitList = array[TFilePart] of TStrings;
{Represents a file of pascal source code internally. It containts a list for
all identifiers in the file and further data. }
TPascalFile = class
private
//the contents of the file
FLines: TStrings;
//an object to parse the pascal source code and extract information
FParser: TObject;
//the dialect of pascal the source code is
FPascalDialect: TPascalDialect; //(only Delphi is really supported)
//the list of files in which this file is and other files can be found
FFileList: TFileList;
//absolute short unique path of the file
FFilePath: String;
//the internal name of the file
FInternalFileName: String;
//index, if there are more files with the same internal name
FInternalNameIndex: Cardinal;
//the list of all identifiers in the file
FIdents: TIdentifierList;
//portability issues of the file, i.e. of the identifiers in it
FPortability: TIdentPortabilities;
//if the file is included by another file
FIncludedCode: Boolean;
//the kind of the file (program, unit, etc.)
FFileType: TSourceFileType;
//position where the implementation part of a unit starts
FImplementationStart: TPosition;
//parameters of the program (not used anymore in Delphi)
FProgramParameters: String;
//the uses-clauses in the file
FUsesClauses: TUsesClauses;
//the list of exported identifiers in the file
FExporteds: TIdentifierList;
//the list of required packages for this program/package
FRequiredPackages: TStringList;
//parsed file of the project that includes this file (or nil)
FProjectFile: TPascalFile;
//list of all known used/contained units
FUsedUnitList: TUsedUnitList;
//list of all unknown units
FUnknownUnits: TStringList;
//list of all unknown identifiers in the file
FUnknownIdents: TStringList;
//list of all unknown type identifiers in the file
FUnknownTypeIdents: TStringList;
//compiler options when starting to parse the file
FStartDefines: TDefines;
//compiler options while/after parsing the file
FDefines: TDefines;
//statistics on the file
FStatistic: TStatisticOfPascalFile;
//the kind of comments ({} or (**)) each line starts in;~[br]
//i.e. if in the previous line a comment is started but not ended, the
//following lines will inherit the same comment, until the comment is
//closed, to avoid parsing always the whole file, these comment-states are
//once parsed and saved
FComments: TCommentArray;
//Parses to get the kinds of comments the beginnings of the lines are in.
procedure ParseCommentedLines;
//Gets the text of the line without comments.
function GetUncommentedLine(Index: Integer): String;
//Gets the comment in the line.
function GetLineComment(Index: Integer): String;
//Gets the comment at the end of the line.
function GetLineCommentAtEnd(Index: Integer): String;
//Gets the kind of comment the beginning of the line is in.
function GetLineStartComment(Index: Integer): TCommentType;
//Called after the content of the file has been set.
procedure ContentHasBeenSet;
public
//Creates the object for a file and the different lists.
constructor Create;
//Frees the different lists and this object of the file.
destructor Destroy; override;
//Gets the version the file will save its data in by ~[link Save].
class function GetVersion: TIdentClassVersion;
//Checks if the file can read data of the version by ~[link Load].
class function CheckVersion(Version: TIdentClassVersion): Boolean;
//Adds all owned identifiers to the list.
procedure AddIdentifiersToList(List: TIdentifierList); virtual;
//Saves the data of the file to the stream.
procedure Save(Stream: TIdentStream);
//Loads the data of a file from the stream.
procedure Load(Stream: TIdentStream; Version: TIdentClassVersion);
//Compares this parsed file with the other one.
function CompareWith(OtherFile: TPascalFile; const MsgPrefix: String;
Messages: TStrings): Boolean;
//Reads the content of a file with pascal code.
procedure ReadContent(const FileName: String);
//Sets the content of a file with pascal code.
procedure SetContent(const Content: String);
//Checks if the name of a unit is aliased and returns the real name.
function GetUnitAlias(const UnitName: String): String;
//Sets the (predefined) compiler options for the file.
procedure SetCompilerDefines(CompDefines: TDefines; Start: Boolean);
//The text without comments of each line.
property UncommentedLine[Index: Integer]: String read GetUncommentedLine;
//The comments of each line.
property LineComment[Index: Integer]: String read GetLineComment;
//The comments at the end of each line.
property LineCommentAtEnd[Index: Integer]: String read GetLineCommentAtEnd;
//The kind of comment the beginning of each line is in.
property LineStartComment[Index: Integer]: TCommentType
read GetLineStartComment;
property Lines: TStrings read FLines;
property Parser: TObject read FParser write FParser;
property PascalDialect: TPascalDialect read FPascalDialect
write FPascalDialect;
property FileList: TFileList read FFileList write FFileList;
property FilePath: String read FFilePath write FFilePath;
property InternalFileName: String read FInternalFileName
write FInternalFileName;
property InternalNameIndex: Cardinal read FInternalNameIndex
write FInternalNameIndex;
property Idents: TIdentifierList read FIdents;
property Portability: TIdentPortabilities read FPortability
write FPortability;
property IncludedCode: Boolean read FIncludedCode write FIncludedCode;
property FileType: TSourceFileType read FFileType write FFileType;
property ImplementationStart: TPosition read FImplementationStart
write FImplementationStart;
property ProgramParameters: String read FProgramParameters
write FProgramParameters;
property UsesClauses: TUsesClauses read FUsesClauses write FUsesClauses;
property Exporteds: TIdentifierList read FExporteds;
property RequiredPackages: TStringList read FRequiredPackages;
property ProjectFile: TPascalFile read FProjectFile write FProjectFile;
property UsedUnitList: TUsedUnitList read FUsedUnitList;
property UnknownUnits: TStringList read FUnknownUnits;
property UnknownIdents: TStringList read FUnknownIdents;
property UnknownTypeIdents: TStringList read FUnknownTypeIdents;
property StartDefines: TDefines read FStartDefines;
property Defines: TDefines read FDefines;
property Statistic: TStatisticOfPascalFile read FStatistic;
end;
{ * * * *** * * * *** TIdentifierList *** * * * *** * * * }
{Serves as a simple list of identifiers. Besides the methods to insert,
remove, test, count etc. identifiers, the list can also be sorted.
The identifiers of the list are seen to be owned by the list, this means by
default all identifiers are freed, when they are removed from the list or
the list is freed. }
TIdentifierList = class
private
//the list of identifiers
FList: TList;
protected
//Gets the identifier.
function GetIdent(Index: Integer): TIdentifier;
public
//Creates the list.
constructor Create;
//Frees all identifiers and the list.
destructor Destroy; override;
//Adds the identifier to the list.
procedure AddIdent(Ident: TIdentifier);
//Gets the identifier with the name.
function GetIdentByName(IdentName: String): TIdentifier;
//Gets an identifier with the name.
function GetIdentByNameList(IdentName: String;
var Index: Integer): TIdentifier;
//Tests if an identifier is in the list.
function IsIn(Ident: TIdentifier): Boolean;
//Tests if an identifier is in the list or in one of the identifiers of the
//list.
function RecursiveIsIn(Ident: TIdentifier): Boolean;
//Returns the index of the identifier in the list.
function FindIdentIndex(Ident: TIdentifier): Integer;
//Sorts the list by the record-like type.
procedure SortTo(TheType: TRecordType);
//Sorts the list by the scope of the identifiers.
procedure SortByScope;
//Sorts the identifiers alphabetically (case insensitive).
procedure Sort;
//Sorts the identifiers by their record-like types alphabetically.
procedure SortByClass;
//Sorts the identifiers of export identifiers.
procedure SortExportIdentifiers;
//Sorts the functions by the length descendingly.
procedure SortFunctionLength;
//Removes the identifier from the list and frees it optionally.
procedure Remove(Index: Integer; Free: Boolean = True);
//Removes the identifier from the list and frees it optionally.
procedure RemoveIdent(Ident: TIdentifier; Free: Boolean = True);
//Removes all identifiers and frees them optionally.
procedure RemoveAll(Free: Boolean = True);
//Returns the number of identifiers in the list.
function Count: Integer;
//Tests if the list is empty.
function IsEmpty: Boolean;
//default property to access the identifiers
property Identifiers[Index: Integer]: TIdentifier read GetIdent; default;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -