1 .\" $Id: mdoc.3,v 1.7 2009/02/23 09:46:59 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 23 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
70 In general, applications initiate a parsing sequence with
72 parse each line in a document with
74 close the parsing session with
76 operate over the syntax tree returned by
80 then free all allocated memory with
84 section for a full example.
87 This section further defines the
92 available to programmers. The last sub-section,
93 .Sx Abstract Syntax Tree ,
94 documents the output tree.
101 may use the following types:
105 An opaque type defined in
107 Its values are only used privately within the library.
109 .It Vt struct mdoc_cb
110 A set of message callbacks defined in
113 .It Vt struct mdoc_node
114 A parsed node. Defined in
117 .Sx Abstract Syntax Tree
122 Function descriptions follow:
126 Allocates a parsing structure. The
128 pointer is passed to callbacks in
130 which are documented further in the header file. Returns NULL on
131 failure. If non-NULL, the pointer must be freed with
135 Free all resources of a parser. The pointer is no longer valid after
139 Parse a nil-terminated line of input. This line should not contain the
140 trailing newline. Returns 0 on failure, 1 on success. The input buffer
142 is modified by this function.
145 Signals that the parse is complete. Note that if
147 is called subsequent to
149 the resulting tree is incomplete. Returns 0 on failure, 1 on success.
152 Returns the first node of the parse. Note that if
156 return 0, the tree will be incomplete.
158 Returns the document's parsed meta-data. If this information has not
163 return 0, the data will be incomplete.
167 The following variables are also defined:
170 .It Va mdoc_macronames
171 An array of string-ified token names.
174 An array of string-ified token argument names.
177 .Ss Abstract Syntax Tree
180 functions produce an abstract syntax tree (AST) describing the input
181 lines in a regular form. It may be reviewed at any time with
183 however, if called before
189 fail, it may be incomplete.
192 The AST is composed of
194 nodes with block, head, body, element, root and text types as declared
197 field. Each node also provides its parse point (the
202 fields), its position in the tree (the
208 fields) and type-specific data (the
213 The tree itself is arranged according to the following normal form,
214 where capitalised non-terminals represent nodes.
216 .Bl -tag -width "ELEMENTXX" -compact
221 \(<- BLOCK | ELEMENT | TEXT
223 \(<- (HEAD [TEXT])+ [BODY [TEXT]] [TAIL [TEXT]]
225 \(<- BODY [TEXT] [TAIL [TEXT]]
239 Of note are the TEXT nodes following the HEAD, BODY and TAIL nodes of
240 the BLOCK production. These refer to punctuation marks. Furthermore,
241 although a TEXT node will generally have a non-zero-length string, it
242 certain cases, such as
243 .Dq \&.Bd \-literal ,
244 an empty line will produce a zero-length string.
247 The rule-of-thumb for mapping node types to macros follows: in-line
250 are classified as ELEMENT nodes, which can only contain text.
251 Multi-line elements such as
253 are BLOCK elements, where the HEAD constitutes line contents and the
254 BODY constitutes subsequent lines. In-line elements with matching
259 are BLOCK elements with no HEAD tag. The only exception to this is
263 which has a HEAD and TAIL node corresponding to the enclosure string.
264 TEXT nodes, obviously, constitute text; the ROOT node is the document's
268 The following example reads lines from stdin and parses them, operating
269 on the finished parse tree with
271 Note that, if the last line of the file isn't newline-terminated, this
272 will truncate the file's last character (see
274 Further, this example does not error-check nor free memory upon failure.
277 struct mdoc_node *node;
283 mdoc = mdoc_alloc(NULL, NULL);
285 while ((buf = fgetln(fp, &len))) {
286 buf[len - 1] = '\\0';
287 if ( ! mdoc_parseln(mdoc, line, buf))
288 errx(1, "mdoc_parseln");
292 if ( ! mdoc_endparse(mdoc))
293 errx(1, "mdoc_endparse");
294 if (NULL == (node = mdoc_node(mdoc)))
295 errx(1, "mdoc_node");
310 utility was written by
311 .An Kristaps Dzonsons Aq kristaps@kth.se .
314 Bugs, un-implemented macros and incompabilities are documented in this
315 section. The baseline for determining whether macro parsing is
326 macros aren't handled when used to span lines for the
328 macro. Such usage is specifically discouraged in
333 is invoked, whitespace is not stripped around
335 or tab-character separators.
339 macro only accepts a single parameter. Furthermore, several macros
342 and possibly others) accept multiple arguments with a warning.
344 Incompatible: only those macros specified by
350 are supported; support for
354 systems is in progress.