File: qmglsyntax.cpp

package info (click to toggle)
mathgl 8.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 248,044 kB
  • sloc: cpp: 87,365; ansic: 3,299; javascript: 3,284; pascal: 1,562; python: 52; sh: 51; makefile: 47; f90: 22
file content (121 lines) | stat: -rw-r--r-- 5,183 bytes parent folder | download | duplicates (5)
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
/***************************************************************************
 *   Copyright (C) 2008 by Alexey Balakin                                  *
 *   mathgl.abalakin@gmail.com                                             *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include <QPushButton>
#include <QLabel>
#include <QLayout>
#include "qmglsyntax.h"
#include <mgl2/mgl.h>
mglParse parser;
int mgl_cmd_cmp(const void *a, const void *b);
// comment string keyword option suffix number
QColor mglColorScheme[10] = {QColor(0,127,0), QColor(255,0,0), QColor(0,0,127), QColor(127,0,0), QColor(127,0,0), QColor(0,0,255), QColor(127,0,127), QColor(0,127,127), QColor(0,0,127), QColor(255,255,204)};
//-----------------------------------------------------------------------------
QMGLSyntax::QMGLSyntax(QTextEdit *textEdit) : QSyntaxHighlighter(textEdit)	{}
//-----------------------------------------------------------------------------
void QMGLSyntax::highlightBlock(const QString &text)
{
	int i, j, m = text.length(),s=0;
	bool arg = false, nl = true;
	QString num("+-.0123456789:");
	i=0;
	setCurrentBlockState(-1);
	if(previousBlockState()==1)
	{
		bool cont=false;
		j=i;	i++;
		for(;i<m && text[i]!='\'';i++)
		{
			if(text[i]>' ')		cont=false;
			if(text[i]=='\\')	cont=true;
		}
		setFormat(j,i-j+1,mglColorScheme[1]);
		if(cont && i==m)	setCurrentBlockState(1);
	}
	for(;i<m;i++)				// highlight paragraph
	{
		if(text[i]=='(')	s++;
		if(text[i]==')')	s--;
		if(text[i]==' ' || text[i]=='\t')	continue;
		else if(text[i]=='#')	// comment
		{	setFormat(i,m-i,mglColorScheme[0]);	break;	}
		else if(text[i]=='\'')	// string
		{
			bool cont=false;
			j=i;	i++;
			for(;i<m && text[i]!='\'';i++)
			{
				if(text[i]>' ')		cont=false;
				if(text[i]=='\\')	cont=true;
			}
			setFormat(j,i-j+1,mglColorScheme[1]);
			if(cont && i==m)	setCurrentBlockState(1);
		}
		else if(nl)				// keyword
		{
			char *s = new char[m+1];
			for(j=i;j<text.length() && !text[j].isSpace() && text[j]!=':';j++)
				s[j-i] = text[j].toLatin1();
			s[j-i]=0;
			int type = parser.CmdType(s);
			if(type)	setFormat(i,j-i+1,type!=7 ? (type==5 ? mglColorScheme[6] : mglColorScheme[2]) : mglColorScheme[7]);
			delete []s;
		}
		else if(text[i]==';')	{	arg = true;	nl = false;	continue;	}
		else if(text[i]==':' && s==0)	{	nl=true;	continue;	}
		else if(arg)			// option
		{
			const char *o[13]={"xrange","yrange","zrange","cut","meshnum","alpha","light","ambient","diffuse","size","legend","number","value"};
			unsigned l;
			for(j=0;j<13;j++)
			{
				l = strlen(o[j]);
				if(text.indexOf(o[j],i)==i && (i+l==long(text.length()) || text[i+l].isSpace()))
					setFormat(i,l,mglColorScheme[3]);
			}
		}
		else if(text[i]=='.' && i+1<text.length() && text[i+1].isLetter())	// suffix
		{
			for(j=i;j<text.length() && !text[j].isSpace();j++){};
			setFormat(i,j-i+1,mglColorScheme[4]);
		}
		else if(num.contains(text[i]))	// number
			setFormat(i,1,mglColorScheme[5]);
		else if((text[i]=='e' || text[i]=='E') && i+1<text.length() && num.contains(text[i-1]) && num.contains(text[i+1]) )
			setFormat(i,1,mglColorScheme[5]);
		else		// number as its symbolic id
		{
			const char *o[6]={"nan","inf","pi","on","off","all"};
			int l[6] = {3, 3, 2, 2, 3, 3};
			for(j=0;j<6;j++)
				if(text.indexOf(o[j],i)==i && (i+l[j]==text.length() || text[i+l[j]].isSpace()))
					setFormat(i,l[j],mglColorScheme[5]);
		}
		arg = nl = false;
	}
}
//-----------------------------------------------------------------------------
MessSyntax::MessSyntax(QTextEdit *textEdit) : QSyntaxHighlighter(textEdit)	{}
//-----------------------------------------------------------------------------
void MessSyntax::highlightBlock(const QString &text)
{
	if(text.contains("in line "))
		setFormat(0, text.length(), QColor(255,0,0));
}
//-----------------------------------------------------------------------------