Fix EOF handling

pull/1/head
trivernis 5 years ago
parent 86ff2e17d1
commit cb8f469853

2
Cargo.lock generated

@ -566,7 +566,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "snekdown" name = "snekdown"
version = "0.12.2" version = "0.12.3"
dependencies = [ dependencies = [
"chrono 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
"colored 1.9.3 (registry+https://github.com/rust-lang/crates.io-index)", "colored 1.9.3 (registry+https://github.com/rust-lang/crates.io-index)",

@ -1,6 +1,6 @@
[package] [package]
name = "snekdown" name = "snekdown"
version = "0.12.2" version = "0.12.3"
authors = ["trivernis <trivernis@protonmail.com>"] authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018" edition = "2018"
license-file = "LICENSE" license-file = "LICENSE"

@ -17,6 +17,7 @@ pub trait CharStateMachine {
fn check_special_sequence(&mut self, sequence: &[char]) -> bool; fn check_special_sequence(&mut self, sequence: &[char]) -> bool;
fn check_special_sequence_group(&mut self, sequences: &[&[char]]) -> bool; fn check_special_sequence_group(&mut self, sequences: &[&[char]]) -> bool;
fn check_linebreak(&self) -> bool; fn check_linebreak(&self) -> bool;
fn check_eof(&self) -> bool;
fn assert_special(&mut self, character: &char, revert_index: usize) -> Result<(), ParseError>; fn assert_special(&mut self, character: &char, revert_index: usize) -> Result<(), ParseError>;
fn assert_special_group( fn assert_special_group(
&mut self, &mut self,
@ -66,7 +67,7 @@ impl CharStateMachine for Parser {
/// skips to the next char /// skips to the next char
#[inline] #[inline]
fn skip_char(&mut self) { fn skip_char(&mut self) {
let _ = self.next_char(); self.next_char();
} }
/// Returns to an index position /// Returns to an index position
@ -209,6 +210,10 @@ impl CharStateMachine for Parser {
self.current_char == LB && !self.check_escaped() self.current_char == LB && !self.check_escaped()
} }
fn check_eof(&self) -> bool {
self.index >= (self.text.len() - 1)
}
#[inline] #[inline]
fn assert_special(&mut self, character: &char, revert_index: usize) -> Result<(), ParseError> { fn assert_special(&mut self, character: &char, revert_index: usize) -> Result<(), ParseError> {
if self.check_special(character) { if self.check_special(character) {

@ -26,6 +26,8 @@ impl ParseInline for Parser {
fn parse_inline(&mut self) -> ParseResult<Inline> { fn parse_inline(&mut self) -> ParseResult<Inline> {
if self.check_special(&PIPE) || self.check_linebreak() { if self.check_special(&PIPE) || self.check_linebreak() {
Err(ParseError::new(self.index)) Err(ParseError::new(self.index))
} else if self.check_eof() {
Err(ParseError::eof(self.index))
} else if let Ok(image) = self.parse_image() { } else if let Ok(image) = self.parse_image() {
Ok(Inline::Image(image)) Ok(Inline::Image(image))
} else if let Ok(url) = self.parse_url(false) { } else if let Ok(url) = self.parse_url(false) {
@ -197,7 +199,10 @@ impl ParseInline for Parser {
if characters.len() > 0 { if characters.len() > 0 {
Ok(PlainText { value: characters }) Ok(PlainText { value: characters })
} else { } else {
Err(ParseError::new(self.index)) Err(ParseError::new_with_message(
self.index,
"no plaintext characters parsed",
))
} }
} }

@ -13,15 +13,6 @@ use std::path::Path;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::thread; use std::thread;
macro_rules! parse_option {
($option:expr, $index:expr) => {
if let Some(_) = $option {
} else {
return Err(ParseError::new($index));
}
};
}
pub struct Parser { pub struct Parser {
pub(crate) index: usize, pub(crate) index: usize,
pub(crate) text: Vec<char>, pub(crate) text: Vec<char>,
@ -58,8 +49,7 @@ impl Parser {
paths: Arc<Mutex<Vec<String>>>, paths: Arc<Mutex<Vec<String>>>,
is_child: bool, is_child: bool,
) -> Self { ) -> Self {
let mut text: Vec<char> = text.chars().collect(); let text: Vec<char> = text.chars().collect();
text.append(&mut vec!['\n', ' ']); // it fixes stuff and I don't know why.
let current_char = text.get(0).unwrap().clone(); let current_char = text.get(0).unwrap().clone();
if let Some(path) = path.clone() { if let Some(path) = path.clone() {
let path_info = Path::new(&path); let path_info = Path::new(&path);
@ -167,6 +157,9 @@ impl Parser {
match self.parse_block() { match self.parse_block() {
Ok(block) => self.document.add_element(block), Ok(block) => self.document.add_element(block),
Err(err) => { Err(err) => {
if err.eof {
break;
}
if let Some(path) = &self.path { if let Some(path) = &self.path {
if let Some(position) = err.get_position(&self.get_text()) { if let Some(position) = err.get_position(&self.get_text()) {
println!( println!(
@ -439,7 +432,7 @@ impl Parser {
return Err(self.revert_with_error(start_index)); return Err(self.revert_with_error(start_index));
} }
if self.check_special(&IMPORT_CLOSE) { if self.check_special(&IMPORT_CLOSE) {
parse_option!(self.next_char(), self.index); self.skip_char();
} }
// parsing success // parsing success
@ -613,7 +606,7 @@ impl Parser {
let mut element = TextLine::new(); let mut element = TextLine::new();
while let Ok(inline) = self.parse_inline() { while let Ok(inline) = self.parse_inline() {
element.subtext.push(inline); element.subtext.push(inline);
if self.check_linebreak() || self.check_special(&PIPE) { if self.check_linebreak() || self.check_special(&PIPE) || self.check_eof() {
break; break;
} }
} }
@ -623,7 +616,7 @@ impl Parser {
if self.check_special(&PIPE) { if self.check_special(&PIPE) {
self.skip_char(); self.skip_char();
} }
if self.check_linebreak() { if self.check_linebreak() || self.check_eof() {
break; break;
} }
self.seek_inline_whitespace(); self.seek_inline_whitespace();
@ -709,17 +702,19 @@ impl Parser {
let mut text = TextLine::new(); let mut text = TextLine::new();
while let Ok(subtext) = self.parse_inline() { while let Ok(subtext) = self.parse_inline() {
text.add_subtext(subtext); text.add_subtext(subtext);
let current_index = self.index; if self.check_eof() {
if self.next_char() == None {
break; break;
} }
self.revert_to(current_index)?;
} }
if self.check_linebreak() { if self.check_linebreak() {
parse_option!(self.next_char(), self.index); self.skip_char();
} }
if text.subtext.len() > 0 || !self.check_eof() {
Ok(text) Ok(text)
} else {
Err(ParseError::eof(self.index))
}
} }
} }

@ -9,6 +9,7 @@ pub type ParseResult<T> = Result<T, ParseError>;
pub struct ParseError { pub struct ParseError {
index: usize, index: usize,
message: Option<String>, message: Option<String>,
pub(crate) eof: bool,
} }
impl Display for ParseError { impl Display for ParseError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
@ -33,6 +34,7 @@ impl ParseError {
Self { Self {
index, index,
message: None, message: None,
eof: false,
} }
} }
@ -40,6 +42,15 @@ impl ParseError {
Self { Self {
index, index,
message: Some(message.to_string()), message: Some(message.to_string()),
eof: false,
}
}
pub fn eof(index: usize) -> Self {
Self {
index,
message: Some("EOF".to_string()),
eof: true,
} }
} }

Loading…
Cancel
Save