-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
81 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use super::{code_span, SourcePos, Span}; | ||
use std::fmt; | ||
|
||
#[derive(Debug)] | ||
pub struct ParseError { | ||
msg: String, | ||
pos: SourcePos, | ||
} | ||
|
||
impl ParseError { | ||
pub fn remaining(span: Span) -> ParseError { | ||
ParseError { | ||
msg: "Expected end of file.".into(), | ||
pos: span.into(), | ||
} | ||
} | ||
pub fn incomplete() -> ParseError { | ||
ParseError { | ||
msg: "Unexpected end of file.".into(), | ||
pos: code_span(b"").into(), // This should not happen? | ||
} | ||
} | ||
pub fn err(kind: nom::error::ErrorKind, span: Span) -> ParseError { | ||
ParseError { | ||
msg: format!("Parse Error: {:?}", kind), | ||
pos: span.into(), | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Display for ParseError { | ||
fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result { | ||
let line_no = self.pos.line_no.to_string(); | ||
write!( | ||
out, | ||
"{msg}\ | ||
\n{0:lnw$} ,\ | ||
\n{ln} | {line}\ | ||
\n{0:lnw$} |{0:>lpos$}^\ | ||
\n{0:lnw$} '", | ||
"", | ||
line = self.pos.line, | ||
msg = self.msg, | ||
ln = line_no, | ||
lnw = line_no.len(), | ||
lpos = self.pos.line_pos, | ||
)?; | ||
let mut nextpos = Some(&self.pos); | ||
while let Some(pos) = nextpos { | ||
write!( | ||
out, | ||
"\n{0:lnw$} {file} {row}:{col} {cause}", | ||
"", | ||
lnw = line_no.len(), | ||
file = pos.file.name(), | ||
row = pos.line_no, | ||
col = pos.line_pos, | ||
cause = if pos.file.imported_from().is_some() { | ||
"import" | ||
} else { | ||
"root stylesheet" | ||
}, | ||
)?; | ||
nextpos = pos.file.imported_from(); | ||
} | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters