Skip to main content

Crate rable

Crate rable 

Source
Expand description

§Rable — A complete GNU Bash 5.3-compatible parser

Rable parses bash source code into an abstract syntax tree (AST) of Node values. Each node can be formatted as an S-expression via its [Display] implementation, producing output identical to Parable.

§Quick Start

use rable::{parse, NodeKind};

let nodes = parse("echo hello | grep h", false).unwrap();
assert_eq!(nodes.len(), 1);

// S-expression output via Display
let sexp = nodes[0].to_string();
assert!(sexp.contains("pipe"));

§Parsing Options

The extglob parameter enables extended glob patterns (@(), ?(), *(), +(), !()). Set to false for standard bash parsing.

// Standard parsing
let nodes = rable::parse("echo hello", false).unwrap();

// With extended globs
let nodes = rable::parse("echo @(foo|bar)", true).unwrap();

§Error Handling

Parse errors include line and position information:

match rable::parse("if", false) {
    Ok(_) => unreachable!(),
    Err(e) => {
        assert_eq!(e.line(), 1);
        println!("Error: {e}");
    }
}

§Working with the AST

The AST uses a Node struct wrapping a NodeKind enum with a Span. Pattern matching on node.kind is the primary way to inspect nodes:

use rable::{parse, NodeKind};

let nodes = parse("echo hello world", false).unwrap();
match &nodes[0].kind {
    NodeKind::Command { words, redirects, .. } => {
        assert_eq!(words.len(), 3); // echo, hello, world
        assert!(redirects.is_empty());
    }
    _ => panic!("expected Command"),
}

Re-exports§

pub use ast::CasePattern;
pub use ast::ListItem;
pub use ast::ListOperator;
pub use ast::Node;
pub use ast::NodeKind;
pub use ast::PipeSep;
pub use ast::Span;
pub use error::RableError;
pub use error::Result;
pub use token::Token;
pub use token::TokenType;

Modules§

ast
error
sexp
token

Functions§

parse
Parses a bash source string into a list of top-level AST nodes.