Add newline token \\n

pull/9/head
trivernis 4 years ago
parent 9692f19b4b
commit d30a227df8

@ -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.4.8" version = "0.5.0"
authors = ["trivernis <trivernis@protonmail.com>"] authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018" edition = "2018"
readme = "README.md" readme = "README.md"
@ -13,7 +13,7 @@ categories = ["mathematics", "parser-implementations"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
charred = "0.3.0" charred = "0.3.1"
maplit = "1.0.2" maplit = "1.0.2"
lazy_static = "1.4.0" lazy_static = "1.4.0"
htmlescape = "0.3.1" htmlescape = "0.3.1"

@ -13,6 +13,7 @@ pub enum Literal {
Arrow(Arrow), Arrow(Arrow),
Misc(Misc), Misc(Misc),
Operation(Operation), Operation(Operation),
NewLine,
} }
#[derive(Debug, Clone, PartialOrd, PartialEq)] #[derive(Debug, Clone, PartialOrd, PartialEq)]

@ -32,6 +32,7 @@ impl ToMathML for Literal {
Literal::Arrow(a) => a.to_mathml(), Literal::Arrow(a) => a.to_mathml(),
Literal::Misc(m) => m.to_mathml(), Literal::Misc(m) => m.to_mathml(),
Literal::Operation(o) => o.to_mathml(), Literal::Operation(o) => o.to_mathml(),
Literal::NewLine => "<mspace linebreak='newline' />".to_string(),
} }
} }
} }

@ -118,7 +118,7 @@ mod tests {
#[test] #[test]
fn it_tokenizes_expressions3() { fn it_tokenizes_expressions3() {
let expression = "[[1, 2],[3, 4]] //"; let expression = "[[1, 2],[3, 4]] // \\\n";
let mut tokenizer = Tokenizer::new(expression.to_string()); let mut tokenizer = Tokenizer::new(expression.to_string());
let tokens = tokenizer.parse(); let tokens = tokenizer.parse();
assert_eq!( assert_eq!(
@ -141,6 +141,8 @@ mod tests {
Token::Grouping(Grouping::LBracket), Token::Grouping(Grouping::LBracket),
Token::Text(Text::Whitespace), Token::Text(Text::Whitespace),
Token::Operation(Operation::Slash), Token::Operation(Operation::Slash),
Token::Text(Text::Whitespace),
Token::Text(Text::NewLine),
] ]
); );
} }
@ -363,10 +365,10 @@ mod tests {
} }
#[allow(dead_code)] #[allow(dead_code)]
//#[test] #[test]
fn it_writes_mathml() { fn it_writes_mathml() {
let str_expression = let str_expression =
"alpha sqrt 1 in NN implies 2^4 + <=> sum_(k = 1)^3 - ((1),(2))[[2, 3 + 3],[4, 5]] + alpha"; "alpha sqrt 1 in NN implies 2^4 + \\\n<=> sum_(k = 1)^3 - ((1),(2))[[2, 3 + 3],[4, 5]] + alpha";
let expression = parse(str_expression.to_string()); let expression = parse(str_expression.to_string());
fs::write( fs::write(
"test-files/test.html", "test-files/test.html",

@ -1,6 +1,6 @@
use crate::tokens::constants::accents::G_COLOR; use crate::tokens::constants::accents::G_COLOR;
use crate::tokens::constants::grouping::T_LPAREN; use crate::tokens::constants::grouping::T_LPAREN;
use crate::tokens::constants::misc::{A_TEXT, G_NUMALLOWED}; use crate::tokens::constants::misc::{A_TEXT, G_NEWLINE, G_NUMALLOWED};
use crate::tokens::constants::TokenPattern; use crate::tokens::constants::TokenPattern;
use crate::tokens::mappings::{ use crate::tokens::mappings::{
get_accent_mappings, get_arrow_mapping, get_font_mappings, get_function_mappings, get_accent_mappings, get_arrow_mapping, get_font_mappings, get_function_mappings,
@ -227,6 +227,9 @@ impl Tokenizer {
} }
fn parse_whitespace(&mut self) -> Option<Text> { fn parse_whitespace(&mut self) -> Option<Text> {
if self.ctm.check_any_str_sequence(G_NEWLINE) {
return Some(Text::NewLine);
}
if self.ctm.get_current().is_whitespace() { if self.ctm.get_current().is_whitespace() {
self.ctm.seek_whitespace(); self.ctm.seek_whitespace();
self.ctm.rewind(self.ctm.get_index() - 1); self.ctm.rewind(self.ctm.get_index() - 1);

@ -197,6 +197,7 @@ impl TreeParser {
text: p, text: p,
formatting: None, formatting: None,
})), })),
Text::NewLine => Some(Literal::NewLine),
_ => None, _ => None,
} }
} }

@ -41,5 +41,6 @@ pub const A_TEXT: char = '"';
pub const G_T_TEX: &'static [&str] = &["text"]; pub const G_T_TEX: &'static [&str] = &["text"];
pub const A_NUMCOMMA: char = '.'; pub const A_NUMCOMMA: char = '.';
pub const A_SCIEXP: char = 'e'; pub const A_SCIEXP: char = 'e';
pub const G_NEWLINE: &'static [&str] = &["\\\n"];
pub const G_NUMALLOWED: &'static [char] = &[A_NUMCOMMA, A_SCIEXP]; pub const G_NUMALLOWED: &'static [char] = &[A_NUMCOMMA, A_SCIEXP];

@ -23,6 +23,7 @@ pub enum Text {
Symbol(String), Symbol(String),
Plain(String), Plain(String),
Whitespace, Whitespace,
NewLine,
} }
#[derive(Debug, Clone, PartialOrd, PartialEq)] #[derive(Debug, Clone, PartialOrd, PartialEq)]

Loading…
Cancel
Save