diff options
Diffstat (limited to 'src/parser.rs')
-rw-r--r-- | src/parser.rs | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/src/parser.rs b/src/parser.rs index e13363e..83be505 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,6 +1,5 @@ -use crate::error::RikiError; use crate::token::{TokenKind, TokenParser, TokenPatterns}; -use crate::wikitext::{ParsedDirective, Snippet, WikiLink}; +use crate::wikitext::{ParsedDirective, Snippet, WikiLink, WikitextError}; use line_col::LineColLookup; use log::{debug, trace}; use std::collections::HashMap; @@ -9,6 +8,22 @@ use std::collections::HashMap; pub enum ParserError { #[error("failed to parse wikitext, line {0}, column {1}: {2:?}")] WikitextSyntax(usize, usize, Vec<crate::token::TokenKind>), + + #[error(transparent)] + Wikitext(#[from] Box<WikitextError>), +} + +impl ParserError { + pub fn wikitext_syntax(line: usize, col: usize, tokens: &[crate::token::TokenKind]) -> Self { + let tokens = tokens.to_vec(); + crate::parser::ParserError::WikitextSyntax(line, col, tokens) + } +} + +impl From<WikitextError> for ParserError { + fn from(e: WikitextError) -> Self { + Self::Wikitext(Box::new(e)) + } } #[derive(Debug)] @@ -50,7 +65,7 @@ impl WikitextParser { self.tokens.is_empty() } - pub fn parse(&mut self) -> Result<Option<Snippet>, RikiError> { + pub fn parse(&mut self) -> Result<Option<Snippet>, ParserError> { if self.is_empty() { return Ok(None); } @@ -116,7 +131,9 @@ impl WikitextParser { self.drain(3); break; } - _ => return Err(RikiError::wikitext_syntax(line, col, &self.tokens[..5])), + _ => { + return Err(ParserError::wikitext_syntax(line, col, &self.tokens[..5])) + } } } if target.is_none() { @@ -169,7 +186,9 @@ impl WikitextParser { args.insert(value.to_string(), "".to_string()); self.drain(1); } - _ => return Err(RikiError::wikitext_syntax(line, col, &self.tokens[..5])), + _ => { + return Err(ParserError::wikitext_syntax(line, col, &self.tokens[..5])) + } } } Snippet::Directive(ParsedDirective::new(&name, args)?) @@ -205,7 +224,7 @@ impl WikitextParser { break; } _ => { - return Err(RikiError::wikitext_syntax( + return Err(ParserError::wikitext_syntax( line, col, &self.tokens[..std::cmp::min(5, self.tokens.len())], @@ -285,7 +304,7 @@ impl WikitextParser { self.drain(1); Snippet::Markdown("]]".into()) } - _ => return Err(RikiError::wikitext_syntax(line, col, &self.tokens[..5])), + _ => return Err(ParserError::wikitext_syntax(line, col, &self.tokens[..5])), }; Ok(Some(snippet)) } |