-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSyntaxTreeMdl.cpp
More file actions
252 lines (228 loc) · 7.47 KB
/
SyntaxTreeMdl.cpp
File metadata and controls
252 lines (228 loc) · 7.47 KB
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
/*
* Copyright 2019 Rochus Keller <mailto:me@rochus-keller.ch>
*
* 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 me@rochus-keller.ch.
*
* 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 "SyntaxTreeMdl.h"
#include <QBrush>
#include <QPixmap>
#include <QtDebug>
#include <QTreeView>
SyntaxTreeMdl::SyntaxTreeMdl(QTreeView* parent) :
QAbstractItemModel(parent)
{
}
QTreeView*SyntaxTreeMdl::getParent() const
{
return static_cast<QTreeView*>(QObject::parent());
}
void SyntaxTreeMdl::setSyntax( EbnfSyntax* syn )
{
beginResetModel();
d_root = Slot();
d_syn = syn;
fillTop();
endResetModel();
}
const Ast::Symbol* SyntaxTreeMdl::getSymbol(const QModelIndex& index) const
{
if( !index.isValid() || d_syn.constData() == 0 )
return 0;
Slot* s = static_cast<Slot*>( index.internalPointer() );
Q_ASSERT( s != 0 );
return s->d_sym;
}
QModelIndex SyntaxTreeMdl::findSymbol(quint32 line, quint16 col)
{
return findSymbol( &d_root, line, col );
}
static inline QString _quant( quint8 q, const QString& txt, bool nullable = false, bool repeatable = false )
{
switch( q )
{
case Ast::Node::One:
if( repeatable )
return QString("{ %1").arg(txt);
else if( nullable )
return QString("[ %1").arg(txt);
else
return txt;
case Ast::Node::ZeroOrOne:
return QString("[ %1 ]").arg(txt);
break;
case Ast::Node::ZeroOrMore:
return QString("{ %1 }").arg(txt);
default:
return "<unknown quantifier>";
}
}
QVariant SyntaxTreeMdl::data(const QModelIndex& index, int role) const
{
if( !index.isValid() || d_syn.constData() == 0 )
return QVariant();
Slot* s = static_cast<Slot*>( index.internalPointer() );
Q_ASSERT( s != 0 );
switch( role )
{
case Qt::DisplayRole:
if( s->d_sym->d_tok.d_type != EbnfToken::Production )
{
const Ast::Node* node = static_cast<const Ast::Node*>( s->d_sym );
switch( node->d_type )
{
case Ast::Node::Terminal:
case Ast::Node::Nonterminal:
return _quant( node->d_quant, node->d_tok.d_val.toStr(), node->isNullable(), node->isRepeatable() );
case Ast::Node::Sequence:
return _quant( node->d_quant, "seq" );
case Ast::Node::Alternative:
return _quant( node->d_quant, "alt" );
default:
return s->d_sym->d_tok.d_val.toStr();
}
}else if( s->d_sym->isRepeatable() )
return QString("{ %1").arg(s->d_sym->d_tok.d_val.toStr());
else if( s->d_sym->isNullable() )
return QString("[ %1").arg(s->d_sym->d_tok.d_val.toStr());
else
return s->d_sym->d_tok.d_val.toStr();
break;
case Qt::ForegroundRole:
if( s->d_sym->d_tok.d_type != EbnfToken::Production )
{
const Ast::Node* node = static_cast<const Ast::Node*>( s->d_sym );
switch( node->d_type )
{
case Ast::Node::Terminal:
return QBrush( Qt::red );
case Ast::Node::Predicate:
return QBrush( Qt::darkCyan );
case Ast::Node::Sequence:
case Ast::Node::Alternative:
return QBrush( Qt::blue );
default:
break;
}
}
break;
case Qt::FontRole:
if( s->d_sym->d_tok.d_type != EbnfToken::NonTerm && s->d_sym->d_tok.d_type != EbnfToken::Predicate &&
s->d_sym->d_tok.d_type != EbnfToken::Production && s->d_sym->d_tok.d_type != EbnfToken::Invalid )
{
QFont f = getParent()->font();
f.setBold(true);
return f;
}else if( s->d_sym->d_tok.d_op == EbnfToken::Transparent )
{
QFont f = getParent()->font();
f.setItalic(true);
return f;
}
break;
case Qt::BackgroundRole:
if( s->d_sym->doIgnore() && s->d_sym->d_tok.d_type != EbnfToken::Predicate )
return QBrush( Qt::lightGray );
break;
}
return QVariant();
}
QModelIndex SyntaxTreeMdl::parent ( const QModelIndex & index ) const
{
if( index.isValid() )
{
Slot* s = static_cast<Slot*>( index.internalPointer() );
Q_ASSERT( s != 0 );
if( s->d_parent == &d_root )
return QModelIndex();
// else
Q_ASSERT( s->d_parent != 0 );
Q_ASSERT( s->d_parent->d_parent != 0 );
return createIndex( s->d_parent->d_parent->d_children.indexOf( s->d_parent ), 0, s->d_parent );
}else
return QModelIndex();
}
int SyntaxTreeMdl::rowCount ( const QModelIndex & parent ) const
{
if( parent.isValid() )
{
Slot* s = static_cast<Slot*>( parent.internalPointer() );
Q_ASSERT( s != 0 );
return s->d_children.size();
}else
return d_root.d_children.size();
}
QModelIndex SyntaxTreeMdl::index ( int row, int column, const QModelIndex & parent ) const
{
const Slot* s = &d_root;
if( parent.isValid() )
{
s = static_cast<Slot*>( parent.internalPointer() );
Q_ASSERT( s != 0 );
}
if( row < s->d_children.size() && column < columnCount( parent ) )
return createIndex( row, column, s->d_children[row] );
else
return QModelIndex();
}
Qt::ItemFlags SyntaxTreeMdl::flags( const QModelIndex & index ) const
{
Q_UNUSED(index)
return Qt::ItemIsEnabled | Qt::ItemIsSelectable; // | Qt::ItemIsDragEnabled;
}
void SyntaxTreeMdl::fillTop()
{
if( d_syn.constData() == 0 )
return;
typedef QMultiMap<QString,Ast::Definition*> Sorter;
Sorter sorter;
for( EbnfSyntax::Definitions::const_iterator i = d_syn->getDefs().begin(); i != d_syn->getDefs().end(); ++i )
sorter.insert( i.key().toStr().toLower(), i.value() );
for( Sorter::const_iterator j = sorter.begin(); j != sorter.end(); ++j )
{
Slot* s = new Slot();
s->d_parent = &d_root;
s->d_sym = j.value();
d_root.d_children.append( s );
fill( s, j.value()->d_node );
}
}
QModelIndex SyntaxTreeMdl::findSymbol(SyntaxTreeMdl::Slot* slot, quint32 line, quint16 col) const
{
for( int i = 0; i < slot->d_children.size(); i++ )
{
Slot* s = slot->d_children[i];
if( s->d_sym->d_tok.d_lineNr == line && s->d_sym->d_tok.d_colNr <= col &&
col < ( s->d_sym->d_tok.d_colNr + s->d_sym->d_tok.d_len ) )
return createIndex( i, 0, s );
QModelIndex index = findSymbol( s, line, col );
if( index.isValid() )
return index;
}
return QModelIndex();
}
void SyntaxTreeMdl::fill(Slot* super, const Ast::Node* sym )
{
if( sym == 0 )
return;
Slot* s = new Slot();
s->d_parent = super;
s->d_sym = sym;
super->d_children.append( s );
foreach( const Ast::Node* sub, sym->d_subs )
{
fill(s, sub );
}
}