Skip to main content

mos_parse/
lib.rs

1//! Parser for the Mosaic source language (`.mos`).
2//!
3//! See manifest §3 (language design) and §6 stages 1–2 (parse + lower).
4//! Currently covers:
5//!
6//! - `= Heading` / `== Subheading` / `=== Subsubheading`,
7//! - paragraphs (newline-joined non-empty line groups),
8//! - inline `*emphasis*`, `**strong**`, and `` `inline code` ``,
9//! - `#set name(...)` blocks, recorded with span and name but interpreted
10//!   later by the evaluator,
11//! - `#image(...)`, `#figure(...)`, and `#bibliography(...)` directives,
12//!   sharing the same `key: value` body grammar as `#set` plus an optional
13//!   leading positional string literal (`#image("path.png")`,
14//!   `#bibliography("refs.bib")`),
15//! - raw `#pre[[...]]` and `#code[[...]]` long-bracket blocks,
16//! - `<label>` attached to the preceding block (trailing on a heading or
17//!   leading on a paragraph), and `@label` cross-references as inline
18//!   [`InlineKind::Reference`] runs (manifest §3.3 and the MVP 1
19//!   resolver),
20//! - `[@key]` citations as inline [`InlineKind::Citation`] runs. Only
21//!   the single-key form is recognised in this slice; bibliography
22//!   loading and rendering are deferred to MVP 4.
23//!
24//! Anything outside that subset is preserved as text and a recoverable
25//! diagnostic is emitted; the parser never panics on user input
26//! (manifest §31).
27
28#![doc(
29    html_logo_url = "https://mosaic.kjanat.dev/assets/A4.svg",
30    html_favicon_url = "https://mosaic.kjanat.dev/assets/A4.svg"
31)]
32
33use std::path::Path;
34
35use mos_core::{DiagnosticResult, DiagnosticSink};
36
37pub use syntax::{
38    DirectiveKind, Inline, InlineKind, Item, LengthUnit, ListItem, ParseResult, RawBlockKind,
39    RawBlockView, SetArg, SetValue, SyntaxTree,
40};
41
42mod block;
43mod directive;
44mod inline;
45mod list;
46mod parser;
47mod support;
48mod syntax;
49
50use parser::Parser;
51
52/// Parse a Mosaic source string, emitting recoverable diagnostics to
53/// `sink` (manifest §6 stage 1). Returns the [`SyntaxTree`]; the parser
54/// never structurally aborts, so the `Err` arm only fires if the sink
55/// itself asks to stop.
56///
57/// # Examples
58///
59/// ```
60/// use std::path::Path;
61///
62/// use mos_core::CollectingSink;
63/// use mos_parse::{InlineKind, Item, parse};
64///
65/// let mut sink = CollectingSink::new();
66/// let result = parse("= Hello\n", Path::new("main.mos"), &mut sink);
67/// assert!(result.is_ok(), "parse structurally aborted: {result:?}");
68/// if let Ok(tree) = result {
69///
70///     assert!(!sink.had_error());
71///     assert!(matches!(tree.items[0], Item::Heading { .. }));
72///     if let Item::Heading { inlines, .. } = &tree.items[0] {
73///         assert_eq!(inlines[0].kind, InlineKind::Text);
74///     }
75/// }
76/// ```
77///
78/// # Errors
79///
80/// Returns [`DiagnosticAbort`](mos_core::DiagnosticAbort) only if `sink`
81/// asks the parse to stop; the in-tree sinks never do.
82pub fn parse(
83    src: &str,
84    file: &Path,
85    sink: &mut dyn DiagnosticSink,
86) -> DiagnosticResult<SyntaxTree> {
87    let ParseResult { tree, diagnostics } = Parser::new(src, file).run();
88    for diagnostic in diagnostics {
89        sink.emit(diagnostic)?;
90    }
91    Ok(tree)
92}