use chumsky::{input::MapExtra, prelude::*};
use crate::parsing::{
Span,
ast::{
AccessExpression, ContractDefinitionContent, GenericList, IdentName, ParsingState,
ValueExpression, access_expression_parser, ident_name_parser, value_expression_parser,
},
lexer::{Token, TokensData},
};
#[derive(Debug, Clone)]
pub struct Expression {
pub span: Span,
pub kind: ExpressionKind,
}
#[derive(Debug, Clone)]
pub enum ExpressionKind {
Source(ExpressionSource),
Unary {
operator: UnaryExpressionOperator,
expression: Box<Expression>,
},
Binary {
left: Box<Expression>,
operator: BinaryExpressionOperator,
right: Box<Expression>,
},
}
#[derive(Debug, Clone)]
pub enum ExpressionSource {
Value(Box<ValueExpression>),
Access(Box<AccessExpression>),
Structure(Box<StructureExpression>),
Match(Box<MatchExpression>),
}
#[derive(Debug, Clone)]
pub struct MatchExpression {
pub span: Span,
pub value: Box<Expression>,
pub arms: Vec<MatchExpressionArm>,
}
#[derive(Debug, Clone)]
pub struct MatchExpressionArm {
pub span: Span,
pub pattern: EnumVariantPattern,
pub expression: Expression,
}
#[derive(Debug, Clone)]
pub struct EnumVariantPattern {
pub span: Span,
pub path: Vec<IdentName>,
pub binding: Option<IdentName>,
}
#[derive(Debug, Clone)]
pub struct StructureExpression {
pub span: Span,
pub type_path: AccessExpression,
pub fields: Vec<StructureExpressionField>,
}
#[derive(Debug, Clone)]
pub struct StructureExpressionField {
pub span: Span,
pub name: IdentName,
pub expression: Expression,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct UnaryExpressionOperator {
pub span: Span,
pub kind: UnaryExpressionOperatorKind,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnaryExpressionOperatorKind {
Negate,
Not,
Dereference,
Borrow,
BorrowMut,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BinaryExpressionOperator {
pub span: Span,
pub kind: BinaryExpressionOperatorKind,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinaryExpressionOperatorKind {
Multiply,
Divide,
Add,
Subtract,
ShiftLeft,
ShiftRight,
Less,
LessOrEqual,
Greater,
GreaterOrEqual,
Equal,
NotEqual,
}
pub(crate) fn expression_parser<'src, I, GLP, CDCP>(
tokens_data: &'src TokensData,
state: &'src ParsingState,
generic_list_parser: GLP,
contract_definition_content_parser: CDCP,
) -> impl Parser<'src, I, Expression, extra::Err<Rich<'src, Token, Span>>> + Clone
where
I: Input<'src, Token = Token, Span = Span>,
GLP: Parser<'src, I, GenericList, extra::Err<Rich<'src, Token, Span>>> + Clone + 'src,
CDCP: Parser<'src, I, ContractDefinitionContent, extra::Err<Rich<'src, Token, Span>>>
+ Clone
+ 'src,
{
recursive(|expression| {
let source = expression_source_parser(
tokens_data,
state,
expression.clone(),
generic_list_parser,
contract_definition_content_parser,
)
.map_with(|source, extra| Expression {
span: extra.span(),
kind: ExpressionKind::Source(source),
});
let primary = source.or(expression
.clone()
.delimited_by(just(Token::OpenParen), just(Token::CloseParen)));
let unary = unary_expression_operator_parser()
.repeated()
.collect::<Vec<_>>()
.then(primary)
.map(|(operators, expression)| {
operators
.into_iter()
.rev()
.fold(expression, |expression, operator| Expression {
span: Span::new(operator.span.start, expression.span.end),
kind: ExpressionKind::Unary {
operator,
expression: Box::new(expression),
},
})
});
let multiplicative = unary.clone().foldl(
multiplicative_operator_parser().then(unary).repeated(),
binary_expression,
);
let additive = multiplicative.clone().foldl(
additive_operator_parser().then(multiplicative).repeated(),
binary_expression,
);
let shift = additive.clone().foldl(
shift_operator_parser().then(additive).repeated(),
binary_expression,
);
let comparison = shift.clone().foldl(
comparison_operator_parser().then(shift).repeated(),
binary_expression,
);
comparison.clone().foldl(
equality_operator_parser().then(comparison).repeated(),
binary_expression,
)
})
}
fn binary_expression(
left: Expression,
(operator, right): (BinaryExpressionOperator, Expression),
) -> Expression {
Expression {
span: Span::new(left.span.start, right.span.end),
kind: ExpressionKind::Binary {
left: Box::new(left),
operator,
right: Box::new(right),
},
}
}
pub(crate) fn expression_source_parser<'src, I, EP, GLP, CDCP>(
tokens_data: &'src TokensData,
state: &'src ParsingState,
expression_parser: EP,
generic_list_parser: GLP,
contract_definition_content_parser: CDCP,
) -> impl Parser<'src, I, ExpressionSource, extra::Err<Rich<'src, Token, Span>>> + Clone
where
I: Input<'src, Token = Token, Span = Span>,
EP: Parser<'src, I, Expression, extra::Err<Rich<'src, Token, Span>>> + Clone + 'src,
GLP: Parser<'src, I, GenericList, extra::Err<Rich<'src, Token, Span>>> + Clone + 'src,
CDCP: Parser<'src, I, ContractDefinitionContent, extra::Err<Rich<'src, Token, Span>>>
+ Clone
+ 'src,
{
let access = access_expression_parser(
tokens_data,
state,
expression_parser.clone(),
generic_list_parser,
contract_definition_content_parser.clone(),
);
let pattern = ident_name_parser(tokens_data, state)
.separated_by(just(Token::Dot))
.at_least(2)
.collect::<Vec<_>>()
.then(
ident_name_parser(tokens_data, state)
.delimited_by(just(Token::OpenParen), just(Token::CloseParen))
.or_not(),
)
.map_with(|(path, binding), extra| EnumVariantPattern {
span: extra.span(),
path,
binding,
});
let arm = pattern
.then_ignore(just(Token::FatArrow))
.then(expression_parser.clone())
.then_ignore(just(Token::Comma).or_not())
.map_with(|(pattern, expression), extra| MatchExpressionArm {
span: extra.span(),
pattern,
expression,
});
let match_expression = just(Token::MatchKw)
.ignore_then(expression_parser.clone())
.then(
arm.repeated()
.at_least(1)
.collect::<Vec<_>>()
.delimited_by(just(Token::OpenBrace), just(Token::CloseBrace)),
)
.map_with(|(value, arms), extra| {
ExpressionSource::Match(Box::new(MatchExpression {
span: extra.span(),
value: Box::new(value),
arms,
}))
});
let field = ident_name_parser(tokens_data, state)
.then_ignore(just(Token::Colon))
.then(expression_parser.clone())
.map_with(|(name, expression), extra| StructureExpressionField {
span: extra.span(),
name,
expression,
});
let structure = access
.clone()
.then(
field
.then_ignore(just(Token::Comma).or_not())
.repeated()
.collect::<Vec<_>>()
.delimited_by(just(Token::OpenBrace), just(Token::CloseBrace)),
)
.map_with(|(type_path, fields), extra| {
ExpressionSource::Structure(Box::new(StructureExpression {
span: extra.span(),
type_path,
fields,
}))
});
match_expression
.or(structure)
.or(access.map(|access| ExpressionSource::Access(Box::new(access))))
.or(value_expression_parser(
tokens_data,
state,
expression_parser,
contract_definition_content_parser,
)
.map(|value| ExpressionSource::Value(Box::new(value))))
}
pub(crate) fn unary_expression_operator_parser<'src, I>()
-> impl Parser<'src, I, UnaryExpressionOperator, extra::Err<Rich<'src, Token, Span>>> + Clone
where
I: Input<'src, Token = Token, Span = Span>,
{
choice((
select! {
Token::Minus => UnaryExpressionOperatorKind::Negate,
Token::Bang => UnaryExpressionOperatorKind::Not,
Token::Asterisk => UnaryExpressionOperatorKind::Dereference,
},
just(Token::Ampersand)
.ignore_then(just(Token::MutKw).or_not())
.map(|mutable| {
if mutable.is_some() {
UnaryExpressionOperatorKind::BorrowMut
} else {
UnaryExpressionOperatorKind::Borrow
}
}),
))
.map_with(|kind, extra| UnaryExpressionOperator {
span: extra.span(),
kind,
})
}
fn multiplicative_operator_parser<'src, I>()
-> impl Parser<'src, I, BinaryExpressionOperator, extra::Err<Rich<'src, Token, Span>>> + Clone
where
I: Input<'src, Token = Token, Span = Span>,
{
select! {
Token::Asterisk => BinaryExpressionOperatorKind::Multiply,
Token::Slash => BinaryExpressionOperatorKind::Divide,
}
.map_with(binary_operator)
}
fn additive_operator_parser<'src, I>()
-> impl Parser<'src, I, BinaryExpressionOperator, extra::Err<Rich<'src, Token, Span>>> + Clone
where
I: Input<'src, Token = Token, Span = Span>,
{
select! {
Token::Plus => BinaryExpressionOperatorKind::Add,
Token::Minus => BinaryExpressionOperatorKind::Subtract,
}
.map_with(binary_operator)
}
fn shift_operator_parser<'src, I>()
-> impl Parser<'src, I, BinaryExpressionOperator, extra::Err<Rich<'src, Token, Span>>> + Clone
where
I: Input<'src, Token = Token, Span = Span>,
{
choice((
just(Token::LeftAngle)
.then_ignore(just(Token::LeftAngle))
.to(BinaryExpressionOperatorKind::ShiftLeft),
just(Token::RightAngle)
.then_ignore(just(Token::RightAngle))
.to(BinaryExpressionOperatorKind::ShiftRight),
))
.map_with(binary_operator)
}
fn comparison_operator_parser<'src, I>()
-> impl Parser<'src, I, BinaryExpressionOperator, extra::Err<Rich<'src, Token, Span>>> + Clone
where
I: Input<'src, Token = Token, Span = Span>,
{
select! {
Token::LeftAngle => BinaryExpressionOperatorKind::Less,
Token::LeftAngleEq => BinaryExpressionOperatorKind::LessOrEqual,
Token::RightAngle => BinaryExpressionOperatorKind::Greater,
Token::RightAngleEq => BinaryExpressionOperatorKind::GreaterOrEqual,
}
.map_with(binary_operator)
}
fn equality_operator_parser<'src, I>()
-> impl Parser<'src, I, BinaryExpressionOperator, extra::Err<Rich<'src, Token, Span>>> + Clone
where
I: Input<'src, Token = Token, Span = Span>,
{
select! {
Token::EqEq => BinaryExpressionOperatorKind::Equal,
Token::NotEq => BinaryExpressionOperatorKind::NotEqual,
}
.map_with(binary_operator)
}
fn binary_operator<'src, I>(
kind: BinaryExpressionOperatorKind,
extra: &mut MapExtra<'src, '_, I, extra::Err<Rich<'src, Token, Span>>>,
) -> BinaryExpressionOperator
where
I: Input<'src, Token = Token, Span = Span>,
{
BinaryExpressionOperator {
span: extra.span(),
kind,
}
}