1 .\" $Id: mdoc.3,v 1.38 2010/05/25 21:38:05 kristaps Exp $
3 .\" Copyright (c) 2009-2010 Kristaps Dzonsons <kristaps@bsd.lv>
5 .\" Permission to use, copy, modify, and distribute this software for any
6 .\" purpose with or without fee is hereby granted, provided that the above
7 .\" copyright notice and this permission notice appear in all copies.
9 .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 .Dd $Mdocdate: May 25 2010 $
28 .Nd mdoc macro compiler library
32 .Vt extern const char * const * mdoc_macronames;
33 .Vt extern const char * const * mdoc_argnames;
35 .Fn mdoc_alloc "void *data" "int pflags" "mandocmsg msgs"
37 .Fn mdoc_endparse "struct mdoc *mdoc"
39 .Fn mdoc_free "struct mdoc *mdoc"
40 .Ft "const struct mdoc_meta *"
41 .Fn mdoc_meta "const struct mdoc *mdoc"
42 .Ft "const struct mdoc_node *"
43 .Fn mdoc_node "const struct mdoc *mdoc"
45 .Fn mdoc_parseln "struct mdoc *mdoc" "int line" "char *buf"
47 .Fn mdoc_reset "struct mdoc *mdoc"
51 library parses lines of
54 into an abstract syntax tree (AST).
56 In general, applications initiate a parsing sequence with
58 parse each line in a document with
60 close the parsing session with
62 operate over the syntax tree returned by
66 then free all allocated memory with
70 function may be used in order to reset the parser for another input
74 section for a simple example.
76 This section further defines the
81 available to programmers.
83 .Sx Abstract Syntax Tree
84 section documents the output tree.
90 may use the following types:
93 An opaque type defined in
95 Its values are only used privately within the library.
96 .It Vt struct mdoc_node
101 .Sx Abstract Syntax Tree
104 A function callback type defined in
108 Function descriptions follow:
111 Allocates a parsing structure.
114 pointer is passed to callbacks in
116 which are documented further in the header file.
119 arguments are defined in
121 Returns NULL on failure.
122 If non-NULL, the pointer must be freed with
125 Reset the parser for another parse routine.
128 behaves as if invoked for the first time.
129 If it returns 0, memory could not be allocated.
131 Free all resources of a parser.
132 The pointer is no longer valid after invocation.
134 Parse a nil-terminated line of input.
135 This line should not contain the trailing newline.
136 Returns 0 on failure, 1 on success.
139 is modified by this function.
141 Signals that the parse is complete.
144 is called subsequent to
146 the resulting tree is incomplete.
147 Returns 0 on failure, 1 on success.
149 Returns the first node of the parse.
154 return 0, the tree will be incomplete.
156 Returns the document's parsed meta-data.
157 If this information has not yet been supplied or
161 return 0, the data will be incomplete.
164 The following variables are also defined:
166 .It Va mdoc_macronames
167 An array of string-ified token names.
169 An array of string-ified token argument names.
171 .Ss Abstract Syntax Tree
174 functions produce an abstract syntax tree (AST) describing input in a
176 It may be reviewed at any time with
178 however, if called before
184 fail, it may be incomplete.
186 This AST is governed by the ontological
189 and derives its terminology accordingly.
191 elements described in
193 are described simply as
196 The AST is composed of
198 nodes with block, head, body, element, root and text types as declared
202 Each node also provides its parse point (the
207 fields), its position in the tree (the
213 fields) and some type-specific data.
215 The tree itself is arranged according to the following normal form,
216 where capitalised non-terminals represent nodes.
218 .Bl -tag -width "ELEMENTXX" -compact
222 \(<- BLOCK | ELEMENT | TEXT
224 \(<- (HEAD [TEXT])+ [BODY [TEXT]] [TAIL [TEXT]]
226 \(<- BODY [TEXT] [TAIL [TEXT]]
236 \(<- [[:printable:],0x1e]*
239 Of note are the TEXT nodes following the HEAD, BODY and TAIL nodes of
240 the BLOCK production.
241 These refer to punctuation marks.
242 Furthermore, although a TEXT node will generally have a non-zero-length
243 string, in the specific case of
244 .Sq \&.Bd \-literal ,
245 an empty line will produce a zero-length string.
247 The following example reads lines from stdin and parses them, operating
248 on the finished parse tree with
250 This example does not error-check nor free memory upon failure.
251 .Bd -literal -offset indent
253 const struct mdoc_node *node;
259 mdoc = mdoc_alloc(NULL, 0, NULL);
263 while ((len = getline(&buf, &alloc_len, stdin)) >= 0) {
264 if (len && buflen[len - 1] = '\en')
265 buf[len - 1] = '\e0';
266 if ( ! mdoc_parseln(mdoc, line, buf))
267 errx(1, "mdoc_parseln");
271 if ( ! mdoc_endparse(mdoc))
272 errx(1, "mdoc_endparse");
273 if (NULL == (node = mdoc_node(mdoc)))
274 errx(1, "mdoc_node");
282 in the source archive for a rigorous reference.
289 library was written by
290 .An Kristaps Dzonsons Aq kristaps@bsd.lv .