#ast #parameters #reference

zypo-parser

The reference parser for zypo-rs, make with LALRPOP using LR(1)

1 unstable release

0.0.1 Oct 3, 2019

#15 in #abstract-syntax-tree

MIT license

12KB
203 lines

About

zypo-parser is the primary parser implementation for the zypo-rs reference compiler. This package uses the LALRPOP library, a yacc-like LR(1) parser combiner for Rust. This crate also contains the AST (abstract-syntax-tree) for Zypo and all public AST structures, enums and associated functions are avalible at the base-level of this crate.

Please beware as this package is heavily work-in-progress and is developed alongside the main Zypo compiler as a sub-package. If you are looking for the base zypo-rs compiler, you may find it here.

Examples

Shortcut for parsing an AST or panicing if there is an error in any of the given input:

use zypo_parser::{
    Function,
    VarType,
    Parameter,
    ast_result
}; // All but ast_result are from the AST. ast_result is the shortcut func

fn main() {
    let input = "fun shortcut_test_func(my_param: int) {}";
    let expected = vec![
        Function {
            ident: "shortcut_test_func".to_string(),
            params: vec![
                Parameter { ident: "my_param".to_string(), ty: VarType::Int }
            ],
            body: vec![],
            return_type: VarType::Void
        }
    ];

    assert_eq!(ast_result(input), expected);
}

More controllable but lower-level LALRPOP bind for manually handling the generated parsing errors:

use zypo_parser::{
    Function,
    VarType,
    parser
}; // Function and VarType are from the AST, parser is generated by LALRPOP

fn main() {
    let input = "fun test_func() {}";
    let expected = vec![
        Function {
            ident: "test_func".to_string(),
            params: vec![],
            body: vec![],
            return_type: VarType::Void
        }
    ];

    assert_eq!(parser::GrammarParser::new().parse(input).unwrap(), expected);
}

Dependencies

~2.2–4MB
~71K SLoC