From 6f303ff95700beeeda0cafebb71f891031cfdbf9 Mon Sep 17 00:00:00 2001 From: cflmy Date: Mon, 14 Sep 2026 14:39:58 +0800 Subject: [PATCH] fix(parse): diagnose unclosed code-shaped bold Treat `` **x = 1 `` as Code and error with a clear unclosed-`**` diagnostic instead of silently skipping it as a comment. Co-authored-by: Cursor --- doc/roadmap/markup-v0.3.md | 3 +- src/lex/mod.rs | 52 ++++++++++++++++++++++++++++++++ src/parse/mod.rs | 13 ++++++++ src/parse/prose.rs | 31 ++----------------- tests/errors/unclosed-bold.mq.md | 8 +++++ tests/gold.rs | 9 ++++++ 6 files changed, 86 insertions(+), 30 deletions(-) create mode 100644 tests/errors/unclosed-bold.mq.md diff --git a/doc/roadmap/markup-v0.3.md b/doc/roadmap/markup-v0.3.md index 9f6e2d1..51704d8 100644 --- a/doc/roadmap/markup-v0.3.md +++ b/doc/roadmap/markup-v0.3.md @@ -53,7 +53,8 @@ - [x] else 臂 `N. *` 与返回斜体消歧 - [x] 一行多 `**…**` 按序执行 - [x] 金样例:`---` / `*None*` / 整行 `**` / 兼容 `****`;叙述死绑定不升参 -- [ ] 诊断:必选缺失、未知 callee;空返回残缺粗体;死变量仅可选 lint +- [x] 诊断:必选缺失、未知 callee(已有 errors 金样);未闭合代码形粗体;死变量不报错 +- [ ] (可选)死变量 hint lint;空返回其它歧义边角 **出门:** `tests/markup-v03/*` 覆盖设计 §9 示例。 diff --git a/src/lex/mod.rs b/src/lex/mod.rs index 97ea09c..423e898 100644 --- a/src/lex/mod.rs +++ b/src/lex/mod.rs @@ -43,6 +43,47 @@ fn is_code_starter(c: char) -> bool { ) || 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 +} + +/// `` **code-shaped… `` without a closing `**` (should diagnose, not skip as comment). +fn looks_like_unclosed_bold_code(trimmed: &str) -> bool { + let t = trimmed.trim(); + if !t.starts_with("**") || matching_bold_close(t).is_some() { + return false; + } + // Whole-line empty markers are handled by is_marqdo_star_line. + looks_like_bold_code(&t[2..]) +} + /// Whole-line backtick bind `` `name` = … `` (empty RHS / fence / table OK). /// /// Narrative decls are **not** binds: @@ -253,6 +294,7 @@ pub fn classify_line(text: &str) -> LineKind { let first = trimmed.chars().find(|c| !c.is_whitespace()); match first { None => LineKind::Blank, + Some('*') if looks_like_unclosed_bold_code(trimmed) => LineKind::Code, Some('*') if !is_marqdo_star_line(trimmed) => LineKind::Comment, Some('`') if looks_like_backtick_assign(trimmed) => LineKind::Code, Some('`') => LineKind::Comment, @@ -433,6 +475,16 @@ mod tests { LineKind::Code, "spaced = is backtick assign" ); + assert_eq!( + classify_line("**x = 1"), + LineKind::Code, + "unclosed code-shaped bold → Code for diagnosis" + ); + assert_eq!( + classify_line("**说明"), + LineKind::Comment, + "unclosed soft emphasis stays comment" + ); } #[test] diff --git a/src/parse/mod.rs b/src/parse/mod.rs index d43f84c..7a246cf 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -363,6 +363,19 @@ impl<'a> Cursor<'a> { }); } + // Unclosed code-shaped bold (classified Code so we can diagnose). + if trimmed.starts_with("**") + && !(trimmed.ends_with("**") && trimmed.len() >= 4) + && crate::lex::looks_like_bold_code(&trimmed[2..]) + { + return Err(Diagnostic::new( + None, + span, + "unclosed bold code: expected closing `**`".to_string(), + ) + .into()); + } + // v0.3: **…** = code segment (assign / call / expr) if trimmed.starts_with("**") && trimmed.ends_with("**") && trimmed.len() >= 4 { let inner = &trimmed[2..trimmed.len() - 2]; diff --git a/src/parse/prose.rs b/src/parse/prose.rs index 9f0bd1c..586d2f8 100644 --- a/src/parse/prose.rs +++ b/src/parse/prose.rs @@ -5,6 +5,8 @@ use crate::lex::{matching_bold_close_inline, matching_italic_close_inline}; +pub use crate::lex::looks_like_bold_code; + #[derive(Debug, Clone, PartialEq, Eq)] pub enum ProseBit { /// `` `ident` `` or `` `ident`=default `` (default string is raw RHS text). @@ -71,35 +73,6 @@ pub fn scan_prose_line(line: &str) -> Vec { out } -/// 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 -} - /// Whether `*inner*` is a return (vs mid-prose English/CJK emphasis). fn looks_like_italic_return(line: &str, at: usize, after: usize, inner: &str) -> bool { let t = inner.trim(); diff --git a/tests/errors/unclosed-bold.mq.md b/tests/errors/unclosed-bold.mq.md new file mode 100644 index 0000000..5d5f1db --- /dev/null +++ b/tests/errors/unclosed-bold.mq.md @@ -0,0 +1,8 @@ +--- +title: unclosed bold code +description: code-shaped `**…` without closing `**` must diagnose +--- + +# main + +**x = 1 diff --git a/tests/gold.rs b/tests/gold.rs index 5b73cc6..66403a0 100644 --- a/tests/gold.rs +++ b/tests/gold.rs @@ -610,6 +610,15 @@ fn error_syntax_bad_line() { ); } +#[test] +fn error_unclosed_bold() { + assert_err( + "tests/errors/unclosed-bold.mq.md", + "8:1", + "unclosed bold code", + ); +} + #[test] fn error_div_zero() { assert_err("tests/errors/div-zero.mq.md", "7:1", "division by zero");