use crate::parser::context::ParserCtx;
use crate::parser::lexer::{TokKind, Token, is_op_suffix_char};
use crate::parser::structural::is_op_name;
use super::juxtapose::is_word_operator_tok;
pub(super) const PREFIX_BP: u8 = 32;
pub(super) const TERNARY_L: u8 = 3;
pub(super) const TERNARY_BRANCH_BP: u8 = COMMA_BP;
pub(super) const JUXTAPOSE_L: u8 = 34;
pub(super) const JUXTAPOSE_R: u8 = 33;
pub(super) const WHERE_BP: u8 = 31;
pub(super) const WHERE_BOUND_BP: u8 = 10;
pub(super) const WORD_OP_L: u8 = 10;
pub(super) const WORD_OP_R: u8 = 11;
pub(super) const COMMA_BP: u8 = 2;
pub(super) const COMMA_ITEM_BP: u8 = 3;
pub(super) const SPLAT_BP: u8 = 14;
pub(crate) fn is_comparison_op(kind: TokKind) -> bool {
matches!(
kind,
TokKind::EqEq
| TokKind::NotEq
| TokKind::EqEqEq
| TokKind::NotEqEq
| TokKind::Lt
| TokKind::Le
| TokKind::Gt
| TokKind::Ge
| TokKind::Subtype
| TokKind::Supertype
| TokKind::DotEqEq
| TokKind::DotNotEq
| TokKind::DotEqEqEq
| TokKind::DotNotEqEq
| TokKind::DotLt
| TokKind::DotLe
| TokKind::DotGt
| TokKind::DotGe
| TokKind::DotSubtype
| TokKind::DotSupertype
| TokKind::UniComparison
)
}
pub(super) fn is_flat_arith_op(tok: &Token) -> bool {
matches!(
tok.kind,
TokKind::Plus
| TokKind::Star
| TokKind::PlusPercent
| TokKind::StarPercent
| TokKind::PlusPlus
) && !tok.text.chars().any(is_op_suffix_char)
}
pub(super) fn is_lone_error_operator(kind: TokKind) -> bool {
use TokKind::*;
is_assignment_op(kind) || matches!(kind, ColonEq | AndAnd | OrOr | Arrow | Dot | DotDotDot)
}
pub(super) fn is_unary_prefix_op(kind: TokKind, text: &str) -> bool {
use TokKind::*;
matches!(
kind,
Plus | Minus
| PlusPercent
| MinusPercent
| DotPlus
| DotMinus
| DotPlusPercent
| DotMinusPercent
| Bang
| DotBang
| Tilde
| DotTilde
| Amp
| Subtype
| Supertype
| ColonColon
| UniRadical
) || (matches!(kind, UniPlus | UniTimes)
&& matches!(text, "±" | "∓" | "⋆" | ".±" | ".∓" | ".⋆"))
}
pub(super) fn is_value_operator(kind: TokKind) -> bool {
use TokKind::*;
(is_op_name(kind) && !matches!(kind, AndAnd | OrOr | Arrow))
|| matches!(
kind,
Colon
| DotDot
| UniAssign
| UniRadical
| UniArrow
| UniComparison
| UniColon
| UniPlus
| UniTimes
| UniPower
| DotPlus
| DotMinus
| DotStar
| DotPlusPercent
| DotMinusPercent
| DotStarPercent
| DotSlash
| DotBackslash
| DotSlashSlash
| DotCaret
| DotPercent
| DotTilde
| DotEqEq
| DotNotEq
| DotEqEqEq
| DotNotEqEq
| DotLt
| DotLe
| DotGt
| DotGe
| DotShl
| DotShr
| DotUShr
| DotSubtype
| DotSupertype
| DotFatArrow
| DotLongArrow
| DotLeftLongArrow
| DotLeftRightArrow
| DotPipeGt
| DotAmp
| DotPipe
)
}
pub(super) fn is_quotable_operator(kind: TokKind) -> bool {
use TokKind::*;
matches!(
kind,
Dot | DotDot
| UniRadical
| UniArrow
| UniComparison
| UniColon
| UniPlus
| UniTimes
| UniPower
| Question
| ColonEq
| UniAssign
| Dollar
)
}
pub(super) fn next_operator(
ctx: &ParserCtx<'_>,
from: usize,
allow_newlines: bool,
) -> Option<(usize, TokKind)> {
let op_idx = if allow_newlines {
ctx.skip_trivia(from)
} else {
ctx.skip_ws_and_block_comments(from)
};
let op = ctx.token(op_idx)?;
is_operator(op.kind).then_some((op_idx, op.kind))
}
pub(super) fn word_operator(
ctx: &ParserCtx<'_>,
from: usize,
allow_newlines: bool,
) -> Option<usize> {
let op_idx = if allow_newlines {
ctx.skip_trivia(from)
} else {
ctx.skip_ws_and_block_comments(from)
};
is_word_operator_tok(ctx.token(op_idx)?).then_some(op_idx)
}
pub(super) fn is_operator(kind: TokKind) -> bool {
matches!(kind, TokKind::Question | TokKind::WhereKw)
|| is_assignment_op(kind)
|| infix_binding_power(kind).is_some()
}
pub(super) fn is_assignment_op(kind: TokKind) -> bool {
matches!(
kind,
TokKind::Eq
| TokKind::DotEq
| TokKind::PlusEq
| TokKind::MinusEq
| TokKind::StarEq
| TokKind::SlashEq
| TokKind::BackslashEq
| TokKind::SlashSlashEq
| TokKind::CaretEq
| TokKind::PercentEq
| TokKind::PlusPercentEq
| TokKind::MinusPercentEq
| TokKind::StarPercentEq
| TokKind::PipeEq
| TokKind::DollarEq
| TokKind::AmpEq
| TokKind::ShlEq
| TokKind::ShrEq
| TokKind::UShrEq
| TokKind::DivEq
| TokKind::XorEq
| TokKind::DotPlusEq
| TokKind::DotMinusEq
| TokKind::DotStarEq
| TokKind::DotPlusPercentEq
| TokKind::DotMinusPercentEq
| TokKind::DotStarPercentEq
| TokKind::DotSlashEq
| TokKind::DotBackslashEq
| TokKind::DotSlashSlashEq
| TokKind::DotCaretEq
| TokKind::DotPercentEq
| TokKind::DotAmpEq
| TokKind::DotPipeEq
| TokKind::DotShlEq
| TokKind::DotShrEq
| TokKind::DotUShrEq
| TokKind::DotDivEq
| TokKind::DotXorEq
)
}
pub(super) fn infix_binding_power(kind: TokKind) -> Option<(u8, u8)> {
Some(match kind {
TokKind::Tilde | TokKind::DotTilde => (2, 1),
TokKind::ColonEq => (2, 1),
TokKind::UniAssign => (2, 1),
TokKind::UniArrow => (4, 3),
TokKind::UniComparison => (10, 11),
TokKind::UniColon => (14, 15),
TokKind::UniPlus => (20, 21),
TokKind::UniTimes => (24, 25),
TokKind::UniPower => (34, 33),
TokKind::Arrow => (35, 1),
TokKind::FatArrow
| TokKind::DotFatArrow
| TokKind::LongArrow
| TokKind::LeftRightArrow
| TokKind::LeftLongArrow
| TokKind::DotLongArrow
| TokKind::DotLeftLongArrow
| TokKind::DotLeftRightArrow => (4, 3),
TokKind::OrOr | TokKind::DotOrOr => (6, 5),
TokKind::AndAnd | TokKind::DotAndAnd => (8, 7),
TokKind::EqEq
| TokKind::NotEq
| TokKind::EqEqEq
| TokKind::NotEqEq
| TokKind::Lt
| TokKind::Le
| TokKind::Gt
| TokKind::Ge
| TokKind::Subtype
| TokKind::Supertype
| TokKind::DotEqEq
| TokKind::DotNotEq
| TokKind::DotEqEqEq
| TokKind::DotNotEqEq
| TokKind::DotLt
| TokKind::DotLe
| TokKind::DotGt
| TokKind::DotGe
| TokKind::DotSubtype
| TokKind::DotSupertype => (10, 11),
TokKind::PipeLt => (12, 11),
TokKind::PipeGt | TokKind::DotPipeGt => (13, 14),
TokKind::Colon | TokKind::DotDot => (14, 15),
TokKind::StarStar | TokKind::MinusMinus | TokKind::DotStarStar | TokKind::DotMinusMinus => {
(18, 19)
}
TokKind::Dollar
| TokKind::Plus
| TokKind::Minus
| TokKind::PlusPercent
| TokKind::MinusPercent
| TokKind::PlusPlus
| TokKind::DotPlus
| TokKind::DotMinus
| TokKind::DotPlusPercent
| TokKind::DotMinusPercent
| TokKind::Pipe
| TokKind::DotPipe => (20, 21),
TokKind::Star
| TokKind::Slash
| TokKind::Backslash
| TokKind::Percent
| TokKind::StarPercent
| TokKind::Amp
| TokKind::DotAmp
| TokKind::DotStar
| TokKind::DotStarPercent
| TokKind::DotSlash
| TokKind::DotBackslash
| TokKind::DotPercent => (24, 25),
TokKind::SlashSlash | TokKind::DotSlashSlash => (28, 29),
TokKind::Shl | TokKind::Shr | TokKind::UShr => (30, 31),
TokKind::DotShl | TokKind::DotShr | TokKind::DotUShr => (30, 31),
TokKind::Caret | TokKind::DotCaret => (34, 33),
TokKind::ColonColon => (36, 37),
TokKind::Dot => (40, 41),
_ => return None,
})
}