Implement object inheritance: # Child = > Parent (I0-I2)
- Parse `# Child = > Parent` object decls (base must be a simple name after `>`; dotted bases and `# Foo = 1` are diagnostics). - Validate in load: unknown base types and cyclic inheritance (A → B → A) fail with line/col. - Method resolution walks the base chain (most specific first) on both tree and bytecode backends; `_type` stays the child name; same-name child ## overrides the parent. - Add inherit.rs, wire Program.object_bases, gold fixtures (inherit-parent-method / inherit-override + cycle/unknown-base/bad-rhs) and public 09-inherit demo pages. Docs: objects.md, markdown-mapping, roadmap, skill. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
a23fdedfa2
commit
5456d9ff83
25 files changed
+590
-41
No files matched your search
+78
-6
@@ -129,22 +129,25 @@ impl<'a> Cursor<'a> {
|
||||
|
||||
fn parse_function(&mut self, min_level: u8) -> Result<Function> {
|
||||
let line = self.bump().unwrap();
|
||||
let (level, name) = parse_heading(line.text.trim())
|
||||
let (level, raw_name) = parse_heading(line.text.trim())
|
||||
.ok_or_else(|| anyhow::anyhow!("{}:1: expected heading", line.line_no))?;
|
||||
if level < min_level {
|
||||
bail!("{}:1: heading level {level} < expected {min_level}", line.line_no);
|
||||
}
|
||||
let span = Span {
|
||||
line: line.line_no,
|
||||
col: 1,
|
||||
};
|
||||
let (name, base) = parse_object_decl(level, raw_name, span)?;
|
||||
|
||||
let mut fun = Function {
|
||||
name: name.to_string(),
|
||||
name,
|
||||
level,
|
||||
span: Span {
|
||||
line: line.line_no,
|
||||
col: 1,
|
||||
},
|
||||
span,
|
||||
params: Vec::new(),
|
||||
body: Vec::new(),
|
||||
children: Vec::new(),
|
||||
base,
|
||||
};
|
||||
|
||||
// Parameter zone: indented `- name` pure identifiers.
|
||||
@@ -766,6 +769,75 @@ fn parse_heading(trimmed: &str) -> Option<(u8, &str)> {
|
||||
Some((level, name))
|
||||
}
|
||||
|
||||
/// `# Child = > Parent` → `(Child, Some(Parent))`; plain `# Child` → `(Child, None)`.
|
||||
fn parse_object_decl(level: u8, raw: &str, span: Span) -> Result<(String, Option<String>)> {
|
||||
let raw = raw.trim();
|
||||
if let Some(eq) = raw.find('=') {
|
||||
if level != 1 {
|
||||
return Err(Diagnostic::new(
|
||||
None,
|
||||
span,
|
||||
"inheritance (`= > Base`) is only allowed on `#` object headings",
|
||||
)
|
||||
.into());
|
||||
}
|
||||
let name = raw[..eq].trim();
|
||||
let rhs = raw[eq + 1..].trim();
|
||||
if name.is_empty() {
|
||||
return Err(Diagnostic::new(None, span, "object name missing before `=`").into());
|
||||
}
|
||||
if !is_simple_type_name(name) {
|
||||
return Err(Diagnostic::new(
|
||||
None,
|
||||
span,
|
||||
format!("invalid object name `{name}`"),
|
||||
)
|
||||
.into());
|
||||
}
|
||||
let Some(after_gt) = rhs.strip_prefix('>') else {
|
||||
return Err(Diagnostic::new(
|
||||
None,
|
||||
span,
|
||||
"object inheritance requires `# Name = > Base` (base must follow `>`)",
|
||||
)
|
||||
.into());
|
||||
};
|
||||
let base = after_gt.trim();
|
||||
if base.is_empty() {
|
||||
return Err(Diagnostic::new(None, span, "missing base type after `>`").into());
|
||||
}
|
||||
if base.contains('.') {
|
||||
return Err(Diagnostic::new(
|
||||
None,
|
||||
span,
|
||||
format!("base type must be a simple name, not a path (`{base}`)"),
|
||||
)
|
||||
.into());
|
||||
}
|
||||
if !is_simple_type_name(base) {
|
||||
return Err(Diagnostic::new(
|
||||
None,
|
||||
span,
|
||||
format!("invalid base type name `{base}`"),
|
||||
)
|
||||
.into());
|
||||
}
|
||||
return Ok((name.to_string(), Some(base.to_string())));
|
||||
}
|
||||
Ok((raw.to_string(), None))
|
||||
}
|
||||
|
||||
fn is_simple_type_name(s: &str) -> bool {
|
||||
let mut chars = s.chars();
|
||||
let Some(first) = chars.next() else {
|
||||
return false;
|
||||
};
|
||||
if !(first.is_alphabetic() || first == '_' || !first.is_ascii()) {
|
||||
return false;
|
||||
}
|
||||
chars.all(|c| c.is_alphanumeric() || c == '_' || !c.is_ascii())
|
||||
}
|
||||
|
||||
/// Parse `lib/time.mq.md` / `lib/time.mq.md as time` / `lib/时间.mq.md 作为 时间`.
|
||||
pub fn parse_import_spec(rest: &str) -> Option<Import> {
|
||||
let rest = rest.trim();
|
||||
|
||||
Reference in new issue
Block a user