Debian Codegraph @

src/tools/qlalr/examples/dummy-xml/xml.g

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 QtCore 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 %parser XMLTable
30
31 %impl xmlreader.cpp
32
33 %token LEFT_ANGLE
34 %token RIGHT_ANGLE
35 %token ANY
36
37 %start XmlStream
38
39 /.
40 #ifndef XMLREADER_H
41 #define XMLREADER_H
42
43 #include <QtCore>
44 #include <cstdio>
45 #include <cstdlib>
46 #include <cstring>
47 #include "$header"
48
49 class XMLReader: protected $table
50 {
51 public:
52 XMLReader(const QByteArray &bytes);
53 ~XMLReader();
54
55 bool parse();
56
57 inline int nextToken()
58 {
59 switch (*bufptr++) {
60 case '\0':
61 return EOF_SYMBOL;
62
63 case '<':
64 in_tag = true;
65 return LEFT_ANGLE;
66
67 case '>':
68 if (! in_tag)
69 break;
70 in_tag = false;
71 return RIGHT_ANGLE;
72 break;
73
74 } // switch
75
76 return ANY;
77 }
78
79 protected:
80 inline void reallocateStack();
81
82 inline int &sym(int index)
83 { return stack [tos + index - 1].ival; }
84
85 protected:
86 int tos;
87 int stack_size;
88
89 struct StackItem {
90 int state;
91 int ival;
92 };
93
94 QVarLengthArray<StackItem> stack;
95 unsigned in_tag: 1;
96 QByteArray bytes;
97 const char *bufptr;
98 };
99
100 inline void XMLReader::reallocateStack()
101 {
102 if (! stack_size)
103 stack_size = 128;
104 else
105 stack_size <<= 1;
106
107 stack.resize (stack_size);
108 }
109
110 #endif // XMLREADER_H
111
112 XMLReader::XMLReader(const QByteArray &bytes):
113 tos(0),
114 stack_size(0),
115 bytes(bytes)
116 {
117 bufptr = bytes.constData();
118 }
119
120 XMLReader::~XMLReader()
121 {
122 }
123
124 bool XMLReader::parse()
125 {
126 const int INITIAL_STATE = 0;
127
128 in_tag = 0;
129 bufptr = bytes.constData();
130
131 int yytoken = -1;
132 reallocateStack();
133
134 tos = 0;
135 stack [++tos].state = INITIAL_STATE;
136
137 while (true)
138 {
139 const int state = stack [tos].state;
140
141 if (yytoken == -1 && - TERMINAL_COUNT != action_index [state])
142 yytoken = nextToken();
143
144 int act = t_action (state, yytoken);
145
146 if (act == ACCEPT_STATE)
147 return true;
148
149 else if (act > 0)
150 {
151 if (++tos == stack_size)
152 reallocateStack();
153
154 stack [tos].ival = *bufptr; // ### save the token value here
155 stack [tos].state = act;
156 yytoken = -1;
157 }
158
159 else if (act < 0)
160 {
161 int r = - act - 1;
162
163 tos -= rhs [r];
164 act = stack [tos++].state;
165
166 switch (r) {
167 ./
168
169
170
171
172 XmlStream: TagOrWord ;
173 XmlStream: XmlStream TagOrWord ;
174
175 TagOrWord: Tag ;
176 TagOrWord: ANY ;
177
178 Tag: LEFT_ANGLE TagName RIGHT_ANGLE ;
179 /.
180 case $rule_number: {
181 fprintf (stderr, "--* found a tag\n");
182 } break;
183 ./
184
185 TagName: ANY ;
186 TagName: TagName ANY ;
187
188
189 /.
190 } // switch
191
192 stack [tos].state = nt_action (act, lhs [r] - TERMINAL_COUNT);
193 }
194
195 else
196 {
197 // ### ERROR RECOVERY HERE
198 break;
199 }
200 }
201
202 return false;
203 }
204
205
206
207 /////////////////////////////
208 // entry point
209 /////////////////////////////
210 int main(int, char *argv[])
211 {
212 QFile f (argv[1]);
213
214 if (f.open(QFile::ReadOnly)) {
215 QByteArray contents = f.readAll();
216 XMLReader parser (contents);
217
218 if (parser.parse())
219 printf ("OK\n");
220 else
221 printf ("KO\n");
222 }
223 }
224
225
226
227
228 ./
229