1 .\" $Vendor-Id: man.3,v 1.10 2009/10/03 16:36:06 kristaps Exp $
3 .\" Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>
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.
29 .Nd man macro compiler library
33 .Vt extern const char * const * man_macronames;
35 .Fn man_alloc "void *data" "int pflags" "const struct man_cb *cb"
37 .Fn man_reset "struct man *man"
39 .Fn man_free "struct man *man"
41 .Fn man_parseln "struct man *man" "int line" "char *buf"
42 .Ft "const struct man_node *"
43 .Fn man_node "const struct man *man"
44 .Ft "const struct man_meta *"
45 .Fn man_meta "const struct man *man"
47 .Fn man_endparse "struct man *man"
52 library parses lines of
56 man) into an abstract syntax tree (AST).
59 In general, applications initiate a parsing sequence with
61 parse each line in a document with
63 close the parsing session with
65 operate over the syntax tree returned by
69 then free all allocated memory with
73 function may be used in order to reset the parser for another input
76 section for a full example.
79 This section further defines the
84 available to programmers. Following that, the
85 .Sx Abstract Syntax Tree
86 section documents the output tree.
93 may use the following types:
94 .Bl -ohang -offset "XXXX"
97 An opaque type defined in
99 Its values are only used privately within the library.
102 A set of message callbacks defined in
105 .It Vt struct man_node
106 A parsed node. Defined in
109 .Sx Abstract Syntax Tree
114 Function descriptions follow:
115 .Bl -ohang -offset "XXXX"
118 Allocates a parsing structure. The
120 pointer is passed to callbacks in
122 which are documented further in the header file.
125 arguments are defined in
127 Returns NULL on failure. If non-NULL, the pointer must be freed with
131 Reset the parser for another parse routine. After its use,
133 behaves as if invoked for the first time.
136 Free all resources of a parser. The pointer is no longer valid after
140 Parse a nil-terminated line of input. This line should not contain the
141 trailing newline. Returns 0 on failure, 1 on success. The input buffer
143 is modified by this function.
146 Signals that the parse is complete. Note that if
148 is called subsequent to
150 the resulting tree is incomplete. Returns 0 on failure, 1 on success.
153 Returns the first node of the parse. Note that if
157 return 0, the tree will be incomplete.
159 Returns the document's parsed meta-data. If this information has not
164 return 0, the data will be incomplete.
168 The following variables are also defined:
169 .Bl -ohang -offset "XXXX"
171 .It Va man_macronames
172 An array of string-ified token names.
175 .Ss Abstract Syntax Tree
178 functions produce an abstract syntax tree (AST) describing input in a
179 regular form. It may be reviewed at any time with
181 however, if called before
187 fail, it may be incomplete.
190 This AST is governed by the ontological
193 and derives its terminology accordingly.
196 The AST is composed of
198 nodes with element, root and text types as declared
201 field. Each node also provides its parse point (the
206 fields), its position in the tree (the
212 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 -offset "XXXX"
223 \(<- ELEMENT | TEXT | BLOCK
237 The only elements capable of nesting other elements are those with
238 next-lint scope as documented in
242 The following example reads lines from stdin and parses them, operating
243 on the finished parse tree with
245 Note that, if the last line of the file isn't newline-terminated, this
246 will truncate the file's last character (see
248 Further, this example does not error-check nor free memory upon failure.
249 .Bd -literal -offset "XXXX"
251 struct man_node *node;
257 man = man_alloc(NULL, 0, NULL);
259 while ((buf = fgetln(fp, &len))) {
260 buf[len - 1] = '\\0';
261 if ( ! man_parseln(man, line, buf))
262 errx(1, "man_parseln");
266 if ( ! man_endparse(man))
267 errx(1, "man_endparse");
268 if (NULL == (node = man_node(man)))
282 utility was written by
283 .An Kristaps Dzonsons Aq kristaps@kth.se .