Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Specy committed Oct 1, 2024
1 parent 4525bf1 commit 30a44e5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
38 changes: 17 additions & 21 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)?),
}
}
Expand All @@ -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) {
Expand All @@ -279,7 +275,7 @@ impl Parser {
});
}
}
}
},
}
}
}
Expand Down Expand Up @@ -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() {
Expand All @@ -342,7 +338,7 @@ impl Parser {
span: current_token.get_span().clone(),
}
});
}
},
};

if traced {
Expand All @@ -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();
Expand All @@ -391,12 +387,12 @@ impl Parser {
match self.goto_table()[new_state].get(rule.symbol()) {
Some(state) => {
state_stack.push(*state);
}
},
None => {
unreachable!();
}
},
}
}
},
}
}
}
Expand Down Expand Up @@ -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(),
}
};
Expand Down Expand Up @@ -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)]);
}
},
}
}

Expand All @@ -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)
)]);
}
},
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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 '^', '+', '-', '*', '/', ')')",
);
}

Expand Down

0 comments on commit 30a44e5

Please sign in to comment.