Treat bare ids as variables in italic/bold, and land table/quantum follow-ups.
Prefer-var parsing in *…* / **…* (quoted strings for text), table cell expressions, digit map footnotes, and quantum steps/draw with updated golds and LLM sources. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
ff6425e6c5
commit
91d51f3f0a
63 files changed
+1542
-477
No files matched your search
+132
-34
@@ -5,7 +5,16 @@ use anyhow::{bail, Result};
|
||||
use crate::ast::{BinaryOp, CallExpr, Expr, InterpPart, Literal, UnaryOp};
|
||||
|
||||
pub fn parse_expr(input: &str) -> Result<Expr> {
|
||||
let mut p = Parser::new(input.trim());
|
||||
parse_expr_mode(input, false)
|
||||
}
|
||||
|
||||
/// Italic / bold value syntax: bare words are variables; text needs `"…"` / `'…'`.
|
||||
pub fn parse_expr_prefer_var(input: &str) -> Result<Expr> {
|
||||
parse_expr_mode(input, true)
|
||||
}
|
||||
|
||||
fn parse_expr_mode(input: &str, prefer_var: bool) -> Result<Expr> {
|
||||
let mut p = Parser::new(input.trim(), prefer_var);
|
||||
let e = p.parse_or()?;
|
||||
p.skip_ws();
|
||||
if !p.rest().is_empty() {
|
||||
@@ -22,7 +31,7 @@ pub fn parse_value_or_interp(input: &str) -> Result<Expr> {
|
||||
if s.is_empty() {
|
||||
return Ok(Expr::Literal(Literal::Text(String::new())));
|
||||
}
|
||||
if s.starts_with('"') {
|
||||
if s.starts_with('"') || s.starts_with('\'') {
|
||||
return match parse_expr(s) {
|
||||
Ok(e) => Ok(e),
|
||||
Err(_) => Ok(parse_interp(s)),
|
||||
@@ -45,7 +54,7 @@ pub fn parse_call_arg_value(input: &str) -> Result<Expr> {
|
||||
if s.is_empty() {
|
||||
return Ok(Expr::Literal(Literal::Text(String::new())));
|
||||
}
|
||||
if s.starts_with('"') {
|
||||
if s.starts_with('"') || s.starts_with('\'') {
|
||||
return match parse_expr(s) {
|
||||
Ok(e) => Ok(e),
|
||||
Err(_) => Ok(parse_interp(s)),
|
||||
@@ -56,7 +65,15 @@ pub fn parse_call_arg_value(input: &str) -> Result<Expr> {
|
||||
}
|
||||
match parse_expr(s) {
|
||||
Ok(e) => Ok(collapse_text_div_path(e, s)),
|
||||
Err(_) => Ok(parse_interp(s)),
|
||||
Err(_) => {
|
||||
// ext/web paths: `nav.`nav``, `articles.`articles`.title` — ticks are path
|
||||
// punctuation, not interpolation.
|
||||
if s.contains(".`") {
|
||||
Ok(Expr::Literal(Literal::Text(s.to_string())))
|
||||
} else {
|
||||
Ok(parse_interp(s))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,11 +162,17 @@ pub fn parse_interp(s: &str) -> Expr {
|
||||
struct Parser<'a> {
|
||||
src: &'a str,
|
||||
i: usize,
|
||||
/// When true (italic/bold), bare words are variables, not text literals.
|
||||
prefer_var: bool,
|
||||
}
|
||||
|
||||
impl<'a> Parser<'a> {
|
||||
fn new(src: &'a str) -> Self {
|
||||
Self { src, i: 0 }
|
||||
fn new(src: &'a str, prefer_var: bool) -> Self {
|
||||
Self {
|
||||
src,
|
||||
i: 0,
|
||||
prefer_var,
|
||||
}
|
||||
}
|
||||
|
||||
fn rest(&self) -> &str {
|
||||
@@ -348,7 +371,10 @@ impl<'a> Parser<'a> {
|
||||
return Ok(e);
|
||||
}
|
||||
if self.eat("\"") {
|
||||
return self.parse_quoted_string();
|
||||
return self.parse_quoted_string('"');
|
||||
}
|
||||
if self.eat("'") {
|
||||
return self.parse_quoted_string('\'');
|
||||
}
|
||||
if self.eat("`") {
|
||||
let start = self.i;
|
||||
@@ -362,20 +388,7 @@ impl<'a> Parser<'a> {
|
||||
if !self.eat("`") {
|
||||
bail!("unterminated `name`");
|
||||
}
|
||||
let mut expr = Expr::Var(name);
|
||||
// Postfix footnotes: `xs`[^1] / `m`[^key] / chained `m`[^k][^1]
|
||||
loop {
|
||||
self.skip_ws();
|
||||
if !self.starts_with("[^") {
|
||||
break;
|
||||
}
|
||||
let label = self.parse_footnote_label()?;
|
||||
expr = Expr::Index {
|
||||
base: Box::new(expr),
|
||||
label,
|
||||
};
|
||||
}
|
||||
return Ok(expr);
|
||||
return self.parse_index_chain(Expr::Var(name));
|
||||
}
|
||||
if self.eat(">") {
|
||||
// Inline call: `> name k=v`
|
||||
@@ -417,39 +430,83 @@ impl<'a> Parser<'a> {
|
||||
let n: i64 = self.src[start..self.i].parse()?;
|
||||
return Ok(Expr::Literal(Literal::Int(n)));
|
||||
}
|
||||
// Bare identifier / text word
|
||||
// Bare identifier / text word. If immediately followed by `[^…]`, treat as
|
||||
// variable + footnote index (exemption: ticks optional when `[^` marks the slot).
|
||||
if self.peek_char().map(|c| c.is_alphanumeric() || c == '_' || !c.is_ascii()).unwrap_or(false)
|
||||
{
|
||||
let start = self.i;
|
||||
while let Some(c) = self.peek_char() {
|
||||
if c.is_whitespace() || matches!(c, '=' | '+' | '-' | '*' | '/' | '<' | '>' | '(' | ')' | '`' | '|' | ',' | '"')
|
||||
if c.is_whitespace()
|
||||
|| matches!(
|
||||
c,
|
||||
'=' | '+'
|
||||
| '-'
|
||||
| '*'
|
||||
| '/'
|
||||
| '<'
|
||||
| '>'
|
||||
| '('
|
||||
| ')'
|
||||
| '`'
|
||||
| '|'
|
||||
| ','
|
||||
| '"'
|
||||
| '\''
|
||||
| '['
|
||||
| ']'
|
||||
)
|
||||
{
|
||||
break;
|
||||
}
|
||||
self.bump_char();
|
||||
}
|
||||
let word = &self.src[start..self.i];
|
||||
return Ok(Expr::Literal(Literal::Text(word.to_string())));
|
||||
let word = self.src[start..self.i].to_string();
|
||||
self.skip_ws();
|
||||
if self.starts_with("[^") {
|
||||
return self.parse_index_chain(Expr::Var(word));
|
||||
}
|
||||
if self.prefer_var {
|
||||
return Ok(Expr::Var(word));
|
||||
}
|
||||
return Ok(Expr::Literal(Literal::Text(word)));
|
||||
}
|
||||
bail!("unexpected expression input: {:?}", self.rest());
|
||||
}
|
||||
|
||||
/// `"..."` string with `\n` `\t` `\r` `\\` `\"` and `` `var` `` interpolation.
|
||||
fn parse_quoted_string(&mut self) -> Result<Expr> {
|
||||
/// `` `xs`[^1] `` / `m[^key]` / chained `m[^k][^1]` (base already parsed).
|
||||
fn parse_index_chain(&mut self, mut expr: Expr) -> Result<Expr> {
|
||||
loop {
|
||||
self.skip_ws();
|
||||
if !self.starts_with("[^") {
|
||||
break;
|
||||
}
|
||||
let label = self.parse_footnote_label()?;
|
||||
expr = Expr::Index {
|
||||
base: Box::new(expr),
|
||||
label,
|
||||
};
|
||||
}
|
||||
Ok(expr)
|
||||
}
|
||||
|
||||
/// `"..."` or `'...'` with escapes and `` `var` `` interpolation.
|
||||
fn parse_quoted_string(&mut self, quote: char) -> Result<Expr> {
|
||||
use crate::ast::InterpPart;
|
||||
let mut parts: Vec<InterpPart> = Vec::new();
|
||||
let mut lit = String::new();
|
||||
loop {
|
||||
let Some(c) = self.peek_char() else {
|
||||
bail!("unterminated \" string");
|
||||
bail!("unterminated {quote} string");
|
||||
};
|
||||
if c == '"' {
|
||||
if c == quote {
|
||||
self.bump_char();
|
||||
break;
|
||||
}
|
||||
if c == '\\' {
|
||||
self.bump_char();
|
||||
let esc = self.peek_char().ok_or_else(|| anyhow::anyhow!("dangling \\ in string"))?;
|
||||
let esc = self
|
||||
.peek_char()
|
||||
.ok_or_else(|| anyhow::anyhow!("dangling \\ in string"))?;
|
||||
self.bump_char();
|
||||
lit.push(match esc {
|
||||
'n' => '\n',
|
||||
@@ -457,6 +514,7 @@ impl<'a> Parser<'a> {
|
||||
'r' => '\r',
|
||||
'\\' => '\\',
|
||||
'"' => '"',
|
||||
'\'' => '\'',
|
||||
other => bail!("unknown escape \\{other} in string"),
|
||||
});
|
||||
continue;
|
||||
@@ -756,11 +814,51 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn cmp_and_arith() {
|
||||
let e = parse_expr("`x` > 0").unwrap();
|
||||
assert!(matches!(e, Expr::Binary { op: BinaryOp::Gt, .. }));
|
||||
let e = parse_expr("`n` + 1").unwrap();
|
||||
fn bare_var_footnote_index() {
|
||||
let e = parse_expr("ev[^result]").unwrap();
|
||||
match e {
|
||||
Expr::Index { base, label } => {
|
||||
assert!(matches!(base.as_ref(), Expr::Var(n) if n == "ev"));
|
||||
assert_eq!(label, "result");
|
||||
}
|
||||
other => panic!("expected Index, got {other:?}"),
|
||||
}
|
||||
let e = parse_expr("data[^choices][^1][^message]").unwrap();
|
||||
assert!(matches!(e, Expr::Index { .. }));
|
||||
// Without `[^`, bare word stays text literal.
|
||||
let e = parse_expr("ev").unwrap();
|
||||
assert!(matches!(e, Expr::Literal(Literal::Text(ref s)) if s == "ev"));
|
||||
// Ticked form still works.
|
||||
let e = parse_expr("`ev`[^result]").unwrap();
|
||||
assert!(matches!(e, Expr::Index { .. }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prefer_var_bare_id_and_quotes() {
|
||||
let e = parse_expr_prefer_var("answer").unwrap();
|
||||
assert!(matches!(e, Expr::Var(ref n) if n == "answer"));
|
||||
let e = parse_expr_prefer_var("n + 1").unwrap();
|
||||
assert!(matches!(e, Expr::Binary { op: BinaryOp::Add, .. }));
|
||||
let e = parse_expr_prefer_var("\"ok\"").unwrap();
|
||||
assert!(matches!(e, Expr::Literal(Literal::Text(ref s)) if s == "ok"));
|
||||
let e = parse_expr_prefer_var("'hi'").unwrap();
|
||||
assert!(matches!(e, Expr::Literal(Literal::Text(ref s)) if s == "hi"));
|
||||
let e = parse_expr_prefer_var("`answer`").unwrap();
|
||||
assert!(matches!(e, Expr::Var(ref n) if n == "answer"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn call_arg_module_path_stays_text() {
|
||||
let e = parse_call_arg_value("nav.`nav`").unwrap();
|
||||
assert!(matches!(
|
||||
e,
|
||||
Expr::Literal(Literal::Text(ref s)) if s == "nav.`nav`"
|
||||
));
|
||||
let e = parse_call_arg_value("articles.`articles`.title").unwrap();
|
||||
assert!(matches!(
|
||||
e,
|
||||
Expr::Literal(Literal::Text(ref s)) if s == "articles.`articles`.title"
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
+144
-36
@@ -2,7 +2,10 @@
|
||||
|
||||
mod expr;
|
||||
|
||||
pub use expr::{parse_call_after_gt, parse_call_arg_value, parse_expr, parse_value_or_interp};
|
||||
pub use expr::{
|
||||
parse_call_after_gt, parse_call_arg_value, parse_expr, parse_expr_prefer_var,
|
||||
parse_value_or_interp,
|
||||
};
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
|
||||
@@ -248,7 +251,7 @@ impl<'a> Cursor<'a> {
|
||||
let value = if inner.trim().is_empty() {
|
||||
Expr::Literal(Literal::None)
|
||||
} else {
|
||||
parse_expr_str(inner)?
|
||||
parse_expr_prefer_var(inner)?
|
||||
};
|
||||
return Ok(Stmt::Return { value, span });
|
||||
}
|
||||
@@ -277,12 +280,12 @@ impl<'a> Cursor<'a> {
|
||||
|
||||
fn parse_italic_assign(&mut self, inner: &str, span: Span) -> Result<Stmt> {
|
||||
let Some((name, rhs)) = split_assign_inner(inner) else {
|
||||
bail!("{span}: italic statement must be an assignment (`name` = …)");
|
||||
bail!("{span}: italic statement must be an assignment (name = …)");
|
||||
};
|
||||
if crate::aliases::is_reserved_keyword(&name) {
|
||||
bail!("{span}: `{name}` is a reserved keyword");
|
||||
}
|
||||
let (value, end_line) = self.resolve_assign_rhs(&rhs, span)?;
|
||||
let (value, end_line) = self.resolve_assign_rhs(&rhs, span, true)?;
|
||||
Ok(Stmt::Assign {
|
||||
name,
|
||||
value,
|
||||
@@ -308,7 +311,7 @@ impl<'a> Cursor<'a> {
|
||||
return Ok(None);
|
||||
}
|
||||
let rhs = after[1..].trim().to_string();
|
||||
let (value, end_line) = self.resolve_assign_rhs(&rhs, span)?;
|
||||
let (value, end_line) = self.resolve_assign_rhs(&rhs, span, false)?;
|
||||
Ok(Some(Stmt::Assign {
|
||||
name,
|
||||
value,
|
||||
@@ -319,7 +322,13 @@ impl<'a> Cursor<'a> {
|
||||
|
||||
/// RHS on the same line, or empty RHS → following `$$` / ```lang fence or table.
|
||||
/// Returns `(expr, inclusive end_line)`.
|
||||
fn resolve_assign_rhs(&mut self, rhs: &str, span: Span) -> Result<(Expr, u32)> {
|
||||
/// `prefer_var`: italic RHS uses variable-preferring expression syntax.
|
||||
fn resolve_assign_rhs(
|
||||
&mut self,
|
||||
rhs: &str,
|
||||
span: Span,
|
||||
prefer_var: bool,
|
||||
) -> Result<(Expr, u32)> {
|
||||
let rhs = rhs.trim();
|
||||
if rhs.is_empty() {
|
||||
self.skip_blanks();
|
||||
@@ -344,7 +353,12 @@ impl<'a> Cursor<'a> {
|
||||
if rhs.starts_with('>') {
|
||||
return Ok((Expr::Call(call_after_gt(rhs[1..].trim())?), span.line));
|
||||
}
|
||||
Ok((parse_value_or_interp(rhs)?, span.line))
|
||||
let value = if prefer_var {
|
||||
parse_expr_prefer_var(rhs)?
|
||||
} else {
|
||||
parse_value_or_interp(rhs)?
|
||||
};
|
||||
Ok((value, span.line))
|
||||
}
|
||||
|
||||
fn skip_blanks(&mut self) {
|
||||
@@ -463,11 +477,13 @@ impl<'a> Cursor<'a> {
|
||||
/// - 1-col → List
|
||||
/// - first header `@` / `行` / `row` → List of row Maps (marker col excluded)
|
||||
/// - else ≥2-col → Map (column-oriented)
|
||||
///
|
||||
/// Data cells are expressions ([`parse_call_arg_value`]); headers stay key text.
|
||||
fn consume_table(&mut self) -> Result<Expr> {
|
||||
self.skip_noise();
|
||||
let mut header: Vec<String> = Vec::new();
|
||||
let mut header_line: u32 = 1;
|
||||
let mut data_rows: Vec<Vec<String>> = Vec::new();
|
||||
let mut data_rows: Vec<(u32, Vec<String>)> = Vec::new();
|
||||
let mut header_done = false;
|
||||
let mut sep_done = false;
|
||||
while let Some(l) = self.peek() {
|
||||
@@ -496,7 +512,7 @@ impl<'a> Cursor<'a> {
|
||||
continue;
|
||||
}
|
||||
sep_done = true;
|
||||
data_rows.push(cells);
|
||||
data_rows.push((line_no, cells));
|
||||
}
|
||||
if !header_done {
|
||||
bail!("expected GFM table after empty assignment");
|
||||
@@ -510,9 +526,9 @@ impl<'a> Cursor<'a> {
|
||||
}
|
||||
if header.len() <= 1 {
|
||||
let mut rows = Vec::new();
|
||||
for cells in data_rows {
|
||||
for (line_no, cells) in data_rows {
|
||||
if let Some(cell) = cells.first() {
|
||||
rows.push(Expr::Literal(cell_literal(cell)));
|
||||
rows.push(parse_table_cell(cell, line_no)?);
|
||||
}
|
||||
}
|
||||
return Ok(Expr::List(rows));
|
||||
@@ -536,14 +552,15 @@ impl<'a> Cursor<'a> {
|
||||
let mut pairs = Vec::with_capacity(header.len());
|
||||
for (col, key) in header.iter().enumerate() {
|
||||
if n_rows == 1 {
|
||||
let raw = data_rows[0].get(col).map(|s| s.as_str()).unwrap_or("");
|
||||
pairs.push((key.clone(), Expr::Literal(cell_literal(raw))));
|
||||
let (line_no, cells) = &data_rows[0];
|
||||
let raw = cells.get(col).map(|s| s.as_str()).unwrap_or("");
|
||||
pairs.push((key.clone(), parse_table_cell(raw, *line_no)?));
|
||||
} else {
|
||||
// 0 rows → empty lists; ≥2 rows → column lists.
|
||||
let mut col_vals = Vec::with_capacity(n_rows);
|
||||
for cells in &data_rows {
|
||||
for (line_no, cells) in &data_rows {
|
||||
let raw = cells.get(col).map(|s| s.as_str()).unwrap_or("");
|
||||
col_vals.push(Expr::Literal(cell_literal(raw)));
|
||||
col_vals.push(parse_table_cell(raw, *line_no)?);
|
||||
}
|
||||
pairs.push((key.clone(), Expr::List(col_vals)));
|
||||
}
|
||||
@@ -661,12 +678,38 @@ impl<'a> Cursor<'a> {
|
||||
|
||||
fn split_assign_inner(inner: &str) -> Option<(String, String)> {
|
||||
let inner = inner.trim();
|
||||
let rest = inner.strip_prefix('`')?;
|
||||
let end = rest.find('`')?;
|
||||
let name = rest[..end].to_string();
|
||||
let after = rest[end + 1..].trim_start();
|
||||
let rhs = after.strip_prefix('=')?.trim().to_string();
|
||||
Some((name, rhs))
|
||||
if let Some(rest) = inner.strip_prefix('`') {
|
||||
let end = rest.find('`')?;
|
||||
let name = rest[..end].to_string();
|
||||
if name.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let after = rest[end + 1..].trim_start();
|
||||
let rhs = after.strip_prefix('=')?.trim().to_string();
|
||||
return Some((name, rhs));
|
||||
}
|
||||
// Bare LHS: `answer = …` (italic exemption).
|
||||
let eq = inner.find('=')?;
|
||||
let name = inner[..eq].trim();
|
||||
if !is_bare_assign_ident(name) {
|
||||
return None;
|
||||
}
|
||||
let rhs = inner[eq + 1..].trim().to_string();
|
||||
Some((name.to_string(), rhs))
|
||||
}
|
||||
|
||||
fn is_bare_assign_ident(s: &str) -> bool {
|
||||
if s.is_empty() || s.contains('`') || s.chars().any(char::is_whitespace) {
|
||||
return false;
|
||||
}
|
||||
let mut chars = s.chars();
|
||||
let Some(first) = chars.next() else {
|
||||
return false;
|
||||
};
|
||||
if !(first.is_alphanumeric() || first == '_' || !first.is_ascii()) {
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// Same-line `$$body$$` → Formula; otherwise None (caller parses normally).
|
||||
@@ -731,19 +774,39 @@ fn parse_param_line(trimmed: &str) -> Option<crate::ast::Param> {
|
||||
Some(Param { name, default })
|
||||
}
|
||||
|
||||
fn parse_foreach_ident(s: &str) -> Option<String> {
|
||||
let s = s.trim();
|
||||
if s.starts_with('`') {
|
||||
return parse_backtick_ident(s);
|
||||
}
|
||||
if s.is_empty() {
|
||||
return None;
|
||||
}
|
||||
// Bare name exemption: `[item](list)` without ticks (foreach only).
|
||||
if s.chars().any(|c| c.is_whitespace() || c == '`' || c == '[' || c == ']') {
|
||||
return None;
|
||||
}
|
||||
let mut chars = s.chars();
|
||||
let first = chars.next()?;
|
||||
if !(first.is_alphanumeric() || first == '_' || !first.is_ascii()) {
|
||||
return None;
|
||||
}
|
||||
Some(s.to_string())
|
||||
}
|
||||
|
||||
fn parse_foreach_header(rest: &str) -> Option<(String, String)> {
|
||||
// [`item`](`collection`)
|
||||
// [`item`](`collection`) or bare [item](collection); sides may mix.
|
||||
let rest = rest.trim();
|
||||
if !rest.starts_with('[') {
|
||||
return None;
|
||||
}
|
||||
let end_item = rest.find(']')?;
|
||||
let item = parse_backtick_ident(&rest[1..end_item])?;
|
||||
let item = parse_foreach_ident(&rest[1..end_item])?;
|
||||
let after = rest[end_item + 1..].trim_start();
|
||||
if !after.starts_with('(') || !after.ends_with(')') {
|
||||
return None;
|
||||
}
|
||||
let coll = parse_backtick_ident(&after[1..after.len() - 1])?;
|
||||
let coll = parse_foreach_ident(&after[1..after.len() - 1])?;
|
||||
Some((item, coll))
|
||||
}
|
||||
|
||||
@@ -1003,15 +1066,15 @@ fn indent_of(text: &str) -> usize {
|
||||
.sum()
|
||||
}
|
||||
|
||||
fn cell_literal(raw: &str) -> Literal {
|
||||
let t = raw.trim();
|
||||
let digits = |s: &str| !s.is_empty() && s.bytes().all(|b| b.is_ascii_digit());
|
||||
if digits(t) || t.strip_prefix('-').is_some_and(digits) {
|
||||
if let Ok(n) = t.parse::<i64>() {
|
||||
return Literal::Int(n);
|
||||
}
|
||||
}
|
||||
Literal::Text(t.to_string())
|
||||
fn parse_table_cell(raw: &str, line: u32) -> Result<Expr> {
|
||||
parse_call_arg_value(raw).map_err(|e| {
|
||||
Diagnostic::new(
|
||||
None,
|
||||
Span { line, col: 1 },
|
||||
format!("table cell: {e}"),
|
||||
)
|
||||
.into()
|
||||
})
|
||||
}
|
||||
|
||||
/// First-column header that opts into row-oriented records (`List` of `Map`).
|
||||
@@ -1022,7 +1085,7 @@ fn is_row_table_marker(header: &str) -> bool {
|
||||
fn build_row_oriented_table(
|
||||
header: Vec<String>,
|
||||
header_line: u32,
|
||||
data_rows: Vec<Vec<String>>,
|
||||
data_rows: Vec<(u32, Vec<String>)>,
|
||||
) -> Result<Expr> {
|
||||
if header.len() < 2 {
|
||||
return Err(Diagnostic::new(
|
||||
@@ -1051,12 +1114,12 @@ fn build_row_oriented_table(
|
||||
}
|
||||
}
|
||||
let mut rows = Vec::with_capacity(data_rows.len());
|
||||
for cells in data_rows {
|
||||
for (line_no, cells) in data_rows {
|
||||
let mut pairs = Vec::with_capacity(fields.len());
|
||||
for (i, key) in fields.iter().enumerate() {
|
||||
// Skip marker column (index 0); fields start at column 1.
|
||||
let raw = cells.get(i + 1).map(|s| s.as_str()).unwrap_or("");
|
||||
pairs.push((key.clone(), Expr::Literal(cell_literal(raw))));
|
||||
pairs.push((key.clone(), parse_table_cell(raw, line_no)?));
|
||||
}
|
||||
rows.push(Expr::Map(pairs));
|
||||
}
|
||||
@@ -1123,6 +1186,51 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_foreach_bare_ids() {
|
||||
let src = "# main\n\n- [x](xs)\n > print text=`x`\n";
|
||||
let m = parse_source(src).unwrap();
|
||||
match &m.functions[0].body[0] {
|
||||
Stmt::ForEach { item, collection, .. } => {
|
||||
assert_eq!(item, "x");
|
||||
assert_eq!(collection, "xs");
|
||||
}
|
||||
other => panic!("expected foreach, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_foreach_mixed_ticks() {
|
||||
let src = "# main\n\n- [果](`篮子`)\n > print text=`果`\n";
|
||||
let m = parse_source(src).unwrap();
|
||||
match &m.functions[0].body[0] {
|
||||
Stmt::ForEach { item, collection, .. } => {
|
||||
assert_eq!(item, "果");
|
||||
assert_eq!(collection, "篮子");
|
||||
}
|
||||
other => panic!("expected foreach, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_italic_bare_lhs_prefer_var() {
|
||||
let src = "# main\n\n*answer = 1*\n**answer**\n";
|
||||
let m = parse_source(src).unwrap();
|
||||
match &m.functions[0].body[0] {
|
||||
Stmt::Assign { name, value, .. } => {
|
||||
assert_eq!(name, "answer");
|
||||
assert!(matches!(value, Expr::Literal(Literal::Int(1))));
|
||||
}
|
||||
other => panic!("expected assign, got {other:?}"),
|
||||
}
|
||||
match &m.functions[0].body[1] {
|
||||
Stmt::Return { value, .. } => {
|
||||
assert!(matches!(value, Expr::Var(n) if n == "answer"));
|
||||
}
|
||||
other => panic!("expected return, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_branch_rejects_plus_arm() {
|
||||
let src = "# main\n\n+ `x` > 0\n > print text=ok\n";
|
||||
|
||||
Reference in new issue
Block a user