improve Precedence error message

This commit is contained in:
Folkert 2020-03-31 23:11:35 +02:00
parent d213c7316b
commit 11c8e2bfaa
8 changed files with 257 additions and 40 deletions

View file

@ -165,7 +165,7 @@ pub enum Expr<'a> {
MalformedClosure,
// Both operators were non-associative, e.g. (True == False == False).
// We should tell the author to disambiguate by grouping them with parens.
PrecedenceConflict(Loc<BinOp>, Loc<BinOp>, &'a Loc<Expr<'a>>),
PrecedenceConflict(Region, Loc<BinOp>, Loc<BinOp>, &'a Loc<Expr<'a>>),
}
#[derive(Debug, Clone, PartialEq)]

View file

@ -323,7 +323,7 @@ fn expr_to_pattern<'a>(arena: &'a Bump, expr: &Expr<'a>) -> Result<Pattern<'a>,
| Expr::If(_, _, _)
| Expr::When(_, _)
| Expr::MalformedClosure
| Expr::PrecedenceConflict(_, _, _)
| Expr::PrecedenceConflict(_, _, _, _)
| Expr::Record {
update: Some(_), ..
}

View file

@ -1,5 +1,6 @@
use self::BinOp::*;
use std::cmp::Ordering;
use std::fmt;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CalledVia {
@ -109,3 +110,29 @@ impl Ord for BinOp {
self.precedence().cmp(&other.precedence())
}
}
impl std::fmt::Display for BinOp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let as_str = match self {
Caret => "^",
Star => "*",
Slash => "/",
DoubleSlash => "//",
Percent => "%",
DoublePercent => "%%",
Plus => "+",
Minus => "-",
Equals => "==",
NotEquals => "!=",
LessThan => "<",
GreaterThan => ">",
LessThanOrEq => "<=",
GreaterThanOrEq => ">=",
And => "&&",
Or => "||",
Pizza => "|>",
};
write!(f, "({})", as_str)
}
}