improve and move theming traits

move pretty_print.rs from mers to mers_lib
This commit is contained in:
Mark
2024-06-26 12:54:04 +02:00
parent b3d6b227b5
commit 7a945e80ba
13 changed files with 488 additions and 260 deletions

View File

@@ -308,6 +308,9 @@ impl Source {
line
}
pub fn get_pos_last_char(&self) -> SourcePos {
SourcePos(self.i.saturating_sub(1))
}
pub fn get_pos(&self) -> SourcePos {
SourcePos(self.i)
}

View File

@@ -555,7 +555,7 @@ pub fn parse_string(
srca: &Arc<Source>,
double_quote: SourcePos,
) -> Result<String, CheckError> {
parse_string_custom_end(src, srca, double_quote, '"', '"')
parse_string_custom_end(src, srca, double_quote, '"', '"', "", EColor::StringEOF)
}
pub fn parse_string_custom_end(
src: &mut Source,
@@ -563,6 +563,8 @@ pub fn parse_string_custom_end(
opening: SourcePos,
opening_char: char,
closing_char: char,
string_prefix: &str,
eof_color: EColor,
) -> Result<String, CheckError> {
let mut s = String::new();
loop {
@@ -602,13 +604,13 @@ pub fn parse_string_custom_end(
return Err(CheckError::new()
.src(vec![(
(opening, src.get_pos(), srca).into(),
Some(EColor::StringEOF),
Some(eof_color),
)])
.msg_str(format!(
"EOF in string literal{}",
"EOF in {string_prefix}string literal{}",
if closing_char != '"' {
format!(
"{opening_char}...{closing_char} (end string with '{closing_char}')"
" {opening_char}...{closing_char} (end string with '{closing_char}')"
)
} else {
String::new()

View File

@@ -24,6 +24,7 @@ pub fn parse_single_type(src: &mut Source, srca: &Arc<Source>) -> Result<ParsedT
Ok(match src.peek_char() {
// Reference
Some('&') => {
let pos_in_src = src.get_pos();
src.next_char();
if let Some('[') = src.peek_char() {
src.next_char();
@@ -35,9 +36,14 @@ pub fn parse_single_type(src: &mut Source, srca: &Arc<Source>) -> Result<ParsedT
} else {
format!("EOF")
};
return Err(CheckError::new().msg_str(format!(
"No closing ] in reference type with opening [! Found {nc} instead"
)));
return Err(CheckError::new()
.src(vec![(
(pos_in_src, src.get_pos(), srca).into(),
Some(EColor::BracketedRefTypeNoClosingBracket),
)])
.msg_str(format!(
"No closing ] in reference type with opening [! Found {nc} instead"
)));
}
ParsedType::Reference(types)
} else {
@@ -177,13 +183,28 @@ pub fn parse_single_type(src: &mut Source, srca: &Arc<Source>) -> Result<ParsedT
src.next_char();
ParsedType::TypeWithInfo(
t,
super::statements::parse_string_custom_end(src, srca, pos, '<', '>')?,
super::statements::parse_string_custom_end(
src,
srca,
pos,
'<',
'>',
"type-info ",
EColor::TypeEOF,
)?,
)
} else {
ParsedType::Type(t)
}
}
None => todo!(),
None => {
return Err(CheckError::new()
.src(vec![(
(src.get_pos_last_char(), src.get_pos(), srca).into(),
Some(EColor::TypeEOF),
)])
.msg_str(format!("Expected type, got EOF")))
}
})
}