|
Nuria Framework - Core
Core module of the NuriaProject Framework
|
A simple lexer. You can use this class if you want to parse some data based on regular expressions. It is not really smart, as it is meant to be used for simple tasks. NQL uses it for example to parse queries. More...
#include <minilexer.hpp>
Public Types | |
| typedef QPair< int, QString > | TokenValue |
| typedef QList< TokenValue > | TokenValueList |
Public Member Functions | |
| MiniLexer (QObject *parent=0) | |
| Qt::CaseSensitivity | matchSensitivity () const |
| void | setMatchSensitivity (Qt::CaseSensitivity value) |
| int | length () const |
| TokenValueList | tokenValueList () const |
| TokenValue | tokenValue (int at) const |
| const QString & | value (int at) const |
| int | token (int at) const |
| void | addRule (const QString &name, const QRegExp ®Exp, int token) |
| void | addRule (const QString &name, const QString &string, int token) |
| void | addRule (const QString &name, const QRegularExpression ®Exp, int token) |
| void | addDefinition (const QString &name, const QVariantList &def) |
| bool | lex (const QString &data) |
| QString | lastError () const |
| int | errorPosition () const |
| bool | hasStartDefinition () const |
| bool | hasRule (const QString &name) const |
| bool | hasDefinition (const QString &name) const |
Static Public Member Functions | |
| static MiniLexer * | createInstanceFromDefinition (const QString &definition, QString &error) |
A simple lexer. You can use this class if you want to parse some data based on regular expressions. It is not really smart, as it is meant to be used for simple tasks. NQL uses it for example to parse queries.
Lets see a little example. Lets say we want to write a INI-Parser. First, we need a MiniLexer instance. We also create a enumeration of possible tokens.
Next we need some rules. A line in an ini file is either a group declaration or a Key=Value pair.
A group looks like this: [Groupname]
A Key=Value pair looks like: Key = Value
The whitespace in front and back of the equal sign is optional. So, our first rule will take care of groups:
We head directly to the next two rules:
Okay, now we need to tell MiniLexer how a line must look like. For this, we define two definitions. One for groups, and the other one for Key=Value pairs:
Next part, the lexing itself. We open up a file called "test.ini" using QFile and lex line after line, printing whatever we get. We break up if something goes wrong.
And thats it. You could also use QSettings when you want to read ini files, but wheres the fun in it if you can also use a lexer? :P
| void Nuria::MiniLexer::addDefinition | ( | const QString & | name, |
| const QVariantList & | def | ||
| ) |
Adds a definition to the lexer. A definition declares how the input should look like (Think of it being like BNF, but using C++ stuff to describe things instead of some text input). You can do that by using strings, regular expressions, rules and other definitions in the def list. You can add multiple definitions to a name. You can't remove a definition later. MiniLexer isn't really smart when it comes to matching. That being said, make sure that when a specific match order of definitions with the same name is important, MiniLexer tries from the top which is least-important to most-important at the bottom.
QString() as name. | void Nuria::MiniLexer::addRule | ( | const QString & | name, |
| const QRegExp & | regExp, | ||
| int | token | ||
| ) |
Adds a rule to the lexer. name must be unique (if there is already a rule with the same name, it will be overwritten). regExp is the regular expressions which is connected to this rule. token is a user-defined token id which is used to distinguish between different rules after the lexer has been run.
-1 as token, matches for this rule won't be stored for later use. | void Nuria::MiniLexer::addRule | ( | const QString & | name, |
| const QString & | string, | ||
| int | token | ||
| ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
| void Nuria::MiniLexer::addRule | ( | const QString & | name, |
| const QRegularExpression & | regExp, | ||
| int | token | ||
| ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
static |
Creates a MiniLexer instance based on a definition string. If parsing failed, the hasStartDefinition() method of the returned MiniLexer instance will return false. error will contain a human-readable error message.
definition must be a string with the following format:
# Starts a comment. It ends at the end of the line. It can only appear at the beginning of a line Rule(Token ): Body
Rule = The name of the rule. May be prefixed with a $ Token = The token id as integer Body = The rule body. May be a "string" or a /regex/ Definiton = Body ; Definition = The name of the definition. Must not be prefixed with a $ Body = The body of the definition. The body contains one or more items separated with whitespace. The body ends with a semicolon ';'. Possible items are: | int Nuria::MiniLexer::errorPosition | ( | ) | const |
If lex fails, this function will return the position where the error occured.
| bool Nuria::MiniLexer::hasDefinition | ( | const QString & | name | ) | const |
Returns true if there is a definition with the name name.
| bool Nuria::MiniLexer::hasRule | ( | const QString & | name | ) | const |
Returns true if there is a rule with the name name.
| bool Nuria::MiniLexer::hasStartDefinition | ( | ) | const |
Returns true if this instance has a start definition (e.g. a definition with the name "").
| QString Nuria::MiniLexer::lastError | ( | ) | const |
If lex fails, this function will return a human-readable string describing the problem.
| int Nuria::MiniLexer::length | ( | ) | const |
Returns the count of parsed tokens.
| bool Nuria::MiniLexer::lex | ( | const QString & | data | ) |
Lexes data. Returns true if data could be completely parsed, returns false otherwise.
| Qt::CaseSensitivity Nuria::MiniLexer::matchSensitivity | ( | ) | const |
Returns if strings are matched case-sensitive or case-insensitive.
| void Nuria::MiniLexer::setMatchSensitivity | ( | Qt::CaseSensitivity | value | ) |
Sets if strings should be matched case-sensitive or case-insensitive. This only applies to strings and has no effect on regular expressions. Default is case-sensitive matching.
| int Nuria::MiniLexer::token | ( | int | at | ) | const |
Returns the token of a token/value pair at position at.
| TokenValue Nuria::MiniLexer::tokenValue | ( | int | at | ) | const |
Returns a single TokenValue pair. TokenValue is a typedef for QPair<int,QString>. first is the token id, second the value.
| TokenValueList Nuria::MiniLexer::tokenValueList | ( | ) | const |
Returns a list of TokenValue's containing the current list of token and values.
| const QString& Nuria::MiniLexer::value | ( | int | at | ) | const |
Returns the value of a token/value pair at position at.
1.8.7