-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeblock.cpp
596 lines (505 loc) · 14.8 KB
/
codeblock.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
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
#include "codeblock.h"
#include <QStringList>
#include <QHash>
CodeBlock::CodeBlock(QString& line, int lineNum, const CodeBlock* previous) :
_fileLineNum(lineNum),
_original(line),
_blockDelete(false),
_percent(false),
_writtenLineNumber(0),
_comment(NULL),
_impliedCommand(NULL),
_previous(previous),
_cursor(0),
_ignoreNumbers(false),
_currentWord(NULL),
_commentParser(NULL),
_nextParser(NULL)
{
parse();
}
CodeBlock::CodeBlock(const CodeBlock* other) :
_fileLineNum(other->_fileLineNum),
_original(other->_original),
_blockDelete(other->_blockDelete),
_percent(other->_percent),
_writtenLineNumber(other->_writtenLineNumber),
_comment(NULL),
_impliedCommand(NULL),
_previous(NULL), // Will not copy previous or other reasonable parsing state
_cursor(0),
_ignoreNumbers(false),
_currentWord(NULL),
_commentParser(NULL),
_nextParser(NULL)
{
if (other->_comment) _comment = new QString(*(other->_comment));
// Should deep copy error list....
// MUST deep copy the words
QHash<Word*, Word*> map;
for(int i = 0; i< other->_words.size(); i++)
{
Word* old = other->_words.at(i);
Word* neu = new Word(old);
map[old] = neu;
_words.append(neu);
}
for(int i = 0; i< other->_commands.size(); i++)
{
Word* neu = map.value(other->_commands.at(i));
if (neu) _commands.append(neu);
}
for(int i = 0; i< other->_parameters.size(); i++)
{
Word* neu = map.value(other->_parameters.at(i));
if (neu) _parameters.append(neu);
}
QMap<Word*, char>::const_iterator it = other->_orderedCommands.constBegin();
for(; it != other->_orderedCommands.constEnd(); it++)
{
Word* old = it.key();
Word* neu = map.value(old);
if (!neu) continue;
_orderedCommands[neu] = it.value();
}
if (other->_impliedCommand) _impliedCommand = new Word(other->_impliedCommand);
}
CodeBlock::~CodeBlock()
{
for(int i=0; i<_errorList.size(); i++)
{
delete _errorList.at(i);
}
_errorList.clear();
if (_comment) delete _comment;
_comment = NULL;
for(int i=0; i<_words.size(); i++)
{
delete _words.at(i);
}
_words.clear();
// _currentWord is inside the _words vector so it gets
// deleted from there
_currentWord = NULL;
}
const QString&
CodeBlock::original()
{
return _original;
}
const QString&
CodeBlock::formatted()
{
return _formatted;
}
bool
CodeBlock::hasError() const
{
return _errorList.size() > 0;
}
const QList<ParseError *> &
CodeBlock::errors() const
{
return _errorList;
}
QMap<Word*, char>::const_iterator
CodeBlock::commandsBegin() const
{
return _orderedCommands.constBegin();
}
QMap<Word*, char>::const_iterator
CodeBlock::commandsEnd() const
{
return _orderedCommands.constEnd();
}
////////////////////////////////////////////////////////////////
void
CodeBlock::parse()
{
_cursor = 0;
int len = _original.length();
_value.clear();
_commentParser = NULL;
_nextParser = &CodeBlock::parseStart;
while(_cursor < len)
{
_char = _original[_cursor++];
// Ignore all spaces from a parsing perspective at all times
if (_commentParser)
{
(this->*_commentParser)();
continue;
}
// When not a comment, ignore all whitespace
if (!_char.isSpace())
{
(this->*_nextParser)();
}
}
// We've run out of useful characters, so let our parsing funciton know that
// the line has ended (as long as it exists that is)
_char = QChar();
if (_commentParser) (this->*_commentParser)();
if (_nextParser) (this->*_nextParser)();
// Okay, now it's time to condense all the parameters into commands and so forth.
// Everything has been put into a command or parameter list as the words objects were created
// so our task here is to attach the parameters to the commands they belong to detecting
// any places where there are ambiguities
for(int i=0; i<_parameters.size(); i++)
{
Word* parameter = _parameters.at(i);
Word* attached = NULL;
for(int j=0; j<_commands.size(); j++)
{
Word* command = _commands.at(j);
if (command->acceptsParameter(parameter))
{
if (attached)
{
// Error condition
recordError(QString("Ambigous parameter %1 already attached to %2, but could also apply to %3")
.arg(parameter->toString())
.arg(attached->toString())
.arg(command->toString()));
}
else
{
command->addParameter(parameter);
attached = command;
}
}
}
if (!attached)
{
// Maybe there is an implied command it can be associated with?
if (!_impliedCommand)
{
const Word* parentCommand = searchForImpliedCommandFor(parameter);
// Save it away if found
if (parentCommand)
{
_impliedCommand = new Word(parentCommand);
_words.append(_impliedCommand);
}
}
if (_impliedCommand)
{
if (_impliedCommand->acceptsParameter(parameter))
{
_impliedCommand->addParameter(parameter);
}
else
{
// Since there is already something implied we don't go back further
// trying to pull in something else. Just error
recordError(QString("Orphaned parameter %1 did not apply to any command on this line or the current implied command")
.arg(parameter->toString()));
}
}
else
{
// Couldn't find an implied command to take it, so just bail
recordError(QString("Orphaned parameter %1 did not apply to any command on this line.")
.arg(parameter->toString()));
}
}
}
// Now see if any of the commands are sorely missing parameters
// Right now the only implied commands are G0 and G1. Since they only need one parameter, and they
// would only be implied if they had gotten a parameter we don't have to consider errors from them.
for(int i=0; i<_commands.size(); i++)
{
Word* cmd = _commands.at(i);
QString cmdError = cmd->getCommandError();
if (!cmdError.isEmpty())
{
recordError(cmdError);
}
// Final step is to re-order the commands
_orderedCommands[cmd] = 1;
}
// But of course an implied command needs to be in the proper order so this...
if (_impliedCommand)
{
_orderedCommands[_impliedCommand] = 1;
}
updateFormatted();
}
void
CodeBlock::updateFormatted()
{
// Make our formatted output
QStringList strs;
if (_percent)
{
// Special percent handling
strs.append("%");
}
else
{
// Possibly some commands
if (_blockDelete) strs.append("/ ");
if (_writtenLineNumber) strs.append(QString("N%1 ").arg(_writtenLineNumber));
if (_impliedCommand) strs.append(" ");
// In order of course
QMap<Word*, char>::const_iterator it;
for(it = _orderedCommands.constBegin(); it != _orderedCommands.constEnd(); ++it)
{
strs.append(it.key()->toCommandString());
strs.append(" ");
}
}
// Comments always at the EOL
if (_comment && _comment->length() > 0) strs.append(QString("(%1)").arg(_comment->trimmed()));
_formatted = strs.join("");
}
/**
* @brief CodeBlock::searchForImpliedCommandFor
* @param parameter
* @return
*
* Recurses up our chain of parents to see if anyone can tell us what motion mode we are in.
*/
const Word*
CodeBlock::searchForImpliedCommandFor(const Word* parameter) const
{
for(int i=0; i<_commands.size(); i++)
{
Word* cmd = _commands.at(i);
if (cmd->canBeImplied() && cmd->acceptsParameter(parameter)) return cmd;
}
// Didn't find it, recurse to a parent if we have one
if (_previous) return _previous->searchForImpliedCommandFor(parameter);
// Nope
return NULL;
}
void
CodeBlock::recordError(const QString& error)
{
ParseError* e = new ParseError(_fileLineNum, _cursor+1, error, this);
_errorList.append(e);
}
bool
CodeBlock::checkForComment()
{
if (_char == '(')
{
// Yep! go into comment mode
if (!_comment) _comment = new QString();
_comment->clear();
_commentParser = &CodeBlock::parseInlineComment;
return true;
}
if (_char == ';')
{
// From here until the end of line it is a comment
if (!_comment) _comment = new QString();
_comment->clear();
_commentParser = &CodeBlock::parseEOLComment;
return true;
}
// Not a comment;
return false;
}
void
CodeBlock::startNumber()
{
_ignoreNumbers = false;
_value.clear();
}
bool
CodeBlock::appendNumberComponent()
{
// Digits for sure
if (_char.isDigit()) goto append;
// Decimals as long as there isn't a decimal in the value already
if (_char == '.')
{
if (_value.indexOf('.') != -1)
{
recordError(QString("Warning: Multiple decimal points makes this number invalid"));
_ignoreNumbers = true;
}
else
{
_value.append('.');
}
// Still allow it, but we will ignore things now
return true;
}
// Plus or minus at the start of the line
if ((_char == '+' || _char == '-') && (_value.length() == 0)) goto append;
// We know it's not a comment (that's a separate check) so set the position back by one
_cursor--;
return false;
append:
if (!_ignoreNumbers) _value.append(_char);
return true;
}
////////////////////////////////////////////////////////////////////////////////////
void
CodeBlock::parseInlineComment()
{
// Are we done?
if (_char.isNull() || _char == ')')
{
// All done with the comment. We saved it as we went so just return
_commentParser = NULL;
return;
}
// Collect the character
_comment->append(_char);
}
void
CodeBlock::parseEOLComment()
{
// If EOL we are done
if (_char.isNull()) return;
// Collect all characters. Nothing can stop us! (except the end of the line)
_comment->append(_char);
}
void
CodeBlock::parseStart()
{
// If EOL we are done. Nothing to do. Not in the middle of anything because
// we haven't even started yet.
if (_char.isNull()) return;
// Can start with the block delete marker if we want
if (_char == '/')
{
_blockDelete = true;
return;
}
_blockDelete = false;
// Treat % markers as representing EOL comment start markers
if (_char == '%')
{
_percent = true;
if (!_comment) _comment = new QString();
_comment->clear();
_commentParser = &CodeBlock::parseEOLComment;
return;
}
if (checkForComment()) return;
// It can either be a line number or a standard word
if (_char.toUpper() == 'N')
{
startNumber();
_nextParser = &CodeBlock::parseLineNo;
return;
}
// Else, it's not a line number so behave as if it is parseWordStart right away
parseWordStart();
}
void
CodeBlock::parseLineNo()
{
if (_char.isNull() || checkForComment() || !appendNumberComponent())
{
// done parsing the number
if (_value.length() == 0)
{
recordError(QString("Line number word should have a valid number"));
}
else
{
_writtenLineNumber = _value.toDouble();
}
// No matter what, go on to the next word
_nextParser = &CodeBlock::parseWordStart;
}
}
void
CodeBlock::parseWordStart()
{
if (_char.isNull() || checkForComment()) return;
if (!(_char.isLetter() || _char == '#'))
{
recordError(QString("Expect a word beginning with a letter"));
return;
}
_currentWord = new Word(_char);
// Hold identity in this list
_words.append(_currentWord);
// Put it into it's proper list based on being a command or not
if (_currentWord->isCommand())
{
_commands.append(_currentWord);
}
else
{
_parameters.append(_currentWord);
}
// So far all we have is the character which starts the word, so now get
// the number for this word
startNumber();
_nextParser = &CodeBlock::parseWordNumber;
}
void
CodeBlock::parseWordNumber()
{
if (_char.isNull() || checkForComment() || !appendNumberComponent())
{
// done parsing the number
if (_value.length() == 0)
{
// Instead of complaining right away, we're going to save up this word
// and mark the whole thing as bogus
_value.clear();
_value.append(_currentWord->_char);
// We started a word though, so nuke the one we started because we no longer care
if (_currentWord->isCommand())
{
_commands.pop_back();
}
else
{
_parameters.pop_back();
}
_words.pop_back();
delete _currentWord;
// But jump in here as a comment parser so we get everything including whitespace
_commentParser = &CodeBlock::parseUnknownWord;
}
else
{
_currentWord->setValue(_value);
}
// No matter what, go on to the next word
_nextParser = &CodeBlock::parseWordStart;
}
}
void
CodeBlock::parseUnknownWord()
{
if (_char.isNull() || _char.isSpace())
{
// Consider this the end of the unknown word/comment thing
recordError(QString("Unrecognized syntax word '%1'").arg(_value));
_commentParser = NULL;
}
else
{
_value.append(_char);
if (_char.isDigit() || (_char == '-') || (_char == '+') || (_char == '.'))
{
_commentParser = &CodeBlock::parseUnknownDigits;
}
}
}
void
CodeBlock::parseUnknownDigits()
{
if (_char.isDigit() || (_char == '-') || (_char == '+') || (_char == '.'))
{
_value.append(_char);
}
else
{
// A space or a letter or any other character ends the unknown block
recordError(QString("Unrecognized syntax word & number '%1'").arg(_value));
_commentParser = NULL;
// But we have to consider this the start of a real word (possibly)
parseWordStart();
}
}