feat(markup): prefer [key](collection) for map and list access
Add GFM link-shaped index as the primary get surface, keep footnote index as transitional compatibility, and align the coffee example. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
5e74ff39f2
commit
4a1c9c48a2
12 files changed
+235
-23
No files matched your search
+6
-5
@@ -145,10 +145,11 @@ pub enum Expr {
|
||||
List(Vec<Expr>),
|
||||
/// Object literal / horizontal table (`map` value).
|
||||
Map(Vec<(String, Expr)>),
|
||||
/// Footnote index: `` `xs`[^1] `` / `` `m`[^key] `` / `` `m`[^`var`] `` (dynamic key).
|
||||
/// Collection index: `` [拿铁](菜单) `` / `` [`名`](菜单) `` (preferred), or
|
||||
/// legacy footnote `` `菜单`[^拿铁] `` / `` `菜单`[^`名`] ``.
|
||||
Index {
|
||||
base: Box<Expr>,
|
||||
/// Label inside `[^…]` (digits → 1-based list index, or map key; see [`IndexKey`]).
|
||||
/// Key or 1-based list index; see [`IndexKey`].
|
||||
label: IndexKey,
|
||||
},
|
||||
/// Parsed `$$…$$` formula tree (from assignment RHS).
|
||||
@@ -166,12 +167,12 @@ pub enum InterpPart {
|
||||
Index { base: String, labels: Vec<String> },
|
||||
}
|
||||
|
||||
/// Footnote / map key after `[^…]`.
|
||||
/// Map / list index key (`[key](coll)` or legacy `[^…]`).
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum IndexKey {
|
||||
/// Static text: `[^拿铁]` / `[^1]`.
|
||||
/// Static text: `[拿铁](…)` / `[^拿铁]` / `[^1]`.
|
||||
Lit(String),
|
||||
/// Dynamic: `` [^`品名`] `` — evaluate variable, then stringify as key.
|
||||
/// Dynamic: `` [`品名`](…) `` / `` [^`品名`] `` — evaluate variable, then stringify as key.
|
||||
Var(String),
|
||||
}
|
||||
|
||||
|
||||
@@ -461,6 +461,11 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
return Ok(e);
|
||||
}
|
||||
// Link-shaped index: `[key](collection)` / `` [`var`](collection) ``
|
||||
// (same Markdown shape as foreach `- [item](coll)`; see collection-access-link.md).
|
||||
if self.starts_with("[") && !self.starts_with("[^") {
|
||||
return self.parse_link_index();
|
||||
}
|
||||
if self.eat("\"") {
|
||||
return self.parse_quoted_string('"');
|
||||
}
|
||||
@@ -608,6 +613,69 @@ impl<'a> Parser<'a> {
|
||||
Ok(expr)
|
||||
}
|
||||
|
||||
/// `[key](collection)` — preferred collection get (GFM link shape).
|
||||
fn parse_link_index(&mut self) -> Result<Expr> {
|
||||
if !self.eat("[") {
|
||||
bail!("expected '[' for link index");
|
||||
}
|
||||
self.skip_ws();
|
||||
let label = if self.eat("`") {
|
||||
let start = self.i;
|
||||
while let Some(c) = self.peek_char() {
|
||||
if c == '`' {
|
||||
break;
|
||||
}
|
||||
if c == '\n' || c == '\r' || c == ']' {
|
||||
bail!("unterminated link-index var `[`…`](…)`");
|
||||
}
|
||||
self.bump_char();
|
||||
}
|
||||
let name = self.src[start..self.i].trim().to_string();
|
||||
if !self.eat("`") {
|
||||
bail!("unterminated link-index var `[`…`](…)`");
|
||||
}
|
||||
if name.is_empty() {
|
||||
bail!("empty link-index var `[`](…)`");
|
||||
}
|
||||
IndexKey::Var(name)
|
||||
} else {
|
||||
let start = self.i;
|
||||
while let Some(c) = self.peek_char() {
|
||||
if c == ']' {
|
||||
break;
|
||||
}
|
||||
if c == '\n' || c == '\r' || c == '[' {
|
||||
bail!("unterminated or nested link-index key `[…](…)`");
|
||||
}
|
||||
self.bump_char();
|
||||
}
|
||||
let key = self.src[start..self.i].trim().to_string();
|
||||
if key.is_empty() {
|
||||
bail!("empty link-index key `[](…)`");
|
||||
}
|
||||
IndexKey::Lit(key)
|
||||
};
|
||||
self.skip_ws();
|
||||
if !self.eat("]") {
|
||||
bail!("expected ']' after link-index key");
|
||||
}
|
||||
self.skip_ws();
|
||||
if !self.eat("(") {
|
||||
bail!("expected '(' collection after link-index `[key]`");
|
||||
}
|
||||
let base = self.parse_or()?;
|
||||
self.skip_ws();
|
||||
if !self.eat(")") {
|
||||
bail!("expected ')' after link-index collection");
|
||||
}
|
||||
let expr = Expr::Index {
|
||||
base: Box::new(base),
|
||||
label,
|
||||
};
|
||||
// Legacy: allow trailing footnote chain on the result.
|
||||
self.parse_index_chain(expr)
|
||||
}
|
||||
|
||||
/// `"..."` or `'...'` with escapes and `` `var` `` interpolation.
|
||||
fn parse_quoted_string(&mut self, quote: char) -> Result<Expr> {
|
||||
use crate::ast::InterpPart;
|
||||
@@ -1327,6 +1395,48 @@ mod tests {
|
||||
} if s == "拿铁"
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn link_shaped_index() {
|
||||
let e = parse_expr_prefer_var("[拿铁](菜单)").unwrap();
|
||||
match e {
|
||||
Expr::Index { base, label } => {
|
||||
assert!(matches!(base.as_ref(), Expr::Var(n) if n == "菜单"));
|
||||
assert_eq!(label, IndexKey::Lit("拿铁".into()));
|
||||
}
|
||||
other => panic!("expected link Index, got {other:?}"),
|
||||
}
|
||||
let e = parse_expr_prefer_var("[`名`](菜单)").unwrap();
|
||||
match e {
|
||||
Expr::Index { base, label } => {
|
||||
assert!(matches!(base.as_ref(), Expr::Var(n) if n == "菜单"));
|
||||
assert_eq!(label, IndexKey::Var("名".into()));
|
||||
}
|
||||
other => panic!("expected dynamic link Index, got {other:?}"),
|
||||
}
|
||||
let e = parse_expr_prefer_var("[1](篮子)").unwrap();
|
||||
assert!(matches!(
|
||||
e,
|
||||
Expr::Index {
|
||||
label: IndexKey::Lit(ref s),
|
||||
..
|
||||
} if s == "1"
|
||||
));
|
||||
let e = parse_expr_prefer_var("[1]([苹果](货架))").unwrap();
|
||||
match e {
|
||||
Expr::Index { base, label } => {
|
||||
assert_eq!(label, IndexKey::Lit("1".into()));
|
||||
assert!(matches!(
|
||||
base.as_ref(),
|
||||
Expr::Index {
|
||||
label: IndexKey::Lit(ref k),
|
||||
..
|
||||
} if k == "苹果"
|
||||
));
|
||||
}
|
||||
other => panic!("expected nested link Index, got {other:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -1345,7 +1345,8 @@ fn expr_prec(expr: &Expr, parent_prec: u8) -> String {
|
||||
crate::ast::IndexKey::Lit(s) => s.clone(),
|
||||
crate::ast::IndexKey::Var(n) => format!("`{n}`"),
|
||||
};
|
||||
format!("{}[^{}]", expr_prec(base, 8), key)
|
||||
// Preferred surface: GFM link shape (legacy footnote still accepted by parser).
|
||||
format!("[{}]({})", key, expr_prec(base, 0))
|
||||
}
|
||||
Expr::Formula(e) => format!("$$ {} $$", e.as_display()),
|
||||
Expr::Code(c) => format!("```{} …```", c.lang),
|
||||
|
||||
Reference in new issue
Block a user