forked from dsuarezv/Netduino-gcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGcodeParser.cs
More file actions
315 lines (257 loc) · 8.28 KB
/
Copy pathGcodeParser.cs
File metadata and controls
315 lines (257 loc) · 8.28 KB
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
using System;
using System.Text;
namespace gcodeparser
{
public enum State
{
}
public class GCodeParser
{
#if DESKTOP
private static System.Globalization.CultureInfo USCulture = new System.Globalization.CultureInfo("en-US");
#endif
private static char[] NumberBuffer = new char[20];
private static int NumberBufferIndex = 0;
private static bool DeletedLine = false;
private static int LineNumber = 0;
private static int CurrentIndex = 0;
public static string Line;
public static int ParserLineNumber = 0;
public static void ParseLine(string line)
{
ParserLineNumber++;
Line = line.ToUpper();
CurrentIndex = 0;
//Logger.Log("=== {0} ===", line);
ParseCommand();
}
private static bool SkipSpaces()
{
while (CurrentIndex < Line.Length)
{
char c = Line[CurrentIndex];
switch (c)
{
// Skip spaces and tabs
case ' ':
case '\t':
CurrentIndex++;
break;
default:
return true;
}
}
return false;
}
internal static char PeekChar()
{
if (!SkipSpaces()) return (char)0;
if (CurrentIndex >= Line.Length) return (char)0;
return Line[CurrentIndex];
}
internal static char ReadChar()
{
if (!SkipSpaces()) return (char)0;
if (CurrentIndex >= Line.Length) return (char)0;
return Line[CurrentIndex++];
}
internal static string ReadNumber()
{
NumberBufferIndex = 0;
bool isFirst = true;
while (true)
{
char c = PeekChar();
switch (c)
{
case '(':
SkipToEndComment();
break;
case ' ':
case '\t':
if (NumberBufferIndex > 0) return GetNumberString();
break;
case ';':
return GetNumberString();
case '-':
if (isFirst)
AddDigitToNumber(c);
else
return GetNumberString();
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '.':
AddDigitToNumber(c);
break;
case '\0':
default:
return GetNumberString();
}
isFirst = false;
}
}
private static void SkipToEndComment()
{
while (true)
{
char c = ReadChar();
if (c == ')') break;
}
}
private static void AddDigitToNumber(char d)
{
ReadChar();
NumberBuffer[NumberBufferIndex] = d;
NumberBufferIndex++;
}
private static string GetNumberString()
{
if (NumberBufferIndex > 0)
{
return new string(NumberBuffer, 0, NumberBufferIndex);
}
else
{
return null;
}
}
internal static int ParseInt()
{
string val = ReadNumber();
if (val == null)
{
Logger.Error("ParseInt: no numbers at {0} in '{1}'. Skipped.", CurrentIndex, Line);
return -1;
}
try
{
return int.Parse(val);
}
catch (Exception ex)
{
Logger.Error("ParseInt ERROR parsing '{0}': {1}", val, ex);
return -1;
}
}
internal static double ParseDouble()
{
string val = ReadNumber();
if (val == null)
{
Logger.Error("ParseDouble: no numbers at {0} in '{1}'. Skipped.", CurrentIndex, Line);
return double.MinValue;
}
try
{
return Math2.StringToDouble(val);
}
catch (Exception ex)
{
Logger.Error("ParseDouble ERROR parsing '{0}': {1}", val, ex);
return double.MinValue;
}
}
private static bool ParseComment()
{
while (true)
{
char c = ReadChar();
switch (c)
{
case '\0': return false;
case ')': return true;
}
}
}
private static bool ParseDashComment()
{
int numDashes = 1;
while (true)
{
char c = ReadChar();
switch (c)
{
case '\0': return false;
case '-': numDashes++; break;
default:
if (numDashes < 3)
{
Logger.Error("ERROR: Parse --- comment: not enough '-' to interpret as comment. Returning control to parser, but it's an unsuported combination.");
return true;
}
return false; // otherwise, the rest of the line is a comment, no need to continue.
}
}
}
private static void ParseCommand()
{
while (true)
{
char c = ReadChar();
switch (c)
{
case '\0': return; // EOL
case ';': return; // comment line
case '(': // Begin comment
if (!ParseComment()) return;
break;
case '-': // comment --- (not parsed by readnumber)
if (!ParseDashComment()) return;
break;
case '/':
DeletedLine = true;
break;
case 'N':
LineNumber = ParseInt();
break;
case 'G':
ProcessCommand(new CommandG());
break;
case 'T':
ProcessCommand(new CommandT());
break;
case 'F':
ProcessCommand(new CommandF());
break;
case 'M':
ProcessCommand(new CommandM());
break;
case 'S':
ProcessCommand(new CommandS());
break;
case 'D':
ProcessCommand(new CommandD());
break;
case 'X': // Primary axes
case 'Y':
case 'Z':
case 'I':
case 'J':
case 'K':
case 'R':
ProcessCommand(new CommandAxis(c));
break;
default:
Logger.Log("ParseCommand: Unknown state at {0}: '{1}'", CurrentIndex, c);
ReadChar();
break;
}
}
}
private static void ProcessCommand(BaseCommand cmd)
{
cmd.Deleted = DeletedLine;
cmd.LineNumber = LineNumber;
cmd.Parse();
}
}
}