-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathEbnfToken.h
88 lines (75 loc) · 2.73 KB
/
EbnfToken.h
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
#ifndef EBNFTOKEN_H
#define EBNFTOKEN_H
/*
* Copyright 2019 Rochus Keller <mailto:[email protected]>
*
* This file is part of the EbnfStudio application.
*
* The following is the license that applies to this copy of the
* application. For a license to use the application under conditions
* other than those described here, please email to [email protected].
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
#include <QString>
#include <QHash>
struct EbnfToken
{
class Sym
{
public:
Sym(const Sym& rhs ):d_str(rhs.d_str){}
Sym():d_str(0){}
operator QByteArray() const { return toBa(); }
QByteArray toBa() const;
QString toStr() const;
operator const char*() const { return c_str(); }
const char* c_str() const;
const char* data() const { return d_str; }
int size() const;
bool isEmpty() const;
bool operator==(const Sym& rhs) const { return d_str == rhs.d_str; }
private:
friend class EbnfToken;
const char* d_str; // stores utf-8
};
enum TokenType { Invalid, Production, Assig, NonTerm, Keyword, Literal,
Bar, LPar, RPar, LBrack, RBrack, LBrace, RBrace, Predicate, Comment,
AddTo, // +=
PpDefine, PpUndef, PpIfdef, PpIfndef, PpElse, PpEndif, Eof };
enum Handling { Normal,
Transparent, // *
Keep, // !
Skip // -
};
#ifdef _DEBUG
TokenType d_type;
Handling d_op;
#else
uint d_type : 5;
uint d_op : 2;
#endif
uint d_len : 10;
uint d_colNr : 15;
quint32 d_lineNr;
Sym d_val; // utf-8
EbnfToken(TokenType t = Invalid, quint32 line = 0,quint16 col = 0, quint16 len = 0, const QByteArray& val = QByteArray() ):
d_type(t),d_lineNr(line),d_colNr(col),d_len(len),d_op(Normal){ d_val = getSym(val);}
QString toString(bool labeled = true) const;
bool isValid() const { return d_type != Eof && d_type != Invalid; }
bool isErr() const { return d_type == Invalid; }
static Sym getSym( const QByteArray& );
static void resetSymTbl();
static bool isPpType(int );
private:
static QHash<QByteArray,Sym> s_symTbl;
};
inline uint qHash(const EbnfToken::Sym& r ) { return qHash(r.data()); }
#endif // EBNFTOKEN_H