2005. június 22., szerda

TParser, an undocumented Delphi class

TParser is an undocumented Delphi class. It is used by Delphi to read a stream and break it into tokens. Its primary use is to convert a text file to a binary form file (.dfm file).

Answer:

Description

TParser is an undocumented Delphi class. It is used by Delphi to read a stream and break it into tokens. Its primary use is to convert a text file to a binary form file (.dfm file).

TParser performs a lexical analysis of the input stream. It breaks the stream up into floating point numbers, strings, Pascal identifiers or punctuation. Any other non-space characters are considered to be punctuation. Binary data, appearing as hexadecimal digits, is also handled by TParser. Binary data is enclosed in curly braces, and is handled specially by TParser.

TParser cannot handle Pascal comments, so it cannot be considered a general purpose parsing class. It is intended to parse a correctly formatted textual form description.

TParser raises an exception when it encounters an error, but there is no error recovery.

To use the TParser class, instantiate it using the Create method, supplying the input stream you want parsed to that method. Then repeatedly call NextToken and evaluate the token returned until the token is toEOF. Finally, destroy the instance using the Free method.

Properties

property FloatType: Char read FFloatType;

Read-only property that indicates the type of floating point number. Can be 'C', 'D' or 'S'.

property SourceLine: Integer read FSourceLine;

Read-only property that indicates the current input line number. This number is included in all error messages resulting from problems with TParser conversion.

property Token: Char read FToken;

Read-only property that indicates the type of token to be read next. Tokens are either a punctuation mark or they are of type toEOF, toSymbol, toString, toInteger, toFloat or toWString.

Methods

Create

constructor Create(Stream: TStream);

Set up the stream, initialize pointers and allocate a buffer to receive characters from the stream. Then call NextToken to get a token from the stream.

Destroy

destructor Destroy;

Free the memory allocated for use with TParser.

CheckTokenSymbol

procedure CheckTokenSymbol(const S: string);

Check an identifier token for a specific string (S). If the identifier is not the same as the string, CheckTokenSymbol will raise an EParserError exception.

Error

procedure Error(const Ident: string);

Take a string resource identifier (Ident) and pass it to the ErrorStr procedure so that an exception may be raised.

ErrorFmt

procedure ErrorFmt(const Ident: string; const Args: array of const);

Format the string resource identifier and arguments, then pass the result to ErrorStr so that an exception may be raised.

ErrorStr

procedure ErrorStr(const Message: string);

Raise an EParserError exception, displaying the Message. All error handling in TParser ends up calling ErrorStr.

HexToBinary

procedure HexToBinary(Stream: TStream);

Read hexadecimal data from the input stream, convert it to binary and write the binary out to the Stream output stream. Hexadecimal data in the input stream is enclosed in curly braces.

NextToken

function NextToken: Char;

Advance the input stream to the next token and return that token. The returned token is either a single character (punctuation) or a special token character. Based on the token type, you can obtain the value of the actual token by calling one of the token functions, as shown below:

Returns the position of the current token in the input stream. If an error occurs, SourcePos can be used to display the position in the input stream where the error occurred.

TokenComponentIdent

function TokenComponentIdent: string;

Returns an identifier path string, which is a series of identifiers separated by periods. For this function to work, the current token must be a toSymbol token. The function then reads and builds the identifier path (for example, Font.Name).

TokenFloat

function TokenFloat: Extended;

Converts the current toFloat token to a floating point number by calling the StrToFloat function. If the number is not correctly formatted, an EConvertError exception will be raised. For this function to work, the current token must be toFloat.

TokenInt

function TokenInt: Int64;

Converts the current toInteger token to an integer number by calling the StrToInt64 function. For this function to work, the current token must be toInteger.

TokenString

function TokenString: string;

Returns the current token as a string of 255 characters or less. If the token type is toString, then the returned string is the actual string, after converting non-printing characters and embedded quotes. If the token is toSymbol, the actual string is returned without any conversions.

TokenWideString

function TokenWideString: WideString;

Returns the current token as a string more than 255 characters. For this function to work, the token type must be toWString.

TokenSymbolIs

function TokenSymbolIs(const S: string): Boolean;

Returns True if the current token type is toSymbol and the token is equal to the string S (disregarding case). Otherwise, it returns False. This is handy to test for specific symbols, such as object or end.