-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathGenUtils.cpp
More file actions
199 lines (183 loc) · 4.67 KB
/
GenUtils.cpp
File metadata and controls
199 lines (183 loc) · 4.67 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
/*
* 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 "GenUtils.h"
#include <QtDebug>
#include <QHash>
GenUtils::TokMap GenUtils::s_tokMap;
GenUtils::GenUtils()
{
}
QString GenUtils::escapeDollars(QString name)
{
const char dollar = '$';
const char dot = '.';
if( name.startsWith( dollar ))
name = "dlr_" + name.mid(1);
if( name.endsWith(dollar))
name = name.left(name.size()-1) + "_dlr";
name.replace(dollar,"_dlr_");
if( name.startsWith( dot ))
name = "dot_" + name.mid(1);
if( name.endsWith(dot))
name = name.left(name.size()-1) + "_dot";
name.replace(dot,"_dot_");
name.replace('-','_');
return name;
}
bool GenUtils::containsAlnum( const QString& str )
{
// '$' is not considered isLetterOrNumber
for( int i = 0; i < str.size(); i++ )
{
if( str[i].isLetterOrNumber() )
return true;
}
return false;
}
bool GenUtils::looksLikeKeyword(const QString& str)
{
if( str.isEmpty() || !str[0].isLetter() )
return false;
for( int i = 1; i < str.size(); i++ )
{
if( !str[i].isLetterOrNumber() && str[i] != '_' )
return false;
}
return true;
}
QString GenUtils::symToString(const QString& str)
{
if( str.isEmpty() )
return str;
if( s_tokMap.isEmpty() )
{
s_tokMap.insert("(*","Latt");
s_tokMap.insert("*)", "Ratt");
s_tokMap.insert("/*", "Lcmt");
s_tokMap.insert("*/", "Rcmt");
s_tokMap.insert("<=", "Leq");
s_tokMap.insert(">=", "Geq");
}
TokMap::const_iterator it = s_tokMap.find(str);
if( it != s_tokMap.end() )
return it.value();
if( containsAlnum(str) ) // as soon as there
return escapeDollars(str);
QString res;
int i = 0;
while( i < str.size() )
{
int n = 1;
while( i + n < str.size() && str[i] == str[i+n] )
n++;
if( n > 1 )
res += QString::number(n);
res += charToString(str[i]);
i += n;
}
return res;
}
QString GenUtils::charToString(QChar c)
{
TokMap::const_iterator i = s_tokMap.find(c);
if( i != s_tokMap.end() )
return i.value();
switch( c.unicode() )
{
case '+':
return "Plus";
case '-':
return "Minus";
case '!':
return "Bang";
case '=':
return "Eq";
case '~':
return "Tilde";
case '|':
return "Bar";
case '&':
return "Amp";
case '^':
return "Hat";
case '/':
return "Slash";
case '%':
return "Percent";
case '*':
return "Star";
case '<':
return "Lt";
case '>':
return "Gt";
case '#':
return "Hash";
case '@':
return "At";
case '?':
return "Qmark";
case '(':
return "Lpar";
case ')':
return "Rpar";
case '[':
return "Lbrack";
case ']':
return "Rbrack";
case '{':
return "Lbrace";
case '}':
return "Rbrace";
case ',':
return "Comma";
case '.':
return "Dot";
case ';':
return "Semi";
case ':':
return "Colon";
case '$':
return "Dlr";
case '"':
return "Quote";
default:
return QString::number( c.unicode(), 16 );
}
return QString();
}
static bool lessThan( const QString& lhs, const QString& rhs )
{
const bool lhsAlnum = GenUtils::containsAlnum(lhs);
const bool rhsAlnum = GenUtils::containsAlnum(rhs);
if( (lhsAlnum && rhsAlnum) || (!lhsAlnum && !rhsAlnum) )
return lhs < rhs;
else
return !lhsAlnum && rhsAlnum;
}
QStringList GenUtils::orderedTokenList(const QSet<QString>& tokens, bool applySymToString)
{
QStringList res = tokens.toList();
std::sort( res.begin(), res.end(), lessThan );
if( applySymToString )
{
for( int i = 0; i < res.size(); i++ )
res[i] = symToString( res[i] );
}
return res;
}