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
|
/*
This file is part of KDE.
Copyright (c) 2004 Cornelius Schumacher <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "parser.h"
#include <kode/code.h>
#include <kode/printer.h>
#include <kode/typedef.h>
#include <kaboutdata.h>
#include <kapplication.h>
#include <kdebug.h>
#include <klocale.h>
#include <kcmdlineargs.h>
#include <kglobal.h>
#include <kconfig.h>
#include <ksimpleconfig.h>
#include <kstandarddirs.h>
#include <tqfile.h>
#include <textstream.h>
#include <tqdom.h>
#include <tqregexp.h>
#include <tqmap.h>
#include <iostream>
Pattern::Pattern()
: optional( false ), zeroOrMore( false ), oneOrMore( false ),
choice( false )
{
}
bool Pattern::isEmpty()
{
return !optional && !zeroOrMore && !oneOrMore && !choice;
}
TQString Pattern::asString()
{
if ( isEmpty() ) return "";
TQString str = "( ";
if ( optional ) str += "optional ";
if ( zeroOrMore ) str += "zeroOrMore ";
if ( oneOrMore ) str += "oneOrMore ";
if ( choice ) str += "choice ";
str += ")";
return str;
}
void Pattern::merge( Pattern p )
{
if ( p.optional ) optional = true;
if ( p.zeroOrMore ) zeroOrMore = true;
if ( p.oneOrMore ) oneOrMore = true;
if ( p.choice ) choice = true;
}
Element::Element()
: hasText( false ), isEmpty( false )
{
}
Parser::Parser()
{
}
Element *Parser::parse( const TQDomElement &docElement )
{
Element *start = 0;
TQDomNode n1;
for( n1 = docElement.firstChild(); !n1.isNull(); n1 = n1.nextSibling() ) {
TQDomElement e1 = n1.toElement();
kdDebug() << "TOP LEVEL element " << e1.tagName() << endl;
if ( e1.tagName() == "define" ) {
Element *d = new Element;
d->name = e1.attribute( "name" );
parseElement( e1, d, Pattern() );
Element::List definitions;
TQMap<TQString,Element::List >::ConstIterator it;
it = mDefinitionMap.find( d->name );
if ( it != mDefinitionMap.end() ) definitions = *it;
definitions.append( d );
mDefinitionMap.replace( d->name, definitions );
} else if ( e1.tagName() == "start" ) {
start = new Element;
parseElement( e1, start, Pattern() );
} else {
kdDebug() << "parseGrammar: Unrecognized tag: " << e1.tagName() << endl;
}
}
return start;
}
Reference *Parser::parseReference( const TQDomElement &referenceElement )
{
Reference *r = new Reference;
r->name = referenceElement.attribute( "name" );
return r;
}
bool Parser::parseAttribute( const TQDomElement &attributeElement,
Attribute *a )
{
a->name = attributeElement.attribute( "name" );
return true;
}
bool Parser::parseElement( const TQDomElement &elementElement, Element *e,
Pattern pattern )
{
kdDebug() << "parseElement " << e->name << endl;
TQDomNode n1;
for( n1 = elementElement.firstChild(); !n1.isNull(); n1 = n1.nextSibling() ) {
TQDomElement e1 = n1.toElement();
if ( e1.tagName() == "element" ) {
Element *element = new Element;
element->name = e1.attribute( "name" );
element->pattern = pattern;
parseElement( e1, element, Pattern() );
e->elements.append( element );
} else if ( e1.tagName() == "attribute" ) {
Attribute *a = new Attribute;
a->name = e1.attribute( "name" );
a->pattern = pattern;
kdDebug() << "ATTRIBUTE: " << a->name << " " << a->pattern.asString()
<< endl;
parseAttribute( e1, a );
e->attributes.append( a );
} else if ( e1.tagName() == "ref" ) {
Reference *r = parseReference( e1 );
r->pattern = pattern;
e->references.append( r );
} else if ( e1.tagName() == "text" ) {
e->hasText = true;
} else if ( e1.tagName() == "empty" ) {
e->isEmpty = true;
} else {
Pattern p = pattern;
if ( e1.tagName() == "optional" ) p.optional = true;
else if ( e1.tagName() == "zeroOrMore" ) p.zeroOrMore = true;
else if ( e1.tagName() == "oneOrMore" ) p.oneOrMore = true;
else if ( e1.tagName() == "choice" ) p.choice = true;
else {
kdDebug() << "Unsupported pattern '" << e1.tagName() << "'" << endl;
}
parseElement( e1, e, p );
}
}
return true;
}
void Parser::substituteReferences( Element *s )
{
kdDebug() << "substituteReferences for '" << s->name << "'" << endl;
Reference::List::Iterator it = s->references.begin();
while( it != s->references.end() ) {
Reference *r = *it;
kdDebug() << "REF " << r->name << endl;
if ( r->name == s->name ) {
kdDebug() << "Don't resolve self reference" << endl;
return;
}
if ( r->substituted ) {
kdDebug() << "Already substituted." << endl;
++it;
continue;
} else {
r->substituted = true;
}
TQMap<TQString,Element::List >::ConstIterator it1;
it1 = mDefinitionMap.find( r->name );
if ( it1 != mDefinitionMap.end() ) {
Element::List elements = *it1;
Element::List::ConstIterator it4;
for( it4 = elements.begin(); it4 != elements.end(); ++it4 ) {
Element *d = *it4;
substituteReferences( d );
Element::List::ConstIterator it2;
for( it2 = d->elements.begin(); it2 != d->elements.end(); ++it2 ) {
Element *e = *it2;
e->pattern.merge( r->pattern );
substituteReferences( e );
s->elements.append( e );
}
Attribute::List::ConstIterator it3;
for( it3 = d->attributes.begin(); it3 != d->attributes.end();
++it3 ) {
Attribute *a = *it3;
a->pattern.merge( r->pattern );
s->attributes.append( a );
}
}
it = s->references.erase( it );
} else {
kdDebug() << "Reference not found" << endl;
++it;
}
}
}
void Parser::doIndent( int cols )
{
for( int i = 0; i < cols; ++i ) std::cout << " ";
}
void Parser::dumpPattern( Pattern pattern )
{
std::cout << pattern.asString().utf8().data();
}
void Parser::dumpReferences( const Reference::List &references, int indent )
{
Reference::List::ConstIterator it;
for( it = references.begin(); it != references.end(); ++it ) {
Reference *r = *it;
doIndent( indent );
std::cout << "REFERENCE " << r->name.utf8().data();
dumpPattern( r->pattern );
std::cout << std::endl;
}
}
void Parser::dumpAttributes( const Attribute::List &attributes, int indent )
{
Attribute::List::ConstIterator it;
for( it = attributes.begin(); it != attributes.end(); ++it ) {
Attribute *a = *it;
doIndent( indent );
std::cout << "ATTRIBUTE " << a->name.utf8().data();
dumpPattern( a->pattern );
std::cout << std::endl;
}
}
void Parser::dumpElements( const Element::List &elements, int indent )
{
Element::List::ConstIterator it;
for( it = elements.begin(); it != elements.end(); ++it ) {
Element *e = *it;
dumpElement( e, indent );
}
}
void Parser::dumpElement( Element *e, int indent )
{
doIndent( indent );
std::cout << "ELEMENT " << e->name.utf8().data();
dumpPattern( e->pattern );
std::cout << std::endl;
if ( e->hasText ) {
doIndent( indent + 2 );
std::cout << "TEXT" << std::endl;
}
dumpAttributes( e->attributes, indent + 2 );
dumpElements( e->elements, indent + 2 );
dumpReferences( e->references, indent + 2 );
}
void Parser::dumpTree( Element *s )
{
std::cout << "START " << s->name.utf8().data() << std::endl;
dumpElements( s->elements, 2 );
dumpReferences( s->references, 2 );
}
void Parser::dumpDefinitionMap()
{
std::cout << "DEFINITION MAP" << std::endl;
TQMap<TQString,Element::List >::ConstIterator it;
for( it = mDefinitionMap.begin(); it != mDefinitionMap.end(); ++it ) {
dumpElements( *it, 2 );
}
}
|