rename: 'r' → 'expression' & 'seq_r' → 'sequence'
Better readability.
This commit is contained in:
parent
66296df9c3
commit
4f6defcf3e
|
@ -5,7 +5,7 @@ use crate::ast;
|
|||
use super::{error::ParseError, token::Token};
|
||||
|
||||
pub fn parser() -> impl Parser<Token, ast::Annotation, Error = ParseError> {
|
||||
recursive(|r| {
|
||||
recursive(|expression| {
|
||||
choice((
|
||||
// Type hole
|
||||
select! {Token::DiscardName { name } => name}.map_with_span(|name, span| {
|
||||
|
@ -15,7 +15,8 @@ pub fn parser() -> impl Parser<Token, ast::Annotation, Error = ParseError> {
|
|||
}
|
||||
}),
|
||||
// Tuple
|
||||
r.clone()
|
||||
expression
|
||||
.clone()
|
||||
.separated_by(just(Token::Comma))
|
||||
.at_least(2)
|
||||
.allow_trailing()
|
||||
|
@ -30,13 +31,14 @@ pub fn parser() -> impl Parser<Token, ast::Annotation, Error = ParseError> {
|
|||
// Function
|
||||
just(Token::Fn)
|
||||
.ignore_then(
|
||||
r.clone()
|
||||
expression
|
||||
.clone()
|
||||
.separated_by(just(Token::Comma))
|
||||
.allow_trailing()
|
||||
.delimited_by(just(Token::LeftParen), just(Token::RightParen)),
|
||||
)
|
||||
.then_ignore(just(Token::RArrow))
|
||||
.then(r.clone())
|
||||
.then(expression.clone())
|
||||
.map_with_span(|(arguments, ret), span| ast::Annotation::Fn {
|
||||
location: span,
|
||||
arguments,
|
||||
|
@ -45,7 +47,8 @@ pub fn parser() -> impl Parser<Token, ast::Annotation, Error = ParseError> {
|
|||
// Constructor function
|
||||
select! {Token::UpName { name } => name}
|
||||
.then(
|
||||
r.clone()
|
||||
expression
|
||||
.clone()
|
||||
.separated_by(just(Token::Comma))
|
||||
.allow_trailing()
|
||||
.delimited_by(just(Token::Less), just(Token::Greater))
|
||||
|
@ -63,7 +66,8 @@ pub fn parser() -> impl Parser<Token, ast::Annotation, Error = ParseError> {
|
|||
just(Token::Dot)
|
||||
.ignore_then(select! {Token::UpName {name} => name})
|
||||
.then(
|
||||
r.separated_by(just(Token::Comma))
|
||||
expression
|
||||
.separated_by(just(Token::Comma))
|
||||
.allow_trailing()
|
||||
.delimited_by(just(Token::Less), just(Token::Greater))
|
||||
.or_not(),
|
||||
|
|
|
@ -7,7 +7,7 @@ use crate::{
|
|||
};
|
||||
|
||||
pub fn parser(
|
||||
seq_r: Recursive<'_, Token, UntypedExpr, ParseError>,
|
||||
sequence: Recursive<'_, Token, UntypedExpr, ParseError>,
|
||||
) -> impl Parser<Token, UntypedExpr, Error = ParseError> + '_ {
|
||||
just(Token::Fn)
|
||||
.ignore_then(
|
||||
|
@ -17,7 +17,7 @@ pub fn parser(
|
|||
.delimited_by(just(Token::LeftParen), just(Token::RightParen)),
|
||||
)
|
||||
.then(just(Token::RArrow).ignore_then(annotation()).or_not())
|
||||
.then(seq_r.delimited_by(just(Token::LeftBrace), just(Token::RightBrace)))
|
||||
.then(sequence.delimited_by(just(Token::LeftBrace), just(Token::RightBrace)))
|
||||
.map_with_span(
|
||||
|((arguments, return_annotation), body), span| UntypedExpr::Fn {
|
||||
arguments,
|
||||
|
|
|
@ -6,13 +6,13 @@ use crate::{
|
|||
};
|
||||
|
||||
pub fn parser(
|
||||
seq_r: Recursive<'_, Token, UntypedExpr, ParseError>,
|
||||
sequence: Recursive<'_, Token, UntypedExpr, ParseError>,
|
||||
) -> impl Parser<Token, UntypedExpr, Error = ParseError> + '_ {
|
||||
choice((
|
||||
seq_r
|
||||
sequence
|
||||
.clone()
|
||||
.delimited_by(just(Token::LeftBrace), just(Token::RightBrace)),
|
||||
seq_r.clone().delimited_by(
|
||||
sequence.clone().delimited_by(
|
||||
choice((just(Token::LeftParen), just(Token::NewLineLeftParen))),
|
||||
just(Token::RightParen),
|
||||
),
|
||||
|
|
|
@ -9,31 +9,37 @@ use crate::{
|
|||
use super::block;
|
||||
|
||||
pub fn parser<'a>(
|
||||
seq_r: Recursive<'a, Token, UntypedExpr, ParseError>,
|
||||
r: Recursive<'a, Token, UntypedExpr, ParseError>,
|
||||
sequence: Recursive<'a, Token, UntypedExpr, ParseError>,
|
||||
expression: Recursive<'a, Token, UntypedExpr, ParseError>,
|
||||
) -> impl Parser<Token, UntypedExpr, Error = ParseError> + 'a {
|
||||
just(Token::If)
|
||||
.ignore_then(r.clone().then(block(seq_r.clone())).map_with_span(
|
||||
|(condition, body), span| ast::IfBranch {
|
||||
condition,
|
||||
body,
|
||||
location: span,
|
||||
},
|
||||
))
|
||||
.ignore_then(
|
||||
expression
|
||||
.clone()
|
||||
.then(block(sequence.clone()))
|
||||
.map_with_span(|(condition, body), span| ast::IfBranch {
|
||||
condition,
|
||||
body,
|
||||
location: span,
|
||||
}),
|
||||
)
|
||||
.then(
|
||||
just(Token::Else)
|
||||
.ignore_then(just(Token::If))
|
||||
.ignore_then(r.clone().then(block(seq_r.clone())).map_with_span(
|
||||
|(condition, body), span| ast::IfBranch {
|
||||
condition,
|
||||
body,
|
||||
location: span,
|
||||
},
|
||||
))
|
||||
.ignore_then(
|
||||
expression
|
||||
.clone()
|
||||
.then(block(sequence.clone()))
|
||||
.map_with_span(|(condition, body), span| ast::IfBranch {
|
||||
condition,
|
||||
body,
|
||||
location: span,
|
||||
}),
|
||||
)
|
||||
.repeated(),
|
||||
)
|
||||
.then_ignore(just(Token::Else))
|
||||
.then(block(seq_r))
|
||||
.then(block(sequence))
|
||||
.map_with_span(|((first, alternative_branches), final_else), span| {
|
||||
let mut branches = vec1::vec1![first];
|
||||
|
||||
|
|
|
@ -40,9 +40,9 @@ use crate::{
|
|||
use super::{error::ParseError, token::Token};
|
||||
|
||||
pub fn parser(
|
||||
seq_r: Recursive<'_, Token, UntypedExpr, ParseError>,
|
||||
sequence: Recursive<'_, Token, UntypedExpr, ParseError>,
|
||||
) -> impl Parser<Token, UntypedExpr, Error = ParseError> + '_ {
|
||||
recursive(|r| {
|
||||
recursive(|expression| {
|
||||
let field_access_constructor = select! {Token::Name { name } => name}
|
||||
.map_with_span(|module, span| (module, span))
|
||||
.then_ignore(just(Token::Dot))
|
||||
|
@ -59,20 +59,20 @@ pub fn parser(
|
|||
let expr_unit_parser = choice((
|
||||
string(),
|
||||
int(),
|
||||
record_update(r.clone()),
|
||||
record(r.clone()),
|
||||
record_update(expression.clone()),
|
||||
record(expression.clone()),
|
||||
field_access_constructor,
|
||||
var(),
|
||||
tuple(r.clone()),
|
||||
tuple(expression.clone()),
|
||||
bytearray(),
|
||||
list(r.clone()),
|
||||
anonymous_function(seq_r.clone()),
|
||||
list(expression.clone()),
|
||||
anonymous_function(sequence.clone()),
|
||||
anonymous_binop(),
|
||||
block(seq_r.clone()),
|
||||
when(r.clone()),
|
||||
assignment::let_(r.clone()),
|
||||
assignment::expect(r.clone()),
|
||||
if_else(seq_r, r.clone()),
|
||||
block(sequence.clone()),
|
||||
when(expression.clone()),
|
||||
assignment::let_(expression.clone()),
|
||||
assignment::expect(expression.clone()),
|
||||
if_else(sequence, expression.clone()),
|
||||
));
|
||||
|
||||
// Parsing a function call into the appropriate structure
|
||||
|
@ -118,7 +118,7 @@ pub fn parser(
|
|||
select! { Token::Name { name } => name }
|
||||
.then_ignore(just(Token::Colon))
|
||||
.or_not()
|
||||
.then(r)
|
||||
.then(expression)
|
||||
.map_with_span(|(label, value), span| {
|
||||
ParserArg::Arg(Box::new(ast::CallArg {
|
||||
label,
|
||||
|
|
|
@ -7,11 +7,11 @@ use crate::{
|
|||
};
|
||||
|
||||
pub fn parser() -> impl Parser<Token, UntypedExpr, Error = ParseError> {
|
||||
recursive(|r| {
|
||||
recursive(|expression| {
|
||||
choice((
|
||||
just(Token::Trace)
|
||||
.ignore_then(super::parser(r.clone()))
|
||||
.then(r.clone())
|
||||
.ignore_then(super::parser(expression.clone()))
|
||||
.then(expression.clone())
|
||||
.map_with_span(|(text, then_), span| UntypedExpr::Trace {
|
||||
kind: TraceKind::Trace,
|
||||
location: span,
|
||||
|
@ -19,17 +19,17 @@ pub fn parser() -> impl Parser<Token, UntypedExpr, Error = ParseError> {
|
|||
text: Box::new(super::string::flexible(text)),
|
||||
}),
|
||||
just(Token::ErrorTerm)
|
||||
.ignore_then(super::parser(r.clone()).or_not())
|
||||
.ignore_then(super::parser(expression.clone()).or_not())
|
||||
.map_with_span(|reason, span| {
|
||||
UntypedExpr::error(span, reason.map(super::string::flexible))
|
||||
}),
|
||||
just(Token::Todo)
|
||||
.ignore_then(super::parser(r.clone()).or_not())
|
||||
.ignore_then(super::parser(expression.clone()).or_not())
|
||||
.map_with_span(|reason, span| {
|
||||
UntypedExpr::todo(span, reason.map(super::string::flexible))
|
||||
}),
|
||||
super::parser(r.clone())
|
||||
.then(r.repeated())
|
||||
super::parser(expression.clone())
|
||||
.then(expression.repeated())
|
||||
.foldl(|current, next| current.append_in_sequence(next)),
|
||||
))
|
||||
})
|
||||
|
|
|
@ -6,7 +6,7 @@ use crate::{
|
|||
};
|
||||
|
||||
pub fn parser() -> impl Parser<Token, ast::UntypedClauseGuard, Error = ParseError> {
|
||||
recursive(|r| {
|
||||
recursive(|expression| {
|
||||
let var_parser = select! {
|
||||
Token::Name { name } => name,
|
||||
Token::UpName { name } => name,
|
||||
|
@ -19,7 +19,7 @@ pub fn parser() -> impl Parser<Token, ast::UntypedClauseGuard, Error = ParseErro
|
|||
|
||||
let constant_parser = definition::constant::value().map(ast::ClauseGuard::Constant);
|
||||
|
||||
let block_parser = r
|
||||
let block_parser = expression
|
||||
.clone()
|
||||
.delimited_by(just(Token::LeftParen), just(Token::RightParen));
|
||||
|
||||
|
|
|
@ -8,11 +8,11 @@ use super::{
|
|||
};
|
||||
|
||||
pub fn parser() -> impl Parser<Token, ast::UntypedPattern, Error = ParseError> {
|
||||
recursive(|r| {
|
||||
recursive(|expression| {
|
||||
let record_constructor_pattern_arg_parser = choice((
|
||||
select! {Token::Name {name} => name}
|
||||
.then_ignore(just(Token::Colon))
|
||||
.then(r.clone())
|
||||
.then(expression.clone())
|
||||
.map_with_span(|(name, pattern), span| ast::CallArg {
|
||||
location: span,
|
||||
label: Some(name),
|
||||
|
@ -37,7 +37,7 @@ pub fn parser() -> impl Parser<Token, ast::UntypedPattern, Error = ParseError> {
|
|||
)
|
||||
.delimited_by(just(Token::LeftBrace), just(Token::RightBrace));
|
||||
|
||||
let tuple_constructor_pattern_arg_parser = r
|
||||
let tuple_constructor_pattern_arg_parser = expression
|
||||
.clone()
|
||||
.map(|pattern| ast::CallArg {
|
||||
location: pattern.location(),
|
||||
|
@ -121,7 +121,8 @@ pub fn parser() -> impl Parser<Token, ast::UntypedPattern, Error = ParseError> {
|
|||
base,
|
||||
},
|
||||
),
|
||||
r.clone()
|
||||
expression
|
||||
.clone()
|
||||
.separated_by(just(Token::Comma))
|
||||
.allow_trailing()
|
||||
.delimited_by(
|
||||
|
@ -133,10 +134,13 @@ pub fn parser() -> impl Parser<Token, ast::UntypedPattern, Error = ParseError> {
|
|||
elems,
|
||||
}),
|
||||
just(Token::LeftSquare)
|
||||
.ignore_then(r.clone().separated_by(just(Token::Comma)))
|
||||
.ignore_then(expression.clone().separated_by(just(Token::Comma)))
|
||||
.then(choice((
|
||||
just(Token::Comma)
|
||||
.ignore_then(just(Token::DotDot).ignore_then(r.clone().or_not()).or_not()),
|
||||
just(Token::Comma).ignore_then(
|
||||
just(Token::DotDot)
|
||||
.ignore_then(expression.clone().or_not())
|
||||
.or_not(),
|
||||
),
|
||||
just(Token::Comma).ignored().or_not().map(|_| None),
|
||||
)))
|
||||
.then_ignore(just(Token::RightSquare))
|
||||
|
|
Loading…
Reference in New Issue