From 6826dad61dc66d17c08895dbc6108a494772b27f Mon Sep 17 00:00:00 2001 From: Guillaume Seguin Date: Sat, 18 May 2013 21:16:09 +0200 Subject: [PATCH] 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). --- printrun/gcoder.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/printrun/gcoder.py b/printrun/gcoder.py index 3ae9c19..1bead3b 100755 --- a/printrun/gcoder.py +++ b/printrun/gcoder.py @@ -20,6 +20,7 @@ import math import datetime 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): @@ -46,7 +47,7 @@ class Line(object): self.raw = l.lower() if ";" in self.raw: 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.is_move = self.command in ["G0", "G1"]