Debian Codegraph @

src/tools/qlalr/examples/qparser/qparser.h

raw
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QLALR module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28
29 #ifndef QPARSER_H
30 #define QPARSER_H
31
32 #include <QtCore/QSharedDataPointer>
33 #include <QtCore/QVarLengthArray>
34
35 template <typename _Parser, typename _Table, typename _Value = int>
36 class QParser: protected _Table
37 {
38 public:
39 QParser();
40 ~QParser();
41
42 bool parse();
43
44 inline _Value &sym(int index);
45
46 private:
47 inline int nextToken()
48 {
49 return static_cast<_Parser*> (this)->nextToken();
50 }
51
52 inline void consumeRule(int rule)
53 {
54 static_cast<_Parser*> (this)->consumeRule(rule);
55 }
56
57 enum { DefaultStackSize = 128 };
58
59 struct Data: public QSharedData
60 {
61 Data(): stackSize (DefaultStackSize), tos (0) {}
62
63 QVarLengthArray<int, DefaultStackSize> stateStack;
64 QVarLengthArray<_Value, DefaultStackSize> parseStack;
65 int stackSize;
66 int tos;
67
68 void reallocateStack() {
69 stackSize <<= 1;
70 stateStack.resize(stackSize);
71 parseStack.resize(stackSize);
72 }
73 };
74
75 QSharedDataPointer<Data> d;
76 };
77
78 template <typename _Parser, typename _Table, typename _Value>
79 inline _Value &QParser<_Parser, _Table, _Value>::sym(int n)
80 {
81 return d->parseStack [d->tos + n - 1];
82 }
83
84 template <typename _Parser, typename _Table, typename _Value>
85 QParser<_Parser, _Table, _Value>::QParser():
86 d(new Data())
87 {
88 }
89
90 template <typename _Parser, typename _Table, typename _Value>
91 QParser<_Parser, _Table, _Value>::~QParser()
92 {
93 }
94
95 template <typename _Parser, typename _Table, typename _Value>
96 bool QParser<_Parser, _Table, _Value>::parse()
97 {
98 const int INITIAL_STATE = 0;
99
100 d->tos = 0;
101 d->reallocateStack();
102
103 int act = d->stateStack[++d->tos] = INITIAL_STATE;
104 int token = -1;
105
106 forever {
107 if (token == -1 && - _Table::TERMINAL_COUNT != _Table::action_index[act])
108 token = nextToken();
109
110 act = _Table::t_action(act, token);
111
112 if (d->stateStack[d->tos] == _Table::ACCEPT_STATE)
113 return true;
114
115 else if (act > 0) {
116 if (++d->tos == d->stackSize)
117 d->reallocateStack();
118
119 d->parseStack[d->tos] = d->parseStack[d->tos - 1];
120 d->stateStack[d->tos] = act;
121 token = -1;
122 }
123
124 else if (act < 0) {
125 int r = - act - 1;
126 d->tos -= _Table::rhs[r];
127 act = d->stateStack[d->tos++];
128 consumeRule(r);
129 act = d->stateStack[d->tos] = _Table::nt_action(act, _Table::lhs[r] - _Table::TERMINAL_COUNT);
130 }
131
132 else break;
133 }
134
135 return false;
136 }
137
138
139 #endif // QPARSER_H