1 .\" $Id: mdoc.3,v 1.11 2009/02/25 17:02:47 kristaps Exp $
3 .\" Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>
5 .\" Permission to use, copy, modify, and distribute this software for any
6 .\" purpose with or without fee is hereby granted, provided that the
7 .\" above copyright notice and this permission notice appear in all
10 .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 .\" WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 .\" WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 .\" AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 .\" DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 .\" PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 .\" TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 .\" PERFORMANCE OF THIS SOFTWARE.
19 .Dd $Mdocdate: February 25 2009 $
30 .Nd mdoc macro compiler library
34 .Vt extern const char * const * mdoc_macronames;
35 .Vt extern const char * const * mdoc_argnames;
37 .Fn mdoc_alloc "void *data" "const struct mdoc_cb *cb"
39 .Fn mdoc_free "struct mdoc *mdoc"
41 .Fn mdoc_parseln "struct mdoc *mdoc" "int line" "char *buf"
42 .Ft "const struct mdoc_node *"
43 .Fn mdoc_node "struct mdoc *mdoc"
44 .Ft "const struct mdoc_meta *"
45 .Fn mdoc_meta "struct mdoc *mdoc"
47 .Fn mdoc_endparse "struct mdoc *mdoc"
52 library parses lines of mdoc input into an abstract syntax tree.
54 which is used to format BSD manual pages, is a macro package of the
58 library implements only those macros documented in the
62 manuals. Documents with
65 and other pre-processor sections aren't accomodated.
73 In general, applications initiate a parsing sequence with
75 parse each line in a document with
77 close the parsing session with
79 operate over the syntax tree returned by
83 then free all allocated memory with
87 section for a full example.
90 This section further defines the
95 available to programmers. Following that,
96 .Sx Character Encoding
97 describes input format. Lastly,
98 .Sx Abstract Syntax Tree ,
99 documents the output tree.
106 may use the following types:
107 .Bl -ohang -offset "XXXX"
110 An opaque type defined in
112 Its values are only used privately within the library.
114 .It Vt struct mdoc_cb
115 A set of message callbacks defined in
118 .It Vt struct mdoc_node
119 A parsed node. Defined in
122 .Sx Abstract Syntax Tree
127 Function descriptions follow:
128 .Bl -ohang -offset "XXXX"
131 Allocates a parsing structure. The
133 pointer is passed to callbacks in
135 which are documented further in the header file. Returns NULL on
136 failure. If non-NULL, the pointer must be freed with
140 Free all resources of a parser. The pointer is no longer valid after
144 Parse a nil-terminated line of input. This line should not contain the
145 trailing newline. Returns 0 on failure, 1 on success. The input buffer
147 is modified by this function.
150 Signals that the parse is complete. Note that if
152 is called subsequent to
154 the resulting tree is incomplete. Returns 0 on failure, 1 on success.
157 Returns the first node of the parse. Note that if
161 return 0, the tree will be incomplete.
163 Returns the document's parsed meta-data. If this information has not
168 return 0, the data will be incomplete.
172 The following variables are also defined:
173 .Bl -ohang -offset "XXXX"
175 .It Va mdoc_macronames
176 An array of string-ified token names.
179 An array of string-ified token argument names.
182 .Ss Character Encoding
185 library accepts only printable ASCII characters as defined by
187 Non-ASCII character sequences are escaped with an escape character
189 and followed by either an open-parenthesis
191 for two-character sequences; an open-bracket
193 for n-character sequences (terminated at a close-bracket
195 or one of a small set of single characters for other escapes.
197 .Ss Abstract Syntax Tree
200 functions produce an abstract syntax tree (AST) describing the input
201 lines in a regular form. It may be reviewed at any time with
203 however, if called before
209 fail, it may be incomplete.
212 The AST is composed of
214 nodes with block, head, body, element, root and text types as declared
217 field. Each node also provides its parse point (the
222 fields), its position in the tree (the
228 fields) and type-specific data (the
233 The tree itself is arranged according to the following normal form,
234 where capitalised non-terminals represent nodes.
236 .Bl -tag -width "ELEMENTXX" -compact -offset "XXXX"
241 \(<- BLOCK | ELEMENT | TEXT
243 \(<- (HEAD [TEXT])+ [BODY [TEXT]] [TAIL [TEXT]]
245 \(<- BODY [TEXT] [TAIL [TEXT]]
259 Of note are the TEXT nodes following the HEAD, BODY and TAIL nodes of
260 the BLOCK production. These refer to punctuation marks. Furthermore,
261 although a TEXT node will generally have a non-zero-length string, in
263 .Sq \&.Bd \-literal ,
264 an empty line will produce a zero-length string.
267 The rule-of-thumb for mapping node types to macros follows. In-line
270 are classified as ELEMENT nodes, which can only contain text.
271 Multi-line elements, such as
273 are BLOCK elements, where the HEAD constitutes line contents and the
274 BODY constitutes subsequent lines. In-line elements with matching
279 are BLOCK elements with no HEAD tag. The only exception to this is
283 which has a HEAD and TAIL node corresponding to the enclosure string.
284 TEXT nodes, obviously, constitute text, and the ROOT node is the
288 The following example reads lines from stdin and parses them, operating
289 on the finished parse tree with
291 Note that, if the last line of the file isn't newline-terminated, this
292 will truncate the file's last character (see
294 Further, this example does not error-check nor free memory upon failure.
295 .Bd -literal -offset "XXXX"
297 struct mdoc_node *node;
303 mdoc = mdoc_alloc(NULL, NULL);
305 while ((buf = fgetln(fp, &len))) {
306 buf[len - 1] = '\\0';
307 if ( ! mdoc_parseln(mdoc, line, buf))
308 errx(1, "mdoc_parseln");
312 if ( ! mdoc_endparse(mdoc))
313 errx(1, "mdoc_endparse");
314 if (NULL == (node = mdoc_node(mdoc)))
315 errx(1, "mdoc_node");
330 utility was written by
331 .An Kristaps Dzonsons Aq kristaps@kth.se .
334 Bugs, un-implemented macros and incompabilities are documented in this
335 section. The baseline for determining whether macro parsing is
347 macros aren't handled when used to span lines for the
349 macro. Such usage is specifically discouraged in
355 is invoked, whitespace is not stripped around
357 or tab-character separators.
360 Bugs: elements within columns for
362 are not yet supported.
367 macro only accepts a single parameter. Furthermore, several macros
370 and possibly others) accept multiple arguments with a warning.
373 Incompatible: only those macros specified by
379 are supported; support for
383 systems is in progress.