Crate ngc[][src]

Expand description

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.


Data types to represent a parsed G-code program.

Parser for G-code.