8 releases

0.2.6 Mar 4, 2022
0.2.5 Dec 4, 2021
0.2.4 Jan 8, 2021
0.2.3 Nov 14, 2020
0.1.0 May 23, 2019

#1569 in Parser implementations

30 downloads per month

MIT/Apache

68KB
1.5K SLoC

ngc - G-Code parser/evaluator for Rust

Build Status Build Status

Work in progress!

Currently, only the parser is functional.

Documentation

Module documentation is hosted on docs.rs.

Examples

The following code (the same as the "ngc-parse" demo binary) takes a file as an argument, parses it and outputs the display form, which is the same G-code, but in a consistent format and cleaned of comments.

use std::{env, fs};
use ngc::parse::parse;

fn main() {
    let filename = env::args().nth(1).unwrap();
    let input = fs::read_to_string(&filename).unwrap();

    match parse(&filename, &input) {
        Err(e) => eprintln!("Parse error: {}", e),
        Ok(prog) => println!("{}", prog),
    }
}

lib.rs:

A G-Code parsing and evaluation library, aiming for compatibility with the LinuxCNC dialect of G-Code.

Currently, it can parse G-Code into an AST, using the Pest parser library. An evaluator is work in progress and the current state can be enabled with the eval feature flag.

Basic usage

Use ngc::parse::parse to get an AST, then work with the abstract syntax tree datastructures from ngc::ast.

Later, you will be able to feed this into an evaluator from ngc::eval, which takes care of evaluating expressions, checking invalid codes and combinations, and yielding a series of more machine-level instructions.

The following code (the same as the "ngc-parse" demo binary) takes a file as an argument, parses it and outputs the display form, which is the same G-code, but in a consistent format and cleaned of comments.

use std::{env, fs};
use ngc::parse::parse;

fn main() {
    let filename = env::args().nth(1).unwrap();
    let input = fs::read_to_string(&filename).unwrap();

    match parse(&filename, &input) {
        Err(e) => eprintln!("Parse error: {}", e),
        Ok(prog) => println!("{}", prog),
    }
}

Unsupported features

Currently, LinuxCNC's control flow constructs ("O codes") are completely unsupported.

Dependencies

~4MB
~77K SLoC