1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! Documentation Intermediate Representation (DocIR) for oxilean-codegen.
//!
//! This module defines [`DocIR`] — a flat, serialisable representation of the
//! documented declarations in a compiled LCNF module — and [`emit_doc_ir`],
//! which extracts it from an [`LcnfModule`].
//!
//! ## Limitations
//!
//! Doc comments are attached to declarations at the *parse* stage and are not
//! propagated into the codegen IR. Consequently, [`DocIRItem::doc_comment`]
//! will always be an empty string when produced by [`emit_doc_ir`]. For
//! richly-documented output, callers should prefer source-level extraction
//! (see `oxilean-doc`'s `extractor` module) and use [`DocIR`] only as a
//! lightweight index of compiled declarations.
use crate::lcnf::types::{LcnfExternDecl, LcnfFunDecl, LcnfModule};
// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------
/// Documentation IR — a flat list of documented declarations from one module.
///
/// Produced by [`emit_doc_ir`] and consumed by `oxilean-doc` to generate
/// documentation without re-parsing the source file.
#[derive(Debug, Clone, PartialEq)]
pub struct DocIR {
/// The module name (typically the file path without extension).
pub module_name: String,
/// Documented declarations in declaration order.
pub items: Vec<DocIRItem>,
}
/// A single documented declaration extracted from an [`LcnfModule`].
#[derive(Debug, Clone, PartialEq)]
pub struct DocIRItem {
/// Fully-qualified declaration name.
pub name: String,
/// Declaration kind: `"def"`, `"axiom"`, or `"extern"`.
///
/// `"def"` covers all top-level function declarations (including theorems
/// and instances whose proofs have been compiled). `"axiom"` / `"extern"`
/// is used for declarations that have no compiled body (external stubs,
/// axioms, opaques).
pub kind: String,
/// Pretty-printed type signature.
///
/// Currently always an empty string — type information is partially erased
/// by the LCNF lowering pass and cannot be reliably reconstructed here.
pub signature: String,
/// Doc comment text.
///
/// Currently always empty; doc comments are not propagated into codegen.
/// Use `oxilean-doc`'s source-level extractor for accurate doc comments.
pub doc_comment: String,
/// Whether this declaration was annotated with `@[deprecated]`.
///
/// Not preserved through the LCNF pipeline; always `false` here.
pub deprecated: bool,
}
// ---------------------------------------------------------------------------
// Extraction
// ---------------------------------------------------------------------------
/// Extract documentation IR from a compiled [`LcnfModule`].
///
/// Walks the module's [`LcnfFunDecl`]s (kind `"def"`) and
/// [`LcnfExternDecl`]s (kind `"axiom"`) in order, building one
/// [`DocIRItem`] per declaration.
///
/// # Limitations
///
/// - `signature` and `doc_comment` are empty strings: type information is
/// partially erased in LCNF, and doc comments are not plumbed through from
/// the parse stage.
/// - `deprecated` is always `false` for the same reason.
///
/// If you need signatures or doc comments, use `oxilean-doc`'s source-level
/// `extract()` instead, and use this function only for cross-referencing
/// compiled declaration *names*.
pub fn emit_doc_ir(module_name: &str, module: &LcnfModule) -> DocIR {
let mut items: Vec<DocIRItem> =
Vec::with_capacity(module.fun_decls.len() + module.extern_decls.len());
for decl in &module.fun_decls {
items.push(doc_ir_item_from_fun(decl));
}
for decl in &module.extern_decls {
items.push(doc_ir_item_from_extern(decl));
}
DocIR {
module_name: module_name.to_string(),
items,
}
}
// ---------------------------------------------------------------------------
// Private helpers
// ---------------------------------------------------------------------------
fn doc_ir_item_from_fun(decl: &LcnfFunDecl) -> DocIRItem {
DocIRItem {
name: decl.name.clone(),
kind: "def".to_string(),
signature: String::new(),
doc_comment: String::new(),
deprecated: false,
}
}
fn doc_ir_item_from_extern(decl: &LcnfExternDecl) -> DocIRItem {
DocIRItem {
name: decl.name.clone(),
kind: "axiom".to_string(),
signature: String::new(),
doc_comment: String::new(),
deprecated: false,
}
}
// ---------------------------------------------------------------------------
// Serialisation helpers (JSON via manual formatting — no serde dependency)
// ---------------------------------------------------------------------------
impl DocIRItem {
/// Serialise this item to a JSON object string.
pub fn to_json(&self) -> String {
format!(
r#"{{"name":{},"kind":{},"signature":{},"doc_comment":{},"deprecated":{}}}"#,
json_string(&self.name),
json_string(&self.kind),
json_string(&self.signature),
json_string(&self.doc_comment),
self.deprecated,
)
}
}
impl DocIR {
/// Serialise the entire DocIR to a JSON object string.
pub fn to_json(&self) -> String {
let items_json: Vec<String> = self.items.iter().map(|i| i.to_json()).collect();
format!(
r#"{{"module_name":{},"items":[{}]}}"#,
json_string(&self.module_name),
items_json.join(","),
)
}
/// Deserialise a [`DocIR`] from a JSON string previously produced by
/// [`DocIR::to_json`].
///
/// This is a minimal round-trip parser covering only the exact output
/// format emitted by `to_json`. It is not a general JSON parser.
///
/// Returns `None` if the input cannot be parsed.
pub fn from_json(s: &str) -> Option<DocIR> {
let s = s.trim();
// Expect: {"module_name":"...","items":[...]}
let module_name = extract_json_string_field(s, "module_name")?;
let items_raw = extract_json_array_field(s, "items")?;
let items = parse_json_items(&items_raw);
Some(DocIR { module_name, items })
}
}
/// Escape a string for JSON embedding.
fn json_string(s: &str) -> String {
let mut out = String::with_capacity(s.len() + 2);
out.push('"');
for ch in s.chars() {
match ch {
'"' => out.push_str("\\\""),
'\\' => out.push_str("\\\\"),
'\n' => out.push_str("\\n"),
'\r' => out.push_str("\\r"),
'\t' => out.push_str("\\t"),
c => out.push(c),
}
}
out.push('"');
out
}
/// Extract the value of a JSON string field from a flat JSON object string.
///
/// Looks for `"key":"value"` patterns only; does not handle nested objects.
fn extract_json_string_field(s: &str, key: &str) -> Option<String> {
let needle = format!("\"{}\":\"", key);
let start = s.find(&needle)? + needle.len();
let rest = &s[start..];
let mut value = String::new();
let mut chars = rest.chars();
while let Some(ch) = chars.next() {
match ch {
'"' => return Some(value),
'\\' => match chars.next()? {
'"' => value.push('"'),
'\\' => value.push('\\'),
'n' => value.push('\n'),
'r' => value.push('\r'),
't' => value.push('\t'),
other => {
value.push('\\');
value.push(other);
}
},
c => value.push(c),
}
}
None
}
/// Extract the raw JSON array value of a field from a flat JSON object string.
fn extract_json_array_field(s: &str, key: &str) -> Option<String> {
let needle = format!("\"{}\":[", key);
let start = s.find(&needle)? + needle.len() - 1; // points at '['
let rest = &s[start..];
let mut depth = 0i32;
let mut end = 0;
for (i, ch) in rest.char_indices() {
match ch {
'[' => depth += 1,
']' => {
depth -= 1;
if depth == 0 {
end = i;
break;
}
}
_ => {}
}
}
if end == 0 && depth != 0 {
return None;
}
Some(rest[1..end].to_string()) // strip outer '[' and ']'
}
/// Parse a comma-separated list of JSON item objects from the raw array body.
fn parse_json_items(s: &str) -> Vec<DocIRItem> {
if s.trim().is_empty() {
return Vec::new();
}
// Split on `},{` boundaries
let mut items = Vec::new();
let mut depth = 0i32;
let mut current_start = 0;
for (i, ch) in s.char_indices() {
match ch {
'{' => depth += 1,
'}' => {
depth -= 1;
if depth == 0 {
let obj = &s[current_start..=i];
if let Some(item) = parse_json_item(obj) {
items.push(item);
}
current_start = i + 1;
}
}
_ => {}
}
}
items
}
/// Parse a single `DocIRItem` from a JSON object string.
fn parse_json_item(s: &str) -> Option<DocIRItem> {
let name = extract_json_string_field(s, "name")?;
let kind = extract_json_string_field(s, "kind")?;
let signature = extract_json_string_field(s, "signature").unwrap_or_default();
let doc_comment = extract_json_string_field(s, "doc_comment").unwrap_or_default();
let deprecated = s.contains("\"deprecated\":true");
Some(DocIRItem {
name,
kind,
signature,
doc_comment,
deprecated,
})
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
use crate::lcnf::types::{LcnfExpr, LcnfModule, LcnfType};
fn make_fun_decl(name: &str) -> crate::lcnf::types::LcnfFunDecl {
crate::lcnf::types::LcnfFunDecl {
name: name.to_string(),
original_name: None,
params: vec![],
ret_type: LcnfType::Unit,
body: LcnfExpr::Unreachable,
is_recursive: false,
is_lifted: false,
inline_cost: 0,
}
}
fn make_extern_decl(name: &str) -> crate::lcnf::types::LcnfExternDecl {
crate::lcnf::types::LcnfExternDecl {
name: name.to_string(),
params: vec![],
ret_type: LcnfType::Unit,
}
}
/// DocIRItem can be constructed and Debug-printed.
#[test]
fn test_doc_ir_item_construct_and_debug() {
let item = DocIRItem {
name: "Nat.add".to_string(),
kind: "def".to_string(),
signature: "(n m : Nat) : Nat".to_string(),
doc_comment: "Add two natural numbers.".to_string(),
deprecated: false,
};
let debug_str = format!("{:?}", item);
assert!(debug_str.contains("Nat.add"));
assert!(debug_str.contains("def"));
}
/// DocIR can be serialised to a JSON string.
#[test]
fn test_doc_ir_to_json() {
let ir = DocIR {
module_name: "Nat".to_string(),
items: vec![DocIRItem {
name: "Nat.zero".to_string(),
kind: "def".to_string(),
signature: String::new(),
doc_comment: String::new(),
deprecated: false,
}],
};
let json = ir.to_json();
assert!(json.contains("\"module_name\":\"Nat\""));
assert!(json.contains("\"Nat.zero\""));
assert!(json.contains("\"kind\":\"def\""));
}
/// emit_doc_ir with an empty module returns DocIR with no items.
#[test]
fn test_emit_doc_ir_empty() {
let module = LcnfModule::default();
let ir = emit_doc_ir("Empty", &module);
assert_eq!(ir.module_name, "Empty");
assert!(ir.items.is_empty());
}
/// emit_doc_ir with N fun_decls returns N items with correct names.
#[test]
fn test_emit_doc_ir_fun_decls() {
let mut module = LcnfModule::default();
module.fun_decls.push(make_fun_decl("Foo.bar"));
module.fun_decls.push(make_fun_decl("Foo.baz"));
let ir = emit_doc_ir("Foo", &module);
assert_eq!(ir.items.len(), 2);
assert_eq!(ir.items[0].name, "Foo.bar");
assert_eq!(ir.items[0].kind, "def");
assert_eq!(ir.items[1].name, "Foo.baz");
}
/// DocIR round-trips through JSON: serialise then deserialise, check equality.
#[test]
fn test_doc_ir_json_roundtrip() {
let original = DocIR {
module_name: "MyModule".to_string(),
items: vec![
DocIRItem {
name: "MyModule.foo".to_string(),
kind: "def".to_string(),
signature: String::new(),
doc_comment: String::new(),
deprecated: false,
},
DocIRItem {
name: "MyModule.Axiom1".to_string(),
kind: "axiom".to_string(),
signature: String::new(),
doc_comment: String::new(),
deprecated: true,
},
],
};
let json = original.to_json();
let recovered = DocIR::from_json(&json).expect("round-trip parse failed");
assert_eq!(recovered.module_name, original.module_name);
assert_eq!(recovered.items.len(), original.items.len());
assert_eq!(recovered.items[0].name, original.items[0].name);
assert_eq!(recovered.items[1].name, original.items[1].name);
}
/// The deprecated field is preserved in JSON round-trips.
#[test]
fn test_deprecated_field_preserved() {
let item = DocIRItem {
name: "OldApi.fn".to_string(),
kind: "def".to_string(),
signature: String::new(),
doc_comment: String::new(),
deprecated: true,
};
let ir = DocIR {
module_name: "OldApi".to_string(),
items: vec![item],
};
let json = ir.to_json();
let recovered = DocIR::from_json(&json).expect("parse failed");
assert!(recovered.items[0].deprecated);
}
/// emit_doc_ir with extern_decls returns items with kind "axiom".
#[test]
fn test_emit_doc_ir_extern_decls() {
let mut module = LcnfModule::default();
module.extern_decls.push(make_extern_decl("Nat.rec"));
let ir = emit_doc_ir("Nat", &module);
assert_eq!(ir.items.len(), 1);
assert_eq!(ir.items[0].kind, "axiom");
assert_eq!(ir.items[0].name, "Nat.rec");
}
}