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
//! Assembling the content of a document from its primary file and the files
//! it includes with `\input`.
//!
//! A document may be made of several files: the **primary file**, the one a
//! host is asked to process, and the files it includes with the command
//! `\input`. An included file is a content input or a heading input ([content
//! inputs and heading inputs](crate::lang::input#content-inputs-and-heading-inputs)).
//! The library reads no file itself: the
//! program that embeds it supplies an input resolver
//! ([`InputResolver`](crate::lang::InputResolver)), which
//! finds and reads the file that an `\input` refers to.
//!
//! [`assemble`] parses the primary file into a document's content
//! ([`DocContent`]), bringing in the included files as it goes:
//!
//! - The content of an included file (a **content input**) is parsed at the
//! place of its `\input` command, inside the tree of the file that includes
//! it, and the rewrite stage of the `input` feature
//! ([`input`](crate::stdlib::features::input)) later inserts it in place of
//! the command, so every later stage sees one continuous text.
//! - An included file whose front matter (the block of settings at the head of
//! the file, read by the input resolver) declares that it opens with a
//! heading (a **heading input**,
//! [`IncludedFile::heading`](crate::lang::IncludedFile::heading)) becomes a
//! tree of its own instead. It is parsed under the parsing state that applies
//! at its `\input` command, with the heading its front matter declares at
//! the start, and the tree is placed at the command's node
//! ([`DocContent::resolve`]), so that the walk visits it there.
//!
//! The primary file is parsed from the initial parsing state of the language;
//! [`assemble_with_state`] parses it from a state the caller chooses, and each
//! included file follows, since it is parsed under the state at its `\input`
//! command.
//!
//! A heading input may include files in turn, of either kind. Later stages see
//! trees and the walk only; whether a tree came from an included file plays no
//! part in them.
use Arc;
use NodeId;
use ;
use Diagnostic;
use crate;
use crate;
use crate;
use crateBuiltLang;
use crateNodeAddr;
use crate;
/// Parses `source`, the primary file of a document, with the language `lang`,
/// and returns the document's content: the tree of the primary file, with each
/// content input parsed at its `\input` command, and one more tree for each
/// heading input, placed at its `\input` command (see the [module
/// documentation](self)).
///
/// - `options.input_resolver` is the input resolver for this call; when it is
/// `None`, the language's own input resolver is used. Without either, each
/// `\input` is reported and brings nothing in.
/// - `layout` is the layout of the top level of the primary file's tree; the
/// tree of a heading input always holds blocks
/// ([`FragmentLayout::Blocks`]).
///
/// Every problem found while parsing is a diagnostic of the fragment of the
/// tree it was found in ([`Fragment::diags`](crate::model::doc::Fragment::diags)).
/// A heading input whose file yields no tree, or whose command was not kept by
/// the parse ([`InputPlacementLost`]), is reported in the tree of the file that
/// includes it, and adds no tree.
///
/// Each file is parsed on a worker thread (a thread that declared its stack
/// size; see [`on_worker_thread`](crate::lang::guard::on_worker_thread)),
/// created when the calling thread is not one.
///
/// The primary file is parsed from the initial parsing state of `lang`
/// ([`BuiltLang::initial_state`]). To parse it from another state, use
/// [`assemble_with_state`].
///
/// Use [`Doc::new`](crate::model::doc::Doc::new) or
/// [`Doc::with_options`](crate::model::doc::Doc::with_options) to make a
/// document of the result.
///
/// # Errors
///
/// Returns the [`FragmentError`] of the parse when the primary file yields no
/// tree, for the same reasons as
/// [`parse_fragment`](crate::lang::parse_fragment): the content nests too
/// deeply, the parse was aborted, or no worker thread could be created.
///
/// # Examples
///
/// ```
/// use std::sync::Arc;
/// use flm_core::lang::{
/// build_language, FlmOrigin, FlmParsing, FlmSource, IncludedFile, InputError,
/// InputResolver, LangInputs, ParseOptions,
/// };
/// use flm_core::model::assemble::assemble;
/// use flm_core::model::doc::FragmentLayout;
///
/// // A host that knows one file, held in memory.
/// struct OneFile;
/// impl InputResolver for OneFile {
/// fn resolve(&self, reference: &str, _including: Option<&str>, _parsing: &FlmParsing) -> Result<IncludedFile, InputError> {
/// match reference {
/// "part.flm" => Ok(IncludedFile {
/// name: String::from("part.flm"),
/// text: String::from("world"),
/// first_line: 1,
/// ..IncludedFile::default()
/// }),
/// _ => Err(InputError::new(format!("there is no file `{reference}`"))),
/// }
/// }
/// }
///
/// // The source holds no `\input`, so the document is the primary file's tree
/// // alone. (The bare language below would not recognize `\input` anyway: the
/// // standard feature `input` declares it.)
/// let lang = build_language(LangInputs::new());
/// let source = Arc::new(FlmSource::new("Hello").with_origin(FlmOrigin::new("main.flm")));
/// let options = ParseOptions { input_resolver: Some(Arc::new(OneFile)), ..ParseOptions::default() };
/// let content = assemble(&lang, source, FragmentLayout::default(), &options)
/// .expect("the input is shallow enough to parse");
/// assert_eq!(content.len(), 1);
/// ```
/// Parses `source`, the primary file of a document, with the language `lang`,
/// starting from the parsing state `state`, and returns the document's
/// content, as [`assemble`] does.
///
/// `state` must be derived from the initial parsing state of `lang` with
/// [`BuiltLang::derive_state`], as for
/// [`parse_fragment_with_state`](crate::lang::parse_fragment_with_state). A
/// program uses it to parse a document under a root state of its own: for
/// instance a state with a [scoped package](BuiltLang::scoped_package) added,
/// or with a scope stack of its own built from [`BuiltLang::base_scope`].
///
/// Only the primary file starts from `state`. Each included file is parsed
/// under the parsing state that applies at its `\input` command, so a content
/// input or a heading input written at the top level of the primary file is
/// parsed under `state`, and one written in an argument or an environment's
/// body under the state that applies there.
///
/// The parameters `layout` and `options` and the behavior of the assembly are
/// the same as for [`assemble`]. [`assemble`] calls this function with the
/// initial parsing state of `lang`.
///
/// # Errors
///
/// Returns the same errors as [`assemble`].
///
/// # Examples
///
/// ```
/// use std::sync::Arc;
/// use flm_core::lang::{build_language, FlmOrigin, FlmSource, LangInputs, ParseOptions};
/// use flm_core::model::assemble::assemble_with_state;
/// use flm_core::model::doc::FragmentLayout;
/// use flm_core::techy::core::specs::ScopeOp;
/// use flm_core::techy::core::ParsingStateDelta;
///
/// // A root state whose scope stack keeps the base scope alone.
/// let lang = build_language(LangInputs::new());
/// let root = lang
/// .derive_state(&ParsingStateDelta::new().scope_op(ScopeOp::ReplaceStack(lang.base_scope().to_vec())))
/// .expect("a stack replacement applies outside a parse");
/// let source = Arc::new(FlmSource::new("Hello").with_origin(FlmOrigin::new("main.flm")));
/// let content = assemble_with_state(
/// &lang, source, FragmentLayout::default(), &ParseOptions::default(), root,
/// )
/// .expect("the input is shallow enough to parse");
/// assert_eq!(content.len(), 1);
/// ```
/// A parsed tree and the trees of the heading inputs placed in it, each with
/// the node that places it.
/// Parses, as trees of their own, the heading inputs found by the parse of
/// `fragment` (the records of `placements` from index `from` on), and in turn
/// the heading inputs found by those parses.
/// Adds the tree of `assembled` to `content` (appended to the traversal list
/// when `listed`, otherwise reached through the node that places it), then
/// each tree placed in it, placed at its node; returns the tree's identifier.