File: Tokenizer.cpp

package info (click to toggle)
structure-synth 1.5.0-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,268 kB
  • ctags: 1,966
  • sloc: cpp: 10,209; python: 164; makefile: 71; sh: 15
file content (202 lines) | stat: -rw-r--r-- 5,764 bytes parent folder | download | duplicates (9)
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
#include "Tokenizer.h"

#include <QStringList>

#include "../../SyntopiaCore/Exceptions/Exception.h"
#include "../../SyntopiaCore/Logging/Logging.h"

using namespace SyntopiaCore::Exceptions;
using namespace SyntopiaCore::Logging;


namespace StructureSynth {
	namespace Parser {	

		Tokenizer::~Tokenizer() {
		}

		Tokenizer::Tokenizer(QString input) {

			QStringList operators;
			operators << "c" << "reflect" << "color" << "blend" << "a" << "alpha" << "matrix" << "h" << "hue" << "sat" << "b" << "brightness" << "v" << "x" << "y" << "z" << "rx" << "ry" << "rz" << "s" << "fx" << "fy" << "fz" << "maxdepth" << "weight" << "md" << "w";

			currentSymbol = -1;

			// We will split on whitespace and line breaks
			// TODO: Respect quotations ( file = "C:\Program Files\Test" )
			QStringList l;
			QVector<int> positions;

			// We will use our own split routine
			QString current;
			int startPos = 0;
			input += " "; // to ensure last symbol gets parsed.
			bool inMultiComment = false;
			bool inComment = false;
			int newlines = 0;
			for (int i = 0; i < input.length(); i++) {
				
				if (input.at(i) == '\r') {
					inComment = false;
				    newlines++;
			    }

				// Check if we found a preprocessor comment (there must occur at the beginning of a line.
				if (input.at(i) == '#' && ((i == 0) || (input.at(i-1) == '\r') || (input.at(i-1) == '\n'))) {
					inComment = true; i++; continue;
				}


				if (i < input.length()-1) {
					if (input.at(i) == '*' && input.at(i+1) == '/') {
						inMultiComment = false; i++; continue;
					}

					if (input.at(i) == '/' && input.at(i+1) == '/') {
						inComment = true; i++; continue;
					}

					if (input.at(i) == '/' && input.at(i+1) == '*') {
						inMultiComment = true; i++; continue;
					}
				}

				if (inMultiComment || inComment) continue;

				if (input.at(i) == '[') {
					while (input.at(i) != ']' && i < input.length()) {
						current += input.at(i);
						i++;
					}
					current += ']';

					if (input.at(i) != ']') {
						throw ParseError("No matching ']' found for '['", startPos);
					}

					l.append(current);
					positions.append(startPos-newlines);
					startPos = i;
					current = "";
				} else 
				if (input.at(i) == '{' || input.at(i) == '}' || input.at(i) == ' ' || (input.at(i) == '\r') || (input.at(i) == '\n')) {
					QString trimmed = current.remove(QRegExp("\\s|\\r|\\n"));
					if (!current.trimmed().isEmpty()) { l.append(trimmed); positions.append(startPos-newlines);	}
					if (input.at(i) == '{' || input.at(i) == '}') { l.append(QString(input.at(i))); positions.append(i-newlines);	}
					current = "";
					startPos = i;
				} else {
					current += input.at(i);
				}
			}
		
			for (int i = 0; i < l.size(); i++) {
				QString s = l[i];
				int pos = positions[i];
				QString sl = l[i].toLower();

				if (sl == "rule") {
					symbols.append(Symbol(pos, Symbol::Rule, s));
				} else if (sl == "{") {
					symbols.append(Symbol(pos, Symbol::LeftBracket, s));
				} else if (sl == ">") {
					symbols.append(Symbol(pos, Symbol::MoreThan, s));
				}else if (sl == "}") {
					symbols.append(Symbol(pos, Symbol::RightBracket, s));
				} else if (sl == "*") {
					symbols.append(Symbol(pos, Symbol::Multiply, s));
				} else if (sl == "set") {
					symbols.append(Symbol(pos, Symbol::Set, s));
				} else if (QString("+-0123456789").contains(s[0])) {
					// It is a number (hopefully)

					if (s.count("/") == 1) {
						QString s1 = s.section("/",0,0);
						QString s2 = s.section("/",1,1);
						bool succes1 = false;
						int i1 = s1.toInt(&succes1);
						bool succes2 = false;
						int i2 = s2.toInt(&succes2);

						if ((i1 && i2) && (i2 != 0)) {
							Symbol ns(pos, Symbol::Number, s);
							ns.isInteger = false;
							ns.floatValue = ((double)i1)/i2;
							symbols.append(ns);
							continue;

						} else {
							throw ParseError("Invalid fraction found: " + s, pos);
						}
					}

					bool succes = false;
					int i = s.toInt(&succes);

					if (succes) {
						Symbol ns(pos, Symbol::Number, s);
						ns.isInteger = true;
						ns.intValue = i;
						symbols.append(ns);
						continue;
					}

					// the number was not an integer... Is it a floating-point value?

					double d = s.toDouble(&succes);

					if (succes) {
						Symbol ns(pos, Symbol::Number, s);
						ns.isInteger = false;
						ns.floatValue = d;
						//INFO(QString("Added float value:%1").arg(ns.floatValue));
						symbols.append(ns);
						continue;
					}

					throw ParseError("Invalid symbol found: " + s, pos);
				} else if (operators.contains(sl) ) {
					QString longName = sl;
					
					// Resolve abbreviations
					if (longName == "md") longName = "maxdepth";
					if (longName == "w") longName = "weight";
					if (longName == "h") longName = "hue";
					if (longName == "b") longName = "brightness";
					if (longName == "a") longName = "alpha";
					if (longName == "c") longName = "color";
					
					
					Symbol ns(pos, Symbol::Operator, longName);
					symbols.append(ns);

				} else {
					// TODO: We should check syntax of userstring here... (we dont want strings like slk"{/})
					Symbol ns(pos, Symbol::UserString, sl);
					symbols.append(ns);
				}
			}



			for (int i = 0; i < symbols.size(); i++) {

				//INFO(QString("%1. Symbol: %2, Position: %3").arg(i).arg(symbols[i].text).arg(positions[i]));
			}



		}

		Symbol Tokenizer::getSymbol() {
			currentSymbol++;

			if (currentSymbol < symbols.size()) {
				return symbols[currentSymbol];
			} 	

			return Symbol(-1, Symbol::End, "#END#");
		}
	}
}