//! Line classification (M1) — no Flex. //! //! Narrative comments are paragraph-scoped: blank lines separate paragraphs. //! After a comment line, subsequent non-blank lines stay comments until a blank //! (so mid-paragraph lines starting with `` ` `` are not treated as code). use std::fmt; /// Kind of a source line after skipping leading indentation for the test. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum LineKind { Blank, Comment, /// Jupyter-style persisted output (``). Writeback, Code, } impl fmt::Display for LineKind { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { LineKind::Blank => write!(f, "Blank"), LineKind::Comment => write!(f, "Comment"), LineKind::Writeback => write!(f, "Writeback"), LineKind::Code => write!(f, "Code"), } } } /// One classified source line (1-based line number). #[derive(Debug, Clone, PartialEq, Eq)] pub struct ClassifiedLine { pub line_no: u32, pub kind: LineKind, pub text: String, } /// Characters that start a code line when they appear as the first non-ws char. fn is_code_starter(c: char) -> bool { matches!( c, '#' | '*' | '>' | '+' | '-' | '|' | '~' | '$' ) || c.is_ascii_digit() } /// Whether `**inner**` should execute (assign / call / expr), not soft emphasis. pub fn looks_like_bold_code(inner: &str) -> bool { let t = inner.trim(); if t.is_empty() { return false; } if t.contains('=') || t.starts_with('>') { return true; } if t.chars() .any(|c| matches!(c, '+' | '-' | '*' | '/' | '(' | ')' | '[' | ']')) { return true; } let parts: Vec<&str> = t.split_whitespace().collect(); if parts.len() >= 2 { return parts[1..].iter().any(|p| { p.contains('=') || p.starts_with('"') || p.starts_with('\'') || p.starts_with('`') || p.chars().all(|c| c.is_ascii_digit()) || (p.contains('.') && p.chars() .filter(|c| *c != '.') .all(|c| c.is_ascii_digit())) }); } false } /// Whole-line backtick bind `` `name` = … `` (empty RHS / fence / table OK). /// /// Narrative decls are **not** binds: /// - `` `捕获=True` 时… `` (`=` inside ticks) /// - `` `n`,我们… `` (no `=` after ticks) /// - `` `礼貌`=False 时… `` (glued default + trailing prose; same shape as prose Decl) fn looks_like_backtick_assign(trimmed: &str) -> bool { let t = trimmed.trim(); if !t.starts_with('`') { return false; } let rest = &t[1..]; let Some(end) = rest.find('`') else { return false; }; let name = &rest[..end]; if name.is_empty() || !is_backtick_bind_name(name) { return false; } let after_tick = &rest[end + 1..]; let after = after_tick.trim_start(); if !after.starts_with('=') { return false; } // Glued `` `name`=default …prose `` → Decl, not assign (prose.rs default terminator). let glued = after_tick.starts_with('='); if glued { let stripped = &after[1..]; let def_end = stripped .find(|c: char| { c.is_whitespace() || matches!(c, ',' | '。' | ';' | '、' | ',' | ';' | '*' | '`') }) .unwrap_or(stripped.len()); if !stripped[def_end..].trim_start().is_empty() { return false; } } true } fn is_backtick_bind_name(name: &str) -> bool { let mut chars = name.chars(); let Some(first) = chars.next() else { return false; }; if !(first.is_alphabetic() || first == '_' || !first.is_ascii()) { return false; } chars.all(|c| c.is_alphanumeric() || c == '_' || !c.is_ascii()) } /// `*` / `**` lines that are Marqdo executable (statement / return / else), not narrative Markdown. /// /// Closing `*` / `**` must not be taken from inside `` `…` `` or `"…"` (GAP-06/07 nested markers). fn is_marqdo_star_line(trimmed: &str) -> bool { // Empty italic return (v0.3) or else `*` / empty returns. if trimmed == "*" || trimmed == "**" || trimmed == "****" { return true; } if trimmed.starts_with("**") { if trimmed.trim() == "** **" { return true; } if matching_bold_close(trimmed).is_some() { return true; } // D9: a bold-starting line whose string literal is never closed has no // findable closing `**` -- the scanner stays "inside the string" forever. // Classifying it as Comment would silently drop the line; treat it as // Code so the parser can report `unterminated " string`. return has_unterminated_string(trimmed); } if !trimmed.starts_with('*') || trimmed.len() < 2 { return false; } matching_italic_close(trimmed).is_some() } /// True when `line` contains an odd number of unescaped double quotes, /// i.e. a string literal that is opened but never closed. /// /// Used only by [`is_marqdo_star_line`] to keep a malformed bold line in the /// Code class so the parser surfaces the error (D9). Prose lines never reach /// here -- they do not start with `**`. pub fn has_unterminated_string(line: &str) -> bool { let bytes = line.as_bytes(); let mut i = 0usize; let mut count = 0usize; while i < bytes.len() { match bytes[i] { b'\\' => i += 2, b'"' => { count += 1; i += 1; } _ => i += 1, } } count % 2 == 1 } /// Closing `**` for a bold segment, skipping strings and backticks. /// Returns the index within `trimmed` of the first `*` of the closing `**`. /// When `require_eol`, the closing `**` must be the last non-ws on the line (whole-line code). pub fn matching_bold_close(trimmed: &str) -> Option { matching_bold_close_opts(trimmed, true) } /// Like [`matching_bold_close`] but allows trailing text after the close (inline prose). pub fn matching_bold_close_inline(trimmed: &str) -> Option { matching_bold_close_opts(trimmed, false) } // An occlusion label is prose: stars, quotes and backticks inside it are not // statement/return delimiters. Payload nesting is tracked by the callers. fn occlusion_payload_start(bytes: &[u8], start: usize) -> Option { if bytes.get(start) != Some(&b'[') { return None; } let mut i = start + 1; while i < bytes.len() { match bytes[i] { b'\\' => i += 2, b']' => return (bytes.get(i + 1) == Some(&b'(')).then_some(i + 1), b'\n' | b'\r' => return None, _ => i += 1, } } None } fn matching_bold_close_opts(trimmed: &str, require_eol: bool) -> Option { let Some(rest) = trimmed.strip_prefix("**") else { return None; }; let bytes = rest.as_bytes(); let mut i = 0usize; let mut in_bt = false; let mut in_str = false; let mut nesting = Vec::new(); while i + 1 < bytes.len() { let c = bytes[i]; if in_str { if c == b'\\' && i + 1 < bytes.len() { i += 2; continue; } if c == b'"' { in_str = false; } i += 1; continue; } if in_bt { if c == b'`' { in_bt = false; } i += 1; continue; } if c == b'[' { if let Some(payload) = occlusion_payload_start(bytes, i) { i = payload; continue; } } match c { b'(' | b'[' | b'{' => { nesting.push(match c { b'(' => b')', b'[' => b']', _ => b'}', }); i += 1; } b')' | b']' | b'}' => { if nesting.last() == Some(&c) { nesting.pop(); } i += 1; } b'`' => { in_bt = true; i += 1; } b'"' => { in_str = true; i += 1; } b'*' if bytes[i + 1] == b'*' && nesting.is_empty() => { let after = i + 2; if !require_eol || rest[after..].trim().is_empty() { return Some(2 + i); } i += 2; } _ => i += 1, } } // Empty bold `****` handled by caller; `**` alone has no close inside rest. if !require_eol && rest.is_empty() { return None; } None } /// Index of the closing `*` for a single-star segment. pub fn matching_italic_close(trimmed: &str) -> Option { matching_italic_close_opts(trimmed, true) } /// Italic close allowing trailing prose after the closing `*`. pub fn matching_italic_close_inline(trimmed: &str) -> Option { matching_italic_close_opts(trimmed, false) } fn matching_italic_close_opts(trimmed: &str, require_eol: bool) -> Option { if trimmed.starts_with("**") { return None; } let bytes = trimmed.as_bytes(); let mut i = 1usize; // skip opening * let mut in_bt = false; let mut in_str = false; let mut nesting = Vec::new(); while i < bytes.len() { let c = bytes[i]; if in_str { if c == b'\\' && i + 1 < bytes.len() { i += 2; continue; } if c == b'"' { in_str = false; } i += 1; continue; } if in_bt { if c == b'`' { in_bt = false; } i += 1; continue; } if c == b'[' { if let Some(payload) = occlusion_payload_start(bytes, i) { i = payload; continue; } } match c { b'(' | b'[' | b'{' => { nesting.push(match c { b'(' => b')', b'[' => b']', _ => b'}', }); i += 1; } b')' | b']' | b'}' => { if nesting.last() == Some(&c) { nesting.pop(); } i += 1; } b'`' => { in_bt = true; i += 1; } b'"' => { in_str = true; i += 1; } b'*' if nesting.is_empty() => { if !require_eol || trimmed[i + 1..].trim().is_empty() { return Some(i); } i += 1; } _ => i += 1, } } None } /// Headings / calls / finished star lines should escape a narrative paragraph (GAP-06/07 recovery). fn breaks_comment_paragraph(trimmed: &str) -> bool { if is_structural_code_line(trimmed) { return true; } if trimmed.starts_with('#') || trimmed.starts_with('>') { return true; } if is_marqdo_star_line(trimmed) { return true; } // Branch arms / loops / params must not be swallowed after prose in an arm body. if looks_like_ordered_list_item(trimmed) { return true; } if trimmed.starts_with("- ") || trimmed.starts_with("-\t") || trimmed == "-" { return true; } if trimmed.starts_with('+') { return true; } if is_image_writeback_line(trimmed) { return true; } false } /// `![marqdo-out …](image-url)` — image writeback surface (see stdlib-writeback). pub fn is_image_writeback_line(trimmed: &str) -> bool { let t = trimmed.trim(); let Some(rest) = t.strip_prefix("![") else { return false; }; let Some(alt_end) = rest.find("](") else { return false; }; let alt = rest[..alt_end].trim(); alt.split_whitespace().any(|tok| tok == "marqdo-out") } fn looks_like_ordered_list_item(trimmed: &str) -> bool { let mut chars = trimmed.chars(); let mut saw_digit = false; while let Some(c) = chars.next() { if c.is_ascii_digit() { saw_digit = true; continue; } if c == '.' && saw_digit { return true; } return false; } false } /// Classify a single logical line (no trailing newline required). /// /// This is the **paragraph-start** rule only. Prefer [`classify_source`] for /// full-file classification (paragraph continuation). pub fn classify_line(text: &str) -> LineKind { let trimmed = text.trim_end_matches(['\r', '\n']); if trimmed.chars().all(|c| c.is_whitespace()) { return LineKind::Blank; } let trimmed = text.trim(); let first = trimmed.chars().find(|c| !c.is_whitespace()); match first { None => LineKind::Blank, Some('*') if !is_marqdo_star_line(trimmed) => LineKind::Comment, Some('`') if looks_like_backtick_assign(trimmed) => LineKind::Code, Some('`') => LineKind::Comment, Some(c) if is_code_starter(c) => LineKind::Code, Some(_) => LineKind::Comment, } } /// Classify every line of a source file (blank-line paragraph comments). /// /// Structural end markers (`---` / `***` / empty `****`) always stay Code and /// break a comment paragraph, so frontmatter closers and function ends work /// even when adjacent to narrative. pub fn classify_source(source: &str) -> Vec { let source = source.strip_prefix('\u{feff}').unwrap_or(source); let lines: Vec<&str> = if source.is_empty() { Vec::new() } else { source.lines().collect() }; let mut out = Vec::with_capacity(lines.len()); let mut in_comment_paragraph = false; let mut in_math_fence = false; let mut in_md_fence = false; let mut in_writeback = false; for (i, text) in lines.into_iter().enumerate() { let base = classify_line(text); let trimmed = text.trim(); let structural = is_structural_code_line(trimmed); let kind = if in_writeback { if trimmed.contains("-->") { in_writeback = false; } LineKind::Writeback } else if in_math_fence { if trimmed == "$$" { in_math_fence = false; } LineKind::Code } else if in_md_fence { // Markdown ``` fences are narrative / foreign sources — not Marqdo stmts. if is_md_fence_line(trimmed) { in_md_fence = false; } LineKind::Comment } else if is_md_fence_opener(trimmed) { in_md_fence = true; in_comment_paragraph = false; LineKind::Comment } else if trimmed.starts_with("") { in_writeback = false; } LineKind::Writeback } else if is_image_writeback_line(trimmed) { in_comment_paragraph = false; LineKind::Writeback } else { match base { LineKind::Blank => { in_comment_paragraph = false; LineKind::Blank } LineKind::Comment => { in_comment_paragraph = true; LineKind::Comment } LineKind::Code if structural || breaks_comment_paragraph(trimmed) => { in_comment_paragraph = false; LineKind::Code } LineKind::Code if in_comment_paragraph => LineKind::Comment, LineKind::Writeback => LineKind::Writeback, LineKind::Code => { // Multi-line math fence opener (not same-line `$$…$$`). if trimmed == "$$" { in_math_fence = true; } LineKind::Code } } }; out.push(ClassifiedLine { line_no: (i + 1) as u32, kind, text: text.to_string(), }); } out } fn is_md_fence_opener(trimmed: &str) -> bool { trimmed.starts_with("```") && !trimmed .chars() .skip_while(|c| *c == '`') .all(|c| c.is_whitespace()) } fn is_md_fence_line(trimmed: &str) -> bool { trimmed.starts_with("```") } /// Function-end / frontmatter HR or empty bold return — never swallowed by paragraphs. fn is_structural_code_line(trimmed: &str) -> bool { if trimmed.len() >= 3 && (trimmed.chars().all(|c| c == '-') || trimmed.chars().all(|c| c == '*')) { return true; } false } /// Human-readable dump for `--dump-lines`. pub fn format_lines_dump(path: &str, lines: &[ClassifiedLine]) -> String { let mut out = String::new(); out.push_str(&format!("=== marqdo: lines ({path}) ===\n")); for line in lines { let preview = if line.text.len() > 80 { format!("{}…", &line.text.chars().take(80).collect::()) } else { line.text.clone() }; match line.kind { LineKind::Blank => { out.push_str(&format!("{:4} {:7}\n", line.line_no, line.kind)); } _ => { out.push_str(&format!( "{:4} {:7} | {}\n", line.line_no, line.kind, preview )); } } } out.push_str("=== marqdo: end lines ===\n"); out } #[cfg(test)] mod tests { use super::*; #[test] fn ordered_arm_breaks_prose_paragraph() { let src = "\ # main\n\ \n\ 1. `n` > 0\n\ 此时我们 **打印 内容=\"正\"**\n\ 2. *\n\ 否则 **打印 内容=\"非正\"**\n\ "; let lines = classify_source(src); let l12 = lines.iter().find(|l| l.text.trim() == "2. *").expect("2. *"); assert_eq!( l12.kind, LineKind::Code, "else arm must stay Code after prose, got dump:\n{}", format_lines_dump("t", &lines) ); } #[test] fn blank_and_comment_and_code() { assert_eq!(classify_line(""), LineKind::Blank); assert_eq!(classify_line(" "), LineKind::Blank); assert_eq!(classify_line("hello world"), LineKind::Comment); assert_eq!(classify_line(" 中文说明"), LineKind::Comment); assert_eq!(classify_line("> print text=hi"), LineKind::Code); assert_eq!(classify_line("# main"), LineKind::Code); assert_eq!(classify_line("*`x` = 1*"), LineKind::Code); assert_eq!(classify_line("**ret**"), LineKind::Code); assert_eq!(classify_line("+ `x` > 0"), LineKind::Code); assert_eq!(classify_line("- item"), LineKind::Code); assert_eq!(classify_line("1. arm"), LineKind::Code); assert_eq!(classify_line("| a |"), LineKind::Code); assert_eq!(classify_line("`x` = 1"), LineKind::Code); assert_eq!( classify_line("`捕获=True` 时返回映射。"), LineKind::Comment, "prose decl / narrative, not backtick assign" ); assert_eq!( classify_line("`n`,我们执行操作"), LineKind::Comment, "leading decl in narrative" ); assert_eq!(classify_line("`礼貌`=False"), LineKind::Code); assert_eq!( classify_line("`礼貌`=False 时用短称呼。"), LineKind::Comment, "glued default + trailing prose is Decl" ); assert_eq!( classify_line("`礼貌` = False"), LineKind::Code, "spaced = is backtick assign" ); } #[test] fn narrative_bold_in_comment_paragraph() { let src = "说明如下:\n\n**强调信息**是什么什么\n\n> print text=ok\n"; let lines = classify_source(src); assert_eq!(lines[0].kind, LineKind::Comment); assert_eq!(lines[1].kind, LineKind::Blank); assert_eq!(lines[2].kind, LineKind::Comment, "bold prose is comment"); assert_eq!(lines[3].kind, LineKind::Blank); assert_eq!(lines[4].kind, LineKind::Code); } #[test] fn writeback_block_classified() { let src = "*`x` = 1*\n\n"; let lines = classify_source(src); assert_eq!(lines[0].kind, LineKind::Code); assert_eq!(lines[1].kind, LineKind::Writeback); assert_eq!(lines[2].kind, LineKind::Writeback); assert_eq!(lines[3].kind, LineKind::Writeback); } #[test] fn image_writeback_line_classified() { assert!(is_image_writeback_line("![marqdo-out](a.svg)")); assert!(is_image_writeback_line("![marqdo-out ok](plots/x.png)")); assert!(!is_image_writeback_line("![plot](a.svg)")); let src = "# main\n\n**x = 1**\n![marqdo-out](out.svg)\n\n> print text=x\n"; let lines = classify_source(src); let img = lines.iter().find(|l| l.text.contains("marqdo-out]")).unwrap(); assert_eq!(img.kind, LineKind::Writeback, "{:?}", lines); } #[test] fn paragraph_comment_keeps_backtick_continuation() { let src = "采用了\n`.mq.md`的双层后缀\n\n> print text=ok\n"; let lines = classify_source(src); assert_eq!(lines[0].kind, LineKind::Comment); assert_eq!(lines[1].kind, LineKind::Comment); assert_eq!(lines[2].kind, LineKind::Blank); assert_eq!(lines[3].kind, LineKind::Code); } #[test] fn frontmatter_closing_hr_stays_code() { let src = "---\ntitle: x\n---\n\n# main\n"; let lines = classify_source(src); assert_eq!(lines[0].kind, LineKind::Code); assert_eq!(lines[1].kind, LineKind::Comment); assert_eq!(lines[2].kind, LineKind::Code); assert_eq!(lines[2].text.trim(), "---"); } #[test] fn classify_hello_fixture() { let src = include_str!("../../tests/structure/hello.mq.md"); let lines = classify_source(src); assert!(lines.iter().any(|l| l.kind == LineKind::Comment)); assert!(lines.iter().any(|l| l.kind == LineKind::Code)); } #[test] fn unterminated_quote_inside_bold_is_code_not_comment() { // D9: "**s = \"abc" has an unclosed string. Classification must keep it // as Code so the parser reports "unterminated \" string" instead of // silently demoting the line to narration. let lines = classify_source("**s = \"abc\n"); let code = lines.iter().find(|l| l.text.contains("abc")); assert!( matches!(code.map(|l| l.kind), Some(LineKind::Code)), "expected Code (so parser reports the error), got {lines:?}" ); } #[test] fn balanced_quote_inside_bold_still_code() { let lines = classify_source("**s = \"abc\"**\n"); let code = lines.iter().find(|l| l.text.contains("abc")); assert!(matches!(code.map(|l| l.kind), Some(LineKind::Code))); } #[test] fn prose_with_quotes_stays_comment() { // Narration that merely mentions quotes must stay narration. let lines = classify_source("他说 \"你好\",然后走了。\n"); assert!(matches!(lines.first().map(|l| l.kind), Some(LineKind::Comment))); } #[test] fn prose_bold_emphasis_with_quote_stays_comment() { // **强调** with a stray quote in surrounding prose is still narration. let lines = classify_source("这一句有 \" 引号但不是代码。\n"); assert!(matches!(lines.first().map(|l| l.kind), Some(LineKind::Comment))); } #[test] fn math_fence_body_stays_code() { let lines = classify_source("`f` =\n$$\nx^2 - 2\n$$\n"); assert!( lines .iter() .all(|l| l.kind == LineKind::Code || l.kind == LineKind::Blank), "expected fence body as Code, got {lines:?}" ); } #[test] fn star_line_classification_is_star_terminated() { // A `*…*` Marqdo statement line must END with `*` to be treated as code. // If it accidentally ends with a backtick (e.g. `table="t"`), the line // falls back to narrative Comment — but later executable lines recover // (GAP-06/07: do not swallow the rest of a `##` body). let star_term = "*c = > `store`.count table=\"t\"*"; assert!( is_marqdo_star_line(star_term), "star-terminated assignment must be code" ); assert_eq!(classify_line(star_term), LineKind::Code); let nested_bt = "*code = > sys.exec cmd=\"python3\" args=`args`*"; assert!( is_marqdo_star_line(nested_bt), "args=`args`* must still be a star line" ); let nested_json = "*args = > json.parse text=[\"scripts/legacy/web_search.py\"]*"; assert!( is_marqdo_star_line(nested_json), "text=[…] * must still be a star line" ); assert!( is_marqdo_star_line("*x = > f text=`a*b`*"), "* inside backticks must not steal the closer" ); let backtick_term = "*c = > `store`.count table=\"t\"`"; assert!( !is_marqdo_star_line(backtick_term), "backtick-terminated assignment must NOT be marqdo star line" ); assert_eq!(classify_line(backtick_term), LineKind::Comment); // Bad terminator → comment, but following `>` / `*…*` escape the paragraph. let src = "> `store`.insert table=t rows=`数据`\n\n*c = > `store`.count table=\"t\"`\n> print text=count1-done\n*y = 1*\n"; let lines = classify_source(src); assert_eq!(lines[2].kind, LineKind::Comment, "bad terminator → comment"); assert_eq!(lines[3].kind, LineKind::Code, "call recovers after bad line"); assert_eq!(lines[4].kind, LineKind::Code, "assign recovers after bad line"); } } #[cfg(test)] mod inline_occlusion_boundaries { use super::*; #[test] fn prose_stars_and_payload_products_keep_the_real_return_boundary() { let source = r#"*[multiply x*y and keep "raw" text](2 * (3 + 1))* after the result"#; assert_eq!(matching_italic_close_inline(source), source.find("* after")); let source = r#"**answer = [render **literal** marker](helper(text="**", value=(2 * 3)))** then continue"#; assert_eq!(matching_bold_close_inline(source), source.find("** then")); } }