Release 2.7.0: executable handbooks and native user-site workflows

This commit is contained in:
cflmy committed 2026-11-10 17:03:06 +08:00
1 parent aa0c4d0f55
commit d9aef957f7
562 files changed
+21064 -20387

No files matched your search

+46 -7
View File
@@ -124,7 +124,9 @@ fn parse_call_arg_value_mode(input: &str, prefer_var: bool, allow_call: bool) ->
Err(_) => Ok(parse_interp(s)),
};
}
if has_hyphenated_prose(s) {
// Nested calls may contain hyphenated paths in their own quoted args.
// The inner argument parser still preserves plain hyphenated prose.
if has_hyphenated_prose(s) && !(allow_call && s.contains('(')) {
return Ok(parse_interp(s));
}
match parse_expr_opts(s, prefer_var, allow_call) {
@@ -577,6 +579,16 @@ impl<'a> Parser<'a> {
if !self.eat("`") {
bail!("unterminated `name`");
}
// Parse a quoted receiver's explicit method before its value.
if self.allow_paren_call && self.rest().starts_with('.') {
let call_start = start - 1;
if let Some((call, used)) = try_paren_call(&self.src[call_start..], self.prefer_var)? {
if call.receiver.is_some() {
self.i = call_start + used;
return Ok(Expr::Call(call));
}
}
}
return self.parse_index_chain(Expr::Var(name));
}
// `礼貌 [问候] x` — after quotes/ticks so `[DONE]` inside strings is safe.
@@ -1109,11 +1121,13 @@ pub fn parse_call_tail(s: &str, prefer_var: bool) -> Result<(CallExpr, usize)> {
}
let callee_tok = {
let mut parts = s.split_whitespace();
parts
.next()
.ok_or_else(|| anyhow::anyhow!("missing callee"))?
.to_string()
let mut quoted = false;
let end = s.char_indices().find_map(|(i,c)| {
if c == '`' { quoted = !quoted; }
(!quoted && (c.is_whitespace() || c == ')')).then_some(i)
}).unwrap_or(s.len());
if end == 0 { bail!("missing callee"); }
s[..end].to_string()
};
let (receiver, callee, path) = split_callee(&callee_tok);
@@ -1435,7 +1449,7 @@ fn try_named_arg(s: &str) -> Option<(String, &str)> {
}
return Some((key.to_string(), &s[i + 1..]));
}
if c.is_whitespace() {
if c.is_whitespace() || matches!(c, '(' | ')' | '[' | ']' | '{' | '}' | ',' | '"' | '\'' | '`') {
return None;
}
}
@@ -1695,6 +1709,31 @@ pub fn parse_call_after_gt(after_gt: &str, prefer_var: bool) -> Result<CallExpr>
mod tests {
use super::*;
#[test]
fn occlusion_keeps_receiver_calls_and_closing_delimiters_separate() {
for source in ["[Read probabilities](`c`.probabilities())",
"[Read probabilities](> `c`.probabilities)",
"[Set angle](`c`.rotate(angle=access(m, \"angle\")))"] {
let e = parse_expr_markup(source,true,Markup::Occlusion).unwrap();
let Expr::Occluded { payload: Some(payload), .. } = e else { panic!("{source}") };
let Expr::Call(call) = *payload else { panic!("{source}") };
assert_eq!(call.receiver.as_deref(),Some("c"));
assert!(!call.callee.contains(')'));
}
assert!(parse_expr_markup("[Read file size](access(fs.stat(path=\"a\"), \"size\"))",
true,Markup::Occlusion).is_ok());
}
#[test]
fn nested_call_with_a_hyphenated_file_path_stays_executable() {
let e = parse_expr_prefer_var("toml.parse(text=fs.read_text(path=\"crates/marqdo-wasm/Cargo.toml\"))").unwrap();
let Expr::Call(c) = e else { panic!("expected outer call") };
let crate::ast::Arg::Named { value, .. } = &c.args[0] else { panic!("expected named argument") };
assert!(matches!(value, Expr::Call(_)), "{value:?}");
}
#[test]
fn bare_var_footnote_index() {
let e = parse_expr("ev[^result]").unwrap();
+68 -10
View File
@@ -13,6 +13,7 @@ pub use prose::{
};
use anyhow::{bail, Result};
use std::collections::HashSet;
use crate::ast::{
BranchArm, Expr, Function, Import, IndexKey, Literal, Module, Param, Stmt, Use,
@@ -44,7 +45,7 @@ pub fn parse_classified(lines: &[ClassifiedLine]) -> Result<Module> {
continue;
}
if is_heading(trimmed) {
functions.push(cur.parse_function(1)?);
functions.push(cur.parse_function(1, &HashSet::new())?);
} else if line.kind == LineKind::Comment {
// Intro paragraphs may still contain return-shaped italics (`返回*值*`)
// or leftover soft emphasis; executable prose belongs inside functions.
@@ -78,6 +79,18 @@ pub fn parse_classified(lines: &[ClassifiedLine]) -> Result<Module> {
Ok(module)
}
/// Parse one GFM table with the source language's cell and shape rules.
/// Document readers reject nonliteral expressions before using the value.
pub(crate) fn parse_gfm_table(source: &str) -> Result<Expr> {
let lines = classify_source(source);
let mut cursor = Cursor::new(&lines);
let table = cursor.consume_table()?;
if cursor.skip_noise() {
bail!("unexpected content after the selected GFM table");
}
Ok(table)
}
struct Cursor<'a> {
lines: &'a [ClassifiedLine],
i: usize,
@@ -282,7 +295,7 @@ impl<'a> Cursor<'a> {
Ok(())
}
fn parse_function(&mut self, min_level: u8) -> Result<Function> {
fn parse_function(&mut self, min_level: u8, inherited: &HashSet<String>) -> Result<Function> {
let line = self.bump().unwrap();
let (level, raw_name) = parse_heading(line.text.trim())
.ok_or_else(|| anyhow::anyhow!("{}:1: expected heading", line.line_no))?;
@@ -317,7 +330,13 @@ impl<'a> Cursor<'a> {
if is_heading(trimmed) {
let (nl, _) = parse_heading(trimmed).unwrap();
if nl > level {
fun.children.push(self.parse_function(level + 1)?);
let mut visible = inherited.clone();
let mut unused_reads = HashSet::new();
let mut unused_declarations = Vec::new();
collect_names_needing_input(&fun.body, &mut visible, &mut unused_reads, &HashSet::new(), &mut unused_declarations);
visible.extend(prose_decls.iter().map(|(name, _, _)| name.clone()));
visible.extend(fun.params.iter().map(|param| param.name.clone()));
fun.children.push(self.parse_function(level + 1, &visible)?);
continue;
}
break;
@@ -369,7 +388,13 @@ impl<'a> Cursor<'a> {
if is_heading(trimmed) {
let (nl, _) = parse_heading(trimmed).unwrap();
if nl > level {
fun.children.push(self.parse_function(level + 1)?);
let mut visible = inherited.clone();
let mut unused_reads = HashSet::new();
let mut unused_declarations = Vec::new();
collect_names_needing_input(&fun.body, &mut visible, &mut unused_reads, &HashSet::new(), &mut unused_declarations);
visible.extend(prose_decls.iter().map(|(name, _, _)| name.clone()));
visible.extend(fun.params.iter().map(|param| param.name.clone()));
fun.children.push(self.parse_function(level + 1, &visible)?);
continue;
}
break;
@@ -445,7 +470,7 @@ impl<'a> Cursor<'a> {
// 契约提取(T2.1)必须在升参推断**之前**:契约表的单元不污染推断。
extract_function_contract(&mut fun, self.lines)?;
merge_inferred_params(&mut fun, &prose_decls)?;
merge_inferred_params(&mut fun, &prose_decls, inherited)?;
Ok(fun)
}
@@ -2104,11 +2129,15 @@ fn extract_function_contract(fun: &mut Function, lines: &[ClassifiedLine]) -> Re
fn merge_inferred_params(
fun: &mut Function,
prose_decls: &[(String, Option<String>, u32)],
inherited: &HashSet<String>,
) -> Result<()> {
use std::collections::HashSet;
let mut needs_input = HashSet::new();
let mut bound = HashSet::new();
// Captured entry/object bindings are available before the child body.
// An explicit prose input still shadows an outer binding of the same name.
let explicit: HashSet<_> = prose_decls.iter().map(|(name, _, _)| name.clone()).collect();
let mut bound: HashSet<_> = inherited.difference(&explicit).cloned().collect();
let mut assign_decls = Vec::new();
// Defaults execute before the body. Reads used only there still require
// declared inputs; later body assignments cannot satisfy those reads.
@@ -2129,6 +2158,8 @@ fn merge_inferred_params(
&mut assign_decls,
);
needs_input.retain(|name| !inherited.contains(name) || explicit.contains(name));
// Declaration order is source order: backtick mentions and first assignment
// left-hand sides. An assignment promotes only when its right-hand side (or a
// later read) still needs an external value.
@@ -2211,7 +2242,7 @@ fn collect_names_needing_input(
let mut reads = std::collections::HashSet::new();
collect_reads_expr(value, &mut reads);
for r in reads {
if !bound.contains(&r) {
if !bound.contains(&r) && !loop_items.contains(&r) {
needs.insert(r);
}
}
@@ -2220,7 +2251,7 @@ fn collect_names_needing_input(
let mut reads = std::collections::HashSet::new();
collect_reads_call(call, &mut reads);
for r in reads {
if !bound.contains(&r) {
if !bound.contains(&r) && !loop_items.contains(&r) {
needs.insert(r);
}
}
@@ -2229,7 +2260,7 @@ fn collect_names_needing_input(
let mut reads = std::collections::HashSet::new();
collect_reads_expr(value, &mut reads);
for r in reads {
if !bound.contains(&r) {
if !bound.contains(&r) && !loop_items.contains(&r) {
needs.insert(r);
}
}
@@ -2242,7 +2273,7 @@ fn collect_names_needing_input(
let mut reads = std::collections::HashSet::new();
collect_reads_expr(c, &mut reads);
for r in reads {
if !bound.contains(&r) {
if !bound.contains(&r) && !loop_items.contains(&r) {
needs.insert(r);
}
}
@@ -2931,3 +2962,30 @@ mod escaped_gfm_cell_tests {
assert!(format!("{:?}", module.functions[0].body).contains("source|skill"));
}
}
#[cfg(test)]
mod lexical_input_tests {
use super::*;
#[test]
fn child_updates_capture_existing_entry_state_without_extra_inputs() {
let source="# main\n\n**count = 0**\n\n## bump\n\nWe **count = count + 1** and return *count*.\n";
let module=parse_source(source).unwrap();
assert!(module.functions[0].children[0].params.is_empty());
}
#[test]
fn explicit_prose_inputs_can_shadow_parent_bindings() {
let source="# main\n\n**count = 0**\n\n## add\n\nUse `count` for this independent call.\nReturn *(count + 1)*.\n";
let module=parse_source(source).unwrap();
let params=&module.functions[0].children[0].params;
assert_eq!(params.len(),1);
assert_eq!(params[0].name,"count");
}
#[test]
fn reading_a_loop_item_in_a_branch_does_not_promote_a_later_local() {
let source="## select\n\nUse `values`.\n- [Inspect values](for values as item)\n 1. `item` != \"\"\n We **output = item**.\n\nWe **item = \"done\"** and return *item*.\n";
let module=parse_source(source).unwrap();
let params=&module.functions[0].params;
assert_eq!(params.len(),1);
assert_eq!(params[0].name,"values");
}
}
+27 -1
View File
@@ -292,7 +292,7 @@ fn scan_backtick_decl(line: &str, start: usize) -> Option<(ProseBit, usize)> {
if let Some(stripped) = after.strip_prefix('=') {
// Quoted defaults may contain prose delimiters, glob stars or spaces.
// Keep the same quoted literal accepted by explicit parameter defaults.
let def_end = if let Some(quote @ ('"' | '\'')) = stripped.chars().next() {
let mut def_end = if let Some(quote @ ('"' | '\'')) = stripped.chars().next() {
let mut escaped = false;
stripped
.char_indices()
@@ -316,6 +316,13 @@ fn scan_backtick_decl(line: &str, start: usize) -> Option<(ProseBit, usize)> {
})
.unwrap_or(stripped.len())
};
// English sentence punctuation belongs to narration after a literal.
// Keep quoted strings, dotted names and decimal syntax unchanged.
if let Some(literal) = stripped[..def_end].strip_suffix('.') {
if matches!(literal, "True" | "False" | "true" | "false" | "None" | "真" | "假" | "无" | "空") {
def_end -= 1;
}
}
let default = stripped[..def_end].to_string();
let next = after_tick + 1 + def_end;
return Some((
@@ -389,6 +396,25 @@ mod tests {
}
}
#[test]
fn sentence_period_after_boolean_or_none_is_not_part_of_the_default() {
for (line, expected) in [
("Use `flag`=False. Continue.", "False"),
("Use `flag`=True.", "True"),
("Use `value`=None.", "None"),
("使用`值`=假. 继续。", "假"),
("Use `text`=\"False.\".", "\"False.\""),
("Use `text`=module.value.", "module.value."),
("Use `rate`=0.5.", "0.5."),
] {
let bits = scan_prose_line(line);
match &bits[..] {
[ProseBit::Decl { default: Some(actual), .. }] => assert_eq!(actual, expected, "{line}"),
_ => panic!("unexpected prose scan: {bits:?}"),
}
}
}
#[test]
fn quoted_defaults_keep_delimiters_and_escaped_quotes() {
let line = r#"输入 `pattern`="**";`mime`="text/plain;charset=utf-8";`label`="a, b \"quoted\""。"#;