File: qnfa.h

package info (click to toggle)
texstudio 2.3%2Bdebian-3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 20,992 kB
  • sloc: cpp: 61,734; ansic: 4,300; xml: 726; sh: 125; makefile: 25
file content (306 lines) | stat: -rw-r--r-- 5,713 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/****************************************************************************
**
** Copyright (C) 2006-2009 fullmetalcoder <fullmetalcoder@hotmail.fr>
**
** This file is part of the Edyuk project <http://edyuk.org>
** 
** This file may be used under the terms of the GNU General Public License
** version 3 as published by the Free Software Foundation and appearing in the
** file GPL.txt included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#ifndef _Q_NFA_H_
#define _Q_NFA_H_

/*!
	\file qnfa.h
	\brief Definition of the core QNFA syntax engine
*/

#include <QChar>
#include <QList>
#include <QHash>
#include <QStack>
#include <QString>

#include "light_vector.h"

struct QNFA;

typedef light_vector<quint16> QNFASet;

class QNFABranch : public light_vector<QNFA*>
{
	public:
		~QNFABranch();
};

enum NFAType
{
	Char			= 0,
	
	Match			= 1,
	
	CxtBeg			= 2,
	CxtEnd			= 4,
	CxtEsc			= 8,
	
	ContextBegin	= Match | CxtBeg,
	ContextEnd		= Match | CxtEnd,
	EscapeSeq		= Match | CxtEsc,
	
	Escaped			= 16,
	Exclusive		= 32,
	StayOnLine		= 64,
	
	Reserved		= 128
};

enum NFAAssertion
{
	NoAssertion		= 0,
	
	One				= 0,		// default standard
	ZeroOrOne		= 1,		// ?
	ZeroOrMore		= 2,		// *
	OneOrMore		= 4,		// +
	
	WordStart		= 8,
	WordEnd			= 16,
	
	Word			= 32,
	NonWord			= 64,
	
	Digit			= 128,
	NonDigit		= 256,
	
	Space			= 512,
	NonSpace		= 1024,
	
	CaseSensitive	= 2048
};

struct QCharTreeNode;

typedef QHash<quint16, QCharTreeNode> QCharTreeLevel;

struct QCharTreeNode
{
	inline QCharTreeNode(quint16 v = 0) { value.unicode = v; }
	inline QCharTreeNode(const QCharTreeNode& o) { value = o.value; next = o.next; }
	
	union
	{
		int action;
		quint16 unicode;
	} value;
	
	QCharTreeLevel next;
};

Q_DECLARE_TYPEINFO(QCharTreeNode, Q_MOVABLE_TYPE);

typedef QCharTreeLevel QCharTree;

struct QNFA
{
	QNFA();
	~QNFA();
	
	QNFASet c;
	QCharTree tree;
	
	union
	{
		QNFA *next;
		QNFABranch *branch;
	} out;
	
	quint8 type;
	quint16 assertion;
	
	int actionid;
	
	static quint32 _count;
};

struct QNFAMatchContext
{
	inline QNFAMatchContext(QNFA *root = 0) : context(root) {}
	
	inline QNFAMatchContext& operator = (QNFAMatchContext *c)
	{
		if ( c )
		{
			context = c->context;
			parents = c->parents;
			meaningless = c->meaningless;
		} else {
			reset();
		}
		
		return *this;
	}
	
	inline QNFAMatchContext& operator = (const QNFAMatchContext& c)
	{
		context = c.context;
		parents = c.parents;
		meaningless = c.meaningless;
		
		return *this;
	}
	
	inline void reset()
	{
		context = 0;
		
		while ( parents.count() )
			context = parents.pop();
	}
	
	QNFA *context;
	QNFASet meaningless;
	QStack<QNFA*> parents;
};

class QNFAMatchHandler
{
	public:
		virtual ~QNFAMatchHandler() {}
		
		virtual void matched(int pos, int len, int action) = 0;
};

class QNFAMatchNotifier
{
	private:
		struct Command
		{
			inline Command(int p, int len, int act)
			 : pos(p), length(len), action(act) {}
			
			int pos;
			int length;
			int action;
		};
		
		typedef QList<Command> CommandList;
		
	public:
		inline QNFAMatchNotifier(QNFAMatchHandler *h)
		 : handler(h) {}
		
		inline QNFAMatchNotifier& operator = (const QNFAMatchNotifier& n)
		{
			handler = n.handler;
			
			return *this;
		}
		
		inline void operator () (int pos, int len, int action)
		{
			if ( handler && (m_buffers.isEmpty() || m_pending.count()) )
				handler->matched(pos, len, action);
			else
				m_buffers.top() << Command(pos, len, action);
		}
		
		inline int bufferLevel() const
		{
			return m_buffers.count();
		}
		
		inline void startBuffering()
		{
			m_buffers.push(CommandList());
		}
		
		inline void stopBuffering()
		{
			m_pending = m_buffers.pop();
		}
		
		inline void flush()
		{
			foreach ( Command c, m_pending )
				handler->matched(c.pos, c.length, c.action);
			
			m_pending.clear();
		}
		
		inline void clear()
		{
			m_pending.clear();
			m_buffers.clear();
		}
		
	private:
		QNFAMatchHandler *handler;
		
		CommandList m_pending;
		QStack<CommandList> m_buffers;
};

void match(			QNFAMatchContext *lexer,
					const QChar *d,
					int length,
					QNFAMatchNotifier notify);

inline void match(	QNFAMatchContext *lexer,
					const QString& s,
					QNFAMatchNotifier notify)
{ match(lexer, s.constData(), s.length(), notify); }

QNFA* lexer();

void squeeze(QNFA *nfa);
void squeeze(QCharTreeLevel& lvl);

QNFA* sharedContext(const QString& start,
					QNFA *other,
					bool cs);

QNFA* context(		const QString& start,
					const QString& stop,
					const QString& escape,
					int action,
					QNFA **handler = 0,
					bool cs = true);

inline void addNFA(QNFA *context, QNFA *nfa)
{ context->out.branch->append(nfa); }

bool plain(const QString& word, QString *dest);

void addWord(		QCharTree& tree,
					const QString& w,
					int action,
					bool cs);

void addWord(		QNFA *lexer,
					const QString& w,
					int action,
					bool cs);

void addSequence(	QNFA *lexer,
					const QString& w,
					int action,
					bool cs);

QNFA* sequence(		const QChar *d,
					int length,
					QNFA **end,
					bool cs);

inline QNFA* sequence(
					const QString& s,
					QNFA **end,
					bool cs)
{ return sequence(s.constData(), s.length(), end, cs); }

#endif //!_Q_NFA_H_