Annotation of mandoc/mdoc.3, Revision 1.25
1.25 ! kristaps 1: .\" $Id: mdoc.3,v 1.24 2009/03/26 16:23:22 kristaps Exp $
1.6 kristaps 2: .\"
1.19 kristaps 3: .\" Copyright (c) 2009 Kristaps Dzonsons <kristaps@openbsd.org>
1.6 kristaps 4: .\"
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
8: .\" copies.
9: .\"
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.
1.1 kristaps 18: .\"
19: .Dd $Mdocdate$
20: .Dt mdoc 3
21: .Os
1.6 kristaps 22: .\" SECTION
1.1 kristaps 23: .Sh NAME
24: .Nm mdoc_alloc ,
25: .Nm mdoc_parseln ,
26: .Nm mdoc_endparse ,
1.4 kristaps 27: .Nm mdoc_node ,
28: .Nm mdoc_meta ,
1.20 kristaps 29: .Nm mdoc_free ,
30: .Nm mdoc_reset
1.2 kristaps 31: .Nd mdoc macro compiler library
1.6 kristaps 32: .\" SECTION
1.1 kristaps 33: .Sh SYNOPSIS
1.4 kristaps 34: .Fd #include <mdoc.h>
35: .Vt extern const char * const * mdoc_macronames;
36: .Vt extern const char * const * mdoc_argnames;
1.1 kristaps 37: .Ft "struct mdoc *"
1.20 kristaps 38: .Fn mdoc_alloc "void *data" "int pflags" "const struct mdoc_cb *cb"
39: .Ft void
40: .Fn mdoc_reset "struct mdoc *mdoc"
1.1 kristaps 41: .Ft void
1.2 kristaps 42: .Fn mdoc_free "struct mdoc *mdoc"
1.1 kristaps 43: .Ft int
1.2 kristaps 44: .Fn mdoc_parseln "struct mdoc *mdoc" "int line" "char *buf"
1.1 kristaps 45: .Ft "const struct mdoc_node *"
1.4 kristaps 46: .Fn mdoc_node "struct mdoc *mdoc"
47: .Ft "const struct mdoc_meta *"
48: .Fn mdoc_meta "struct mdoc *mdoc"
1.1 kristaps 49: .Ft int
1.2 kristaps 50: .Fn mdoc_endparse "struct mdoc *mdoc"
1.6 kristaps 51: .\" SECTION
1.1 kristaps 52: .Sh DESCRIPTION
53: The
54: .Nm mdoc
1.17 kristaps 55: library parses lines of
56: .Xr mdoc 7
57: input (and
58: .Em only
1.25 ! kristaps 59: mdoc) into an abstract syntax tree (AST).
1.6 kristaps 60: .\" PARAGRAPH
61: .Pp
1.1 kristaps 62: In general, applications initiate a parsing sequence with
63: .Fn mdoc_alloc ,
64: parse each line in a document with
65: .Fn mdoc_parseln ,
66: close the parsing session with
67: .Fn mdoc_endparse ,
68: operate over the syntax tree returned by
1.4 kristaps 69: .Fn mdoc_node
70: and
71: .Fn mdoc_meta ,
1.1 kristaps 72: then free all allocated memory with
73: .Fn mdoc_free .
1.20 kristaps 74: The
75: .Fn mdoc_reset
76: function may be used in order to reset the parser for another input
77: sequence. See the
1.1 kristaps 78: .Sx EXAMPLES
79: section for a full example.
1.6 kristaps 80: .\" PARAGRAPH
1.2 kristaps 81: .Pp
1.6 kristaps 82: This section further defines the
83: .Sx Types ,
84: .Sx Functions
85: and
86: .Sx Variables
1.17 kristaps 87: available to programmers. Following that, the
88: .Sx Abstract Syntax Tree
89: section documents the output tree.
1.6 kristaps 90: .\" SUBSECTION
91: .Ss Types
92: Both functions (see
93: .Sx Functions )
94: and variables (see
95: .Sx Variables )
96: may use the following types:
1.9 kristaps 97: .Bl -ohang -offset "XXXX"
1.6 kristaps 98: .\" LIST-ITEM
99: .It Vt struct mdoc
100: An opaque type defined in
101: .Pa mdoc.c .
102: Its values are only used privately within the library.
103: .\" LIST-ITEM
104: .It Vt struct mdoc_cb
105: A set of message callbacks defined in
106: .Pa mdoc.h .
107: .\" LIST-ITEM
108: .It Vt struct mdoc_node
109: A parsed node. Defined in
110: .Pa mdoc.h .
111: See
112: .Sx Abstract Syntax Tree
113: for details.
114: .El
115: .\" SUBSECTION
116: .Ss Functions
1.2 kristaps 117: Function descriptions follow:
1.9 kristaps 118: .Bl -ohang -offset "XXXX"
1.6 kristaps 119: .\" LIST-ITEM
1.2 kristaps 120: .It Fn mdoc_alloc
121: Allocates a parsing structure. The
122: .Fa data
123: pointer is passed to callbacks in
124: .Fa cb ,
1.20 kristaps 125: which are documented further in the header file.
126: The
127: .Fa pflags
128: arguments are defined in
129: .Pa mdoc.h .
130: Returns NULL on failure. If non-NULL, the pointer must be freed with
1.2 kristaps 131: .Fn mdoc_free .
1.6 kristaps 132: .\" LIST-ITEM
1.20 kristaps 133: .It Fn mdoc_reset
134: Reset the parser for another parse routine. After its use,
135: .Fn mdoc_parseln
136: behaves as if invoked for the first time.
137: .\" LIST-ITEM
1.2 kristaps 138: .It Fn mdoc_free
139: Free all resources of a parser. The pointer is no longer valid after
140: invocation.
1.6 kristaps 141: .\" LIST-ITEM
1.2 kristaps 142: .It Fn mdoc_parseln
143: Parse a nil-terminated line of input. This line should not contain the
144: trailing newline. Returns 0 on failure, 1 on success. The input buffer
145: .Fa buf
146: is modified by this function.
1.6 kristaps 147: .\" LIST-ITEM
1.2 kristaps 148: .It Fn mdoc_endparse
149: Signals that the parse is complete. Note that if
150: .Fn mdoc_endparse
151: is called subsequent to
1.4 kristaps 152: .Fn mdoc_node ,
1.2 kristaps 153: the resulting tree is incomplete. Returns 0 on failure, 1 on success.
1.6 kristaps 154: .\" LIST-ITEM
1.4 kristaps 155: .It Fn mdoc_node
156: Returns the first node of the parse. Note that if
1.2 kristaps 157: .Fn mdoc_parseln
158: or
159: .Fn mdoc_endparse
160: return 0, the tree will be incomplete.
1.4 kristaps 161: .It Fn mdoc_meta
162: Returns the document's parsed meta-data. If this information has not
163: yet been supplied or
164: .Fn mdoc_parseln
165: or
166: .Fn mdoc_endparse
167: return 0, the data will be incomplete.
168: .El
1.6 kristaps 169: .\" SUBSECTION
170: .Ss Variables
1.4 kristaps 171: The following variables are also defined:
1.9 kristaps 172: .Bl -ohang -offset "XXXX"
1.6 kristaps 173: .\" LIST-ITEM
1.4 kristaps 174: .It Va mdoc_macronames
175: An array of string-ified token names.
1.6 kristaps 176: .\" LIST-ITEM
1.4 kristaps 177: .It Va mdoc_argnames
178: An array of string-ified token argument names.
1.2 kristaps 179: .El
1.6 kristaps 180: .\" SUBSECTION
181: .Ss Abstract Syntax Tree
182: The
183: .Nm
1.17 kristaps 184: functions produce an abstract syntax tree (AST) describing input in a
185: regular form. It may be reviewed at any time with
1.6 kristaps 186: .Fn mdoc_nodes ;
187: however, if called before
188: .Fn mdoc_endparse ,
189: or after
190: .Fn mdoc_endparse
191: or
192: .Fn mdoc_parseln
1.18 kristaps 193: fail, it may be incomplete.
194: .\" PARAGRAPH
195: .Pp
196: This AST is governed by the ontological
1.17 kristaps 197: rules dictated in
198: .Xr mdoc 7
199: and derives its terminology accordingly.
200: .Qq In-line
201: elements described in
202: .Xr mdoc 7
203: are described simply as
204: .Qq elements .
1.6 kristaps 205: .\" PARAGRAPH
206: .Pp
207: The AST is composed of
208: .Vt struct mdoc_node
209: nodes with block, head, body, element, root and text types as declared
210: by the
211: .Va type
212: field. Each node also provides its parse point (the
213: .Va line ,
214: .Va sec ,
215: and
216: .Va pos
217: fields), its position in the tree (the
218: .Va parent ,
219: .Va child ,
220: .Va next
221: and
222: .Va prev
1.25 ! kristaps 223: fields) and some type-specific data.
1.6 kristaps 224: .\" PARAGRAPH
225: .Pp
226: The tree itself is arranged according to the following normal form,
227: where capitalised non-terminals represent nodes.
228: .Pp
1.9 kristaps 229: .Bl -tag -width "ELEMENTXX" -compact -offset "XXXX"
1.6 kristaps 230: .\" LIST-ITEM
231: .It ROOT
232: \(<- mnode+
233: .It mnode
234: \(<- BLOCK | ELEMENT | TEXT
235: .It BLOCK
236: \(<- (HEAD [TEXT])+ [BODY [TEXT]] [TAIL [TEXT]]
237: .It BLOCK
238: \(<- BODY [TEXT] [TAIL [TEXT]]
239: .It ELEMENT
240: \(<- TEXT*
241: .It HEAD
242: \(<- mnode+
243: .It BODY
244: \(<- mnode+
245: .It TAIL
246: \(<- mnode+
247: .It TEXT
248: \(<- [[:alpha:]]*
249: .El
250: .\" PARAGRAPH
1.2 kristaps 251: .Pp
1.6 kristaps 252: Of note are the TEXT nodes following the HEAD, BODY and TAIL nodes of
253: the BLOCK production. These refer to punctuation marks. Furthermore,
1.8 kristaps 254: although a TEXT node will generally have a non-zero-length string, in
255: the specific case of
256: .Sq \&.Bd \-literal ,
1.6 kristaps 257: an empty line will produce a zero-length string.
258: .\" SECTION
1.2 kristaps 259: .Sh EXAMPLES
260: The following example reads lines from stdin and parses them, operating
261: on the finished parse tree with
262: .Fn parsed .
263: Note that, if the last line of the file isn't newline-terminated, this
264: will truncate the file's last character (see
265: .Xr fgetln 3 ) .
266: Further, this example does not error-check nor free memory upon failure.
1.9 kristaps 267: .Bd -literal -offset "XXXX"
1.2 kristaps 268: struct mdoc *mdoc;
269: struct mdoc_node *node;
270: char *buf;
271: size_t len;
272: int line;
273:
274: line = 1;
1.25 ! kristaps 275: mdoc = mdoc_alloc(NULL, 0, NULL);
1.2 kristaps 276:
277: while ((buf = fgetln(fp, &len))) {
278: buf[len - 1] = '\\0';
279: if ( ! mdoc_parseln(mdoc, line, buf))
280: errx(1, "mdoc_parseln");
281: line++;
282: }
283:
284: if ( ! mdoc_endparse(mdoc))
285: errx(1, "mdoc_endparse");
1.4 kristaps 286: if (NULL == (node = mdoc_node(mdoc)))
287: errx(1, "mdoc_node");
1.2 kristaps 288:
289: parsed(mdoc, node);
290: mdoc_free(mdoc);
291: .Ed
1.6 kristaps 292: .\" SECTION
1.17 kristaps 293: .Sh SEE ALSO
1.20 kristaps 294: .Xr mandoc 1 ,
1.14 kristaps 295: .Xr mdoc 7
1.6 kristaps 296: .\" SECTION
1.2 kristaps 297: .Sh AUTHORS
298: The
299: .Nm
300: utility was written by
1.19 kristaps 301: .An Kristaps Dzonsons Aq kristaps@openbsd.org .
1.6 kristaps 302: .\" SECTION
1.14 kristaps 303: .Sh CAVEATS
1.17 kristaps 304: .Bl -dash -compact
1.14 kristaps 305: .\" LIST-ITEM
306: .It
307: The
1.24 kristaps 308: .Sq \&.Xc
1.2 kristaps 309: and
1.24 kristaps 310: .Sq \&.Xo
1.2 kristaps 311: macros aren't handled when used to span lines for the
1.24 kristaps 312: .Sq \&.It
1.17 kristaps 313: macro.
1.16 kristaps 314: .\" LIST-ITEM
315: .It
316: The
1.24 kristaps 317: .Sq \&.Bsx
1.23 kristaps 318: macro family doesn't yet understand version arguments.
1.21 kristaps 319: .\" LIST-ITEM
320: .It
321: If not given a value, the \-offset argument to
1.24 kristaps 322: .Sq \&.Bd
1.21 kristaps 323: and
1.24 kristaps 324: .Sq \&.Bl
1.23 kristaps 325: should be the width of
326: .Qq <string> ;
327: instead, a value of
328: .Li 10n
329: is provided.
1.21 kristaps 330: .\" LIST-ITEM
331: .It
332: Columns widths in
1.24 kristaps 333: .Sq \&.Bl \-column
1.21 kristaps 334: should default to width
335: .Qq <stringx>
336: if not included.
337: .\" LIST-ITEM
338: .It
339: List-width suffix
340: .Qq m
341: isn't handled.
1.22 kristaps 342: .\" LIST-ITEM
343: .It
344: Contents of the SYNOPSIS section aren't checked.
1.14 kristaps 345: .El
CVSweb