Skip to content

Commit

Permalink
parser+lexer: Rust fmt formatted the code
Browse files Browse the repository at this point in the history
  • Loading branch information
hexaredecimal committed Apr 6, 2024
1 parent d2ceb6d commit af0f5fe
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
17 changes: 8 additions & 9 deletions src/lexer/display.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::lexer::{Keyword, TokenKind, Value};

use crate::lexer::{Keyword, TokenKind, Value};

impl std::fmt::Display for Keyword {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Keyword::Let => write!(f, "let"),
Keyword::If => write!(f, "if"),
Keyword::Else => write!(f, "else"),
Expand All @@ -20,16 +21,16 @@ impl std::fmt::Display for Keyword {
Keyword::Import => write!(f, "import"),
Keyword::Selff => write!(f, "self"), // "self"
Keyword::Unknown => write!(f, "unknown"),
}
}
}
}
}
}

impl std::fmt::Display for TokenKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TokenKind::Whitespace => write!(f, "whitespace"),
TokenKind::CarriageReturn => write!(f, "\\n"),
TokenKind::Identifier(id) => write!(f, "{id}"),
TokenKind::Identifier(id) => write!(f, "{id}"),
TokenKind::Literal(value) => write!(f, "{value}"),
TokenKind::Keyword(keyword) => write!(f, "{keyword}"),
TokenKind::Comment => write!(f, "comment"),
Expand Down Expand Up @@ -69,7 +70,6 @@ impl std::fmt::Display for TokenKind {
}
}


impl std::fmt::Display for Value {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand All @@ -78,4 +78,3 @@ impl std::fmt::Display for Value {
}
}
}

15 changes: 11 additions & 4 deletions src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,12 @@ impl Parser {
match &token.kind {
TokenKind::Keyword(ref k) if k == &keyword => Ok(()),
_ => {
let mut error = self.make_error_msg(token.pos, format!("Expected keyword, found {}", token.raw));
let hint = self.make_hint_msg(format!("replace the symbol `{}` with the appropriate keyword. ", token.raw));
let mut error = self
.make_error_msg(token.pos, format!("Expected keyword, found {}", token.raw));
let hint = self.make_hint_msg(format!(
"replace the symbol `{}` with the appropriate keyword. ",
token.raw
));
error.push_str(&hint);
Err(error)
}
Expand All @@ -119,8 +123,11 @@ impl Parser {
match &token.kind {
TokenKind::Identifier(n) => Ok(n.to_string()),
other => {
let mut error = self.make_error_msg(token.pos, format!("Expected Identifier, found `{other}`",));
let hint = self.make_hint_msg(format!("replace the symbol `{other}` with an identifier. Example `Foo`"));
let mut error = self
.make_error_msg(token.pos, format!("Expected Identifier, found `{other}`",));
let hint = self.make_hint_msg(format!(
"replace the symbol `{other}` with an identifier. Example `Foo`"
));
error.push_str(&hint);
Err(error)
}
Expand Down
18 changes: 12 additions & 6 deletions src/parser/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ impl Parser {
}
TokenKind::Identifier(_) => fields.push(self.parse_typed_variable()?),
_ => {
let mut error = self.make_error_msg(next.pos, "Expected struct field or method".into());
let hint = self.make_hint_msg(format!("remove the following symbol `{}`", next.raw));
let mut error =
self.make_error_msg(next.pos, "Expected struct field or method".into());
let hint =
self.make_hint_msg(format!("remove the following symbol `{}`", next.raw));
error.push_str(&hint);
return Err(error)
return Err(error);
}
}
}
Expand Down Expand Up @@ -473,10 +475,14 @@ impl Parser {

let last = self.peek()?;
if last.kind != TokenKind::CurlyBracesClose {
let mut error = self.make_error_msg(last.pos, "Expected a struct field initialization or a closing curly brace (`}`)".into());
let hint = self.make_hint_msg(format!("remove the following symbol `{}`", last.raw));
let mut error = self.make_error_msg(
last.pos,
"Expected a struct field initialization or a closing curly brace (`}`)".into(),
);
let hint =
self.make_hint_msg(format!("remove the following symbol `{}`", last.raw));
error.push_str(&hint);
return Err(error)
return Err(error);
}
}

Expand Down

0 comments on commit af0f5fe

Please sign in to comment.