Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There's a funny bug with the way newlines are handled #44

Open
Michael-F-Bryan opened this issue Mar 6, 2020 · 1 comment · May be fixed by #56
Open

There's a funny bug with the way newlines are handled #44

Michael-F-Bryan opened this issue Mar 6, 2020 · 1 comment · May be fixed by #56

Comments

@Michael-F-Bryan
Copy link
Owner

TL;DR:

/// For some reason we were parsing the G90, then an empty G01 and the
/// actual G01.
#[test]
fn funny_bug_in_crate_example() {
    let src = "G90 \n G01 X50.0 Y-10";
    let expected = vec![
        GCode::new(Mnemonic::General, 90.0, Span::PLACEHOLDER),
        GCode::new(Mnemonic::General, 1.0, Span::PLACEHOLDER)
            .with_argument(Word::new('X', 50.0, Span::PLACEHOLDER))
            .with_argument(Word::new('Y', -10.0, Span::PLACEHOLDER)),
    ];

    let got: Vec<_> = crate::parse(src).collect();

    assert_eq!(got, expected);
}

Fails with

thread 'parser::tests::funny_bug_in_crate_example' panicked at 'assertion failed: `(left == right)`
  left: `[GCode { mnemonic: General, number: 90.0, arguments: [], span: Span { start: 0, end: 3, line: 0 } }, GCode { mnemonic: General, number: 1.0, arguments: [], span: Span { start: 6, end: 9, line: 1 } }, GCode { mnemonic: General, number: 1.0, arguments: [Word { letter: 'X', value: 50.0, span: Span { start: 10, end: 15, line: 1 } }, Word { letter: 'Y', value: -10.0, span: Span { start: 16, end: 20, line: 1 } }], span: Span { start: 6, end: 9, line: 1 } }]`,
 right: `[GCode { mnemonic: General, number: 90.0, arguments: [], span: <placeholder> }, GCode { mnemonic: General, number: 1.0, arguments: [Word { letter: 'X', value: 50.0, span: <placeholder> }, Word { letter: 'Y', value: -10.0, span: <placeholder> }], span: <placeholder> }]`', gcode/src/parser.rs:437:9

(note the G01 with no arguments in the middle)

@ghost
Copy link

ghost commented Jun 17, 2020

I've been able to consistently reproduce this bug with the following code:

extern crate gcode;

fn main() {
    let gcode_string = "G0 X1\nG1 Y2";
    let parse_result: Vec<_> = gcode::parse(gcode_string).collect();
    assert_eq!(parse_result.len(), 2);
}

This fails with actual length 3 instead of 2.

Prepending line numbers for every line seems to solve it:

let gcode_string = "N0 G0 X1\nN1 G1 Y2";

This correctly returns 2 commands.
I've analyzed the parser more closely by using the full_parse_with_callback function and found that Line 0 thinks it contains 2 GCodes and Line 1 contains 1 GCode, but when printing out the actual GCodes returned i see 1 GCode on line 0 and 2 GCodes on line 1: The empty one and the correct one. So there seems to be the difference.

Finally, the parser runs correct when i use 2 empty G-codes:

let gcode_string = "G0\nG1";

This returns 2 commands.

dr0ps added a commit to dr0ps/gcode-rs that referenced this issue Jan 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant