1 .\" $Id: mdoc.3,v 1.27 2009/04/12 19:19:57 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: April 12 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 (AST).
62 In general, applications initiate a parsing sequence with
64 parse each line in a document with
66 close the parsing session with
68 operate over the syntax tree returned by
72 then free all allocated memory with
76 function may be used in order to reset the parser for another input
79 section for a full example.
82 This section further defines the
87 available to programmers. Following that, the
88 .Sx Abstract Syntax Tree
89 section documents the output tree.
96 may use the following types:
97 .Bl -ohang -offset "XXXX"
100 An opaque type defined in
102 Its values are only used privately within the library.
104 .It Vt struct mdoc_cb
105 A set of message callbacks defined in
108 .It Vt struct mdoc_node
109 A parsed node. Defined in
112 .Sx Abstract Syntax Tree
117 Function descriptions follow:
118 .Bl -ohang -offset "XXXX"
121 Allocates a parsing structure. The
123 pointer is passed to callbacks in
125 which are documented further in the header file.
128 arguments are defined in
130 Returns NULL on failure. If non-NULL, the pointer must be freed with
134 Reset the parser for another parse routine. After its use,
136 behaves as if invoked for the first time. If it returns 0, memory could
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 Abstract Syntax Tree
185 functions produce an abstract syntax tree (AST) describing input in a
186 regular form. It may be reviewed at any time with
188 however, if called before
194 fail, it may be incomplete.
197 This AST is governed by the ontological
200 and derives its terminology accordingly.
202 elements described in
204 are described simply as
208 The AST is composed of
210 nodes with block, head, body, element, root and text types as declared
213 field. Each node also provides its parse point (the
218 fields), its position in the tree (the
224 fields) and some type-specific data.
227 The tree itself is arranged according to the following normal form,
228 where capitalised non-terminals represent nodes.
230 .Bl -tag -width "ELEMENTXX" -compact -offset "XXXX"
235 \(<- BLOCK | ELEMENT | TEXT
237 \(<- (HEAD [TEXT])+ [BODY [TEXT]] [TAIL [TEXT]]
239 \(<- BODY [TEXT] [TAIL [TEXT]]
253 Of note are the TEXT nodes following the HEAD, BODY and TAIL nodes of
254 the BLOCK production. These refer to punctuation marks. Furthermore,
255 although a TEXT node will generally have a non-zero-length string, in
257 .Sq \&.Bd \-literal ,
258 an empty line will produce a zero-length string.
261 The following example reads lines from stdin and parses them, operating
262 on the finished parse tree with
264 Note that, if the last line of the file isn't newline-terminated, this
265 will truncate the file's last character (see
267 Further, this example does not error-check nor free memory upon failure.
268 .Bd -literal -offset "XXXX"
270 struct mdoc_node *node;
276 mdoc = mdoc_alloc(NULL, 0, NULL);
278 while ((buf = fgetln(fp, &len))) {
279 buf[len - 1] = '\\0';
280 if ( ! mdoc_parseln(mdoc, line, buf))
281 errx(1, "mdoc_parseln");
285 if ( ! mdoc_endparse(mdoc))
286 errx(1, "mdoc_endparse");
287 if (NULL == (node = mdoc_node(mdoc)))
288 errx(1, "mdoc_node");
301 utility was written by
302 .An Kristaps Dzonsons Aq kristaps@openbsd.org .
312 macros aren't handled when used to span lines for the
319 macro family doesn't yet understand version arguments.
322 If not given a value, the \-offset argument to
326 should be the width of
335 should default to width
345 Contents of the SYNOPSIS section aren't checked.