Skip to content

Commit

Permalink
Add spans to patterns in CST and AST
Browse files Browse the repository at this point in the history
  • Loading branch information
BinderDavid committed Jan 8, 2025
1 parent b60ce0a commit ca70ff0
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 7 deletions.
5 changes: 3 additions & 2 deletions lang/ast/src/exp/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ use super::{Exp, IdBound, Lvl, MetaVar, TelescopeInst};
#[derive(Debug, Clone, Derivative)]
#[derivative(Eq, PartialEq, Hash)]
pub struct Pattern {
pub span: Option<Span>,
pub is_copattern: bool,
pub name: IdBound,
pub params: TelescopeInst,
}

impl Print for Pattern {
fn print<'a>(&'a self, cfg: &PrintCfg, alloc: &'a Alloc<'a>) -> Builder<'a> {
let Pattern { is_copattern, name, params } = self;
let Pattern { span: _, is_copattern, name, params } = self;
if *is_copattern {
alloc.text(DOT).append(alloc.ctor(&name.id)).append(params.print(cfg, alloc))
} else {
Expand All @@ -44,7 +45,7 @@ impl Zonk for Pattern {
&mut self,
meta_vars: &crate::HashMap<MetaVar, crate::MetaVarState>,
) -> Result<(), ZonkError> {
let Pattern { is_copattern: _, name: _, params } = self;
let Pattern { span: _, is_copattern: _, name: _, params } = self;
params.zonk(meta_vars)
}
}
Expand Down
2 changes: 1 addition & 1 deletion lang/backend/src/ast2ir/exprs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl ToIR for ast::Case {

fn to_ir(&self) -> Result<Self::Target, BackendError> {
let ast::Case { pattern, body, .. } = self;
let ast::Pattern { is_copattern, params, name } = pattern;
let ast::Pattern { span: _, is_copattern, params, name } = pattern;

let params = params.to_ir()?;

Expand Down
1 change: 1 addition & 0 deletions lang/elaborator/src/normalizer/val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,7 @@ impl ReadBack for Case {
Ok(ast::Case {
span: *span,
pattern: ast::Pattern {
span: None,
is_copattern: *is_copattern,
name: name.clone(),
params: params.clone(),
Expand Down
2 changes: 2 additions & 0 deletions lang/elaborator/src/typechecker/exprs/local_comatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ impl WithExpectedType<'_> {
let case_out = Case {
span: *span,
pattern: Pattern {
span: None,
is_copattern: true,
name: name.clone(),
params: args_out,
Expand Down Expand Up @@ -323,6 +324,7 @@ impl WithExpectedType<'_> {
let case_out = Case {
span: *span,
pattern: Pattern {
span: None,
is_copattern: true,
name: name.clone(),
params: args_out,
Expand Down
1 change: 1 addition & 0 deletions lang/elaborator/src/typechecker/exprs/local_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ impl WithScrutineeType<'_> {
let case_out = Case {
span,
pattern: Pattern {
span: None,
is_copattern: false,
name: name.clone(),
params: args_out,
Expand Down
15 changes: 13 additions & 2 deletions lang/lowering/src/lower/exp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,12 @@ impl Lower for cst::exp::Case<cst::exp::Pattern> {
};
Ok(ast::Case {
span: Some(*span),
pattern: ast::Pattern { is_copattern: false, name, params },
pattern: ast::Pattern {
span: Some(pattern.span),
is_copattern: false,
name,
params,
},
body: body.lower(ctx)?,
})
})
Expand All @@ -270,7 +275,12 @@ impl Lower for cst::exp::Case<cst::exp::Copattern> {
};
Ok(ast::Case {
span: Some(*span),
pattern: ast::Pattern { is_copattern: true, name, params },
pattern: ast::Pattern {
span: Some(pattern.span),
is_copattern: true,
name,
params,
},
body: body.lower(ctx)?,
})
})
Expand Down Expand Up @@ -543,6 +553,7 @@ impl Lower for cst::exp::Lam {
let case = cst::exp::Case {
span: *span,
pattern: cst::exp::Copattern {
span: *span,
name: Ident { span: *span, id: "ap".to_owned() },
params: vec![
cst::exp::BindingSite::Wildcard { span: Default::default() },
Expand Down
2 changes: 2 additions & 0 deletions lang/parser/src/cst/exp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ pub enum BindingSite {

#[derive(Debug, Clone)]
pub struct Pattern {
pub span: Span,
pub name: Ident,
pub params: Vec<BindingSite>,
}

#[derive(Debug, Clone)]
pub struct Copattern {
pub span: Span,
pub name: Ident,
pub params: Vec<BindingSite>,
}
Expand Down
4 changes: 2 additions & 2 deletions lang/parser/src/grammar/cst.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,11 @@ Let: Let = <l: @L> <doc: DocComment?> <attr: OptAttributes> "let" <name: Ident><


Pattern: Pattern = {
<name: Ident><params: OptTelescopeInst> => Pattern { name, params },
<l: @L><name: Ident><params: OptTelescopeInst><r: @R> => Pattern { span: span(l,r), name, params },
}

Copattern: Copattern = {
"." <name: Ident><params: OptTelescopeInst> => Copattern { name, params },
<l: @L> "." <name: Ident><params: OptTelescopeInst> <r: @R> => Copattern { span: span(l,r), name, params },
}

Case<P> : Case<P> = {
Expand Down
1 change: 1 addition & 0 deletions lang/transformations/src/lifting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ impl Lift for Case {
pattern.params.lift_telescope(ctx, |ctx, params| Case {
span: *span,
pattern: Pattern {
span: None,
is_copattern: pattern.is_copattern,
name: pattern.name.clone(),
params,
Expand Down
2 changes: 2 additions & 0 deletions lang/transformations/src/xfunc/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ impl XData {
body.map(|body| ast::Case {
span: None,
pattern: ast::Pattern {
span: None,
is_copattern: false,
name: IdBound {
span: None,
Expand Down Expand Up @@ -259,6 +260,7 @@ impl XData {
body.map(|body| ast::Case {
span: None,
pattern: ast::Pattern {
span: None,
is_copattern: true,
name: IdBound {
span: None,
Expand Down

0 comments on commit ca70ff0

Please sign in to comment.