From 30a44e5c91204105db3f3ef4e56357e6a5aa2343 Mon Sep 17 00:00:00 2001 From: Specy Date: Tue, 1 Oct 2024 20:17:28 +0200 Subject: [PATCH] update tests --- src/parser.rs | 38 +++++++++++++++++--------------------- tests/parsing.rs | 4 ++-- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 3c35cf1..a8159b9 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -123,12 +123,8 @@ fn count_new_lines(slice: &str) -> usize { slice.chars().filter(|&c| c == '\n').count() } -fn count_col_position(slice: &str, offset: usize, line_num: usize,) -> usize { - if line_num == 0 { - offset - slice[..offset].rfind('\n').unwrap_or(1) - 1 - } else { - offset - } +fn count_col_position(slice: &str, offset: usize, line_num: usize) -> usize { + if line_num == 0 { offset - slice[..offset].rfind('\n').unwrap_or(1) - 1 } else { offset } } impl Parser { @@ -246,7 +242,7 @@ impl Parser { let trace = serde_wasm_bindgen::to_value(&trace)?; let tree = serde_wasm_bindgen::to_value(&tree)?; Ok(vec![trace, tree]) - } + }, Err(error) => Err(serde_wasm_bindgen::to_value(&error)?), } } @@ -269,7 +265,7 @@ impl Parser { rule: rule.clone(), }); } - } + }, AtomicPattern::Token(token) => { if let Token::Regex(regex_token) = token { if !grammar.regular_expressions().contains_key(regex_token) { @@ -279,7 +275,7 @@ impl Parser { }); } } - } + }, } } } @@ -323,7 +319,7 @@ impl Parser { Some(actions) => { assert_eq!(actions.len(), 1); *actions.iter().next().unwrap() - } + }, None => { let mut expected = smallvec![]; for (token, _) in self.action_table()[current_state].iter() { @@ -342,7 +338,7 @@ impl Parser { span: current_token.get_span().clone(), } }); - } + }, }; if traced { @@ -367,13 +363,13 @@ impl Parser { pattern: tree_stack, }; return Ok((trace, parse_tree)); - } + }, Action::Shift { next_state } => { let (token, span) = current_token.clone().into_tuple(); state_stack.push(next_state); tree_stack.push(Tree::Terminal { token, span, slice: current_slice }); (current_token, current_slice) = remaining_tokens.pop().unwrap(); - } + }, Action::Reduce { rule_index } => { let rule = &self.grammar.rules()[rule_index]; let pattern_length = rule.pattern().len(); @@ -391,12 +387,12 @@ impl Parser { match self.goto_table()[new_state].get(rule.symbol()) { Some(state) => { state_stack.push(*state); - } + }, None => { unreachable!(); - } + }, } - } + }, } } } @@ -446,7 +442,7 @@ impl Parser { match self.follow_table.get(symbol) { Some(follow_set) if !follow_set.is_empty() => { format!("{{ {} }}", follow_set.iter().join(", ")) - } + }, _ => "{}".to_owned(), } }; @@ -617,10 +613,10 @@ impl Parser { .map(|action| format_smolstr!("{}", action)) .join(", "); actions_row.add_cell(cell![pad(actions, padding)]); - } + }, None => { actions_row.add_cell(cell![pad("-", padding)]); - } + }, } } @@ -640,13 +636,13 @@ impl Parser { state, symbol.len().max(longest_state_length) )]); - } + }, None => { gotos_row.add_cell(cell![pad( "-", symbol.len().max(longest_state_length) )]); - } + }, } } diff --git a/tests/parsing.rs b/tests/parsing.rs index 0244a31..e6f0d5f 100644 --- a/tests/parsing.rs +++ b/tests/parsing.rs @@ -23,7 +23,7 @@ fn raising_correct_error_when_encountering_unexpected_token_during_parsing_calcu let tokens = parser.tokenize("1 + /").unwrap(); let error = parser.parse(tokens).unwrap_err(); - assert_eq!(error.to_string(), "unexpected token / (expected one of '(', %f)"); + assert_eq!(error.to_string(), "unexpected token / at [1:4] (expected one of '(', %f)"); } #[test] @@ -35,7 +35,7 @@ fn raising_correct_error_when_encountering_unexpected_eof_during_parsing_calcula let error = parser.parse(tokens).unwrap_err(); assert_eq!( error.to_string(), - "unexpected end of input (expected one of '^', '+', '-', '*', '/', ')')", + "unexpected end of input at [1:6] (expected one of '^', '+', '-', '*', '/', ')')", ); }