remove parse::ast::Def

This commit is contained in:
Folkert 2022-07-10 01:10:37 +02:00
parent 7b308d9efe
commit 3dee90ced8
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
8 changed files with 38 additions and 243 deletions

View file

@ -3,7 +3,6 @@
#![allow(unused_imports)] #![allow(unused_imports)]
#![allow(unused_variables)] #![allow(unused_variables)]
use bumpalo::Bump; use bumpalo::Bump;
use roc_can::operator::desugar_def;
use roc_collections::all::{default_hasher, ImMap, ImSet, MutMap, MutSet, SendMap}; use roc_collections::all::{default_hasher, ImMap, ImSet, MutMap, MutSet, SendMap};
use roc_module::ident::Ident; use roc_module::ident::Ident;
use roc_module::ident::Lowercase; use roc_module::ident::Lowercase;

View file

@ -16,7 +16,7 @@ use roc_collections::all::{default_hasher, ImMap, MutMap, MutSet, SendMap};
use roc_error_macros::{todo_abilities, todo_opaques}; use roc_error_macros::{todo_abilities, todo_opaques};
use roc_module::ident::Lowercase; use roc_module::ident::Lowercase;
use roc_module::symbol::Symbol; use roc_module::symbol::Symbol;
use roc_parse::ast::{self, Defs, TypeDef, TypeHeader, ValueDef as AstValueDef}; use roc_parse::ast::{self, CommentOrNewline, Defs, TypeDef, TypeHeader, ValueDef as AstValueDef};
use roc_parse::pattern::PatternType; use roc_parse::pattern::PatternType;
use roc_problem::can::{Problem, RuntimeError, ShadowKind}; use roc_problem::can::{Problem, RuntimeError, ShadowKind};
use roc_region::all::{Loc, Region}; use roc_region::all::{Loc, Region};
@ -124,13 +124,25 @@ pub enum PendingDef<'a> {
InvalidAlias, InvalidAlias,
} }
pub enum AstDef<'a> {
Type(TypeDef<'a>),
Value(AstValueDef<'a>),
// Blank Space (e.g. comments, spaces, newlines) before or after a def.
// We preserve this for the formatter; canonicalization ignores it.
SpaceBefore(&'a AstDef<'a>, &'a [CommentOrNewline<'a>]),
SpaceAfter(&'a AstDef<'a>, &'a [CommentOrNewline<'a>]),
NotYetImplemented(&'static str),
}
fn to_pending_def<'a>( fn to_pending_def<'a>(
env: &mut Env<'a>, env: &mut Env<'a>,
def: &'a ast::Def<'a>, def: &'a AstDef<'a>,
scope: &mut Scope, scope: &mut Scope,
pattern_type: PatternType, pattern_type: PatternType,
) -> Option<(Output, PendingDef<'a>)> { ) -> Option<(Output, PendingDef<'a>)> {
use roc_parse::ast::Def::*; use AstDef::*;
match def { match def {
Value(AstValueDef::Annotation(loc_pattern, loc_ann)) => { Value(AstValueDef::Annotation(loc_pattern, loc_ann)) => {
@ -824,8 +836,8 @@ pub fn canonicalize_defs<'a>(
// once we've finished assembling the entire scope. // once we've finished assembling the entire scope.
for loc_def in loc_defs.defs() { for loc_def in loc_defs.defs() {
let def = match loc_def { let def = match loc_def {
Ok(type_def) => roc_parse::ast::Def::Type(type_def.clone()), Ok(type_def) => AstDef::Type(type_def.clone()),
Err(value_def) => roc_parse::ast::Def::Value(value_def.clone()), Err(value_def) => AstDef::Value(value_def.clone()),
}; };
match to_pending_def(env, env.arena.alloc(def), &mut scope, pattern_type) { match to_pending_def(env, env.arena.alloc(def), &mut scope, pattern_type) {

View file

@ -89,90 +89,6 @@ pub fn toplevel_defs_to_defs2<'a>(
result result
} }
pub fn def_to_def2<'a>(
arena: &'a Bump,
env: &mut Env<'a>,
scope: &mut Scope,
parsed_def: &'a roc_parse::ast::Def<'a>,
region: Region,
) -> Def2 {
use roc_parse::ast::Def::*;
//dbg!(parsed_def);
match parsed_def {
SpaceBefore(inner_def, comments) => {
// filter comments
if !comments.is_empty() && !all_newlines(comments) {
let inner_def = def_to_def2(arena, env, scope, inner_def, region);
let inner_def_id = env.pool.add(inner_def);
let mut all_comments_str = String::new();
for comment in comments.iter().filter(|c_or_nl| !c_or_nl.is_newline()) {
all_comments_str.push_str(&comment.to_string_repr());
}
Def2::CommentsBefore {
comments: all_comments_str,
def_id: inner_def_id,
}
} else {
def_to_def2(arena, env, scope, inner_def, region)
}
}
SpaceAfter(inner_def, comments) => {
// filter comments
if !comments.is_empty() && !all_newlines(comments) {
let inner_def = def_to_def2(arena, env, scope, inner_def, region);
let inner_def_id = env.pool.add(inner_def);
let mut all_comments_str = String::new();
for comment in comments.iter().filter(|c_or_nl| !c_or_nl.is_newline()) {
all_comments_str.push_str(&comment.to_string_repr());
}
Def2::CommentsAfter {
def_id: inner_def_id,
comments: all_comments_str,
}
} else {
def_to_def2(arena, env, scope, inner_def, region)
}
}
Value(roc_parse::ast::ValueDef::Body(&loc_pattern, &loc_expr)) => {
let expr2 = loc_expr_to_expr2(arena, loc_expr, env, scope, region).0;
let expr_id = env.pool.add(expr2);
use roc_parse::ast::Pattern::*;
match loc_pattern.value {
Identifier(id_str) => {
let identifier_id = env.ident_ids.get_or_insert(id_str);
// TODO support with annotation
Def2::ValueDef {
identifier_id,
expr_id,
}
}
other => {
unimplemented!(
"I don't yet know how to convert the pattern {:?} into an expr2",
other
)
}
}
}
other => {
unimplemented!(
"I don't know how to make an expr2 from this def yet: {:?}",
other
)
}
}
}
fn all_newlines(comments: &[CommentOrNewline]) -> bool { fn all_newlines(comments: &[CommentOrNewline]) -> bool {
comments comments
.iter() .iter()

View file

@ -6,7 +6,7 @@ use roc_module::called_via::BinOp::Pizza;
use roc_module::called_via::{BinOp, CalledVia}; use roc_module::called_via::{BinOp, CalledVia};
use roc_module::ident::ModuleName; use roc_module::ident::ModuleName;
use roc_parse::ast::Expr::{self, *}; use roc_parse::ast::Expr::{self, *};
use roc_parse::ast::{AssignedField, Def, TypeDef, ValueDef, WhenBranch}; use roc_parse::ast::{AssignedField, ValueDef, WhenBranch};
use roc_region::all::{Loc, Region}; use roc_region::all::{Loc, Region};
// BinOp precedence logic adapted from Gluon by Markus Westerlind // BinOp precedence logic adapted from Gluon by Markus Westerlind
@ -63,16 +63,6 @@ fn new_op_call_expr<'a>(
Loc { region, value } Loc { region, value }
} }
fn desugar_type_def<'a>(def: &'a TypeDef<'a>) -> TypeDef<'a> {
use TypeDef::*;
match def {
alias @ Alias { .. } => *alias,
opaque @ Opaque { .. } => *opaque,
ability @ Ability { .. } => *ability,
}
}
fn desugar_value_def<'a>(arena: &'a Bump, def: &'a ValueDef<'a>) -> ValueDef<'a> { fn desugar_value_def<'a>(arena: &'a Bump, def: &'a ValueDef<'a>) -> ValueDef<'a> {
use ValueDef::*; use ValueDef::*;
@ -99,17 +89,6 @@ fn desugar_value_def<'a>(arena: &'a Bump, def: &'a ValueDef<'a>) -> ValueDef<'a>
} }
} }
pub fn desugar_def<'a>(arena: &'a Bump, def: &'a Def<'a>) -> Def<'a> {
use roc_parse::ast::Def::*;
match def {
Type(type_def) => Type(desugar_type_def(type_def)),
Value(value_def) => Value(desugar_value_def(arena, value_def)),
SpaceBefore(def, _) | SpaceAfter(def, _) => desugar_def(arena, def),
NotYetImplemented(s) => todo!("{}", s),
}
}
pub fn desugar_defs<'a>(arena: &'a Bump, defs: &mut roc_parse::ast::Defs<'a>) { pub fn desugar_defs<'a>(arena: &'a Bump, defs: &mut roc_parse::ast::Defs<'a>) {
for value_def in defs.value_defs.iter_mut() { for value_def in defs.value_defs.iter_mut() {
*value_def = desugar_value_def(arena, arena.alloc(*value_def)); *value_def = desugar_value_def(arena, arena.alloc(*value_def));

View file

@ -3,7 +3,7 @@ use crate::pattern::fmt_pattern;
use crate::spaces::{fmt_spaces, INDENT}; use crate::spaces::{fmt_spaces, INDENT};
use crate::Buf; use crate::Buf;
use roc_parse::ast::{ use roc_parse::ast::{
AbilityMember, Def, Defs, Expr, ExtractSpaces, Pattern, TypeAnnotation, TypeDef, TypeHeader, AbilityMember, Defs, Expr, ExtractSpaces, Pattern, TypeAnnotation, TypeDef, TypeHeader,
ValueDef, ValueDef,
}; };
use roc_region::all::Loc; use roc_region::all::Loc;
@ -284,46 +284,6 @@ impl<'a> Formattable for ValueDef<'a> {
} }
} }
impl<'a> Formattable for Def<'a> {
fn is_multiline(&self) -> bool {
use roc_parse::ast::Def::*;
match self {
Type(def) => def.is_multiline(),
Value(def) => def.is_multiline(),
SpaceBefore(sub_def, spaces) | SpaceAfter(sub_def, spaces) => {
spaces.iter().any(|s| s.is_comment()) || sub_def.is_multiline()
}
NotYetImplemented(s) => todo!("{}", s),
}
}
fn format_with_options<'buf>(
&self,
buf: &mut Buf<'buf>,
parens: Parens,
newlines: Newlines,
indent: u16,
) {
use roc_parse::ast::Def::*;
match self {
Type(def) => def.format_with_options(buf, parens, newlines, indent),
Value(def) => def.format_with_options(buf, parens, newlines, indent),
SpaceBefore(sub_def, spaces) => {
fmt_spaces(buf, spaces.iter(), indent);
sub_def.format(buf, indent);
}
SpaceAfter(sub_def, spaces) => {
sub_def.format(buf, indent);
fmt_spaces(buf, spaces.iter(), indent);
}
NotYetImplemented(s) => todo!("{}", s),
}
}
}
fn fmt_expect<'a, 'buf>( fn fmt_expect<'a, 'buf>(
buf: &mut Buf<'buf>, buf: &mut Buf<'buf>,
condition: &'a Loc<Expr<'a>>, condition: &'a Loc<Expr<'a>>,

View file

@ -3,7 +3,7 @@ use bumpalo::Bump;
use roc_module::called_via::{BinOp, UnaryOp}; use roc_module::called_via::{BinOp, UnaryOp};
use roc_parse::{ use roc_parse::{
ast::{ ast::{
AbilityMember, AssignedField, Collection, CommentOrNewline, Def, Defs, Derived, Expr, Has, AbilityMember, AssignedField, Collection, CommentOrNewline, Defs, Derived, Expr, Has,
HasClause, Module, Pattern, Spaced, StrLiteral, StrSegment, Tag, TypeAnnotation, TypeDef, HasClause, Module, Pattern, Spaced, StrLiteral, StrSegment, Tag, TypeAnnotation, TypeDef,
TypeHeader, ValueDef, WhenBranch, TypeHeader, ValueDef, WhenBranch,
}, },
@ -545,17 +545,6 @@ impl<'a> RemoveSpaces<'a> for ValueDef<'a> {
} }
} }
impl<'a> RemoveSpaces<'a> for Def<'a> {
fn remove_spaces(&self, arena: &'a Bump) -> Self {
match *self {
Def::Type(def) => Def::Type(def.remove_spaces(arena)),
Def::Value(def) => Def::Value(def.remove_spaces(arena)),
Def::NotYetImplemented(a) => Def::NotYetImplemented(a),
Def::SpaceBefore(a, _) | Def::SpaceAfter(a, _) => a.remove_spaces(arena),
}
}
}
impl<'a> RemoveSpaces<'a> for Has<'a> { impl<'a> RemoveSpaces<'a> for Has<'a> {
fn remove_spaces(&self, _arena: &'a Bump) -> Self { fn remove_spaces(&self, _arena: &'a Bump) -> Self {
Has::Has Has::Has

View file

@ -425,54 +425,6 @@ impl<'a> Defs<'a> {
} }
} }
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Def<'a> {
Type(TypeDef<'a>),
Value(ValueDef<'a>),
// Blank Space (e.g. comments, spaces, newlines) before or after a def.
// We preserve this for the formatter; canonicalization ignores it.
SpaceBefore(&'a Def<'a>, &'a [CommentOrNewline<'a>]),
SpaceAfter(&'a Def<'a>, &'a [CommentOrNewline<'a>]),
NotYetImplemented(&'static str),
}
impl<'a> Def<'a> {
pub fn unroll_spaces_before(&self) -> (&'a [CommentOrNewline<'a>], &Def) {
let (spaces, def): (&'a [_], &Def) = match self {
Def::SpaceBefore(def, spaces) => (spaces, def),
def => (&[], def),
};
debug_assert!(!matches!(def, Def::SpaceBefore(_, _)));
(spaces, def)
}
pub fn unroll_def(&self) -> Result<&TypeDef<'a>, &ValueDef<'a>> {
let mut def = self;
loop {
match def {
Def::Type(type_def) => return Ok(type_def),
Def::Value(value_def) => return Err(value_def),
Def::SpaceBefore(def1, _) | Def::SpaceAfter(def1, _) => def = def1,
Def::NotYetImplemented(s) => todo!("{}", s),
}
}
}
}
impl<'a> From<TypeDef<'a>> for Def<'a> {
fn from(def: TypeDef<'a>) -> Self {
Self::Type(def)
}
}
impl<'a> From<ValueDef<'a>> for Def<'a> {
fn from(def: ValueDef<'a>) -> Self {
Self::Value(def)
}
}
/// Should always be a zero-argument `Apply`; we'll check this in canonicalization /// Should always be a zero-argument `Apply`; we'll check this in canonicalization
pub type AbilityName<'a> = Loc<TypeAnnotation<'a>>; pub type AbilityName<'a> = Loc<TypeAnnotation<'a>>;
@ -1006,15 +958,6 @@ impl<'a> Spaceable<'a> for Tag<'a> {
} }
} }
impl<'a> Spaceable<'a> for Def<'a> {
fn before(&'a self, spaces: &'a [CommentOrNewline<'a>]) -> Self {
Def::SpaceBefore(self, spaces)
}
fn after(&'a self, spaces: &'a [CommentOrNewline<'a>]) -> Self {
Def::SpaceAfter(self, spaces)
}
}
impl<'a> Spaceable<'a> for Has<'a> { impl<'a> Spaceable<'a> for Has<'a> {
fn before(&'a self, spaces: &'a [CommentOrNewline<'a>]) -> Self { fn before(&'a self, spaces: &'a [CommentOrNewline<'a>]) -> Self {
Has::SpaceBefore(self, spaces) Has::SpaceBefore(self, spaces)

View file

@ -1,6 +1,6 @@
use crate::ast::{ use crate::ast::{
AssignedField, Collection, CommentOrNewline, Def, Defs, Derived, Expr, ExtractSpaces, Has, AssignedField, Collection, CommentOrNewline, Defs, Derived, Expr, ExtractSpaces, Has, Pattern,
Pattern, Spaceable, TypeAnnotation, TypeDef, TypeHeader, ValueDef, Spaceable, TypeAnnotation, TypeDef, TypeHeader, ValueDef,
}; };
use crate::blankspace::{ use crate::blankspace::{
space0_after_e, space0_around_ee, space0_before_e, space0_before_optional_after, space0_e, space0_after_e, space0_around_ee, space0_before_e, space0_before_optional_after, space0_e,
@ -999,7 +999,9 @@ fn finish_parsing_alias_or_opaque<'a>(
.validate_is_type_def(arena, loc_op, kind) .validate_is_type_def(arena, loc_op, kind)
.map_err(|fail| (MadeProgress, fail, state.clone()))?; .map_err(|fail| (MadeProgress, fail, state.clone()))?;
let (loc_def, state) = match &expr.value { let mut defs = Defs::default();
let state = match &expr.value {
Expr::Tag(name) => { Expr::Tag(name) => {
let mut type_arguments = Vec::with_capacity_in(arguments.len(), arena); let mut type_arguments = Vec::with_capacity_in(arguments.len(), arena);
@ -1012,7 +1014,7 @@ fn finish_parsing_alias_or_opaque<'a>(
} }
} }
let (loc_def, state) = match kind { match kind {
AliasOrOpaque::Alias => { AliasOrOpaque::Alias => {
let (_, signature, state) = let (_, signature, state) =
alias_signature_with_space_before(indented_more).parse(arena, state)?; alias_signature_with_space_before(indented_more).parse(arena, state)?;
@ -1029,7 +1031,9 @@ fn finish_parsing_alias_or_opaque<'a>(
ann: signature, ann: signature,
}; };
(Loc::at(def_region, Def::Type(def)), state) defs.push_type_def(def, def_region, &[], &[]);
state
} }
AliasOrOpaque::Opaque => { AliasOrOpaque::Opaque => {
@ -1049,11 +1053,11 @@ fn finish_parsing_alias_or_opaque<'a>(
derived, derived,
}; };
(Loc::at(def_region, Def::Type(def)), state) defs.push_type_def(def, def_region, &[], &[]);
}
};
(&*arena.alloc(loc_def), state) state
}
}
} }
_ => { _ => {
@ -1082,9 +1086,12 @@ fn finish_parsing_alias_or_opaque<'a>(
let def_region = Region::span_across(&call.region, &ann_type.region); let def_region = Region::span_across(&call.region, &ann_type.region);
let alias = ValueDef::Annotation(Loc::at(expr_region, good), ann_type); let value_def =
ValueDef::Annotation(Loc::at(expr_region, good), ann_type);
(&*arena.alloc(Loc::at(def_region, alias.into())), state) defs.push_value_def(value_def, def_region, &[], &[]);
state
} }
} }
} }
@ -1102,16 +1109,6 @@ fn finish_parsing_alias_or_opaque<'a>(
} }
}; };
let mut defs = Defs::default();
match loc_def.value {
Def::Type(type_def) => defs.push_type_def(type_def, loc_def.region, &[], &[]),
Def::Value(value_def) => defs.push_value_def(value_def, loc_def.region, &[], &[]),
Def::SpaceBefore(_, _) => todo!(),
Def::SpaceAfter(_, _) => todo!(),
Def::NotYetImplemented(_) => todo!(),
}
parse_defs_expr(options, start_column, defs, arena, state) parse_defs_expr(options, start_column, defs, arena, state)
} }