1 .\" $Id: mdoc.3,v 1.20 2009/03/20 15:14:01 kristaps Exp $
3 .\" Copyright (c) 2009 Kristaps Dzonsons <kristaps@openbsd.org>
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 20 2009 $
31 .Nd mdoc macro compiler library
35 .Vt extern const char * const * mdoc_macronames;
36 .Vt extern const char * const * mdoc_argnames;
38 .Fn mdoc_alloc "void *data" "int pflags" "const struct mdoc_cb *cb"
40 .Fn mdoc_reset "struct mdoc *mdoc"
42 .Fn mdoc_free "struct mdoc *mdoc"
44 .Fn mdoc_parseln "struct mdoc *mdoc" "int line" "char *buf"
45 .Ft "const struct mdoc_node *"
46 .Fn mdoc_node "struct mdoc *mdoc"
47 .Ft "const struct mdoc_meta *"
48 .Fn mdoc_meta "struct mdoc *mdoc"
50 .Fn mdoc_endparse "struct mdoc *mdoc"
55 library parses lines of
59 mdoc) into an abstract syntax tree that generalises the semantic
60 annotation of its input. Common front-ends for
69 In general, applications initiate a parsing sequence with
71 parse each line in a document with
73 close the parsing session with
75 operate over the syntax tree returned by
79 then free all allocated memory with
83 function may be used in order to reset the parser for another input
86 section for a full example.
89 This section further defines the
94 available to programmers. Following that, the
95 .Sx Abstract Syntax Tree
96 section documents the output tree.
103 may use the following types:
104 .Bl -ohang -offset "XXXX"
107 An opaque type defined in
109 Its values are only used privately within the library.
111 .It Vt struct mdoc_cb
112 A set of message callbacks defined in
115 .It Vt struct mdoc_node
116 A parsed node. Defined in
119 .Sx Abstract Syntax Tree
124 Function descriptions follow:
125 .Bl -ohang -offset "XXXX"
128 Allocates a parsing structure. The
130 pointer is passed to callbacks in
132 which are documented further in the header file.
135 arguments are defined in
137 Returns NULL on failure. If non-NULL, the pointer must be freed with
141 Reset the parser for another parse routine. After its use,
143 behaves as if invoked for the first time.
146 Free all resources of a parser. The pointer is no longer valid after
150 Parse a nil-terminated line of input. This line should not contain the
151 trailing newline. Returns 0 on failure, 1 on success. The input buffer
153 is modified by this function.
156 Signals that the parse is complete. Note that if
158 is called subsequent to
160 the resulting tree is incomplete. Returns 0 on failure, 1 on success.
163 Returns the first node of the parse. Note that if
167 return 0, the tree will be incomplete.
169 Returns the document's parsed meta-data. If this information has not
174 return 0, the data will be incomplete.
178 The following variables are also defined:
179 .Bl -ohang -offset "XXXX"
181 .It Va mdoc_macronames
182 An array of string-ified token names.
185 An array of string-ified token argument names.
188 .Ss Abstract Syntax Tree
191 functions produce an abstract syntax tree (AST) describing input in a
192 regular form. It may be reviewed at any time with
194 however, if called before
200 fail, it may be incomplete.
203 This AST is governed by the ontological
206 and derives its terminology accordingly.
208 elements described in
210 are described simply as
214 The AST is composed of
216 nodes with block, head, body, element, root and text types as declared
219 field. Each node also provides its parse point (the
224 fields), its position in the tree (the
230 fields) and type-specific data (the
235 The tree itself is arranged according to the following normal form,
236 where capitalised non-terminals represent nodes.
238 .Bl -tag -width "ELEMENTXX" -compact -offset "XXXX"
243 \(<- BLOCK | ELEMENT | TEXT
245 \(<- (HEAD [TEXT])+ [BODY [TEXT]] [TAIL [TEXT]]
247 \(<- BODY [TEXT] [TAIL [TEXT]]
261 Of note are the TEXT nodes following the HEAD, BODY and TAIL nodes of
262 the BLOCK production. These refer to punctuation marks. Furthermore,
263 although a TEXT node will generally have a non-zero-length string, in
265 .Sq \&.Bd \-literal ,
266 an empty line will produce a zero-length string.
269 The following example reads lines from stdin and parses them, operating
270 on the finished parse tree with
272 Note that, if the last line of the file isn't newline-terminated, this
273 will truncate the file's last character (see
275 Further, this example does not error-check nor free memory upon failure.
276 .Bd -literal -offset "XXXX"
278 struct mdoc_node *node;
284 mdoc = mdoc_alloc(NULL, NULL);
286 while ((buf = fgetln(fp, &len))) {
287 buf[len - 1] = '\\0';
288 if ( ! mdoc_parseln(mdoc, line, buf))
289 errx(1, "mdoc_parseln");
293 if ( ! mdoc_endparse(mdoc))
294 errx(1, "mdoc_endparse");
295 if (NULL == (node = mdoc_node(mdoc)))
296 errx(1, "mdoc_node");
309 utility was written by
310 .An Kristaps Dzonsons Aq kristaps@openbsd.org .
320 macros aren't handled when used to span lines for the
327 macro doesn't yet understand version arguments.