Nuria Framework - Core
Core module of the NuriaProject Framework
 All Classes Functions Typedefs Enumerations Enumerator Groups
minilexer.hpp
1 /* Copyright (c) 2014, The Nuria Project
2  * This software is provided 'as-is', without any express or implied
3  * warranty. In no event will the authors be held liable for any damages
4  * arising from the use of this software.
5  * Permission is granted to anyone to use this software for any purpose,
6  * including commercial applications, and to alter it and redistribute it
7  * freely, subject to the following restrictions:
8  * 1. The origin of this software must not be misrepresented; you must not
9  * claim that you wrote the original software. If you use this software
10  * in a product, an acknowledgment in the product documentation would be
11  * appreciated but is not required.
12  * 2. Altered source versions must be plainly marked as such, and must not be
13  * misrepresented as being the original software.
14  * 3. This notice may not be removed or altered from any source
15  * distribution.
16  */
17 
18 #ifndef NURIA_MINILEXER_HPP
19 #define NURIA_MINILEXER_HPP
20 
21 #include "essentials.hpp"
22 #include <QVariant>
23 #include <QObject>
24 #include <QRegExp>
25 
26 #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
27 #include <QRegularExpression>
28 #endif
29 
30 namespace Nuria {
31 class MiniLexerPrivate;
32 
37 class NURIA_CORE_EXPORT LexerRule {
38 public:
39  inline LexerRule (const QString &name = QString ()) : m_name (name) { }
40  inline LexerRule (const char *name) : m_name (QLatin1String (name)) { }
41  inline const QString &name () const { return this->m_name; }
42  inline void setName (const QString &name) { this->m_name = name; }
43 private:
44  QString m_name;
45 };
46 
51 class NURIA_CORE_EXPORT LexerDefinition {
52 public:
53  inline LexerDefinition (const QString &name = QString ()) : m_name (name) { }
54  inline LexerDefinition (const char *name) : m_name (QLatin1String (name)) { }
55  inline const QString &name () const { return this->m_name; }
56  inline void setName (const QString &name) { this->m_name = name; }
57 private:
58  QString m_name;
59 };
60 
133 class NURIA_CORE_EXPORT MiniLexer : public QObject {
134  Q_OBJECT
135 public:
136  typedef QPair< int, QString > TokenValue;
137  typedef QList< TokenValue > TokenValueList;
138 
139  explicit MiniLexer (QObject *parent = 0);
140  ~MiniLexer ();
141 
146  Qt::CaseSensitivity matchSensitivity () const;
147 
153  void setMatchSensitivity (Qt::CaseSensitivity value);
154 
158  int length () const;
159 
163  TokenValueList tokenValueList () const;
164 
170  TokenValue tokenValue (int at) const;
171 
175  const QString &value (int at) const;
176 
180  int token (int at) const;
181 
194  void addRule (const QString &name, const QRegExp &regExp, int token);
195 
197  void addRule (const QString &name, const QString &string, int token);
198 
199 #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
200 
201  void addRule (const QString &name, const QRegularExpression &regExp, int token);
202 #endif
203 
222  void addDefinition (const QString &name, const QVariantList &def);
223 
229  bool lex (const QString &data);
230 
235  QString lastError () const;
236 
241  int errorPosition () const;
242 
247  bool hasStartDefinition () const;
248 
252  bool hasRule (const QString &name) const;
253 
257  bool hasDefinition (const QString &name) const;
258 
295  static MiniLexer *createInstanceFromDefinition (const QString &definition, QString &error);
296 
297 private:
298 
305  void chopValueList (int lastValidLength);
306 
310  bool lexDefinition (const QVariantList &def, const QString &data, int &pos);
311 
312  //
313  MiniLexerPrivate *d_ptr;
314 
315 };
316 
317 }
318 
319 Q_DECLARE_METATYPE(Nuria::LexerRule)
320 Q_DECLARE_METATYPE(Nuria::LexerDefinition)
321 
322 #endif // NURIA_MINILEXER_HPP
A simple lexer. You can use this class if you want to parse some data based on regular expressions...
Definition: minilexer.hpp:133
Definition: minilexer.hpp:37
Definition: minilexer.hpp:51