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://mosaiclang.dev/assets/A4.svg",
30 html_favicon_url = "https://mosaiclang.dev/assets/A4.svg"
31)]
32
33use std::path::Path;
34
35use mos_core::{DiagnosticResult, DiagnosticSink};
36
37#[doc(hidden)]
38pub use parser::Parser;
39#[doc(hidden)]
40pub use support::*;
41pub use syntax::{
42 DirectiveKind, Inline, InlineKind, Item, LengthUnit, ListItem, ListItemBlock, ParseResult,
43 RawBlockKind, RawBlockView, SetArg, SetValue, SyntaxTree,
44};
45
46mod block;
47mod directive;
48mod inline;
49mod list;
50#[doc(hidden)]
51pub mod parser;
52#[doc(hidden)]
53pub mod support;
54mod syntax;
55
56/// Parse a Mosaic source string into a [`SyntaxTree`].
57///
58/// Recoverable diagnostics are emitted to `sink` (manifest §6 stage 1). The
59/// parser never structurally aborts, so the `Err` arm only fires if the sink
60/// itself asks to stop.
61///
62/// # Examples
63///
64/// ```
65/// use std::path::Path;
66///
67/// use mos_core::CollectingSink;
68/// use mos_parse::{InlineKind, Item, parse};
69///
70/// let mut sink = CollectingSink::new();
71/// let result = parse("= Hello\n", Path::new("main.mos"), &mut sink);
72/// assert!(result.is_ok(), "parse structurally aborted: {result:?}");
73/// if let Ok(tree) = result {
74///
75/// assert!(!sink.had_error());
76/// assert!(matches!(tree.items[0], Item::Heading { .. }));
77/// if let Item::Heading { inlines, .. } = &tree.items[0] {
78/// assert_eq!(inlines[0].kind, InlineKind::Text);
79/// }
80/// }
81/// ```
82///
83/// # Errors
84///
85/// Returns [`DiagnosticAbort`](mos_core::DiagnosticAbort) only if `sink`
86/// asks the parse to stop; the in-tree sinks never do.
87pub fn parse(
88 src: &str,
89 file: &Path,
90 sink: &mut dyn DiagnosticSink,
91) -> DiagnosticResult<SyntaxTree> {
92 let ParseResult { tree, diagnostics } = Parser::new(src, file).run();
93 for diagnostic in diagnostics {
94 sink.emit(diagnostic)?;
95 }
96 Ok(tree)
97}