forked from ferrumc-rs/ferrumc
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
f94bbe8
commit 13bf0b5
Showing
7 changed files
with
679 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// <root> - empty text component | ||
// test - literal | ||
// <blue> - tag | ||
// test - literal | ||
// </blue> | ||
// hello - literal | ||
// </root> | ||
pub struct RootTag<'a> { | ||
pub children: Vec<Tag<'a>>, | ||
} | ||
|
||
pub struct ParsedTag<'a> { | ||
pub name: &'a str, | ||
pub has_end_tag: bool, | ||
pub children: Vec<Tag<'a>>, | ||
} | ||
|
||
pub enum Tag<'a> { | ||
Literal(&'a str), | ||
Tag(ParsedTag<'a>), | ||
} |
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,96 @@ | ||
use crate::message::Span; | ||
use colored::Colorize; | ||
use nom::error::{ErrorKind, FromExternalError, ParseError}; | ||
use std::fmt; | ||
use thiserror::Error; | ||
|
||
#[derive(Debug, PartialEq, Clone)] | ||
pub struct MessageError<'a> { | ||
/*pub input: &'a str, | ||
pub line: usize, | ||
pub column_start: usize, | ||
pu*b column_width: usize,*/ | ||
pub span: Span<'a>, | ||
pub source: InnerMessageError, | ||
} | ||
|
||
impl<'a> ParseError<Span<'a>> for MessageError<'a> { | ||
fn from_error_kind(input: Span<'a>, kind: ErrorKind) -> Self { | ||
Self { | ||
span: input, | ||
source: InnerMessageError::NomError(kind), | ||
} | ||
} | ||
|
||
fn append(input: Span<'a>, kind: ErrorKind, _other: Self) -> Self { | ||
Self::from_error_kind(input, kind) | ||
} | ||
} | ||
|
||
impl<'a, E> FromExternalError<Span<'a>, E> for MessageError<'a> { | ||
fn from_external_error(input: Span<'a>, kind: ErrorKind, _e: E) -> Self { | ||
Self::from_error_kind(input, kind) | ||
} | ||
} | ||
|
||
#[derive(Debug, PartialEq, Clone, Error)] | ||
pub enum InnerMessageError { | ||
#[error("{0:#?}")] | ||
NomError(ErrorKind), | ||
#[error("Unexpected end tag (expected \"</{1}>\", found \"</{0}>\")")] | ||
ExpectedEndTag(&'static str, &'static str), | ||
#[error("Missing end tag for \"<{0}>\"")] | ||
MissingEndTag(&'static str), | ||
#[error("Unexpected end tag for \"<{0}>\"")] | ||
UnexpectedEndTag(&'static str), | ||
#[error("Missing start tag for \"</{0}>\"")] | ||
MissingStartTag(&'static str), | ||
} | ||
|
||
#[derive(Debug, PartialEq, Clone, Error)] | ||
pub enum ResolveError { | ||
#[error("Unable to parse a {expected_type} from '{expected}'. {description}")] | ||
ExpectedType { | ||
expected_type: &'static str, | ||
expected: String, | ||
description: String, | ||
}, | ||
#[error("Process function is unimplemented for tag resolver \"<{0}>\"")] | ||
ProcessUnimplemented(String), | ||
#[error("Couldn't find resolver for tag \"<{0}>\"")] | ||
NoResolverFor(String), | ||
#[error("{0}")] | ||
Custom(String), | ||
#[error("{0}")] | ||
CustomStatic(&'static str), | ||
} | ||
|
||
fn fill_carets(col: (usize, usize), caret: &str) -> String { | ||
format!("{}{}", " ".repeat(col.0), caret.repeat(col.1)) | ||
} | ||
|
||
impl fmt::Display for MessageError<'_> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
let line = self.span.location_line().to_string(); | ||
let space = " ".repeat(std::cmp::max(5 - (2 + line.len()), 0)); | ||
let ln = " |".blue().to_string(); | ||
write!( | ||
f, | ||
"{}: | ||
\r {ln}{space} | ||
\r {ln}{space}{} | ||
\r{num}{} | ||
\r {ln}{space} | ||
", | ||
"error".bright_red(), | ||
self.span.fragment(), | ||
format!( | ||
"{} {}", | ||
fill_carets((self.span.get_column(), self.span.location_offset()), "^"), | ||
self.source | ||
) | ||
.bright_red(), | ||
num = line.blue().to_string() + &ln + &space | ||
) | ||
} | ||
} |
Oops, something went wrong.