Merge pull request #12 from Trivernis/develop

Generic parameters and more doc comments
pull/14/head
Julius Riegel 4 years ago committed by GitHub
commit e15efdfbd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,7 +2,7 @@
name = "asciimath-rs" name = "asciimath-rs"
description = "AsciiMath parser" description = "AsciiMath parser"
repository = "https://github.com/trivernis/asciimath-rs" repository = "https://github.com/trivernis/asciimath-rs"
version = "0.6.0" version = "0.6.1"
authors = ["trivernis <trivernis@protonmail.com>"] authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018" edition = "2018"
readme = "README.md" readme = "README.md"

@ -22,10 +22,10 @@ pub(crate) mod utils;
/// ///
/// Example: /// Example:
/// ///
/// ```rust /// ```
/// let expression = asciimath_rs::parse("sin(2x) + 3".to_string()); /// let expression = asciimath_rs::parse("sin(2x) + 3".to_string());
/// ``` /// ```
pub fn parse(content: String) -> Expression { pub fn parse<S: AsRef<str>>(content: S) -> Expression {
let mut tokenizer = Tokenizer::new(content); let mut tokenizer = Tokenizer::new(content);
let tokens = tokenizer.parse(); let tokens = tokenizer.parse();
let mut tree_parser = TreeParser::new(tokens); let mut tree_parser = TreeParser::new(tokens);

@ -19,14 +19,24 @@ pub struct Tokenizer {
} }
impl Tokenizer { impl Tokenizer {
pub fn new(text: String) -> Self { /// Creates a new AsciiMath tokenizer with a given input string.
let mut chars = text.chars().collect::<Vec<char>>(); /// After creation the parse method can be used to parse a list of tokens
/// Example:
/// ```
/// use asciimath_rs::parsing::tokenizer::Tokenizer;
///
/// let mut tokenizer = Tokenizer::new("2^(2-i)");
/// let tokens = tokenizer.parse();
/// ```
pub fn new<S: AsRef<str>>(text: S) -> Self {
let mut chars = text.as_ref().chars().collect::<Vec<char>>();
chars.push('\n'); chars.push('\n');
Self { Self {
ctm: CharTapeMachine::new(chars), ctm: CharTapeMachine::new(chars),
} }
} }
/// Parses the input string passed on creation of the tokenizer into a list of tokens
pub fn parse(&mut self) -> Vec<Token> { pub fn parse(&mut self) -> Vec<Token> {
let mut tokens = Vec::<Token>::new(); let mut tokens = Vec::<Token>::new();
self.ctm.seek_whitespace(); self.ctm.seek_whitespace();

@ -17,6 +17,21 @@ pub struct TreeParser {
} }
impl TreeParser { impl TreeParser {
/// Creates a new TreeParser that takes tokens parsed by the Tokenizer. The parse method
/// can be used to create the Expression tree.
/// Example:
/// ```
/// use asciimath_rs::parsing::tokenizer::Tokenizer;
/// use asciimath_rs::parsing::tree_parser::TreeParser;
///
/// // use the tokenizer to create a list of tokens
/// let mut tokenizer = Tokenizer::new("2*2");
/// let tokens = tokenizer.parse();
///
/// // use the tree parser to build the expression tree
/// let mut tree_parser = TreeParser::new(tokens);
/// tree_parser.parse();
/// ```
pub fn new(tokens: Vec<Token>) -> Self { pub fn new(tokens: Vec<Token>) -> Self {
Self { Self {
tokens, tokens,
@ -25,6 +40,7 @@ impl TreeParser {
} }
} }
/// Creates an expression tree out of the tokens passed to the TreeParser on creation
pub fn parse(&mut self) -> Expression { pub fn parse(&mut self) -> Expression {
self.remove_whitespace(); self.remove_whitespace();
self.parse_expression() self.parse_expression()

Loading…
Cancel
Save