Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ impl Command {
})
} else {
diag.report(&keyword_loc, Severity::Error, &format!("Applied rule must be a symbol but got {} instead", expr.human_name()));
return None
None
}
}
TokenKind::Equals => {
Expand Down Expand Up @@ -278,7 +278,7 @@ impl Command {
}
}
_ => {
diag.report(&keyword.loc, Severity::Error, &format!("expected symbol"));
diag.report(&keyword.loc, Severity::Error, "expected symbol");
None
}
}
Expand Down Expand Up @@ -352,9 +352,8 @@ fn delete_item_by_key<'a, K, V>(assoc: &'a mut Vec<(K, V)>, needle: &'a K) -> bo

impl Context {
pub fn new(interactive: bool) -> Self {
let mut rules = Vec::new();
// TODO: you can potentially `delete` the replace rule (you should not be able to do that)
rules.push((
let rules = vec![(
Token {
kind: TokenKind::Ident,
text: "replace".to_string(),
Expand All @@ -364,7 +363,7 @@ impl Context {
rule: Rule::Replace,
history: vec![]
}
));
)];
Self {
interactive,
rules,
Expand All @@ -379,7 +378,7 @@ impl Context {
match rule {
Rule::User{head, body, ..} => {
write!(sink, "{name} :: {head}", name = name.text)?;
if history.len() > 0 {
if !history.is_empty() {
writeln!(sink, " {{")?;
for (_, command) in history {
match command {
Expand Down Expand Up @@ -437,7 +436,7 @@ impl Context {
Command::DefineRule{ name, rule } => {
if let Some((existing_name, _)) = get_item_by_key(&self.rules, &name) {
diag.report(&name.loc, Severity::Error, &format!("redefinition of existing rule {}", name.text));
diag.report(&existing_name.loc, Severity::Info, &format!("the original definition is located here"));
diag.report(&existing_name.loc, Severity::Info, "the original definition is located here");
return None
}
if let Rule::User{head, body, ..} = &rule {
Expand All @@ -463,7 +462,7 @@ impl Context {
match rule.clone() {
Rule::User {head, body} => Rule::User{head: body, body: head},
Rule::Replace => {
diag.report(&bar.loc, Severity::Error, &format!("irreversible rule"));
diag.report(&bar.loc, Severity::Error, "irreversible rule");
return None;
}
}
Expand All @@ -490,8 +489,8 @@ impl Context {
println!(" => {}", &frame.expr);
frame.history.push((previous_expr, command));
} else {
diag.report(&bar.loc, Severity::Error, &format!("To apply a rule to an expression you need to first start shaping the expression, but no shaping is currently in place"));
diag.report(&bar.loc, Severity::Info, &format!("<expression> {{ - to start shaping"));
diag.report(&bar.loc, Severity::Error, "To apply a rule to an expression you need to first start shaping the expression, but no shaping is currently in place");
diag.report(&bar.loc, Severity::Info, "<expression> { - to start shaping");
return None
}
}
Expand All @@ -501,7 +500,7 @@ impl Context {
if let Some((name, head)) = frame.rule_via_shaping.take() {
if let Some((existing_name, _)) = get_item_by_key(&self.rules, &name) {
diag.report(&token.loc, Severity::Error, &format!("redefinition of existing rule {}", &name.text));
diag.report(&existing_name.loc, Severity::Info, &format!("the original definition is located here"));
diag.report(&existing_name.loc, Severity::Info, "the original definition is located here");
return None
}
diag.report(&name.loc, Severity::Info, &format!("defined rule {} :: {head} = {body}", &name.text));
Expand Down Expand Up @@ -569,7 +568,7 @@ impl Context {
match get_item_by_key(&self.rules, &name) {
Some((_, RuleDefinition{rule: Rule::User{head, body}, history})) => {
print!("{name} :: {head}", name = name.text);
if history.len() > 0 {
if !history.is_empty() {
println!(" {{");
for (_, command) in history {
match command {
Expand Down
4 changes: 2 additions & 2 deletions src/engine/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl Lexer {
while eol < self.chars.len() && self.chars[eol] != '\n' {
eol += 1;
}
return self.chars[self.bol..eol].to_vec();
self.chars[self.bol..eol].to_vec()
}

pub fn loc(&self) -> Loc {
Expand Down Expand Up @@ -282,7 +282,7 @@ impl Lexer {
'+' => Token {kind: TokenKind::Plus, text, loc},
'-' => Token {kind: TokenKind::Dash, text, loc},
'*' => Token {kind: TokenKind::Asterisk, text, loc},
'/' => if let Some(_) = self.drop_char_if(|x| x == '/') {
'/' => if self.drop_char_if(|x| x == '/').is_some() {
self.drop_line();
continue 'again;
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/engine/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ impl Rule {
Some(false)
}
None => {
diag.report(&apply_command_loc, Severity::Error, &format!("unknown rule application strategy '{}'", meta_strategy_name.report()));
diag.report(apply_command_loc, Severity::Error, &format!("unknown rule application strategy '{}'", meta_strategy_name.report()));
None
}
}
} else {
diag.report(&apply_command_loc, Severity::Error, &format!("strategy must be a symbol but got {} {}", meta_strategy.human_name(), &meta_strategy));
diag.report(apply_command_loc, Severity::Error, &format!("strategy must be a symbol but got {} {}", meta_strategy.human_name(), &meta_strategy));
None
}
} else {
Expand All @@ -161,7 +161,7 @@ impl Rule {
let mut match_count = 0;
apply_impl(self, expr, strategy, apply_command_loc, &mut match_count, diag)?;
if match_count == 0 {
diag.report(&apply_command_loc, Severity::Error, &format!("no match found"));
diag.report(apply_command_loc, Severity::Error, "no match found");
return None
}
Some(())
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn parse_and_process_command(context: &mut Context, lexer: &mut Lexer, diag: &mu

fn interpret_file(file_path: &str) -> Option<()> {
let mut context = Context::new(false);
let source = fs::read_to_string(&file_path).unwrap();
let source = fs::read_to_string(file_path).unwrap();
let mut lexer = Lexer::new(source.chars().collect(), Some(file_path.to_string()));
let mut diag = StdoutDiagnoster{};
while !context.quit && lexer.peek_token().kind != TokenKind::End {
Expand Down