Crate squiid_parser

Crate squiid_parser 

Source
Expand description

Squiid Parser is the algebraic expression parser for Squiid Calculator, designed to parse infix notation into postifix (RPN) notation, which can then be evaluated by squiid-engine.

This module provides functionality for lexing, parsing, and handling algebraic expressions, supporting implicit multiplication and correct operator precedence using the Shunting Yard algorithm.

§Modules

  • error: Defines error types encountered during parsing.
  • lexer: Handles lexical analysis, converting input strings into tokens.
  • parser: Implements parsing logic, including handling implicit multiplication and operator precedence.
  • tokens: Defines token structures used throughout the parsing process.

§Functionality

The primary function exposed is parse, which converts an infix algebraic string into a vector of tokens in RPN format. It ensures proper handling of operators, parentheses, and implicit multiplication.

§Features

  • FFI Support (optional): Enables C-compatible parsing via the ffi module.
  • Strict Safety Guidelines: Uses #![deny(clippy::unwrap_used)] and related lints to enforce error handling best practices.

§Usage

use squiid_parser::parse;
use squiid_parser::error::ParserError;

fn main() -> Result<(), ParserError> {
    let expected = vec!["3", "6", "4", "6", "*", "+", "*", "5", "/"];
    let input = "3(6+4*6)/5";
    assert_eq!(expected, parse(input)?);

    Ok(())
}

§Error Handling

If parsing fails, an appropriate ParserError is returned, such as MismatchedParenthesis when parentheses are unbalanced.

Modules§

error
lexer
parser
tokens

Functions§

parse
Parse an algebraic string into a vec of tokens in RPN format.