Compare commits
1 Commits
jtran/fix-
...
nrc-fix
Author | SHA1 | Date | |
---|---|---|---|
f79cd0f891 |
@ -4,7 +4,7 @@
|
|||||||
use std::{cell::RefCell, collections::HashMap, str::FromStr};
|
use std::{cell::RefCell, collections::HashMap, str::FromStr};
|
||||||
|
|
||||||
use winnow::{
|
use winnow::{
|
||||||
combinator::{alt, opt, peek, preceded, repeat, separated, separated_pair, terminated},
|
combinator::{alt, delimited, opt, peek, preceded, repeat, separated, separated_pair, terminated},
|
||||||
dispatch,
|
dispatch,
|
||||||
error::{ErrMode, StrContext, StrContextValue},
|
error::{ErrMode, StrContext, StrContextValue},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
@ -69,6 +69,7 @@ impl ParseContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Set a new `ParseContext` in thread-local storage. Panics if one already exists.
|
/// Set a new `ParseContext` in thread-local storage. Panics if one already exists.
|
||||||
|
#[track_caller]
|
||||||
fn init() {
|
fn init() {
|
||||||
assert!(CTXT.with_borrow(|ctxt| ctxt.is_none()));
|
assert!(CTXT.with_borrow(|ctxt| ctxt.is_none()));
|
||||||
CTXT.with_borrow_mut(|ctxt| *ctxt = Some(ParseContext::new()));
|
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`
|
/// Take the current `ParseContext` from thread-local storage, leaving `None`. Panics if a `ParseContext`
|
||||||
/// is not present.
|
/// is not present.
|
||||||
|
#[track_caller]
|
||||||
fn take() -> ParseContext {
|
fn take() -> ParseContext {
|
||||||
CTXT.with_borrow_mut(|ctxt| ctxt.take()).unwrap()
|
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> {
|
fn unnecessarily_bracketed(i: &mut TokenSlice) -> PResult<Expr> {
|
||||||
let (bra, result, ket) = (
|
delimited(
|
||||||
terminated(open_paren, opt(whitespace)),
|
terminated(open_paren, opt(whitespace)),
|
||||||
expression,
|
expression,
|
||||||
preceded(opt(whitespace), close_paren),
|
preceded(opt(whitespace), close_paren),
|
||||||
)
|
)
|
||||||
.parse_next(i)?;
|
.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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expr_allowed_in_pipe_expr(i: &mut TokenSlice) -> PResult<Expr> {
|
fn expr_allowed_in_pipe_expr(i: &mut TokenSlice) -> PResult<Expr> {
|
||||||
@ -2949,6 +2939,7 @@ mySk1 = startSketchAt([0, 0])"#;
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_arg() {
|
fn test_arg() {
|
||||||
|
ParseContext::init();
|
||||||
for input in [
|
for input in [
|
||||||
"( sigmaAllow * width )",
|
"( sigmaAllow * width )",
|
||||||
"6 / ( sigmaAllow * width )",
|
"6 / ( sigmaAllow * width )",
|
||||||
@ -2984,6 +2975,7 @@ mySk1 = startSketchAt([0, 0])"#;
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn assign_brackets() {
|
fn assign_brackets() {
|
||||||
|
ParseContext::init();
|
||||||
for (i, test_input) in [
|
for (i, test_input) in [
|
||||||
"thickness_squared = (1 + 1)",
|
"thickness_squared = (1 + 1)",
|
||||||
"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)]
|
#[cfg(test)]
|
||||||
|
@ -62,6 +62,7 @@ impl TokenStream {
|
|||||||
TokenSlice::from(self)
|
TokenSlice::from(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn text(&self, range: SourceRange) -> &str {
|
pub fn text(&self, range: SourceRange) -> &str {
|
||||||
debug_assert_eq!(range.module_id(), self.module_id);
|
debug_assert_eq!(range.module_id(), self.module_id);
|
||||||
&self.source[range.start()..range.end()]
|
&self.source[range.start()..range.end()]
|
||||||
@ -108,6 +109,7 @@ impl<'a> TokenSlice<'a> {
|
|||||||
&self.stream.tokens[i + self.start]
|
&self.stream.tokens[i + self.start]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn text(&self, range: SourceRange) -> &str {
|
pub fn text(&self, range: SourceRange) -> &str {
|
||||||
self.stream.text(range)
|
self.stream.text(range)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user