TrinityCore
|
A simple tokenizer for parsing text files. More...
#include <TextInput.h>
Classes | |
class | BadMSVCSpecial |
class | Settings |
class | TokenException |
class | WrongString |
class | WrongSymbol |
class | WrongTokenType |
Public Types | |
enum | FS { FROM_STRING } |
Public Member Functions | |
TextInput (const std::string &filename, const Settings &settings=Settings()) | |
TextInput (FS fs, const std::string &str, const Settings &settings=Settings()) | |
TextInput (FS fs, const char *str, size_t strLen, const Settings &settings=Settings()) | |
bool | hasMore () |
void | pushSettings (const Settings &settings) |
void | popSettings () |
Token | read () |
void | read (Token &t) |
Token | readSignificant () |
double | readNumber () |
int | readInteger () |
bool | readBoolean () |
Token | readStringToken () |
std::string | readString () |
void | readString (const std::string &s) |
std::string | readUntilNewlineAsString () |
std::string | readUntilDelimiterAsString (const char delimiter1, const char delimiter2= '\0') |
Token | readCommentToken () |
std::string | readComment () |
void | readComment (const std::string &s) |
Token | readNewlineToken () |
std::string | readNewline () |
void | readNewline (const std::string &s) |
Token | readSymbolToken () |
void | readSymbolToken (Token &t) |
std::string | readSymbol () |
void | readSymbol (const std::string &symbol) |
void | readSymbols (const std::string &s1, const std::string &s2) |
void | readSymbols (const std::string &s1, const std::string &s2, const std::string &s3) |
void | readSymbols (const std::string &s1, const std::string &s2, const std::string &s3, const std::string &s4) |
Token | peek () |
int | peekLineNumber () |
int | peekCharacterNumber () |
void | push (const Token &t) |
const std::string & | filename () const |
Static Public Member Functions | |
static double | parseNumber (const std::string &_string) |
static bool | parseBoolean (const std::string &_string) |
Private Member Functions | |
void | init () |
int | eatInputChar () |
int | peekInputChar (int distance=0) |
int | eatAndPeekInputChar () |
void | nextToken (Token &t) |
void | parseQuotedString (unsigned char delimiter, Token &t) |
void | initFromString (const char *str, int len, const Settings &settings) |
Private Attributes | |
Array< Settings > | settingsStack |
std::deque< Token > | stack |
Array< char > | buffer |
int | currentCharOffset |
int | lineNumber |
int | charNumber |
Settings | options |
A simple tokenizer for parsing text files.
TextInput handles a superset of C++,Java, Matlab, and Bash code text including single line comments, block comments, quoted strings with escape sequences, and operators. TextInput recognizes several categories of tokens, which are separated by white space, quotation marks, or the end of a recognized operator:
Token::SINGLE_QUOTED_TYPE
string of characters surrounded by single quotes, e.g., 'x', '\0', 'foo'. Token::DOUBLE_QUOTED_TYPE
string of characters surrounded by double quotes, e.g., "x", "abc\txyz", "b o b". Token::SYMBOL_TYPE
legal C++ operators, keywords, and identifiers. e.g., >=, Foo, _X, class, { Token::INTEGER_TYPE
numbers without decimal places or exponential notation. e.g., 10, 0x17F, 32, 0, -155 Token::FLOATING_POINT_TYPE
numbers with decimal places or exponential notation. e.g., 1e3, -1.2, .4, 0.5 Token::BOOLEAN_TYPE
special symbols like "true" and "false"; the exact details can be configured in TextInput::Settings Token::LINE_COMMENT_TYPE
(disabled by default); generated for line comments as specified by TextInput::Settings Token::BLOCK_COMMENT_TYPE
(disabled by default); generated for c-style block comments as specified by TextInput::Settings Token::NEWLINE_TYPE
(disabled by default); generated for any of "\\r", "\\n" or "\\r\\n" The special ".." and "..." tokens are always recognized in addition to normal C++ operators. Additional tokens can be made available by changing the Settings.
Negative numbers are handled specially because of the ambiguity between unary minus and negative numbers– see the note on TextInput::read.
TextInput does not have helper functions for types with non-obvious formatting, or helpers that would be redundant. Use the serialize methods instead for parsing specific types like int, Vector3, and Color3.
Inside quoted strings escape sequences are converted. Thus the string token for ["a\\nb"] is 'a', followed by a newline, followed by 'b'. Outside of quoted strings, escape sequences are not converted, so the token sequence for [a\nb] is symbol 'a', symbol '\', symbol 'nb' (this matches what a C++ parser would do). The exception is that a specified TextInput::Settings::otherCommentCharacter preceeded by a backslash is assumed to be an escaped comment character and is returned as a symbol token instead of being parsed as a comment (this is what a LaTex or VRML parser would do).
Examples
Assumes that the file is not modified once opened.
enum G3D::TextInput::FS |
G3D::TextInput::TextInput | ( | FS | fs, |
const std::string & | str, | ||
const Settings & | settings = Settings() |
||
) |
Creates input directly from a string. The first argument must be TextInput::FROM_STRING.
G3D::TextInput::TextInput | ( | FS | fs, |
const char * | str, | ||
size_t | strLen, | ||
const Settings & | settings = Settings() |
||
) |
Creates input directly from a fixed-length, non-NULL terminated string. The first argument must be TextInput::FROM_STRING.
|
inlineprivate |
Helper function to consume the next character in the input buffer and peek at the one following (without consuming it).
|
private |
Consumes the next character from the input buffer, and returns that character. Updates lineNumber and charNumber to reflect the location of the next character in the input buffer.
Note: you shouldn't be using the return value of this function in most cases. In general, you should peekInputChar() to get the next character, determine what to do with it, then consume it with this function (or with eatAndPeekInputChar()). Given that usage, in most instances you already know what this function would return!
const std::string & G3D::TextInput::filename | ( | ) | const |
Returns the filename from which this input is drawn, or the first few characters of the string if created from a string. If settings::filename is non-empty that will replace the true filename.
bool G3D::TextInput::hasMore | ( | ) |
Returns true while there are tokens remaining.
|
private |
|
private |
|
private |
Read the next token, returning an END token if no more input is available.
toLower(_string) == "true"
|
static |
Includes MSVC specials parsing
|
private |
Helper for nextToken. Appends characters to t._string until the end delimiter is reached.
When called, the next character in the input buffer should be first the first character after the opening delimiter character.
Token G3D::TextInput::peek | ( | ) |
Return a copy of the next token in the input stream, but don't remove it from the input stream.
int G3D::TextInput::peekCharacterNumber | ( | ) |
Returns the character number (relative to the line) for the next token in the input stream. See also peek.
|
private |
Returns the next character from the input buffer, without consuming any characters. Can also be used to look deeper into the input buffer. Does not modify lineNumber or charNumber.
distance | Index of the character in the input buffer to peek at, relative to the next character. Default is 0, for the next character in the input buffer. |
int G3D::TextInput::peekLineNumber | ( | ) |
Returns the line number for the next token. See also peek.
|
inline |
Take a previously read token and push it back at the front of the input stream.
Can be used in the case where more than one token of read-ahead is needed (i.e., when peek doesn't suffice).
Temporarily switch parsing to use settings. Note that this will override the currently recorded sourceFilename unless you explicitly set it back.
Token G3D::TextInput::read | ( | ) |
Read the next token (which will be the END token if ! hasMore()).
Signed numbers can be handled in one of two modes. If the option TextInput::Settings::signedNumbers is true, A '+' or '-' immediately before a number is prepended onto that number and if there is intervening whitespace, it is read as a separate symbol.
If TextInput::Settings::signedNumbers is false, read() does not distinguish between a plus or minus symbol next to a number and a positive/negative number itself. For example, "x - 1" and "x -1" will be parsed the same way by read().
In both cases, readNumber() will contract a leading "-" or "+" onto a number.
void G3D::TextInput::read | ( | Token & | t | ) |
bool G3D::TextInput::readBoolean | ( | ) |
std::string G3D::TextInput::readComment | ( | ) |
Like readCommentToken, but returns the token's string.
Use this method (rather than readCommentToken) if you want the token's value but don't really care about its location in the input. Use of readCommentToken is encouraged for better error reporting.
void G3D::TextInput::readComment | ( | const std::string & | s | ) |
Reads a specific comment token or throws either WrongTokenType or WrongString. If the next token in the input is a comment matching s
, it will be consumed.
Use this method if you want to match a specific comment from the input. In that case, typically error reporting related to the token is only going to occur because of a mismatch, so no location information is needed by the caller.
WrongTokenType will be thrown if the next token in the input stream is not a comment. WrongString will be thrown if the next token in the input stream is a comment but does not match the s
parameter. When an exception is thrown, no tokens are consumed.
Token G3D::TextInput::readCommentToken | ( | ) |
Reads a comment token or throws WrongTokenType, and returns the token.
Use this method (rather than readComment) if you want the token's location as well as its value.
WrongTokenType will be thrown if the next token in the input stream is not a comment. When an exception is thrown, no tokens are consumed.
int G3D::TextInput::readInteger | ( | ) |
Reads a number that must be in C integer format: [ '+' | '-' ] #+ | '0x'#+
std::string G3D::TextInput::readNewline | ( | ) |
Like readNewlineToken, but returns the token's string.
Use this method (rather than readNewlineToken) if you want the token's value but don't really care about its location in the input. Use of readNewlineToken is encouraged for better error reporting.
void G3D::TextInput::readNewline | ( | const std::string & | s | ) |
Reads a specific newline token or throws either WrongTokenType or WrongString. If the next token in the input is a newline matching s
, it will be consumed.
Use this method if you want to match a specific newline from the input. In that case, typically error reporting related to the token is only going to occur because of a mismatch, so no location information is needed by the caller.
WrongTokenType will be thrown if the next token in the input stream is not a newline. WrongString will be thrown if the next token in the input stream is a newlin but does not match the s
parameter. When an exception is thrown, no tokens are consumed.
Token G3D::TextInput::readNewlineToken | ( | ) |
Reads a newline token or throws WrongTokenType, and returns the token.
Use this method (rather than readNewline) if you want the token's location as well as its value.
WrongTokenType will be thrown if the next token in the input stream is not a newline. When an exception is thrown, no tokens are consumed.
double G3D::TextInput::readNumber | ( | ) |
Read one token (or possibly two, for minus sign) as a number or throws WrongTokenType, and returns the number.
If the first token in the input is a number, it is returned directly.
If TextInput::Settings::signedNumbers is false and the input stream contains a '+' or '-' symbol token immediately followed by a number token, both tokens will be consumed and a single token will be returned by this method.
WrongTokenType will be thrown if one of the input conditions described above is not satisfied. When an exception is thrown, no tokens are consumed.
Token G3D::TextInput::readSignificant | ( | ) |
Calls read() until the result is not a newline or comment
std::string G3D::TextInput::readString | ( | ) |
Like readStringToken, but returns the token's string.
Use this method (rather than readStringToken) if you want the token's value but don't really care about its location in the input. Use of readStringToken is encouraged for better error reporting.
void G3D::TextInput::readString | ( | const std::string & | s | ) |
Reads a specific string token or throws either WrongTokenType or WrongString. If the next token in the input is a string matching s
, it will be consumed.
Use this method if you want to match a specific string from the input. In that case, typically error reporting related to the token is only going to occur because of a mismatch, so no location information is needed by the caller.
WrongTokenType will be thrown if the next token in the input stream is not a string. WrongString will be thrown if the next token in the input stream is a string but does not match the s
parameter. When an exception is thrown, no tokens are consumed.
Token G3D::TextInput::readStringToken | ( | ) |
Reads a string token or throws WrongTokenType, and returns the token.
Use this method (rather than readString) if you want the token's location as well as its value.
WrongTokenType will be thrown if the next token in the input stream is not a string. When an exception is thrown, no tokens are consumed.
std::string G3D::TextInput::readSymbol | ( | ) |
Like readSymbolToken, but returns the token's string.
Use this method (rather than readSymbolToken) if you want the token's value but don't really care about its location in the input. Use of readSymbolToken is encouraged for better error reporting.
void G3D::TextInput::readSymbol | ( | const std::string & | symbol | ) |
Reads a specific symbol token or throws either WrongTokenType or WrongSymbol. If the next token in the input is a symbol matching symbol
, it will be consumed.
Use this method if you want to match a specific symbol from the input. In that case, typically error reporting related to the token is only going to occur because of a mismatch, so no location information is needed by the caller.
WrongTokenType will be thrown if the next token in the input stream is not a symbol. WrongSymbol will be thrown if the next token in the input stream is a symbol but does not match the symbol
parameter. When an exception is thrown, no tokens are consumed.
Read a series of two specific symbols. See readSymbol.
|
inline |
Read a series of three specific symbols. See readSymbol.
|
inline |
Read a series of four specific symbols. See readSymbol.
Token G3D::TextInput::readSymbolToken | ( | ) |
Reads a symbol token or throws WrongTokenType, and returns the token.
Use this method (rather than readSymbol) if you want the token's location as well as its value.
WrongTokenType will be thrown if the next token in the input stream is not a symbol. When an exception is thrown, no tokens are consumed.
void G3D::TextInput::readSymbolToken | ( | Token & | t | ) |
Avoids the copy of readSymbolToken()
std::string G3D::TextInput::readUntilDelimiterAsString | ( | const char | delimiter1, |
const char | delimiter2 = '\0' |
||
) |
Read from the beginning of the next token until the following delimiter character and return the result as a string, ignoring all parsing in between. The delimiter is not returned in the string, and the following token read will begin at the delimiter or end of file token (if they are enabled for parsing).
std::string G3D::TextInput::readUntilNewlineAsString | ( | ) |
Read from the beginning of the next token until the following newline and return the result as a string, ignoring all parsing in between. The newline is not returned in the string, and the following token read will be a newline or end of file token (if they are enabled for parsing).
|
private |
Characters to be tokenized.
|
private |
Character number (within the line) of the next character to be consumed from the input buffer. (1 indicates first character of the line).
Note that this is the character number of the next character to be consumed from the input, not the character number of the last character consumed!
|
private |
Offset of current character (the next character to consumed) in input buffer.
|
private |
Line number of next character to be consumed from the input buffer. (1 indicates first line of input.)
Note that this is the line number of the next character to be consumed from the input, not the line number of the last character consumed!
|
private |
Configuration options. This includes the file name that will be reported in tokens and exceptions.
|
private |