1 .\" $Id: mdoc.3,v 1.14 2009/03/12 15:55:11 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: March 12 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 delimited in various ways. All are
188 preceeded by an escape character
190 and followed by either an open-parenthesis
192 for two-character sequences; an open-bracket
194 for n-character sequences (terminated at a close-bracket
196 an asterisk and open-parenthesis
198 for two-character sequences;
199 an asterisk and non-open-parenthesis
201 for single-character sequences; or one of a small set of standalone
202 single characters for other escapes.
207 .Bl -tag -width "XXXXXXXX" -offset "XXXX" -compact
236 All escaped sequences are syntax-checked, but it's up to the front-end
237 system to correctly render them to the output device.
239 .Ss Abstract Syntax Tree
242 functions produce an abstract syntax tree (AST) describing the input
243 lines in a regular form. It may be reviewed at any time with
245 however, if called before
251 fail, it may be incomplete.
254 The AST is composed of
256 nodes with block, head, body, element, root and text types as declared
259 field. Each node also provides its parse point (the
264 fields), its position in the tree (the
270 fields) and type-specific data (the
275 The tree itself is arranged according to the following normal form,
276 where capitalised non-terminals represent nodes.
278 .Bl -tag -width "ELEMENTXX" -compact -offset "XXXX"
283 \(<- BLOCK | ELEMENT | TEXT
285 \(<- (HEAD [TEXT])+ [BODY [TEXT]] [TAIL [TEXT]]
287 \(<- BODY [TEXT] [TAIL [TEXT]]
301 Of note are the TEXT nodes following the HEAD, BODY and TAIL nodes of
302 the BLOCK production. These refer to punctuation marks. Furthermore,
303 although a TEXT node will generally have a non-zero-length string, in
305 .Sq \&.Bd \-literal ,
306 an empty line will produce a zero-length string.
309 The rule-of-thumb for mapping node types to macros follows. In-line
312 are classified as ELEMENT nodes, which can only contain text.
313 Multi-line elements, such as
315 are BLOCK elements, where the HEAD constitutes line contents and the
316 BODY constitutes subsequent lines. In-line elements with matching
321 are BLOCK elements with no HEAD tag. The only exception to this is
325 which has a HEAD and TAIL node corresponding to the enclosure string.
326 TEXT nodes, obviously, constitute text, and the ROOT node is the
330 The following example reads lines from stdin and parses them, operating
331 on the finished parse tree with
333 Note that, if the last line of the file isn't newline-terminated, this
334 will truncate the file's last character (see
336 Further, this example does not error-check nor free memory upon failure.
337 .Bd -literal -offset "XXXX"
339 struct mdoc_node *node;
345 mdoc = mdoc_alloc(NULL, NULL);
347 while ((buf = fgetln(fp, &len))) {
348 buf[len - 1] = '\\0';
349 if ( ! mdoc_parseln(mdoc, line, buf))
350 errx(1, "mdoc_parseln");
354 if ( ! mdoc_endparse(mdoc))
355 errx(1, "mdoc_endparse");
356 if (NULL == (node = mdoc_node(mdoc)))
357 errx(1, "mdoc_node");
364 In general, only those macros specified by
372 are supported; support for other
374 systems is in progress.
380 is assumed for all lists: any list may be nested and
382 lists will restart the sequence only for the sub-list.
387 syntax, where column widths may be preceeded by other arguments (instead
388 of proceeded), is not supported.
393 macro only accepts a single parameter.
405 utility was written by
406 .An Kristaps Dzonsons Aq kristaps@kth.se .
416 macros aren't handled when used to span lines for the
418 macro. Such usage is specifically discouraged in