-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjsimsyntax.cpp
354 lines (281 loc) · 11.2 KB
/
jsimsyntax.cpp
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include "jsimsyntax.h"
#include <QTextCharFormat>
#include "calcvals.h"
#include <QBrush>
#include <QColor>
#include <QDate>
Jsimsyntax::Jsimsyntax(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
HighlightingRule rule;
keywordFormat.setForeground(Qt::darkBlue);
keywordFormat.setFontWeight(QFont::Bold);
QStringList keywordPatterns;
keywordPatterns << "\\.\\bmodel\\b" << "\\.\\bprint\\b" << "\\.\\bfile\\b"
<< "\\.\\btran\\b";
//Qt::CaseInsensitive;
for (const QString &pattern : keywordPatterns) {
rule.pattern = QRegularExpression(pattern);
rule.pattern.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
rule.format = keywordFormat;
highlightingRules.append(rule);
}
elementFormat.setForeground(Qt::darkMagenta);
elementFormat.setFontWeight(QFont::Bold);
QStringList elementPatterns;
elementPatterns << "^L\\S+" << "^R\\S+" << "^C\\S+"
<< "^K\\S+" << "^B\\S+" << "^I\\S+"
<< "^V\\S+" << "^X\\S+" <<"^P\\S+";
for (const QString &pattern : elementPatterns) {
rule.pattern = QRegularExpression(pattern);
rule.pattern.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
rule.format = elementFormat;
highlightingRules.append(rule);
}
classFormat.setFontWeight(QFont::Bold);
classFormat.setForeground(Qt::darkMagenta);
rule.pattern = QRegularExpression("\\bQ[A-Za-z]+\\b");
rule.format = classFormat;
highlightingRules.append(rule);
singleLineCommentFormat.setForeground(Qt::darkGreen);
rule.pattern = QRegularExpression("\\*[^\n]*");
rule.format = singleLineCommentFormat;
highlightingRules.append(rule);
//multiLinesubFormat.setForeground(Qt::red);
multiLinesubFormat.setBackground(Qt::lightGray);
quotationFormat.setForeground(Qt::red);
rule.pattern = QRegularExpression("\".*\"");
rule.format = quotationFormat;
highlightingRules.append(rule);
functionFormat.setFontItalic(true);
functionFormat.setForeground(Qt::blue);
rule.pattern = QRegularExpression("\\b[A-Za-z0-9_]+(?=\\()");
rule.format = functionFormat;
highlightingRules.append(rule);
subcktStartExpression = QRegularExpression("\\.subckt[^\n]*");
subcktEndExpression = QRegularExpression("\\.ends[^\n]*");
}
void Jsimsyntax::highlightBlock(const QString &text)
{
setCurrentBlockState(0);
int startIndex = 0;
if (previousBlockState() != 1)
startIndex = text.indexOf(subcktStartExpression);
while (startIndex >= 0) {
QRegularExpressionMatch match = subcktEndExpression.match(text, startIndex);
int endIndex = match.capturedStart();
int commentLength = 0;
if (endIndex == -1) {
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
} else {
commentLength = endIndex - startIndex
+ match.capturedLength();
}
setFormat(startIndex, commentLength, multiLinesubFormat);
startIndex = text.indexOf(subcktStartExpression, startIndex + commentLength);
}
for (const HighlightingRule &rule : highlightingRules) {
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
while (matchIterator.hasNext()) {
QRegularExpressionMatch match = matchIterator.next();
rule.format.background();
setFormat(match.capturedStart(), match.capturedLength(), rule.format);
}
}
}
QStringList Jsimsyntax::convertToJSIM(QStringList netlistCommands)
{
QStringList NoCommentCommands;
QStringList NewCommands;
QStringList ParamList={};
QStringList newParamList={};
QStringList NewCommandswithCommands;
QStringList stringNodes={};
for (int lineCNTR=0;lineCNTR<netlistCommands.length();lineCNTR++)
{
if (!netlistCommands.at(lineCNTR).contains(".param",Qt::CaseInsensitive))
{
QStringList temp = netlistCommands.at(lineCNTR).split('*');
if (!temp.at(0).isEmpty())
NoCommentCommands.append(temp.at(0));
//Find if the nodes are string or digit
QRegExp r = QRegExp("^[RLCBVIX]\\S+", Qt::CaseInsensitive);
QStringList SeperatedCommand=temp.at(0).split(" ");
r.indexIn(SeperatedCommand.at(0));
QString elementName = r.cap(0);
if (elementName.front()=='X')
{
for (int tempCntr=2;tempCntr<temp.length();tempCntr++)
{
QRegExp re("\\d*");
if (!re.exactMatch(temp.at(tempCntr)))
{
stringNodes.append(temp.at(tempCntr));
}
}
}else if (elementName.front()=='R'||elementName.front()=='L'||elementName.front()=='C'||elementName.front()=='B'||elementName.front()=='V'||elementName.front()=='I')
{
QRegExp re("\\d*");
if (!re.exactMatch(temp.at(1)))
{
stringNodes.append(temp.at(1));
}
if (!re.exactMatch(temp.at(2)))
{
stringNodes.append(temp.at(2));
}
}
temp.clear();
} else if (netlistCommands.at(lineCNTR).contains(".param",Qt::CaseInsensitive))
{
ParamList.append(netlistCommands.at(lineCNTR));
}
}
if (!ParamList.isEmpty())
{
newParamList = annotateParam(ParamList);
}
for (int cmdCNTR=0;cmdCNTR<NoCommentCommands.length();cmdCNTR++){
//Do the changes to the netlist here
//Check to see if nodes are string and replace them with numbers
QString newConvertLine = changeNodesToNum(stringNodes, NoCommentCommands.at(cmdCNTR));
//Check for the params in the netlist and replace them
for (int paramCntr=0; paramCntr<newParamList.size();paramCntr+=2)
if (newConvertLine.contains(newParamList.at(paramCntr)))
newConvertLine.replace(newParamList.at(paramCntr),newParamList.at(paramCntr+1));
NewCommands.append(newConvertLine);
}
int fileIndex=0;
NewCommandswithCommands.append("*** Converted netlist to JSIM format at " + QDate::currentDate().toString()+" ***");
for (int lineCNTR=0;lineCNTR<netlistCommands.length();lineCNTR++)
{
if (netlistCommands.at(lineCNTR).contains(NoCommentCommands.at(fileIndex)) && fileIndex<NewCommands.length())
{
QString NewLine=netlistCommands.at(lineCNTR);
NewLine.replace(NoCommentCommands.at(fileIndex),NewCommands.at(fileIndex),Qt::CaseInsensitive);
NewCommandswithCommands.append(NewLine);
//qDebug()<<QString::number(fileIndex)<<" : "<<NewLine;
fileIndex++;
}
else
{
NewCommandswithCommands.append(netlistCommands.at(lineCNTR));
//qDebug()<<netlistCommands.at(lineCNTR);
}
}
return NewCommandswithCommands;
}
QStringList Jsimsyntax::annotateParam(QStringList ParamList)
{
CalcVals *values= new CalcVals;
QStringList paramVal;
QStringList paramName;
QStringList paramNameVal;
char instr;
QRegExp numPattern = QRegExp("^[0-9]+(\\.[0-9]*)?[pnµufmKG]?$", Qt::CaseInsensitive);
QRegExp opPattern = QRegExp("[+*-\/]");
QStringList SeperatedCommand;
QStringList temp;
QString paramValtemp;
for (int cntr2=0; cntr2<ParamList.size();cntr2++)
{
SeperatedCommand = ParamList.at(cntr2).split(" ");
temp.clear();
for (int tempcntr=0;tempcntr<SeperatedCommand.length();tempcntr++){
if (!SeperatedCommand.at(tempcntr).isEmpty())
temp.append(SeperatedCommand.at(tempcntr));
}
int indexTemp=0;
while (indexTemp<temp.length())
{
if (temp.at(indexTemp).contains(".param",Qt::CaseInsensitive))
{
indexTemp++;
paramName.append(temp.at(indexTemp));
indexTemp++;
}else if (temp.at(indexTemp) == "=")
{
indexTemp++;
}else{
paramValtemp= "";
for (int cntr3=indexTemp; cntr3<temp.length(); cntr3++)
{
paramValtemp=paramValtemp+temp.at(cntr3);
indexTemp++;
}
paramVal.append(paramValtemp);
}
}
}
for (int cntr1=0; cntr1<ParamList.size();cntr1++)
{
QString parName= ParamList.at(cntr1);
QString parVal = ParamList.at(cntr1+1);
if (parVal.contains('+')||parVal.contains('-')||parVal.contains('*')||parVal.contains('/'))
{
}else{
}
for (int cntr2=0; cntr2<ParamList.size();cntr2++)
{
}
cntr1++;
}
return paramNameVal;
}
QString Jsimsyntax::changeNodesToNum(QStringList stringNodes, QString commandLine)
{
QString newCommandLine= commandLine;
//Start renaming the strings from this point, increase for bigger netlists.
int startNode=9369;
for (int nodeCntr=0; nodeCntr<stringNodes.size(); nodeCntr++)
{
if (commandLine.contains(stringNodes.at(nodeCntr)))
newCommandLine.replace(stringNodes.at(nodeCntr),QString::number(startNode+nodeCntr));
}
return newCommandLine;
}
double Jsimsyntax::annotateConstants(QString constName)
{
if (!QString::compare("PI", constName, Qt::CaseInsensitive))
{
return 3.141592653589793238463;
}else if (!QString::compare("PHI_ZERO", constName, Qt::CaseInsensitive))
{
return 2.067833831170082e-15;
} else if (!QString::compare("BOLTZMANN", constName, Qt::CaseInsensitive))
{
return 1.38064852E-23;
} else if (!QString::compare("EV", constName, Qt::CaseInsensitive))
{
return 1.6021766208E-19;
}else if (!QString::compare("HBAR", constName, Qt::CaseInsensitive))
{
return 1.0545718001391127E-34;
}else if (!QString::compare("C", constName, Qt::CaseSensitive))
{
return 299792458;
}else if (!QString::compare("MU0", constName, Qt::CaseInsensitive))
{
return 12.566370614E-7;
}else if (!QString::compare("EPS0", constName, Qt::CaseInsensitive))
{
return 8.854187817E-12;
}else if (!QString::compare("SIGMA", constName, Qt::CaseInsensitive))
{
return 3.291059757E-16;
}
return 0;
}
double Jsimsyntax::CalculateParam(double x, char oper, double y)
{
if (oper=='+')
return x+y;
else if (oper=='*')
return x*y;
else if (oper=='-')
return x-y;
else if (oper=='/')
return x/y;
return 0;
}