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