Crate zypo_parser

Source
Expand description

§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);
}

Modules§

parser

Structs§

Function
A function statement, the basis for the whole language. An example of a function statement would be the following inside of Zypo itself:
Parameter
A eration of a parameter. This would originally look like my_id: str when being parsed.
Variable
Variable that includes a parameter but extends with a recursive ExpressionNode.
WhileLoop
A while loop. This goes down into WhileLoop and would look something like this in Zypo:

Enums§

BinOp
A binary operation type. Having a seperate enum for this avoids repitition and allows for some optimisations downstream.
Constant
Similar to the VarType enum but with defined data in each possible option.
ExpressionNode
The main expression enum, allowing for grouping of all expressions inside of the AST.
StatementNode
The main statement enum, allowing for grouping of statements inside of the AST.
VarType
Variable types for zypo-rs.

Functions§

ast_result
Gets the abstract syntax tree generated from the parser of zypo-rs. This function will panic is parsing fails and is intended for developers aiming to implament the parser into code generation.