Compare commits

...

1 Commits

Author SHA1 Message Date
f79cd0f891 Fix CI, partial revert of #4769
Signed-off-by: Nick Cameron <nrc@ncameron.org>
2024-12-13 10:52:54 +13:00
2 changed files with 9 additions and 24 deletions

View File

@ -4,7 +4,7 @@
use std::{cell::RefCell, collections::HashMap, str::FromStr};
use winnow::{
combinator::{alt, opt, peek, preceded, repeat, separated, separated_pair, terminated},
combinator::{alt, delimited, opt, peek, preceded, repeat, separated, separated_pair, terminated},
dispatch,
error::{ErrMode, StrContext, StrContextValue},
prelude::*,
@ -69,6 +69,7 @@ impl ParseContext {
}
/// Set a new `ParseContext` in thread-local storage. Panics if one already exists.
#[track_caller]
fn init() {
assert!(CTXT.with_borrow(|ctxt| ctxt.is_none()));
CTXT.with_borrow_mut(|ctxt| *ctxt = Some(ParseContext::new()));
@ -76,6 +77,7 @@ impl ParseContext {
/// Take the current `ParseContext` from thread-local storage, leaving `None`. Panics if a `ParseContext`
/// is not present.
#[track_caller]
fn take() -> ParseContext {
CTXT.with_borrow_mut(|ctxt| ctxt.take()).unwrap()
}
@ -1662,24 +1664,12 @@ fn label(i: &mut TokenSlice) -> PResult<Node<Identifier>> {
}
fn unnecessarily_bracketed(i: &mut TokenSlice) -> PResult<Expr> {
let (bra, result, ket) = (
delimited(
terminated(open_paren, opt(whitespace)),
expression,
preceded(opt(whitespace), close_paren),
)
.parse_next(i)?;
let expr_range: SourceRange = (&result).into();
ParseContext::warn(CompilationError::with_suggestion(
SourceRange::new(bra.start, ket.end, result.module_id()),
None,
"Unnecessary parentheses around sub-expression",
Some(("Remove parentheses", i.text(expr_range))),
Tag::Unnecessary,
));
Ok(result)
.parse_next(i)
}
fn expr_allowed_in_pipe_expr(i: &mut TokenSlice) -> PResult<Expr> {
@ -2949,6 +2939,7 @@ mySk1 = startSketchAt([0, 0])"#;
#[test]
fn test_arg() {
ParseContext::init();
for input in [
"( sigmaAllow * width )",
"6 / ( sigmaAllow * width )",
@ -2984,6 +2975,7 @@ mySk1 = startSketchAt([0, 0])"#;
#[test]
fn assign_brackets() {
ParseContext::init();
for (i, test_input) in [
"thickness_squared = (1 + 1)",
"thickness_squared = ( 1 + 1)",
@ -4255,15 +4247,6 @@ var baz = 2
"#
);
}
#[test]
fn warn_unneccessary_parens() {
let some_program_string = r#"foo((a + b))"#;
let (_, errs) = assert_no_err(some_program_string);
assert_eq!(errs.len(), 1);
let replaced = errs[0].apply_suggestion(some_program_string).unwrap();
assert_eq!(replaced, r#"foo(a + b)"#);
}
}
#[cfg(test)]

View File

@ -62,6 +62,7 @@ impl TokenStream {
TokenSlice::from(self)
}
#[allow(dead_code)]
pub fn text(&self, range: SourceRange) -> &str {
debug_assert_eq!(range.module_id(), self.module_id);
&self.source[range.start()..range.end()]
@ -108,6 +109,7 @@ impl<'a> TokenSlice<'a> {
&self.stream.tokens[i + self.start]
}
#[allow(dead_code)]
pub fn text(&self, range: SourceRange) -> &str {
self.stream.text(range)
}