File: scriptsyntax.cpp

package info (click to toggle)
meshlab 1.3.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 21,096 kB
  • ctags: 33,630
  • sloc: cpp: 224,813; ansic: 8,170; xml: 119; makefile: 80
file content (640 lines) | stat: -rw-r--r-- 15,909 bytes parent folder | download | duplicates (3)
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
#include "scriptsyntax.h"
#include <QFile>

SyntaxTreeNode::SyntaxTreeNode(const QVector<QVariant> &data, SyntaxTreeNode *parent)
{
	parentItem = parent;
	itemData = data;
}

SyntaxTreeNode::~SyntaxTreeNode()
{
	qDeleteAll(childItems);
}

SyntaxTreeNode *SyntaxTreeNode::child(int number)
{
	return childItems.value(number);
}

int SyntaxTreeNode::childCount() const
{
	return childItems.count();
}

int SyntaxTreeNode::childNumber() const
{
	if (parentItem)
		return parentItem->childItems.indexOf(const_cast<SyntaxTreeNode*>(this));

	return 0;
}

int SyntaxTreeNode::columnCount() const
{
	return itemData.count();
}

QVariant SyntaxTreeNode::data(int column) const
{
	return itemData.value(column);
}

bool SyntaxTreeNode::insertChildren(int position, int count, int columns)
{
	if (position < 0 || position > childItems.size())
		return false;

	for (int row = 0; row < count; ++row) 
	{
		QVector<QVariant> dt(columns);
		for(int ii = 0;ii < columns;++ii)
			dt[ii] = QVariant(this->data(ii).type());
		SyntaxTreeNode *item = new SyntaxTreeNode(dt, this);
		childItems.insert(position, item);
	}

	return true;
}

bool SyntaxTreeNode::insertColumns(int position, int columns)
{
	if (position < 0 || position > itemData.size())
		return false;

	for (int column = 0; column < columns; ++column)
		itemData.insert(position, QVariant());

	foreach (SyntaxTreeNode *child, childItems)
		child->insertColumns(position, columns);

	return true;
}

SyntaxTreeNode *SyntaxTreeNode::parent()
{
	return parentItem;
}

bool SyntaxTreeNode::removeChildren(int position, int count)
{
	if (position < 0 || position + count > childItems.size())
		return false;

	for (int row = 0; row < count; ++row)
		delete childItems.takeAt(position);

	return true;
}

bool SyntaxTreeNode::removeColumns(int position, int columns)
{
	if (position < 0 || position + columns > itemData.size())
		return false;

	for (int column = 0; column < columns; ++column)
		itemData.remove(position);

	foreach (SyntaxTreeNode *child, childItems)
		child->removeColumns(position, columns);

	return true;
}

bool SyntaxTreeNode::setData(int column, const QVariant &value)
{
	if (column < 0 || column >= itemData.size())
		return false;

	itemData[column] = value;
	return true;
}

SyntaxTreeNode* SyntaxTreeNode::findChild( const QVector<QVariant>& data )
{
	SyntaxTreeNode* ch = NULL;
	int ind = 0;
	while ((ch == NULL) && (ind < childCount()))
	{
		SyntaxTreeNode* tmp = child(ind);
		bool equal = true;
		int ii = 0;
		while(equal && (ii < tmp->columnCount()))
		{
			equal = (tmp->data(ii) == data[ii]);
			++ii;
		}
		if (equal)
			ch = tmp;
		else
			++ind;
	}
	return ch;
}

SyntaxTreeModel::SyntaxTreeModel(SyntaxTreeNode* root,QObject *parent)
:QAbstractItemModel(parent),rootItem(root)
{
}

SyntaxTreeModel::~SyntaxTreeModel()
{
	delete rootItem;
}

int SyntaxTreeModel::columnCount(const QModelIndex & /* parent */) const
{
	return rootItem->columnCount();
}

QVariant SyntaxTreeModel::data(const QModelIndex &index, int role) const
{
	if (!index.isValid())
		return QVariant();

	if (role != Qt::DisplayRole && role != Qt::EditRole)
		return QVariant();

	SyntaxTreeNode *item = getItem(index);

	return item->data(index.column());
}

Qt::ItemFlags SyntaxTreeModel::flags(const QModelIndex &index) const
{
	if (!index.isValid())
		return 0;

	return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

SyntaxTreeNode *SyntaxTreeModel::getItem(const QModelIndex &index) const
{
	if (index.isValid()) {
		SyntaxTreeNode *item = static_cast<SyntaxTreeNode*>(index.internalPointer());
		if (item) return item;
	}
	return rootItem;
}

QVariant SyntaxTreeModel::headerData(int section, Qt::Orientation orientation,
									 int role) const
{
	if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
		return rootItem->data(section);

	return QVariant();
}

QModelIndex SyntaxTreeModel::index(int row, int column, const QModelIndex &parent) const
{
	if (parent.isValid() && parent.column() != 0)
		return QModelIndex();

	SyntaxTreeNode *parentItem = getItem(parent);

	SyntaxTreeNode *childItem = parentItem->child(row);
	if (childItem)
		return createIndex(row, column, childItem);
	else
		return QModelIndex();
}

bool SyntaxTreeModel::insertColumns(int position, int columns, const QModelIndex &parent)
{
	bool success;

	beginInsertColumns(parent, position, position + columns - 1);
	success = rootItem->insertColumns(position, columns);
	endInsertColumns();

	return success;
}

bool SyntaxTreeModel::insertRows(int position, int rows, const QModelIndex &parent)
{
	SyntaxTreeNode *parentItem = getItem(parent);
	bool success;

	beginInsertRows(parent, position, position + rows - 1);
	success = parentItem->insertChildren(position, rows, rootItem->columnCount());
	endInsertRows();

	return success;
}

QModelIndex SyntaxTreeModel::parent(const QModelIndex &index) const
{
	if (!index.isValid())
		return QModelIndex();

	SyntaxTreeNode *childItem = getItem(index);
	SyntaxTreeNode *parentItem = childItem->parent();

	if (parentItem == rootItem)
		return QModelIndex();

	return createIndex(parentItem->childNumber(), 0, parentItem);
}

bool SyntaxTreeModel::removeColumns(int position, int columns, const QModelIndex &parent)
{
	bool success;

	beginRemoveColumns(parent, position, position + columns - 1);
	success = rootItem->removeColumns(position, columns);
	endRemoveColumns();

	if (rootItem->columnCount() == 0)
		removeRows(0, rowCount());

	return success;
}

bool SyntaxTreeModel::removeRows(int position, int rows, const QModelIndex &parent)
{
	SyntaxTreeNode *parentItem = getItem(parent);
	bool success = true;

	beginRemoveRows(parent, position, position + rows - 1);
	success = parentItem->removeChildren(position, rows);
	endRemoveRows();

	return success;
}

int SyntaxTreeModel::rowCount(const QModelIndex &parent) const
{
	SyntaxTreeNode *parentItem = getItem(parent);

	return parentItem->childCount();
}

bool SyntaxTreeModel::setData(const QModelIndex &index, const QVariant &value,
							  int role)
{
	if (role != Qt::EditRole)
		return false;

	SyntaxTreeNode *item = getItem(index);
	bool result = item->setData(index.column(), value);

	if (result)
		emit dataChanged(index, index);

	return result;
}

bool SyntaxTreeModel::setHeaderData(int section, Qt::Orientation orientation,
									const QVariant &value, int role)
{
	if (role != Qt::EditRole || orientation != Qt::Horizontal)
		return false;

	bool result = rootItem->setData(section, value);

	if (result)
		emit headerDataChanged(orientation, section, section);

	return result;
}

//void SyntaxTreeModel::addCompleteSubTree(const QStringList &signatures)
//{
//	QStringList signs = signatures;
//	foreach(QString sg,signs)
//		createAndAppendBranch(sg,rootItem);
//}

//void SyntaxTreeModel::createAndAppendBranch( QString& st,SyntaxTree* parent )
//{
//	if (st.isEmpty() || (parent == NULL))
//		return;
//	QVector<QVariant> dt;
//	int indexpoint = st.indexOf(".");
//	if (indexpoint == -1)
//	{
//		int indexpar = st.indexOf("(");
//		if (indexpar == -1)
//		{
//			//is a member
//			dt << st;
//		}
//		else 
//		{
//			//is a function. I will add the name of the function and the signature for the tooltip
//			dt << st.left(indexpar)/* << st*/;
//		}
//		AutoCompleterItem* ch = parent->findChild(dt);
//		//Search if the node is already in the tree
//		if (ch == NULL)
//		{
//			ch = new AutoCompleterItem(dt,parent);
//			parent->appendChild(ch);
//		}
//		return;
//	}
//	else
//	{
//		//+1 so I will take also the .
//		QString tmp  = st.left(indexpoint + 1);
//		st.remove(tmp);
//		tmp.remove(".");
//		dt << tmp;
//		int ind = 0;
//
//		AutoCompleterItem* ch = parent->findChild(dt);
//		//Search if the node is already in the tree
//		if (ch == NULL)
//		{
//			ch = new AutoCompleterItem(dt,parent);
//			parent->appendChild(ch);
//		}
//		createAndAppendBranch(st,ch);
//	}
//}

MLScriptLanguage::MLScriptLanguage()
:reserved(),langfuncs(),worddelimiter(),wordsjoiner(),openpar(),closepar(),libraries()
{
	initLibrary();
}



const SyntaxTreeModel* MLScriptLanguage::functionsLibrary() const
{
	return libraries;
}

SyntaxTreeModel* MLScriptLanguage::functionsLibrary()
{
	return libraries;
}

void MLScriptLanguage::addLibrary( const QList<LibraryElementInfo>& funsigns )
{
	if (libraries != NULL)
	{
		SyntaxTreeNode* root = libraries->getItem(QModelIndex());
		foreach(LibraryElementInfo st,funsigns)
			addBranch(st,root);
	}
}

void MLScriptLanguage::addBranch( const LibraryElementInfo& mi,SyntaxTreeNode* parent )
{
	if (mi.completename.isEmpty() || (parent == NULL))
		return;
	QString st = mi.completename;
	QVector<QVariant> dt(5,QVariant(QString()));
	int indexpoint = st.indexOf(wordsjoiner);
	if (indexpoint == -1)
	{
		int indexpar = st.indexOf(openpar);
		if (indexpar == -1)
		{
			//is a member
			dt[0] = st;
			dt[1] = mi.help;
			dt[4] = QString::number(MLScriptLanguage::MEMBERFIELD);
		}
		else 
		{
			//is a function. I will add the name of the function and the signature for the tooltip
			dt[0] = st.left(indexpar);
			dt[1] = mi.help;
			dt[3] = st;
			dt[4] = QString::number(MLScriptLanguage::FUNCTION);
		}
		SyntaxTreeNode* ch = parent->findChild(dt);

		//Search if the node is already in the tree
		if (ch == NULL)
		{
			parent->insertChildren(parent->childCount(),1,parent->columnCount());
			ch = parent->child(parent->childCount() - 1);
			for(int ii = 0;ii < 5;++ii)
				ch->setData(ii,dt[ii]);
		}
		return;
	}
	else
	{
		//+1 so I will take also the sep
		QString tmp  = st.left(indexpoint + 1);
		st.remove(tmp);
		QString specificsep = wordsjoiner.cap();
		tmp.remove(wordsjoiner);
		dt[0] = tmp;
		dt[2] = specificsep;
		dt[4] = QString::number(MLScriptLanguage::NAMESPACE);
		SyntaxTreeNode* ch = parent->findChild(dt);
		//Search if the node is already in the tree
		if (ch == NULL)
		{
			parent->insertChildren(parent->childCount(),1,parent->columnCount());
			ch = parent->child(parent->childCount() - 1);
			for(int ii = 0;ii < 5;++ii)
				ch->setData(ii,dt[ii]);
		}
		LibraryElementInfo minext;
		minext.completename = st;
		minext.help = mi.help; 
		addBranch(minext,ch);
	}
}

MLScriptLanguage::~MLScriptLanguage()
{

}


QString MLScriptLanguage::getExternalLibrariesCode()
{
	QString code;
	QList<ExternalLib*> liblist = this->scriptLibraryFiles();
	for(int ii = 0;ii <liblist.size();++ii)
		code += liblist[ii]->libCode();
	return code;
}

void MLScriptLanguage::initLibrary()
{
	delete libraries;
	QVector<QVariant> v;
	v.push_back("partial function ID");
	v.push_back("help");
	v.push_back("separator");
	v.push_back("signature");
	v.push_back("token");
	SyntaxTreeNode* root = new SyntaxTreeNode(v,NULL);
	libraries = new SyntaxTreeModel(root,NULL);
}

QList<LibraryElementInfo> MLScriptLanguage::getExternalLibrariesMembersInfo() const
{
	QList<LibraryElementInfo> res;
	QList<ExternalLib*> liblist = this->scriptLibraryFiles();
	for(int ii = 0;ii <liblist.size();++ii)
		res.append(liblist[ii]->libraryMembersInfo());
	return res;
}

QRegExp MLScriptLanguage::matchIdentifiersButNotReservedWords() const
{
	QString res = reserved.join("|");
	QRegExp name("([a-z]|[A-Z])+\\w*");
	QRegExp nokey("\\b(?!(?:" + res + ")\\b)");
	return QRegExp(nokey.pattern() + name.pattern() + "\\b(\\s*" + wordsjoiner.pattern() + "\\s*" + nokey.pattern() + name.pattern() + "\\b)*");
}

QRegExp MLScriptLanguage::matchOnlyReservedWords() const
{
	QString res = reserved.join("|");
	return QRegExp("\\b(" + res + ")\\b"); 
}

QRegExp MLScriptLanguage::joinedWordExpression() const
{
	//*\\*/(\\(\\w*\\))?
	QRegExp parameter("(\\w*|\\d*(\\.\\d+)*)");
	QRegExp parameterlist(parameter.pattern() + "(\\s*,\\s*" + parameter.pattern() + ")*");
	QRegExp name("([a-z]|[A-Z])+\\w*(\\(" + parameterlist.pattern() + "\\))?");
	QString st(name.pattern() + "(\\s*" + wordsjoiner.pattern() +"\\s*|\\s*" + wordsjoiner.pattern() +"\\s*"  + name.pattern() + ")*");
	return QRegExp(st);
	
}

JavaScriptLanguage::JavaScriptLanguage()
:MLScriptLanguage()
{
	//HighlightingRule res;
	//res.format.setForeground(Qt::darkBlue);
	//res.format.setFontWeight(QFont::Bold);
	wordsjoiner.setPattern("\\.");
	openpar.setPattern("\\(");
	closepar.setPattern("\\)");
	reserved << "break";
	reserved << "case";	
	reserved << "catch";	
	reserved << "continue";
	reserved << "default";
	reserved << "delete";
	reserved << "do";
	reserved << "else";
	reserved << "finally";
	reserved << "for";
	reserved << "function";
	reserved << "if";
	reserved << "in";
	reserved << "instanceof";
	reserved << "new";
	reserved << "return";
	reserved << "switch";
	reserved << "this";
	reserved << "throw";
	reserved << "try";
	reserved << "typeof";
	reserved << "var";
	reserved << "void";
	reserved << "while";
	reserved << "with";
	reserved << "true";
	reserved << "false";
	reserved << "null";

	worddelimiter.setPattern("[\\s|\\t|\\n|\\r|=|;|,|\\(|\\)|{|}|\\[|\\]|\\||\\&|\\?|\\!|\\+|\\*|\\\\|\\-|%|\"|<|>]");

	//foreach(QString st,reserved)
	//{
	//	res.pattern = QRegExp(st);
	//	highlightingRules << res;
	//}

	//autoCompleteModel(mod);
	////comp.setModel(&ls);
	//comp.setCaseSensitivity(Qt::CaseSensitive);
	//comp.setWidget(parent);
	//comp.setCompletionMode(QCompleter::PopupCompletion);
	//comp.setModel(&mod);
}

const QList<ExternalLib*> JavaScriptLanguage::scriptLibraryFiles() const
{
	QList<ExternalLib*> res;
	SGLMathLib* lib = new SGLMathLib();
	res << lib;
	return res;
}

//QStringList JavaScriptLanguage::splitLineInWords( const QStringList& line )
//{
//	//Regular expression matching all the identifiers but not keywords
//	QRegExp exp = matchIdentifiersButNotReservedWords();
//	int index = 0;
//	while (index >= 0)
//	{
//		line 
//	}
//}

QString ExternalLib::libCode() const
{
	QFile lib(name);
	if (!lib.open(QFile::ReadOnly))
		qDebug("Warning: Library %s has not been loaded.",qPrintable(name));
	QByteArray libcode = lib.readAll();
		/*QScriptValue res = env.evaluate(QString(libcode));
		if (res.isError())
		throw JavaScriptException("Library " + liblist[ii] + " generated a JavaScript Error: " + res.toString() + "\n");*/
	return QString(libcode);
}

ExternalLib::ExternalLib( const QString& filename )
:name(filename)
{
}

SGLMathLib::SGLMathLib()
:ExternalLib(":/script_system/math.js")
{
	
}

QList<LibraryElementInfo> SGLMathLib::libraryMembersInfo() const
{
	QString code = libCode();
	QList<LibraryElementInfo> res;
	int index = 0;
	QRegExp parameter("\\w*");
	QRegExp parameterlist(parameter.pattern() + "(\\s*,\\s*" + parameter.pattern() + ")*");
	QRegExp namespacelist(parameter.pattern() + "(\\s*\\.\\s*" + parameter.pattern() + ")*\\$?");
	QRegExp help("/\\*([^*]|[\\r\\n]|(\\*+([^*/]|[\\r\\n])))*\\*+/");
	QRegExp exp("(" + help.pattern() + ")?" + "\\s*" + namespacelist.pattern() + "\\s*=\\s*function\\s*\\(" + parameterlist.pattern() + "\\)");
	int ii = 0;
	do
	{
		index = code.indexOf(exp,index) + exp.matchedLength();
		if (index >= 0)
		{
			QString fun = exp.cap();
			LibraryElementInfo mi; 
			int helpind = fun.indexOf(help);
			if (helpind >= 0)
			{
				mi.help = help.cap();
				mi.help.remove(QRegExp("\\*\\s+\\n"));
				fun.remove(help.cap());
			}
			fun.remove(QRegExp("\\s*")).remove("=").remove("function");
			mi.completename = fun;
			res << mi;
			++ii;
		}
	}while(index >= 0);
	return res;
}