signature::detect() scanned the function body independently of heading
modes, skipping only level>=3 headings. A 'params(...)' code span in an
### @knowledge section was therefore taken as the function signature
('inspect --signature' reported it as explicit), even though that span is
inert documentation and is never executed.
Unify recognition with the executable traversal:
- MappedBody now reports the params(...) payload the mapper accepted; the
mapper already enforces Exec mode, directness, first-function-wide
position and single declaration
- signature::from_payload validates that text and rewrites default-value
spans; signature::detect is removed along with its mode-blind heuristic
- analyze uses the mapper result, so Knowledge sections cannot declare a
signature and prose/Knowledge subsections may still precede the first
Exec declaration
- tests: Knowledge params ignored; Exec params after prose/Knowledge is the
signature; spec/mapping.md states the recognition rule
Verified: clippy -D warnings clean; cargo test --workspace --locked green
(62 targets).
322 lines
11 KiB
Rust
322 lines
11 KiB
Rust
//! Optional explicit function signatures.
|
|
//!
|
|
//! `` `params(r: Number, precision: Int = 2)` `` as the first function-wide
|
|
//! direct payload of an Exec H2 declares the public parameter list. It is a
|
|
//! *declaration*: it does not execute. Without it, parameters are inferred from
|
|
//! free reads (and an inferred signature keeps the named-arguments-only rule).
|
|
//! See RFC-LANG-0002 and `spec/mapping.md` §6.
|
|
|
|
use marqdo_expression::{parse_expression, Expr, Statement, StatementKind};
|
|
use marqdo_ir::{Param, SignatureKind};
|
|
use marqdo_span::Span;
|
|
use marqdo_value::{codes, Diagnostic};
|
|
use unicode_ident::{is_xid_continue, is_xid_start};
|
|
|
|
use crate::payload::PayloadSegments;
|
|
|
|
/// Parsed signature declaration.
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
pub struct Signature {
|
|
/// Parameters in declaration order.
|
|
pub params: Vec<Param>,
|
|
/// Span of the declaration payload carrier.
|
|
pub span: Span,
|
|
/// Diagnostics raised while parsing the declaration.
|
|
pub diagnostics: Vec<Diagnostic>,
|
|
}
|
|
|
|
impl Signature {
|
|
/// Whether the declaration had errors.
|
|
pub fn has_errors(&self) -> bool {
|
|
self.diagnostics.iter().any(Diagnostic::is_error)
|
|
}
|
|
}
|
|
|
|
/// Parse the `params(...)` payload the mapper accepted for a function.
|
|
///
|
|
/// The mapper owns recognition: only the first function-wide direct payload of
|
|
/// an Exec H2 can be a declaration, and Knowledge sections never produce
|
|
/// payloads. This function only validates the declaration text and rewrites
|
|
/// default-value spans onto the file.
|
|
pub fn from_payload(text: &str, span: Span, segments: &PayloadSegments) -> Signature {
|
|
let mut signature = parse(text, span);
|
|
for param in &mut signature.params {
|
|
if let Some(default) = &mut param.default {
|
|
segments.rewrite_expr(default);
|
|
}
|
|
}
|
|
signature
|
|
}
|
|
|
|
/// Parse signature payload text (without the code span delimiters).
|
|
pub fn parse(source: &str, span: Span) -> Signature {
|
|
let mut diagnostics = Vec::new();
|
|
let trimmed = source.trim();
|
|
let is_params = trimmed.starts_with("params(");
|
|
let syntax_code = if is_params {
|
|
codes::SEM_SYNTAX
|
|
} else {
|
|
codes::EXPR_PARSE
|
|
};
|
|
let inner = if let Some(rest) = trimmed.strip_prefix("params(") {
|
|
match rest.strip_suffix(')') {
|
|
Some(inner) => inner,
|
|
None => {
|
|
diagnostics.push(Diagnostic::error(
|
|
codes::SEM_SYNTAX,
|
|
Some(span),
|
|
"function signature must be closed by `)`",
|
|
));
|
|
rest
|
|
}
|
|
}
|
|
} else if let Some(rest) = trimmed.strip_prefix('(') {
|
|
// Plugin/ABI descriptors keep their existing parenthesised signature
|
|
// spelling; language payloads are detected only as `params(...)`.
|
|
rest.strip_suffix(')').unwrap_or(rest)
|
|
} else {
|
|
diagnostics.push(Diagnostic::error(
|
|
syntax_code,
|
|
Some(span),
|
|
"function signature must be `params(...)`, e.g. `params(r: Number)`",
|
|
));
|
|
trimmed
|
|
};
|
|
|
|
let mut params: Vec<Param> = Vec::new();
|
|
let mut seen: Vec<String> = Vec::new();
|
|
let mut cursor = 0usize;
|
|
for (entry_start, entry_end) in split_top_level(inner, ',') {
|
|
let entry = &inner[entry_start..entry_end];
|
|
if entry.trim().is_empty() {
|
|
if !inner.trim().is_empty() {
|
|
diagnostics.push(Diagnostic::error(
|
|
syntax_code,
|
|
Some(span),
|
|
"empty parameter in function signature",
|
|
));
|
|
}
|
|
cursor = entry_end;
|
|
continue;
|
|
}
|
|
let entry_offset = source.find(entry).unwrap_or(0);
|
|
match parse_entry(entry, span) {
|
|
Ok(mut param) => {
|
|
param.span = Span::new(
|
|
span.start + entry_offset.min(span.len()),
|
|
span.start + (entry_offset + entry.len()).min(span.len()),
|
|
);
|
|
if seen.contains(¶m.name) {
|
|
diagnostics.push(Diagnostic::error(
|
|
codes::EXPR_PARSE,
|
|
Some(param.span),
|
|
format!("duplicate parameter `{}`", param.name),
|
|
));
|
|
} else {
|
|
seen.push(param.name.clone());
|
|
params.push(param);
|
|
}
|
|
}
|
|
Err(message) => diagnostics.push(Diagnostic::error(
|
|
syntax_code,
|
|
Some(span),
|
|
format!("invalid function signature: {message}"),
|
|
)),
|
|
}
|
|
cursor = entry_end;
|
|
}
|
|
let _ = cursor;
|
|
Signature {
|
|
params,
|
|
span,
|
|
diagnostics,
|
|
}
|
|
}
|
|
|
|
fn parse_entry(entry: &str, span: Span) -> Result<Param, String> {
|
|
let entry = entry.trim();
|
|
// Find `=` at top level (not part of `==`).
|
|
let equals = find_top_level(entry, '=');
|
|
let (lhs, default) = match equals {
|
|
Some(index)
|
|
if !entry[index + 1..].starts_with('=')
|
|
&& (index == 0 || entry.as_bytes()[index - 1] != b'=') =>
|
|
{
|
|
let default_text = entry[index + 1..].trim();
|
|
if default_text.is_empty() {
|
|
return Err("default value expression is empty".to_string());
|
|
}
|
|
let expr = parse_expression(default_text)
|
|
.map_err(|error| format!("invalid default value: {}", error.message))?;
|
|
if !is_const_expr(&expr) {
|
|
return Err("default values must be compile-time constants".to_string());
|
|
}
|
|
(&entry[..index], Some(expr))
|
|
}
|
|
_ => (entry, None),
|
|
};
|
|
let colon = find_top_level(lhs, ':');
|
|
let (name_text, type_annotation) = match colon {
|
|
Some(index) => (
|
|
lhs[..index].trim(),
|
|
Some(lhs[index + 1..].trim().to_string()),
|
|
),
|
|
None => (lhs.trim(), None),
|
|
};
|
|
if let Some(annotation) = &type_annotation {
|
|
if annotation.is_empty() {
|
|
return Err("type annotation is empty".to_string());
|
|
}
|
|
}
|
|
validate_ident(name_text)?;
|
|
Ok(Param {
|
|
name: name_text.to_string(),
|
|
// Refined by the caller with the exact source span.
|
|
span,
|
|
type_annotation,
|
|
default,
|
|
})
|
|
}
|
|
|
|
fn validate_ident(name: &str) -> Result<(), String> {
|
|
let mut chars = name.chars();
|
|
let Some(first) = chars.next() else {
|
|
return Err("parameter name is empty".to_string());
|
|
};
|
|
if !(first == '_' || is_xid_start(first)) {
|
|
return Err(format!("`{name}` is not a valid parameter name"));
|
|
}
|
|
for ch in chars {
|
|
if !(ch == '_' || is_xid_continue(ch)) {
|
|
return Err(format!("`{name}` is not a valid parameter name"));
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Compile-time constant check for signature defaults.
|
|
fn is_const_expr(expr: &Expr) -> bool {
|
|
match expr {
|
|
Expr::Int(_) | Expr::Float(_) | Expr::Text(_) | Expr::Bool(_) | Expr::None => true,
|
|
Expr::Unary { operand, .. } => is_const_expr(operand),
|
|
Expr::List(items) => items.iter().all(is_const_expr),
|
|
Expr::Record(entries) => entries.iter().all(|entry| is_const_expr(&entry.value)),
|
|
_ => false,
|
|
}
|
|
}
|
|
|
|
/// Split `text` on a separator at bracket/string depth zero, returning byte
|
|
/// ranges (without the separator).
|
|
fn split_top_level(text: &str, separator: char) -> Vec<(usize, usize)> {
|
|
let mut ranges = Vec::new();
|
|
let mut depth = 0i32;
|
|
let mut in_string = false;
|
|
let mut escaped = false;
|
|
let mut start = 0usize;
|
|
for (index, ch) in text.char_indices() {
|
|
if in_string {
|
|
if escaped {
|
|
escaped = false;
|
|
} else if ch == '\\' {
|
|
escaped = true;
|
|
} else if ch == '"' {
|
|
in_string = false;
|
|
}
|
|
continue;
|
|
}
|
|
match ch {
|
|
'"' => in_string = true,
|
|
'(' | '[' | '{' => depth += 1,
|
|
')' | ']' | '}' => depth -= 1,
|
|
c if c == separator && depth == 0 => {
|
|
ranges.push((start, index));
|
|
start = index + ch.len_utf8();
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
ranges.push((start, text.len()));
|
|
ranges
|
|
}
|
|
|
|
fn find_top_level(text: &str, needle: char) -> Option<usize> {
|
|
let mut depth = 0i32;
|
|
let mut in_string = false;
|
|
let mut escaped = false;
|
|
for (index, ch) in text.char_indices() {
|
|
if in_string {
|
|
if escaped {
|
|
escaped = false;
|
|
} else if ch == '\\' {
|
|
escaped = true;
|
|
} else if ch == '"' {
|
|
in_string = false;
|
|
}
|
|
continue;
|
|
}
|
|
match ch {
|
|
'"' => in_string = true,
|
|
'(' | '[' | '{' => depth += 1,
|
|
')' | ']' | '}' => depth -= 1,
|
|
c if c == needle && depth == 0 => return Some(index),
|
|
_ => {}
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
/// Build a statement-less const check helper used by the mapper for defaults.
|
|
pub fn const_default_ok(statement: &Statement) -> bool {
|
|
match &statement.kind {
|
|
StatementKind::Assign { value, .. } => is_const_expr(value),
|
|
StatementKind::Expr(expr) => is_const_expr(expr),
|
|
}
|
|
}
|
|
|
|
/// Convenience: parameter kind for functions with a signature.
|
|
pub const KIND: SignatureKind = SignatureKind::Explicit;
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn parses_typed_signature_with_default() {
|
|
let signature = parse("params(r: Number, precision: Int = 2)", Span::new(0, 38));
|
|
assert!(!signature.has_errors(), "{:?}", signature.diagnostics);
|
|
assert_eq!(signature.params.len(), 2);
|
|
assert_eq!(signature.params[0].name, "r");
|
|
assert_eq!(
|
|
signature.params[0].type_annotation.as_deref(),
|
|
Some("Number")
|
|
);
|
|
assert_eq!(signature.params[1].name, "precision");
|
|
assert!(signature.params[1].default.is_some());
|
|
}
|
|
|
|
#[test]
|
|
fn parses_untyped_and_empty() {
|
|
let signature = parse("params()", Span::new(0, 8));
|
|
assert!(signature.params.is_empty());
|
|
assert!(!signature.has_errors());
|
|
let signature = parse("params(n)", Span::new(0, 9));
|
|
assert_eq!(signature.params[0].name, "n");
|
|
}
|
|
|
|
#[test]
|
|
fn rejects_duplicates_and_bad_defaults() {
|
|
let duplicate = parse("params(a, a)", Span::new(0, 12));
|
|
assert!(duplicate.has_errors());
|
|
let bad_default = parse("params(a = f())", Span::new(0, 16));
|
|
assert!(bad_default.has_errors());
|
|
let bad_name = parse("params(1x)", Span::new(0, 10));
|
|
assert!(bad_name.has_errors());
|
|
}
|
|
|
|
#[test]
|
|
fn split_respects_nesting_and_strings() {
|
|
let parts = split_top_level("a, f(b, c), d", ',');
|
|
assert_eq!(parts.len(), 3);
|
|
}
|
|
}
|