Commit 6826dad6 authored by Guillaume Seguin's avatar Guillaume Seguin

Use a regular expression to parse GCodes

This should parse parentheses correctly and handle (valid) gcodes such as
G1X2Y3, while before we were assuming spaces between tokens (which is wrong as
per the G-Code spec). This should fix req #333 in a clean and systematic way.
This is also a component for #343 (Marlin M114 response is in the form ok C:
X11Y22Z33E44, which we can parse with the same expression).
parent 06e3a02d
...@@ -20,6 +20,7 @@ import math ...@@ -20,6 +20,7 @@ import math
import datetime import datetime
gcode_parsed_args = ["x", "y", "e", "f", "z", "p", "i", "j"] gcode_parsed_args = ["x", "y", "e", "f", "z", "p", "i", "j"]
gcode_exp = re.compile("\([^\(\)]*\)|[/\*].*\n|\n|[a-z][-+]?[0-9]*\.?[0-9]*")
class Line(object): class Line(object):
...@@ -46,7 +47,7 @@ class Line(object): ...@@ -46,7 +47,7 @@ class Line(object):
self.raw = l.lower() self.raw = l.lower()
if ";" in self.raw: if ";" in self.raw:
self.raw = self.raw.split(";")[0].rstrip() self.raw = self.raw.split(";")[0].rstrip()
self.split_raw = self.raw.split(" ") self.split_raw = gcode_exp.findall(self.raw)
self.command = self.split_raw[0].upper() if not self.split_raw[0].startswith("n") else self.split_raw[1] self.command = self.split_raw[0].upper() if not self.split_raw[0].startswith("n") else self.split_raw[1]
self.is_move = self.command in ["G0", "G1"] self.is_move = self.command in ["G0", "G1"]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment