feat: finish expr_seq_parser

This commit is contained in:
rvcas 2022-08-17 22:34:26 -04:00
parent 3bc507c9e8
commit 59a9bac9b5
No known key found for this signature in database
GPG Key ID: C09B64E263F7D68C
2 changed files with 80 additions and 15 deletions

View File

@ -268,3 +268,57 @@ pub enum UntypedExpr {
value: Box<Self>, value: Box<Self>,
}, },
} }
impl UntypedExpr {
pub fn append_in_sequence(self, next: Self) -> Self {
let location = Span {
start: self.location().start,
end: next.location().end,
..self.location()
};
match self {
Self::Sequence {
mut expressions, ..
} => {
expressions.push(next);
Self::Sequence {
location,
expressions,
}
}
_ => Self::Sequence {
location,
expressions: vec![self, next],
},
}
}
pub fn location(&self) -> Span {
match self {
Self::Try { then, .. } => then.location(),
Self::PipeLine { expressions, .. } => expressions.last().location(),
Self::Fn { location, .. }
| Self::Var { location, .. }
| Self::Int { location, .. }
| Self::Todo { location, .. }
| Self::Case { location, .. }
| Self::Call { location, .. }
| Self::List { location, .. }
| Self::Float { location, .. }
| Self::BinOp { location, .. }
| Self::Tuple { location, .. }
| Self::String { location, .. }
| Self::Assignment { location, .. }
| Self::TupleIndex { location, .. }
| Self::FieldAccess { location, .. }
| Self::RecordUpdate { location, .. }
| Self::Negate { location, .. } => *location,
Self::Sequence {
location,
expressions,
..
} => expressions.last().map(Self::location).unwrap_or(*location),
}
}
}

View File

@ -227,12 +227,13 @@ pub fn fn_param_parser() -> impl Parser<Token, ast::UntypedArg, Error = ParseErr
pub fn expr_seq_parser() -> impl Parser<Token, expr::UntypedExpr, Error = ParseError> { pub fn expr_seq_parser() -> impl Parser<Token, expr::UntypedExpr, Error = ParseError> {
recursive(|r| { recursive(|r| {
choice((just(Token::Try) choice((
just(Token::Try)
.ignore_then(pattern_parser()) .ignore_then(pattern_parser())
.then(just(Token::Colon).ignore_then(type_parser()).or_not()) .then(just(Token::Colon).ignore_then(type_parser()).or_not())
.then_ignore(just(Token::Equal)) .then_ignore(just(Token::Equal))
.then(expr_parser()) .then(expr_parser())
.then(r) .then(r.clone())
.map_with_span(|(((pattern, annotation), value), then_), span| { .map_with_span(|(((pattern, annotation), value), then_), span| {
expr::UntypedExpr::Try { expr::UntypedExpr::Try {
location: span, location: span,
@ -241,12 +242,22 @@ pub fn expr_seq_parser() -> impl Parser<Token, expr::UntypedExpr, Error = ParseE
then: Box::new(then_), then: Box::new(then_),
annotation, annotation,
} }
}),)) }),
expr_parser()
.then(r.repeated())
.map_with_span(|(expr, exprs), span| {
exprs
.into_iter()
.fold(expr, |acc, elem| acc.append_in_sequence(elem))
}),
))
}) })
} }
pub fn expr_parser() -> impl Parser<Token, expr::UntypedExpr, Error = ParseError> {} pub fn expr_parser() -> impl Parser<Token, expr::UntypedExpr, Error = ParseError> {}
pub fn expr_unit_parser() -> impl Parser<Token, expr::UntypedExpr, Error = ParseError> {}
pub fn type_parser() -> impl Parser<Token, ast::Annotation, Error = ParseError> { pub fn type_parser() -> impl Parser<Token, ast::Annotation, Error = ParseError> {
recursive(|r| { recursive(|r| {
choice(( choice((