use super::*;
pub(super) fn signed_literal_fold(ctx: &ParserCtx<'_>, op_idx: usize) -> bool {
let Some(op) = ctx.token(op_idx) else {
return false;
};
if op.text.chars().next_back().is_some_and(is_op_suffix_char) {
return false;
}
let Some(num) = ctx.token(op_idx + 1) else {
return false;
};
let folds = match op.kind {
TokKind::Minus => matches!(
num.kind,
TokKind::Integer | TokKind::Float | TokKind::Float32
),
TokKind::Plus => is_number_tok(num.kind),
_ => return false,
};
if !folds {
return false;
}
let k3 = ctx.token(ctx.skip_ws(op_idx + 2)).map(|t| t.kind);
!matches!(
k3,
Some(
TokKind::Caret
| TokKind::DotCaret
| TokKind::UniPower
| TokKind::LBracket
| TokKind::LBrace
)
)
}
fn is_number_tok(kind: TokKind) -> bool {
matches!(
kind,
TokKind::Integer
| TokKind::BinInt
| TokKind::OctInt
| TokKind::HexInt
| TokKind::Float
| TokKind::Float32
)
}
fn is_error_number_tok(kind: TokKind) -> bool {
matches!(
kind,
TokKind::ErrorInvalidNumber | TokKind::ErrorHexFloatNoP
)
}
pub(super) fn lhs_is_number(ctx: &ParserCtx<'_>, lhs: &ExprParse) -> bool {
if lhs.end == lhs.start + 1
&& ctx
.token(lhs.start)
.is_some_and(|t| is_number_tok(t.kind) || is_error_number_tok(t.kind))
{
return true;
}
matches!(lhs.events.first(), Some(Event::Start(SyntaxKind::LITERAL)))
&& lhs.end == lhs.start + 2
&& ctx
.token(lhs.start + 1)
.is_some_and(|t| is_number_tok(t.kind))
}
fn lhs_value_close(lhs: &ExprParse) -> bool {
matches!(
lhs.events.first(),
Some(Event::Start(
SyntaxKind::PAREN_EXPR
| SyntaxKind::CALL_EXPR
| SyntaxKind::INDEX_EXPR
| SyntaxKind::CURLY_EXPR
| SyntaxKind::VECT_EXPR
| SyntaxKind::MATRIX_EXPR
| SyntaxKind::TYPED_MATRIX_EXPR
| SyntaxKind::BRACESCAT_EXPR
| SyntaxKind::POSTFIX_EXPR
| SyntaxKind::JUXTAPOSE_EXPR
))
)
}
fn lhs_is_paren_block(lhs: &ExprParse) -> bool {
if !matches!(
lhs.events.first(),
Some(Event::Start(SyntaxKind::PAREN_EXPR))
) {
return false;
}
lhs.events
.iter()
.filter_map(|e| match e {
Event::Start(k) => Some(*k),
_ => None,
})
.nth(1)
.is_some_and(is_block_form_kind)
}
fn is_block_form_kind(kind: SyntaxKind) -> bool {
matches!(
kind,
SyntaxKind::IF_EXPR
| SyntaxKind::FUNCTION_DEF
| SyntaxKind::MACRO_DEF
| SyntaxKind::BEGIN_EXPR
| SyntaxKind::QUOTE_EXPR
| SyntaxKind::WHILE_EXPR
| SyntaxKind::FOR_EXPR
| SyntaxKind::LET_EXPR
| SyntaxKind::TRY_EXPR
| SyntaxKind::STRUCT_DEF
| SyntaxKind::MODULE_DEF
| SyntaxKind::ABSTRACT_DEF
| SyntaxKind::PRIMITIVE_DEF
)
}
fn is_juxtapose_closing(kind: TokKind) -> bool {
matches!(
kind,
TokKind::RParen | TokKind::RBracket | TokKind::RBrace | TokKind::Comma | TokKind::Semicolon
)
}
fn is_closing_token(kind: TokKind) -> bool {
is_juxtapose_closing(kind)
|| matches!(
kind,
TokKind::EndKw
| TokKind::ElseKw
| TokKind::ElseifKw
| TokKind::CatchKw
| TokKind::FinallyKw
)
}
fn lhs_is_plain_string(ctx: &ParserCtx<'_>, lhs: &ExprParse) -> bool {
if !matches!(
lhs.events.first(),
Some(Event::Start(SyntaxKind::STRING_LITERAL))
) {
return false;
}
let first_tok = lhs.events.iter().find_map(|e| match e {
Event::Tok(idx) => Some(*idx),
_ => None,
});
match first_tok {
Some(idx) => ctx.token(idx).map(|t| t.kind) != Some(TokKind::StringPrefix),
None => true,
}
}
pub(super) fn should_juxtapose_string_error(
ctx: &ParserCtx<'_>,
lhs: &ExprParse,
min_bp: u8,
) -> bool {
if JUXTAPOSE_L < min_bp {
return false;
}
if lhs_is_paren_block(lhs) {
return false;
}
let Some(next) = ctx.token(lhs.end) else {
return false;
};
let k = next.kind;
if k.is_trivia()
|| is_operator(k)
|| k == TokKind::DotDotDot
|| k == TokKind::At
|| is_closing_token(k)
{
return false;
}
if is_word_operator_tok(next) {
return false;
}
if lhs_is_plain_string(ctx, lhs) {
return !is_number_tok(k);
}
matches!(k, TokKind::StringDelimOpen | TokKind::CmdDelimOpen)
&& (lhs_is_number(ctx, lhs) || lhs_value_close(lhs))
}
pub(super) fn is_word_operator_tok(tok: &Token) -> bool {
tok.kind == TokKind::Ident && (tok.text == "in" || tok.text == "isa")
}
pub(super) fn is_element_of_tok(tok: &Token) -> bool {
tok.kind == TokKind::UniComparison && tok.text == "∈"
}
pub(super) fn is_for_separator_tok(tok: &Token) -> bool {
(tok.kind == TokKind::Ident && tok.text == "in") || is_element_of_tok(tok)
}
fn lhs_allows_radical(ctx: &ParserCtx<'_>, lhs: &ExprParse) -> bool {
let mut events = lhs.events.iter();
let kind = events.find_map(|event| match event {
Event::Start(kind) if *kind != SyntaxKind::PAREN_EXPR => Some(*kind),
_ => None,
});
match kind {
Some(SyntaxKind::TYPE_ANNOTATION | SyntaxKind::INTERPOLATION | SyntaxKind::QUOTE_SYM) => {
false
}
Some(SyntaxKind::UNARY_EXPR) => !matches!(
events.next(),
Some(Event::Tok(idx)) if ctx.token(*idx).is_some_and(|t| t.kind == TokKind::Amp)
),
Some(kind) => !is_block_form_kind(kind),
None => false,
}
}
pub(super) fn should_juxtapose(ctx: &ParserCtx<'_>, lhs: &ExprParse, min_bp: u8) -> bool {
if JUXTAPOSE_L < min_bp {
return false;
}
let Some(next) = ctx.token(lhs.end) else {
return false;
};
let k = next.kind;
if k.is_trivia() {
return false;
}
if k == TokKind::UniRadical {
return matches!(next.text, "√" | "∛" | "∜" | ".√" | ".∛" | ".∜")
&& lhs_allows_radical(ctx, lhs);
}
if k == TokKind::At {
return lhs_is_number(ctx, lhs);
}
if is_operator(k) || k == TokKind::DotDotDot || is_juxtapose_closing(k) || k.is_keyword() {
return false;
}
if is_word_operator_tok(next) {
return false;
}
if lhs_is_number(ctx, lhs) {
return true;
}
let unary_value = matches!(
lhs.events.first(),
Some(Event::Start(SyntaxKind::UNARY_EXPR))
) && ctx.token(lhs.start).is_some_and(|t| t.kind != TokKind::Amp);
!is_number_tok(k) && (lhs_value_close(lhs) || unary_value) && !lhs_is_paren_block(lhs)
}