use super::error::{Located, MdxError, MdxErrorKind};
use super::line::Line;
use super::mdx::{self, Fallback, Parsed};
use super::tree::{Block, InlineSource, JsxTag};
use crate::node::{MdxFlowExpression, Node, Point, Position};
pub(super) enum Flow {
Nok(Option<usize>),
More(Fallback),
Error(Located),
Done(Vec<FlowItem>, usize),
}
pub(super) enum FlowItem {
Tag(mdx::Tag, usize),
Expression(smol_str::SmolStr, usize, usize),
}
fn flow_items(source: &InlineSource) -> Flow {
let text = source.text.as_str();
let mut index = text.len() - text.trim_start_matches([' ', '\t']).len();
let mut items = Vec::new();
loop {
let start = index;
let after_expression = match text[index..].chars().next() {
Some('<') => match mdx::tag(text, index, None) {
Parsed::Ok(tag) => {
index = tag.end;
items.push(FlowItem::Tag(tag, start));
false
}
Parsed::Nok => return Flow::Nok(items.last().map(item_end)),
Parsed::More(fallback) => return Flow::More(fallback),
Parsed::Error(error) => return Flow::Error(error),
},
Some('{') => match mdx::expression(text, index, None) {
Parsed::Ok((end, value)) => {
index = end;
items.push(FlowItem::Expression(value, start, end));
true
}
Parsed::Nok => return Flow::Nok(items.last().map(item_end)),
Parsed::More(fallback) => return Flow::More(fallback),
Parsed::Error(error) => return Flow::Error(error),
},
_ => return Flow::Nok(items.last().map(item_end)),
};
let end = index;
index += text[index..].len() - text[index..].trim_start_matches([' ', '\t']).len();
match text[index..].chars().next() {
None | Some('\n' | '\r') => return Flow::Done(items, end),
Some('<') => {}
Some('{') if !after_expression => {}
Some(_) => return Flow::Nok(Some(end)),
}
}
}
fn item_end(item: &FlowItem) -> usize {
match item {
FlowItem::Tag(tag, _) => tag.end,
FlowItem::Expression(_, _, end) => *end,
}
}
pub(super) enum FlowOutcome {
Flow(usize),
Nok(Option<usize>),
Pending,
}
pub(super) fn mdx_flow(lines: &[Line<'_>], start: usize, blocks: &mut Vec<Block>) -> FlowOutcome {
let mut count = 1;
loop {
let end = (start + count).min(lines.len());
let mut source = InlineSource::new(
lines[start..end]
.iter()
.map(|line| (line.text, line.eol, line.point(0))),
);
source.text.push_str(lines[end - 1].eol);
let container_end = end == lines.len() && lines.last().is_some_and(|line| !line.eof);
match flow_items(&source) {
Flow::More(_) if end < lines.len() => count *= 2,
Flow::More(_) if container_end => {
blocks.push(Block::Error(MdxError::new(
MdxErrorKind::LazyLine,
Some(lines[start].point(0)),
)));
return FlowOutcome::Flow(lines.len());
}
Flow::More(Fallback::Nok) => return FlowOutcome::Pending,
Flow::More(Fallback::Error(error)) => {
blocks.push(Block::Error(error.in_source(&source)));
return FlowOutcome::Flow(lines.len());
}
Flow::Nok(end) => {
let last_line =
end.map(|end| start + source.lines.partition_point(|line| line.offset < end).saturating_sub(1));
return FlowOutcome::Nok(last_line);
}
Flow::Error(error) => {
blocks.push(Block::Error(error.in_source(&source)));
return FlowOutcome::Flow(lines.len());
}
Flow::Done(items, last) => {
let last_line = start
+ source
.lines
.partition_point(|line| line.offset < last)
.saturating_sub(1);
if let Some(lazy) = lines[start + 1..=last_line].iter().find(|line| line.lazy) {
blocks.push(Block::Error(MdxError::new(MdxErrorKind::LazyLine, Some(lazy.point(0)))));
return FlowOutcome::Flow(lines.len());
}
for item in items {
blocks.push(match item {
FlowItem::Tag(tag, from) => Block::Jsx(JsxTag {
name: tag.name,
attributes: tag.attributes,
kind: tag.kind,
position: Position {
start: source.point(from),
end: source.point(tag.end),
},
}),
FlowItem::Expression(value, from, to) => {
Block::Node(Node::MdxFlowExpression(MdxFlowExpression {
value,
position: Some(Position {
start: source.point(from),
end: source.point(to),
}),
}))
}
});
}
return FlowOutcome::Flow(last_line + 1);
}
}
}
}
pub(super) fn blocks_lazy_continuation(lines: &[Line<'_>], index: usize) -> bool {
matches!(
probe_mdx_flow(lines, index),
Some(FlowOutcome::Flow(_) | FlowOutcome::Pending)
)
}
pub(super) fn may_start_flow(line: &Line<'_>) -> bool {
line.flavor.has_jsx() && matches!(line.text.as_bytes().get(line.ws), Some(b'<' | b'{'))
}
pub(super) fn probe_mdx_flow(lines: &[Line<'_>], index: usize) -> Option<FlowOutcome> {
let line = &lines[index];
(may_start_flow(line) && !line.lazy).then(|| mdx_flow(lines, index, &mut Vec::new()))
}
pub(super) enum FlowStart {
Complete,
Pending,
}
pub(super) fn flow_start(line: &Line<'_>) -> Option<FlowStart> {
if !may_start_flow(line) {
return None;
}
let mut source = InlineSource::new(std::iter::once((line.text, "", line.point(0))));
source.text.push_str(line.eol);
match flow_items(&source) {
Flow::Nok(_) => None,
Flow::More(_) => Some(FlowStart::Pending),
_ => Some(FlowStart::Complete),
}
}
pub(super) enum FlowEnd {
Open,
Done,
Text,
}
pub(super) fn flow_end(text: &str) -> FlowEnd {
let mut source = InlineSource::new(std::iter::once((text, "\n", Point { line: 1, column: 1 })));
source.text.push('\n');
match flow_items(&source) {
Flow::More(_) => FlowEnd::Open,
Flow::Nok(_) => FlowEnd::Text,
_ => FlowEnd::Done,
}
}
pub(super) fn absorbed_until(lines: &[Line<'_>], first: usize, last: usize) -> usize {
(first + 1..=last)
.find(|&index| lines[index].is_blank())
.map_or(last, |blank| blank - 1)
}