1 .\" $Id: man.3,v 1.18 2010/05/25 22:16:59 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 $
29 .Nd man macro compiler library
33 .Vt extern const char * const * man_macronames;
35 .Fn man_alloc "void *data" "int pflags" "mandocmsg msgs"
37 .Fn man_endparse "struct man *man"
39 .Fn man_free "struct man *man"
40 .Ft "const struct man_meta *"
41 .Fn man_meta "const struct man *man"
42 .Ft "const struct man_node *"
43 .Fn man_node "const struct man *man"
45 .Fn man_parseln "struct man *man" "int line" "char *buf"
47 .Fn man_reset "struct man *man"
51 library parses lines of
53 input into an abstract syntax tree (AST).
55 In general, applications initiate a parsing sequence with
57 parse each line in a document with
59 close the parsing session with
61 operate over the syntax tree returned by
65 then free all allocated memory with
69 function may be used in order to reset the parser for another input
73 section for a full example.
75 Beyond the full set of macros defined in
79 library also accepts the following macros:
81 .Bl -tag -width Ds -compact
83 Has no effect. Handled as a current-scope line macro.
87 .Pq part of the standard preamble for Perl documentation .
88 Handled as a line macro.
92 .Pq part of the standard preamble for Perl documentation .
93 Handled as a current-scope line macro.
99 .Pq part of the standard preamble for Perl documentation .
100 Handled as a current-scope line macro.
103 Furthermore, the following escapes are accepted to allow
105 documents to be correctly formatted:
108 \e*(L" (left double-quote),
109 \e*(R" (right double-quote),
111 \e*(C` (left single-quote),
112 \e*(C' (right single-quote),
117 \e*/ (forward slash),
127 This section further defines the
132 available to programmers.
134 .Sx Abstract Syntax Tree
135 section documents the output tree.
141 may use the following types:
144 An opaque type defined in
146 Its values are only used privately within the library.
148 A function callback type defined in
150 .It Vt struct man_node
155 .Sx Abstract Syntax Tree
159 Function descriptions follow:
162 Allocates a parsing structure.
169 arguments are defined in
171 Returns NULL on failure.
172 If non-NULL, the pointer must be freed with
175 Reset the parser for another parse routine.
178 behaves as if invoked for the first time.
180 Free all resources of a parser.
181 The pointer is no longer valid after invocation.
183 Parse a nil-terminated line of input.
184 This line should not contain the trailing newline.
185 Returns 0 on failure, 1 on success.
188 is modified by this function.
190 Signals that the parse is complete.
193 is called subsequent to
195 the resulting tree is incomplete.
196 Returns 0 on failure, 1 on success.
198 Returns the first node of the parse.
203 return 0, the tree will be incomplete.
205 Returns the document's parsed meta-data.
206 If this information has not yet been supplied or
210 return 0, the data will be incomplete.
213 The following variables are also defined:
215 .It Va man_macronames
216 An array of string-ified token names.
218 .Ss Abstract Syntax Tree
221 functions produce an abstract syntax tree (AST) describing input in a
223 It may be reviewed at any time with
225 however, if called before
231 fail, it may be incomplete.
233 This AST is governed by the ontological rules dictated in
235 and derives its terminology accordingly.
237 The AST is composed of
239 nodes with element, root and text types as declared by the
242 Each node also provides its parse point (the
247 fields), its position in the tree (the
253 fields) and some type-specific data.
255 The tree itself is arranged according to the following normal form,
256 where capitalised non-terminals represent nodes.
258 .Bl -tag -width "ELEMENTXX" -compact
262 \(<- ELEMENT | TEXT | BLOCK
275 The only elements capable of nesting other elements are those with
276 next-lint scope as documented in
279 The following example reads lines from stdin and parses them, operating
280 on the finished parse tree with
282 This example does not error-check nor free memory upon failure.
283 .Bd -literal -offset indent
285 struct man_node *node;
291 man = man_alloc(NULL, 0, NULL);
295 while ((len = getline(&buf, &alloc_len, stdin)) >= 0) {
296 if (len && buflen[len - 1] = '\en')
297 buf[len - 1] = '\e0';
298 if ( ! man_parseln(man, line, buf))
299 errx(1, "man_parseln");
305 if ( ! man_endparse(man))
306 errx(1, "man_endparse");
307 if (NULL == (node = man_node(man)))
316 in the source archive for a rigorous reference.
323 library was written by
324 .An Kristaps Dzonsons Aq kristaps@bsd.lv .