mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-03 08:34:33 +00:00
Remove extra Loc from AST in SpaceBefore/After
This commit is contained in:
parent
4a196915b0
commit
9f5a359051
3 changed files with 31 additions and 18 deletions
|
@ -1,4 +1,5 @@
|
|||
use bumpalo::collections::vec::Vec;
|
||||
use bumpalo::Bump;
|
||||
use operator::Operator;
|
||||
use region::{Loc, Region};
|
||||
use std::fmt::{self, Display, Formatter};
|
||||
|
@ -69,8 +70,8 @@ pub enum Expr<'a> {
|
|||
|
||||
// Blank Space (e.g. comments, spaces, newlines) before or after an expression.
|
||||
// We preserve this for the formatter; canonicalization ignores it.
|
||||
SpaceBefore(&'a [Space<'a>], &'a Loc<Expr<'a>>),
|
||||
SpaceAfter(&'a Loc<Expr<'a>>, &'a [Space<'a>]),
|
||||
SpaceBefore(&'a [Space<'a>], &'a Expr<'a>),
|
||||
SpaceAfter(&'a Expr<'a>, &'a [Space<'a>]),
|
||||
|
||||
// Problems
|
||||
MalformedIdent(&'a str),
|
||||
|
@ -197,12 +198,26 @@ impl<'a> Expr<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn with_spaces_before(spaces: &'a [Space<'a>], loc_expr: &'a Loc<Expr<'a>>) -> Self {
|
||||
Expr::SpaceBefore(spaces, loc_expr)
|
||||
pub fn with_spaces_before(
|
||||
arena: &'a Bump,
|
||||
spaces: &'a [Space<'a>],
|
||||
loc_expr: Loc<Expr<'a>>,
|
||||
) -> Loc<Self> {
|
||||
let region = loc_expr.region;
|
||||
let value = Expr::SpaceBefore(spaces, arena.alloc(loc_expr.value));
|
||||
|
||||
Loc { region, value }
|
||||
}
|
||||
|
||||
pub fn with_spaces_after(loc_expr: &'a Loc<Expr<'a>>, spaces: &'a [Space<'a>]) -> Self {
|
||||
Expr::SpaceAfter(loc_expr, spaces)
|
||||
pub fn with_spaces_after(
|
||||
arena: &'a Bump,
|
||||
loc_expr: Loc<Expr<'a>>,
|
||||
spaces: &'a [Space<'a>],
|
||||
) -> Loc<Self> {
|
||||
let region = loc_expr.region;
|
||||
let value = Expr::SpaceAfter(arena.alloc(loc_expr.value), spaces);
|
||||
|
||||
Loc { region, value }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -27,8 +27,8 @@ where
|
|||
loc_expr
|
||||
} else {
|
||||
Located {
|
||||
region: loc_expr.region.clone(),
|
||||
value: Expr::SpaceBefore(space_list, arena.alloc(loc_expr)),
|
||||
region: loc_expr.region,
|
||||
value: Expr::SpaceBefore(space_list, arena.alloc(loc_expr.value)),
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -49,8 +49,8 @@ where
|
|||
loc_expr
|
||||
} else {
|
||||
Located {
|
||||
region: loc_expr.region.clone(),
|
||||
value: Expr::SpaceBefore(space_list, arena.alloc(loc_expr)),
|
||||
region: loc_expr.region,
|
||||
value: Expr::SpaceBefore(space_list, arena.alloc(loc_expr.value)),
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -71,8 +71,8 @@ where
|
|||
loc_expr
|
||||
} else {
|
||||
Located {
|
||||
region: loc_expr.region.clone(),
|
||||
value: Expr::SpaceAfter(arena.alloc(loc_expr), space_list),
|
||||
region: loc_expr.region,
|
||||
value: Expr::SpaceAfter(arena.alloc(loc_expr.value), space_list),
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -93,8 +93,8 @@ where
|
|||
loc_expr
|
||||
} else {
|
||||
Located {
|
||||
region: loc_expr.region.clone(),
|
||||
value: Expr::SpaceAfter(arena.alloc(loc_expr), space_list),
|
||||
region: loc_expr.region,
|
||||
value: Expr::SpaceAfter(arena.alloc(loc_expr.value), space_list),
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
@ -56,17 +56,15 @@ fn parse_expr<'a>(min_indent: u16, arena: &'a Bump, state: State<'a>) -> ParseRe
|
|||
),
|
||||
|arena, (loc_expr1, opt_operator)| match opt_operator {
|
||||
Some(((spaces_before_op, (loc_op, spaces_after_op)), loc_expr2)) => {
|
||||
let region1 = loc_expr1.region.clone();
|
||||
let region2 = loc_expr2.region.clone();
|
||||
let loc_expr1 = if spaces_before_op.is_empty() {
|
||||
loc_expr1
|
||||
} else {
|
||||
Expr::with_spaces_after(arena.alloc(loc_expr1), spaces_before_op).loc(region1)
|
||||
Expr::with_spaces_after(arena, loc_expr1, spaces_before_op)
|
||||
};
|
||||
let loc_expr2 = if spaces_after_op.is_empty() {
|
||||
loc_expr2
|
||||
} else {
|
||||
Expr::with_spaces_after(arena.alloc(loc_expr2), spaces_after_op).loc(region2)
|
||||
Expr::with_spaces_after(arena, loc_expr2, spaces_after_op)
|
||||
};
|
||||
let tuple = arena.alloc((loc_expr1, loc_op, loc_expr2));
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue