refactor type in parens

This commit is contained in:
Folkert 2021-02-11 20:27:21 +01:00
parent cc81c6e5ba
commit f6d3b4ed93
2 changed files with 37 additions and 9 deletions

View file

@ -370,6 +370,7 @@ pub type Col = u16;
pub enum Type<'a> { pub enum Type<'a> {
TRecord(TRecord<'a>, Row, Col), TRecord(TRecord<'a>, Row, Col),
TTagUnion(TTagUnion<'a>, Row, Col), TTagUnion(TTagUnion<'a>, Row, Col),
TInParens(TInParens<'a>, Row, Col),
/// ///
TStart(Row, Col), TStart(Row, Col),
TSpace(Row, Col), TSpace(Row, Col),
@ -416,6 +417,23 @@ pub enum TTagUnion<'a> {
IndentEnd(Row, Col), IndentEnd(Row, Col),
} }
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TInParens<'a> {
End(Row, Col),
Open(Row, Col),
///
Type(&'a Type<'a>, Row, Col),
// TODO REMOVE in favor of Type
Syntax(&'a SyntaxError<'a>, Row, Col),
///
Space(BadInputError, Row, Col),
///
IndentOpen(Row, Col),
IndentEnd(Row, Col),
}
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum ContextStack<'a> { pub enum ContextStack<'a> {
Cons(ContextItem, &'a ContextStack<'a>), Cons(ContextItem, &'a ContextStack<'a>),

View file

@ -1,5 +1,5 @@
use crate::ast::{AssignedField, Attempting, CommentOrNewline, Tag, TypeAnnotation}; use crate::ast::{AssignedField, Attempting, CommentOrNewline, Tag, TypeAnnotation};
use crate::blankspace::{space0_around, space0_before, space1, space1_before}; use crate::blankspace::{space0_around, space0_around_e, space0_before, space1, space1_before};
use crate::expr::{global_tag, private_tag}; use crate::expr::{global_tag, private_tag};
use crate::ident::join_module_parts; use crate::ident::join_module_parts;
use crate::keyword; use crate::keyword;
@ -7,7 +7,7 @@ use crate::parser::{
allocated, ascii_char, ascii_string, not, optional, peek_utf8_char, specialize, specialize_ref, allocated, ascii_char, ascii_string, not, optional, peek_utf8_char, specialize, specialize_ref,
unexpected, word1, BadInputError, Either, ParseResult, Parser, unexpected, word1, BadInputError, Either, ParseResult, Parser,
Progress::{self, *}, Progress::{self, *},
State, SyntaxError, TRecord, TTagUnion, Type, State, SyntaxError, TInParens, TRecord, TTagUnion, Type,
}; };
use bumpalo::collections::string::String; use bumpalo::collections::string::String;
use bumpalo::collections::vec::Vec; use bumpalo::collections::vec::Vec;
@ -67,7 +67,7 @@ pub fn term<'a>(min_indent: u16) -> impl Parser<'a, Located<TypeAnnotation<'a>>,
and!( and!(
one_of!( one_of!(
loc_wildcard(), loc_wildcard(),
loc_parenthetical_type(min_indent), loc_type_in_parens(min_indent),
loc!(record_type(min_indent)), loc!(record_type(min_indent)),
loc!(tag_union_type(min_indent)), loc!(tag_union_type(min_indent)),
loc!(applied_type(min_indent)), loc!(applied_type(min_indent)),
@ -127,7 +127,7 @@ fn loc_applied_arg<'a>(
space1_before( space1_before(
one_of!( one_of!(
loc_wildcard(), loc_wildcard(),
loc_parenthetical_type(min_indent), loc_type_in_parens(min_indent),
loc!(record_type(min_indent)), loc!(record_type(min_indent)),
loc!(tag_union_type(min_indent)), loc!(tag_union_type(min_indent)),
loc!(parse_concrete_type), loc!(parse_concrete_type),
@ -145,16 +145,26 @@ fn loc_applied_args<'a>(
} }
#[inline(always)] #[inline(always)]
fn loc_parenthetical_type<'a>( fn loc_type_in_parens<'a>(
min_indent: u16, min_indent: u16,
) -> impl Parser<'a, Located<TypeAnnotation<'a>>, SyntaxError<'a>> { ) -> impl Parser<'a, Located<TypeAnnotation<'a>>, SyntaxError<'a>> {
let f = |x, row, col| SyntaxError::Type(Type::TInParens(x, row, col));
specialize(f, loc_type_in_parens_help(min_indent))
}
fn loc_type_in_parens_help<'a>(
min_indent: u16,
) -> impl Parser<'a, Located<TypeAnnotation<'a>>, TInParens<'a>> {
between!( between!(
ascii_char(b'('), word1(b'(', TInParens::Open),
space0_around( space0_around_e(
move |arena, state| expression(min_indent).parse(arena, state), move |arena, state| specialize_ref(TInParens::Syntax, expression(min_indent))
.parse(arena, state),
min_indent, min_indent,
TInParens::Space,
TInParens::IndentEnd,
), ),
ascii_char(b')') word1(b')', TInParens::End)
) )
} }