1 .\" $Id: mdoc.3,v 1.52 2011/01/01 12:18:37 kristaps Exp $
3 .\" Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4 .\" Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org>
6 .\" Permission to use, copy, modify, and distribute this software for any
7 .\" purpose with or without fee is hereby granted, provided that the above
8 .\" copyright notice and this permission notice appear in all copies.
10 .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 .Dd $Mdocdate: January 1 2011 $
30 .Nd mdoc macro compiler library
34 .Vt extern const char * const * mdoc_macronames;
35 .Vt extern const char * const * mdoc_argnames;
38 .Fa "struct mdoc *mdoc"
39 .Fa "const struct tbl_span *span"
43 .Fa "struct regset *regs"
48 .Fn mdoc_endparse "struct mdoc *mdoc"
50 .Fn mdoc_free "struct mdoc *mdoc"
51 .Ft "const struct mdoc_meta *"
52 .Fn mdoc_meta "const struct mdoc *mdoc"
53 .Ft "const struct mdoc_node *"
54 .Fn mdoc_node "const struct mdoc *mdoc"
57 .Fa "struct mdoc *mdoc"
62 .Fn mdoc_reset "struct mdoc *mdoc"
66 library parses lines of
69 into an abstract syntax tree (AST).
71 In general, applications initiate a parsing sequence with
73 parse each line in a document with
75 close the parsing session with
77 operate over the syntax tree returned by
81 then free all allocated memory with
85 function may be used in order to reset the parser for another input
91 Its values are only used privately within the library.
92 .It Vt struct mdoc_node
95 .Sx Abstract Syntax Tree
101 Add a table span to the parsing stream.
102 Returns 0 on failure, 1 on success.
104 Allocates a parsing structure.
109 Returns NULL on failure.
110 If non-NULL, the pointer must be freed with
113 Reset the parser for another parse routine.
116 behaves as if invoked for the first time.
117 If it returns 0, memory could not be allocated.
119 Free all resources of a parser.
120 The pointer is no longer valid after invocation.
122 Parse a nil-terminated line of input.
123 This line should not contain the trailing newline.
124 Returns 0 on failure, 1 on success.
127 is modified by this function.
129 Signals that the parse is complete.
132 is called subsequent to
134 the resulting tree is incomplete.
135 Returns 0 on failure, 1 on success.
137 Returns the first node of the parse.
142 return 0, the tree will be incomplete.
144 Returns the document's parsed meta-data.
145 If this information has not yet been supplied or
149 return 0, the data will be incomplete.
153 .It Va mdoc_macronames
154 An array of string-ified token names.
156 An array of string-ified token argument names.
158 .Ss Abstract Syntax Tree
161 functions produce an abstract syntax tree (AST) describing input in a
163 It may be reviewed at any time with
165 however, if called before
171 fail, it may be incomplete.
173 This AST is governed by the ontological
176 and derives its terminology accordingly.
178 elements described in
180 are described simply as
183 The AST is composed of
185 nodes with block, head, body, element, root and text types as declared
189 Each node also provides its parse point (the
194 fields), its position in the tree (the
201 fields) and some type-specific data, in particular, for nodes generated
202 from macros, the generating macro in the
206 The tree itself is arranged according to the following normal form,
207 where capitalised non-terminals represent nodes.
209 .Bl -tag -width "ELEMENTXX" -compact
213 \(<- BLOCK | ELEMENT | TEXT
215 \(<- HEAD [TEXT] (BODY [TEXT])+ [TAIL [TEXT]]
221 \(<- mnode* [ENDBODY mnode*]
225 \(<- [[:printable:],0x1e]*
228 Of note are the TEXT nodes following the HEAD, BODY and TAIL nodes of
229 the BLOCK production: these refer to punctuation marks.
230 Furthermore, although a TEXT node will generally have a non-zero-length
231 string, in the specific case of
232 .Sq \&.Bd \-literal ,
233 an empty line will produce a zero-length string.
234 Multiple body parts are only found in invocations of
236 where a new body introduces a new phrase.
237 .Ss Badly-nested Blocks
238 The ENDBODY node is available to end the formatting associated
239 with a given block before the physical end of that block.
242 field, is of the BODY
246 as the BLOCK it is ending, and has a
248 field pointing to that BLOCK's BODY node.
249 It is an indirect child of that BODY node
250 and has no children of its own.
252 An ENDBODY node is generated when a block ends while one of its child
253 blocks is still open, like in the following example:
254 .Bd -literal -offset indent
261 This example results in the following block structure:
262 .Bd -literal -offset indent
267 BLOCK Bo, pending -> Ao
272 ENDBODY Ao, pending -> Ao
277 Here, the formatting of the
279 block extends from TEXT ao to TEXT ac,
280 while the formatting of the
282 block extends from TEXT bo to TEXT bc.
283 It renders as follows in
287 .Dl <ao [bo ac> bc] end
289 Support for badly-nested blocks is only provided for backward
290 compatibility with some older
293 Using badly-nested blocks is
294 .Em strongly discouraged :
299 front-ends are unable to render them in any meaningful way.
300 Furthermore, behaviour when encountering badly-nested blocks is not
301 consistent across troff implementations, especially when using multiple
302 levels of badly-nested blocks.
304 The following example reads lines from stdin and parses them, operating
305 on the finished parse tree with
307 This example does not error-check nor free memory upon failure.
308 .Bd -literal -offset indent
311 const struct mdoc_node *node;
316 bzero(®s, sizeof(struct regset));
318 mdoc = mdoc_alloc(®s, NULL, NULL);
322 while ((len = getline(&buf, &alloc_len, stdin)) >= 0) {
323 if (len && buflen[len - 1] = '\en')
324 buf[len - 1] = '\e0';
325 if ( ! mdoc_parseln(mdoc, line, buf))
326 errx(1, "mdoc_parseln");
330 if ( ! mdoc_endparse(mdoc))
331 errx(1, "mdoc_endparse");
332 if (NULL == (node = mdoc_node(mdoc)))
333 errx(1, "mdoc_node");
339 To compile this, execute
341 .Dl % cc main.c libmdoc.a libmandoc.a
352 library was written by
353 .An Kristaps Dzonsons Aq kristaps@bsd.lv .