mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-24 20:42:29 +00:00
remove parse::ast::Def
This commit is contained in:
parent
7b308d9efe
commit
3dee90ced8
8 changed files with 38 additions and 243 deletions
|
@ -3,7 +3,6 @@
|
|||
#![allow(unused_imports)]
|
||||
#![allow(unused_variables)]
|
||||
use bumpalo::Bump;
|
||||
use roc_can::operator::desugar_def;
|
||||
use roc_collections::all::{default_hasher, ImMap, ImSet, MutMap, MutSet, SendMap};
|
||||
use roc_module::ident::Ident;
|
||||
use roc_module::ident::Lowercase;
|
||||
|
|
|
@ -16,7 +16,7 @@ use roc_collections::all::{default_hasher, ImMap, MutMap, MutSet, SendMap};
|
|||
use roc_error_macros::{todo_abilities, todo_opaques};
|
||||
use roc_module::ident::Lowercase;
|
||||
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_problem::can::{Problem, RuntimeError, ShadowKind};
|
||||
use roc_region::all::{Loc, Region};
|
||||
|
@ -124,13 +124,25 @@ pub enum PendingDef<'a> {
|
|||
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>(
|
||||
env: &mut Env<'a>,
|
||||
def: &'a ast::Def<'a>,
|
||||
def: &'a AstDef<'a>,
|
||||
scope: &mut Scope,
|
||||
pattern_type: PatternType,
|
||||
) -> Option<(Output, PendingDef<'a>)> {
|
||||
use roc_parse::ast::Def::*;
|
||||
use AstDef::*;
|
||||
|
||||
match def {
|
||||
Value(AstValueDef::Annotation(loc_pattern, loc_ann)) => {
|
||||
|
@ -824,8 +836,8 @@ pub fn canonicalize_defs<'a>(
|
|||
// once we've finished assembling the entire scope.
|
||||
for loc_def in loc_defs.defs() {
|
||||
let def = match loc_def {
|
||||
Ok(type_def) => roc_parse::ast::Def::Type(type_def.clone()),
|
||||
Err(value_def) => roc_parse::ast::Def::Value(value_def.clone()),
|
||||
Ok(type_def) => AstDef::Type(type_def.clone()),
|
||||
Err(value_def) => AstDef::Value(value_def.clone()),
|
||||
};
|
||||
|
||||
match to_pending_def(env, env.arena.alloc(def), &mut scope, pattern_type) {
|
||||
|
|
|
@ -89,90 +89,6 @@ pub fn toplevel_defs_to_defs2<'a>(
|
|||
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 {
|
||||
comments
|
||||
.iter()
|
||||
|
|
|
@ -6,7 +6,7 @@ use roc_module::called_via::BinOp::Pizza;
|
|||
use roc_module::called_via::{BinOp, CalledVia};
|
||||
use roc_module::ident::ModuleName;
|
||||
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};
|
||||
|
||||
// BinOp precedence logic adapted from Gluon by Markus Westerlind
|
||||
|
@ -63,16 +63,6 @@ fn new_op_call_expr<'a>(
|
|||
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> {
|
||||
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>) {
|
||||
for value_def in defs.value_defs.iter_mut() {
|
||||
*value_def = desugar_value_def(arena, arena.alloc(*value_def));
|
||||
|
|
|
@ -3,7 +3,7 @@ use crate::pattern::fmt_pattern;
|
|||
use crate::spaces::{fmt_spaces, INDENT};
|
||||
use crate::Buf;
|
||||
use roc_parse::ast::{
|
||||
AbilityMember, Def, Defs, Expr, ExtractSpaces, Pattern, TypeAnnotation, TypeDef, TypeHeader,
|
||||
AbilityMember, Defs, Expr, ExtractSpaces, Pattern, TypeAnnotation, TypeDef, TypeHeader,
|
||||
ValueDef,
|
||||
};
|
||||
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>(
|
||||
buf: &mut Buf<'buf>,
|
||||
condition: &'a Loc<Expr<'a>>,
|
||||
|
|
|
@ -3,7 +3,7 @@ use bumpalo::Bump;
|
|||
use roc_module::called_via::{BinOp, UnaryOp};
|
||||
use roc_parse::{
|
||||
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,
|
||||
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> {
|
||||
fn remove_spaces(&self, _arena: &'a Bump) -> Self {
|
||||
Has::Has
|
||||
|
|
|
@ -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
|
||||
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> {
|
||||
fn before(&'a self, spaces: &'a [CommentOrNewline<'a>]) -> Self {
|
||||
Has::SpaceBefore(self, spaces)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::ast::{
|
||||
AssignedField, Collection, CommentOrNewline, Def, Defs, Derived, Expr, ExtractSpaces, Has,
|
||||
Pattern, Spaceable, TypeAnnotation, TypeDef, TypeHeader, ValueDef,
|
||||
AssignedField, Collection, CommentOrNewline, Defs, Derived, Expr, ExtractSpaces, Has, Pattern,
|
||||
Spaceable, TypeAnnotation, TypeDef, TypeHeader, ValueDef,
|
||||
};
|
||||
use crate::blankspace::{
|
||||
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)
|
||||
.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) => {
|
||||
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 => {
|
||||
let (_, signature, state) =
|
||||
alias_signature_with_space_before(indented_more).parse(arena, state)?;
|
||||
|
@ -1029,7 +1031,9 @@ fn finish_parsing_alias_or_opaque<'a>(
|
|||
ann: signature,
|
||||
};
|
||||
|
||||
(Loc::at(def_region, Def::Type(def)), state)
|
||||
defs.push_type_def(def, def_region, &[], &[]);
|
||||
|
||||
state
|
||||
}
|
||||
|
||||
AliasOrOpaque::Opaque => {
|
||||
|
@ -1049,11 +1053,11 @@ fn finish_parsing_alias_or_opaque<'a>(
|
|||
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 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)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue