1 .\" $Id: man.3,v 1.26 2010/12/05 16:14:16 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: December 5 2010 $
29 .Nd man macro compiler library
33 .Vt extern const char * const * man_macronames;
36 .Fa "struct regset *regs"
41 .Fn man_endparse "struct man *man"
43 .Fn man_free "struct man *man"
44 .Ft "const struct man_meta *"
45 .Fn man_meta "const struct man *man"
46 .Ft "const struct man_node *"
47 .Fn man_node "const struct man *man"
55 .Fn man_reset "struct man *man"
59 library parses lines of
61 input into an abstract syntax tree (AST).
63 In general, applications initiate a parsing sequence with
65 parse each line in a document with
67 close the parsing session with
69 operate over the syntax tree returned by
73 then free all allocated memory with
77 function may be used in order to reset the parser for another input
80 Beyond the full set of macros defined in
84 library also accepts the following macro:
86 .Bl -tag -width Ds -compact
89 Handled as a current-scope line macro.
95 Its values are only used privately within the library.
96 .It Vt struct man_node
99 .Sx Abstract Syntax Tree
105 Allocates a parsing structure.
110 Returns NULL on failure.
111 If non-NULL, the pointer must be freed with
114 Reset the parser for another parse routine.
117 behaves as if invoked for the first time.
119 Free all resources of a parser.
120 The pointer is no longer valid after invocation.
122 Parse a nil-terminated line of input.
123 This line should not contain the trailing newline.
124 Returns 0 on failure, 1 on success.
127 is modified by this function.
129 Signals that the parse is complete.
132 is called subsequent to
134 the resulting tree is incomplete.
135 Returns 0 on failure, 1 on success.
137 Returns the first node of the parse.
142 return 0, the tree will be incomplete.
144 Returns the document's parsed meta-data.
145 If this information has not yet been supplied or
149 return 0, the data will be incomplete.
152 The following variables are also defined:
154 .It Va man_macronames
155 An array of string-ified token names.
157 .Ss Abstract Syntax Tree
160 functions produce an abstract syntax tree (AST) describing input in a
162 It may be reviewed at any time with
164 however, if called before
170 fail, it may be incomplete.
172 This AST is governed by the ontological rules dictated in
174 and derives its terminology accordingly.
176 The AST is composed of
178 nodes with element, root and text types as declared by the
181 Each node also provides its parse point (the
186 fields), its position in the tree (the
192 fields) and some type-specific data.
194 The tree itself is arranged according to the following normal form,
195 where capitalised non-terminals represent nodes.
197 .Bl -tag -width "ELEMENTXX" -compact
201 \(<- ELEMENT | TEXT | BLOCK
214 The only elements capable of nesting other elements are those with
215 next-lint scope as documented in
218 The following example reads lines from stdin and parses them, operating
219 on the finished parse tree with
221 This example does not error-check nor free memory upon failure.
222 .Bd -literal -offset indent
225 struct man_node *node;
230 bzero(®s, sizeof(struct regset));
232 man = man_alloc(®s, NULL, NULL);
236 while ((len = getline(&buf, &alloc_len, stdin)) >= 0) {
237 if (len && buflen[len - 1] = '\en')
238 buf[len - 1] = '\e0';
239 if ( ! man_parseln(man, line, buf))
240 errx(1, "man_parseln");
246 if ( ! man_endparse(man))
247 errx(1, "man_endparse");
248 if (NULL == (node = man_node(man)))
255 To compile this, execute
257 .D1 % cc main.c libman.a libmandoc.a
268 library was written by
269 .An Kristaps Dzonsons Aq kristaps@bsd.lv .