Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions gcodeparser/gcode_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def __init__(self, gcode: str, include_comments=False):


def get_lines(gcode, include_comments=False):
regex = r'(?!; *.+)(G|M|T|g|m|t)(\d+)(([ \t]*(?!G|M|g|m)\w(".*"|([-\d\.]*)))*)[ \t]*(;[ \t]*(.*))?|;[ \t]*(.+)'
regex = r'(?!; *.+)(G|M|T|g|m|t)(\d+)(([ \t]*(?!G|M|g|m)\w(".*"|([-+\d\.]*)))*)[ \t]*(;[ \t]*(.*))?|;[ \t]*(.+)'
regex_lines = re.findall(regex, gcode)
lines = []
for line in regex_lines:
Expand Down Expand Up @@ -114,7 +114,7 @@ def element_type(element: str):


def split_params(line):
regex = r'((?!\d)\w+?)(".*"|(\d+\.?)+|-?\d*\.?\d*)'
regex = r'((?!\d)\w+?)(".*"|(\d+\.?)+|[-+]?\d*\.?\d*)'
elements = re.findall(regex, line)
params = {}
for element in elements:
Expand Down
9 changes: 9 additions & 0 deletions test/test_get_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ def test_params():
assert get_lines('G1 X10 Y20')[0] == line


def test_params_with_explicit_positive_values():
line = GcodeLine(
command=('G', 1),
params={'X': 10, 'Y': 20},
comment='',
)
assert get_lines('G1 X+10 Y+20')[0] == line


def test_2_commands_line():
line1 = GcodeLine(
command=('G', 91),
Expand Down
6 changes: 6 additions & 0 deletions test/test_split_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ def test_split_string_with_semicolon_params():
def test_split_neg_int_params():
assert split_params(' P-0 S-1 X-10') == {'P': 0, 'S': -1, 'X': -10}

def test_split_positive_int_params():
assert split_params(' P+0 S+1 X+10') == {'P': 0, 'S': 1, 'X': 10}


def test_split_neg_float_params():
assert split_params(' P-0.1 S-1.1345 X-10.0') == {'P': -0.1, 'S': -1.1345, 'X': -10.0}

def test_split_positive_float_params():
assert split_params(' P+0.1 S+1.1345 X+10.0') == {'P': 0.1, 'S': 1.1345, 'X': 10.0}


def test_split_ip_params():
assert split_params('P192.168.0.1 S1') == {'P': '192.168.0.1', 'S': 1}
Expand Down