nml/src/elements/list.rs

534 lines
14 KiB
Rust
Raw Normal View History

2024-08-02 10:32:00 +02:00
use std::any::Any;
use std::cell::Ref;
use std::collections::HashMap;
use std::rc::Rc;
use crate::compiler::compiler::Compiler;
use crate::compiler::compiler::Target;
use crate::document::document::Document;
use crate::document::document::DocumentAccessors;
use crate::document::element::ContainerElement;
use crate::document::element::ElemKind;
use crate::document::element::Element;
2024-10-20 12:25:52 +02:00
use crate::lsp::semantic::Semantics;
2024-10-20 19:38:15 +02:00
use crate::parser::parser::ParseMode;
2024-08-05 18:40:17 +02:00
use crate::parser::parser::ParserState;
2024-11-01 22:15:33 +01:00
use crate::parser::property::Property;
use crate::parser::property::PropertyParser;
2024-10-24 10:18:49 +02:00
use crate::parser::reports::macros::*;
2024-10-23 22:25:34 +02:00
use crate::parser::reports::Report;
2024-10-24 10:18:49 +02:00
use crate::parser::reports::*;
2024-08-02 10:32:00 +02:00
use crate::parser::rule::Rule;
use crate::parser::source::Cursor;
use crate::parser::source::Token;
use crate::parser::source::VirtualSource;
use crate::parser::util;
2024-11-01 22:15:33 +01:00
use parser::util::escape_source;
2024-07-19 11:52:12 +02:00
use regex::Regex;
2024-08-02 10:32:00 +02:00
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum MarkerKind {
Open,
Close,
}
2024-07-19 11:52:12 +02:00
#[derive(Debug)]
2024-08-02 10:32:00 +02:00
pub struct ListMarker {
pub(self) location: Token,
pub(self) numbered: bool,
pub(self) kind: MarkerKind,
2024-07-19 11:52:12 +02:00
}
2024-08-02 10:32:00 +02:00
impl Element for ListMarker {
fn location(&self) -> &Token { &self.location }
2024-07-19 11:52:12 +02:00
2024-08-02 10:32:00 +02:00
fn kind(&self) -> ElemKind { ElemKind::Block }
2024-07-19 11:52:12 +02:00
2024-08-02 10:32:00 +02:00
fn element_name(&self) -> &'static str { "List Marker" }
2024-10-20 12:25:52 +02:00
fn compile(
&self,
compiler: &Compiler,
_document: &dyn Document,
_cursor: usize,
) -> Result<String, String> {
2024-08-02 10:32:00 +02:00
match compiler.target() {
Target::HTML => match (self.kind, self.numbered) {
(MarkerKind::Close, true) => Ok("</ol>".to_string()),
(MarkerKind::Close, false) => Ok("</ul>".to_string()),
(MarkerKind::Open, true) => Ok("<ol>".to_string()),
(MarkerKind::Open, false) => Ok("<ul>".to_string()),
},
_ => todo!(),
2024-07-19 11:52:12 +02:00
}
}
2024-08-02 10:32:00 +02:00
}
2024-07-19 11:52:12 +02:00
2024-08-02 10:32:00 +02:00
#[derive(Debug)]
pub struct ListEntry {
pub(self) location: Token,
pub(self) numbering: Vec<(bool, usize)>,
pub(self) content: Vec<Box<dyn Element>>,
pub(self) bullet: Option<String>,
2024-07-19 11:52:12 +02:00
}
2024-08-02 10:32:00 +02:00
impl Element for ListEntry {
fn location(&self) -> &Token { &self.location }
2024-07-19 11:52:12 +02:00
2024-08-02 10:32:00 +02:00
fn kind(&self) -> ElemKind { ElemKind::Block }
2024-07-19 11:52:12 +02:00
2024-08-02 10:32:00 +02:00
fn element_name(&self) -> &'static str { "List Entry" }
2024-07-19 11:52:12 +02:00
2024-10-20 12:25:52 +02:00
fn compile(
&self,
compiler: &Compiler,
document: &dyn Document,
cursor: usize,
) -> Result<String, String> {
2024-08-02 10:32:00 +02:00
match compiler.target() {
2024-07-19 11:52:12 +02:00
Target::HTML => {
2024-08-26 20:50:23 +02:00
let mut result = String::new();
2024-10-20 12:25:52 +02:00
if let Some((numbered, number)) = self.numbering.last() {
2024-08-26 20:50:23 +02:00
if *numbered {
result += format!("<li value=\"{number}\">").as_str();
2024-10-20 12:25:52 +02:00
} else {
2024-08-26 20:50:23 +02:00
result += "<li>";
}
}
2024-08-02 10:32:00 +02:00
for elem in &self.content {
2024-10-20 12:25:52 +02:00
result += elem
.compile(compiler, document, cursor + result.len())?
.as_str();
2024-07-19 11:52:12 +02:00
}
2024-08-02 10:32:00 +02:00
result += "</li>";
2024-07-19 11:52:12 +02:00
Ok(result)
}
2024-08-02 10:32:00 +02:00
_ => todo!(),
2024-07-19 11:52:12 +02:00
}
2024-08-02 10:32:00 +02:00
}
2024-07-19 11:52:12 +02:00
2024-08-02 10:32:00 +02:00
fn as_container(&self) -> Option<&dyn ContainerElement> { Some(self) }
}
2024-07-19 11:52:12 +02:00
2024-08-02 10:32:00 +02:00
impl ContainerElement for ListEntry {
fn contained(&self) -> &Vec<Box<dyn Element>> { &self.content }
2024-07-19 11:52:12 +02:00
2024-08-02 10:32:00 +02:00
fn push(&mut self, elem: Box<dyn Element>) -> Result<(), String> {
if elem.kind() == ElemKind::Block {
return Err("Cannot add block element inside a list".to_string());
2024-07-19 11:52:12 +02:00
}
2024-08-02 10:32:00 +02:00
self.content.push(elem);
Ok(())
2024-07-19 11:52:12 +02:00
}
}
2024-08-08 17:11:32 +02:00
#[auto_registry::auto_registry(registry = "rules", path = "crate::elements::list")]
2024-08-02 10:32:00 +02:00
pub struct ListRule {
2024-07-19 11:52:12 +02:00
start_re: Regex,
2024-08-02 10:32:00 +02:00
continue_re: Regex,
properties: PropertyParser,
2024-07-19 11:52:12 +02:00
}
impl ListRule {
pub fn new() -> Self {
2024-08-02 10:32:00 +02:00
let mut props = HashMap::new();
props.insert(
"offset".to_string(),
2024-11-01 22:15:33 +01:00
Property::new("Entry numbering offset".to_string(), None),
2024-08-02 10:32:00 +02:00
);
props.insert(
"bullet".to_string(),
2024-11-01 22:15:33 +01:00
Property::new("Entry bullet".to_string(), None),
2024-08-02 10:32:00 +02:00
);
2024-07-19 11:52:12 +02:00
Self {
2024-08-02 10:32:00 +02:00
start_re: Regex::new(r"(?:^|\n)(?:[^\S\r\n]+)([*-]+)(?:\[((?:\\.|[^\\\\])*?)\])?(.*)")
.unwrap(),
2024-10-20 12:25:52 +02:00
continue_re: Regex::new(r"(?:^|\n)([^\S\r\n].*)").unwrap(),
2024-08-02 10:32:00 +02:00
properties: PropertyParser { properties: props },
}
}
fn push_markers(
token: &Token,
2024-08-05 18:40:17 +02:00
state: &ParserState,
2024-08-02 10:32:00 +02:00
document: &dyn Document,
current: &Vec<(bool, usize)>,
target: &Vec<(bool, usize)>,
) {
let mut start_pos = 0;
for i in 0..std::cmp::min(target.len(), current.len()) {
if current[i].0 != target[i].0 {
break;
}
start_pos += 1;
}
// Close
for i in start_pos..current.len() {
2024-08-06 18:58:41 +02:00
state.push(
2024-08-02 10:32:00 +02:00
document,
Box::new(ListMarker {
location: token.clone(),
kind: MarkerKind::Close,
numbered: current[current.len() - 1 - (i - start_pos)].0,
}),
);
2024-07-19 11:52:12 +02:00
}
2024-08-02 10:32:00 +02:00
// Open
for i in start_pos..target.len() {
2024-08-06 18:58:41 +02:00
state.push(
2024-08-02 10:32:00 +02:00
document,
Box::new(ListMarker {
location: token.clone(),
kind: MarkerKind::Open,
numbered: target[i].0,
}),
);
}
2024-07-19 11:52:12 +02:00
}
2024-08-02 10:32:00 +02:00
fn parse_depth(depth: &str, document: &dyn Document, offset: usize) -> Vec<(bool, usize)> {
2024-07-19 11:52:12 +02:00
let mut parsed = vec![];
2024-08-02 10:32:00 +02:00
let prev_entry = document
.last_element::<ListEntry>()
.and_then(|entry| Ref::filter_map(entry, |e| Some(&e.numbering)).ok());
2024-07-19 11:52:12 +02:00
let mut continue_match = true;
2024-08-02 10:32:00 +02:00
depth.chars().enumerate().for_each(|(idx, c)| {
2024-11-02 09:54:58 +01:00
let number = if offset == usize::MAX || idx + 1 != depth.len() {
2024-08-02 10:32:00 +02:00
prev_entry
.as_ref()
.and_then(|v| {
if !continue_match {
return None;
2024-07-19 11:52:12 +02:00
}
2024-08-02 10:32:00 +02:00
let numbered = c == '-';
match v.get(idx) {
None => None,
Some((prev_numbered, prev_idx)) => {
if *prev_numbered != numbered {
continue_match = false;
None
}
// New depth
else if idx + 1 == v.len() {
Some(prev_idx + 1)
}
// Increase from previous
else {
Some(*prev_idx)
} // Do nothing
}
}
})
.unwrap_or(1)
} else {
offset
};
2024-07-19 11:52:12 +02:00
2024-08-02 10:32:00 +02:00
match c {
2024-07-19 11:52:12 +02:00
'*' => parsed.push((false, number)),
'-' => parsed.push((true, number)),
2024-08-02 10:32:00 +02:00
_ => panic!("Unimplemented"),
2024-07-19 11:52:12 +02:00
}
});
2024-08-14 22:50:32 +02:00
parsed
2024-07-19 11:52:12 +02:00
}
}
2024-08-02 10:32:00 +02:00
impl Rule for ListRule {
2024-07-19 11:52:12 +02:00
fn name(&self) -> &'static str { "List" }
2024-10-20 19:38:15 +02:00
2024-08-08 14:12:16 +02:00
fn previous(&self) -> Option<&'static str> { Some("Raw") }
2024-07-19 11:52:12 +02:00
2024-10-20 19:38:15 +02:00
fn next_match(
&self,
mode: &ParseMode,
_state: &ParserState,
cursor: &Cursor,
) -> Option<(usize, Box<dyn Any>)> {
if mode.paragraph_only {
return None;
}
2024-08-02 10:32:00 +02:00
self.start_re
2024-10-20 12:25:52 +02:00
.find_at(cursor.source.content(), cursor.pos)
.map(|m| (m.start(), Box::new([false; 0]) as Box<dyn Any>))
2024-07-19 11:52:12 +02:00
}
2024-08-02 10:32:00 +02:00
fn on_match<'a>(
&self,
2024-08-06 18:58:41 +02:00
state: &ParserState,
2024-08-02 10:32:00 +02:00
document: &'a dyn Document<'a>,
cursor: Cursor,
2024-08-05 18:40:17 +02:00
_match_data: Box<dyn Any>,
2024-10-23 22:25:34 +02:00
) -> (Cursor, Vec<Report>) {
2024-07-19 11:52:12 +02:00
let mut reports = vec![];
2024-08-02 10:32:00 +02:00
2024-07-19 11:52:12 +02:00
let content = cursor.source.content();
2024-08-02 10:32:00 +02:00
let mut end_cursor = cursor.clone();
loop {
if let Some(captures) = self.start_re.captures_at(content, end_cursor.pos) {
if captures.get(0).unwrap().start() != end_cursor.pos {
break;
}
// Advance cursor
end_cursor = end_cursor.at(captures.get(0).unwrap().end());
// Properties
2024-11-01 22:15:33 +01:00
let prop_source = escape_source(
end_cursor.source.clone(),
captures.get(2).map_or(0..0, |m| m.range()),
"List Properties".into(),
'\\',
"]",
);
let properties = match self.properties.parse(
"List",
&mut reports,
state,
Token::new(0..prop_source.content().len(), prop_source),
) {
Some(props) => props,
None => return (end_cursor, reports),
};
let (offset, bullet) = match (
properties.get_opt(&mut reports, "offset", |_, value| {
value.value.parse::<usize>()
}),
properties.get_opt(&mut reports, "bullet", |_, value| {
Result::<_, String>::Ok(value.value.clone())
}),
) {
(Some(offset), Some(bullet)) => {
// Get bullet from previous entry if it exists
if bullet.is_none() {
(
offset,
document
.last_element::<ListEntry>()
.and_then(|prev| prev.bullet.clone()),
)
} else {
(offset, bullet)
2024-07-19 11:52:12 +02:00
}
}
2024-11-01 22:15:33 +01:00
_ => return (end_cursor, reports),
};
2024-07-19 11:52:12 +02:00
2024-08-02 10:32:00 +02:00
// Depth
let depth = ListRule::parse_depth(
captures.get(1).unwrap().as_str(),
document,
2024-08-26 20:50:23 +02:00
offset.unwrap_or(usize::MAX),
2024-08-02 10:32:00 +02:00
);
2024-10-20 12:25:52 +02:00
if let Some((sems, tokens)) =
2024-10-25 10:05:13 +02:00
Semantics::from_source(cursor.source.clone(), &state.shared.lsp)
2024-10-20 12:25:52 +02:00
{
sems.add(captures.get(1).unwrap().range(), tokens.list_bullet);
if let Some(props) = captures.get(2).map(|m| m.range()) {
sems.add(props.start - 1..props.start, tokens.list_props_sep);
sems.add(props.end..props.end + 1, tokens.list_props_sep);
}
}
2024-08-02 10:32:00 +02:00
// Content
2024-10-20 12:25:52 +02:00
let entry_start = captures.get(3).unwrap().start();
2024-08-02 10:32:00 +02:00
let mut entry_content = captures.get(3).unwrap().as_str().to_string();
while let Some(captures) = self.continue_re.captures_at(content, end_cursor.pos) {
// Break if next element is another entry
if captures.get(0).unwrap().start() != end_cursor.pos
|| captures
2024-10-20 12:25:52 +02:00
.get(1)
2024-08-02 10:32:00 +02:00
.unwrap()
.as_str()
2024-08-14 22:50:32 +02:00
.find(['*', '-'])
2024-10-20 12:25:52 +02:00
.map(|delim| {
captures.get(1).unwrap().as_str()[0..delim]
.chars()
2024-10-24 10:18:49 +02:00
.all(|c| c.is_whitespace())
2024-10-20 12:25:52 +02:00
}) == Some(true)
2024-08-02 10:32:00 +02:00
{
break;
}
// Advance cursor
end_cursor = end_cursor.at(captures.get(0).unwrap().end());
2024-10-20 12:25:52 +02:00
entry_content += "\n";
entry_content += captures.get(1).unwrap().as_str();
2024-07-19 11:52:12 +02:00
}
2024-08-02 10:32:00 +02:00
// Parse entry content
2024-08-05 09:04:09 +02:00
let token = Token::new(entry_start..end_cursor.pos, end_cursor.source.clone());
2024-08-02 10:32:00 +02:00
let entry_src = Rc::new(VirtualSource::new(
token.clone(),
2024-07-19 11:52:12 +02:00
"List Entry".to_string(),
2024-08-02 10:32:00 +02:00
entry_content,
));
2024-08-05 18:40:17 +02:00
let parsed_content = match util::parse_paragraph(state, entry_src, document) {
2024-08-02 10:32:00 +02:00
Err(err) => {
2024-10-24 10:18:49 +02:00
report_warn!(
&mut reports,
token.source(),
"Unable to parse List Entry".into(),
2024-10-23 22:25:34 +02:00
span(token.range.clone(), err.into())
2024-08-02 10:32:00 +02:00
);
2024-08-27 10:39:30 +02:00
// Return an empty paragraph
vec![]
2024-08-02 10:32:00 +02:00
}
2024-08-14 22:50:32 +02:00
Ok(mut paragraph) => std::mem::take(&mut paragraph.content),
2024-08-02 10:32:00 +02:00
};
2024-07-19 11:52:12 +02:00
2024-08-02 10:32:00 +02:00
if let Some(previous_depth) = document
.last_element::<ListEntry>()
.map(|ent| ent.numbering.clone())
{
2024-08-05 18:40:17 +02:00
ListRule::push_markers(&token, state, document, &previous_depth, &depth);
2024-08-02 10:32:00 +02:00
} else {
2024-08-05 18:40:17 +02:00
ListRule::push_markers(&token, state, document, &vec![], &depth);
2024-08-02 10:32:00 +02:00
}
2024-07-19 11:52:12 +02:00
2024-08-06 18:58:41 +02:00
state.push(
2024-08-02 10:32:00 +02:00
document,
Box::new(ListEntry {
location: Token::new(
entry_start..end_cursor.pos,
end_cursor.source.clone(),
),
numbering: depth,
content: parsed_content,
bullet,
}),
);
} else {
break;
2024-07-19 11:52:12 +02:00
}
2024-08-02 10:32:00 +02:00
}
// Close all lists
let current = document
.last_element::<ListEntry>()
.map(|ent| ent.numbering.clone())
.unwrap();
let token = Token::new(end_cursor.pos..end_cursor.pos, end_cursor.source.clone());
2024-08-05 18:40:17 +02:00
ListRule::push_markers(&token, state, document, &current, &Vec::new());
2024-07-19 11:52:12 +02:00
(end_cursor, reports)
}
}
2024-08-02 10:32:00 +02:00
#[cfg(test)]
mod tests {
use super::*;
use crate::elements::paragraph::Paragraph;
use crate::elements::text::Text;
use crate::parser::langparser::LangParser;
2024-08-05 18:40:17 +02:00
use crate::parser::parser::Parser;
2024-08-02 10:32:00 +02:00
use crate::parser::source::SourceFile;
2024-10-20 19:38:15 +02:00
use crate::validate_document;
use crate::validate_semantics;
2024-08-02 10:32:00 +02:00
#[test]
fn parser() {
let source = Rc::new(SourceFile::with_content(
"".to_string(),
r#"
* 1
*[offset=7] 2
continued
* 3
* New list
*-[bullet=(*)] A
*- B
* Back
*-* More nested
"#
.to_string(),
None,
));
let parser = LangParser::default();
2024-08-05 18:40:17 +02:00
let state = ParserState::new(&parser, None);
2024-10-20 19:38:15 +02:00
let (doc, _) = parser.parse(state, source, None, ParseMode::default());
2024-08-02 10:32:00 +02:00
validate_document!(doc.content().borrow(), 0,
ListMarker { numbered == false, kind == MarkerKind::Open };
ListEntry { numbering == vec![(false, 1)] } {
Text { content == "1" };
};
ListEntry { numbering == vec![(false, 7)] } {
2024-08-02 13:36:04 +02:00
Text { content == "2 continued" };
2024-08-02 10:32:00 +02:00
};
ListEntry { numbering == vec![(false, 8)] } {
Text { content == "3" };
};
ListMarker { numbered == false, kind == MarkerKind::Close };
Paragraph;
ListMarker { numbered == false, kind == MarkerKind::Open };
ListEntry { numbering == vec![(false, 1)] } {
Text { content == "New list" };
};
ListMarker { numbered == true, kind == MarkerKind::Open };
ListEntry { numbering == vec![(false, 2), (true, 1)], bullet == Some("(*)".to_string()) } {
Text { content == "A" };
};
ListEntry { numbering == vec![(false, 2), (true, 2)], bullet == Some("(*)".to_string()) } {
Text { content == "B" };
};
ListMarker { numbered == true, kind == MarkerKind::Close };
ListEntry { numbering == vec![(false, 2)] } {
Text { content == "Back" };
};
ListMarker { numbered == true, kind == MarkerKind::Open };
ListMarker { numbered == false, kind == MarkerKind::Open };
ListEntry { numbering == vec![(false, 3), (true, 1), (false, 1)] } {
Text { content == "More nested" };
};
ListMarker { numbered == false, kind == MarkerKind::Close };
ListMarker { numbered == true, kind == MarkerKind::Close };
ListMarker { numbered == false, kind == MarkerKind::Close };
);
}
2024-10-20 12:25:52 +02:00
#[test]
fn semantic() {
let source = Rc::new(SourceFile::with_content(
"".to_string(),
r#"
*[offset=5] First **bold**
Second line
*- Another
"#
.to_string(),
None,
));
let parser = LangParser::default();
let (_, state) = parser.parse(
ParserState::new_with_semantics(&parser, None),
source.clone(),
None,
2024-10-20 19:38:15 +02:00
ParseMode::default(),
2024-10-20 12:25:52 +02:00
);
validate_semantics!(state, source.clone(), 0,
list_bullet { delta_line == 1, delta_start == 1, length == 1 };
list_props_sep { delta_line == 0, delta_start == 1, length == 1 };
2024-11-01 22:15:33 +01:00
prop_name { delta_line == 0, delta_start == 1, length == 6 };
prop_equal { delta_line == 0, delta_start == 6, length == 1 };
prop_value { delta_line == 0, delta_start == 1, length == 1 };
list_props_sep { delta_line == 0, delta_start == 1, length == 1 };
2024-10-20 12:25:52 +02:00
style_marker { delta_line == 0, delta_start == 8, length == 2 };
style_marker { delta_line == 0, delta_start == 6, length == 2 };
list_bullet { delta_line == 2, delta_start == 1, length == 2 };
);
}
2024-08-02 10:32:00 +02:00
}