Annotation of src/usr.bin/mandoc/mdoc.3, Revision 1.7
1.7 ! schwarze 1: .\" $Id: mdoc.3,v 1.40 2010/05/25 22:16:59 kristaps Exp $
1.1 kristaps 2: .\"
1.6 schwarze 3: .\" Copyright (c) 2009-2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.1 kristaps 4: .\"
5: .\" Permission to use, copy, modify, and distribute this software for any
1.2 schwarze 6: .\" purpose with or without fee is hereby granted, provided that the above
7: .\" copyright notice and this permission notice appear in all copies.
1.1 kristaps 8: .\"
1.2 schwarze 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.
1.4 schwarze 16: .\"
1.7 ! schwarze 17: .Dd $Mdocdate: May 25 2010 $
1.2 schwarze 18: .Dt MDOC 3
1.1 kristaps 19: .Os
20: .Sh NAME
1.7 ! schwarze 21: .Nm mdoc ,
1.1 kristaps 22: .Nm mdoc_alloc ,
23: .Nm mdoc_endparse ,
1.7 ! schwarze 24: .Nm mdoc_free ,
! 25: .Nm mdoc_meta ,
1.1 kristaps 26: .Nm mdoc_node ,
1.7 ! schwarze 27: .Nm mdoc_parseln ,
1.1 kristaps 28: .Nm mdoc_reset
29: .Nd mdoc macro compiler library
30: .Sh SYNOPSIS
1.7 ! schwarze 31: .In mandoc.h
1.5 schwarze 32: .In mdoc.h
1.1 kristaps 33: .Vt extern const char * const * mdoc_macronames;
34: .Vt extern const char * const * mdoc_argnames;
35: .Ft "struct mdoc *"
1.7 ! schwarze 36: .Fn mdoc_alloc "void *data" "int pflags" "mandocmsg msgs"
1.1 kristaps 37: .Ft int
1.7 ! schwarze 38: .Fn mdoc_endparse "struct mdoc *mdoc"
1.1 kristaps 39: .Ft void
40: .Fn mdoc_free "struct mdoc *mdoc"
1.7 ! schwarze 41: .Ft "const struct mdoc_meta *"
! 42: .Fn mdoc_meta "const struct mdoc *mdoc"
! 43: .Ft "const struct mdoc_node *"
! 44: .Fn mdoc_node "const struct mdoc *mdoc"
1.1 kristaps 45: .Ft int
46: .Fn mdoc_parseln "struct mdoc *mdoc" "int line" "char *buf"
47: .Ft int
1.7 ! schwarze 48: .Fn mdoc_reset "struct mdoc *mdoc"
1.1 kristaps 49: .Sh DESCRIPTION
50: The
51: .Nm mdoc
1.4 schwarze 52: library parses lines of
1.1 kristaps 53: .Xr mdoc 7
1.7 ! schwarze 54: input
! 55: into an abstract syntax tree (AST).
1.1 kristaps 56: .Pp
57: In general, applications initiate a parsing sequence with
58: .Fn mdoc_alloc ,
1.4 schwarze 59: parse each line in a document with
1.1 kristaps 60: .Fn mdoc_parseln ,
61: close the parsing session with
62: .Fn mdoc_endparse ,
63: operate over the syntax tree returned by
1.4 schwarze 64: .Fn mdoc_node
1.1 kristaps 65: and
66: .Fn mdoc_meta ,
67: then free all allocated memory with
68: .Fn mdoc_free .
69: The
70: .Fn mdoc_reset
71: function may be used in order to reset the parser for another input
1.7 ! schwarze 72: sequence.
! 73: See the
1.1 kristaps 74: .Sx EXAMPLES
1.7 ! schwarze 75: section for a simple example.
1.1 kristaps 76: .Pp
1.4 schwarze 77: This section further defines the
1.1 kristaps 78: .Sx Types ,
1.4 schwarze 79: .Sx Functions
1.1 kristaps 80: and
81: .Sx Variables
1.7 ! schwarze 82: available to programmers.
! 83: Following that, the
1.4 schwarze 84: .Sx Abstract Syntax Tree
1.1 kristaps 85: section documents the output tree.
86: .Ss Types
87: Both functions (see
88: .Sx Functions )
89: and variables (see
90: .Sx Variables )
91: may use the following types:
1.6 schwarze 92: .Bl -ohang
1.1 kristaps 93: .It Vt struct mdoc
94: An opaque type defined in
95: .Pa mdoc.c .
96: Its values are only used privately within the library.
97: .It Vt struct mdoc_node
1.7 ! schwarze 98: A parsed node.
! 99: Defined in
1.1 kristaps 100: .Pa mdoc.h .
1.4 schwarze 101: See
1.1 kristaps 102: .Sx Abstract Syntax Tree
103: for details.
1.7 ! schwarze 104: .It Vt mandocmsg
! 105: A function callback type defined in
! 106: .Pa mandoc.h .
1.1 kristaps 107: .El
108: .Ss Functions
109: Function descriptions follow:
1.6 schwarze 110: .Bl -ohang
1.1 kristaps 111: .It Fn mdoc_alloc
1.7 ! schwarze 112: Allocates a parsing structure.
! 113: The
1.1 kristaps 114: .Fa data
1.7 ! schwarze 115: pointer is passed to
! 116: .Fa msgs .
1.1 kristaps 117: The
118: .Fa pflags
119: arguments are defined in
120: .Pa mdoc.h .
1.7 ! schwarze 121: Returns NULL on failure.
! 122: If non-NULL, the pointer must be freed with
1.1 kristaps 123: .Fn mdoc_free .
124: .It Fn mdoc_reset
1.7 ! schwarze 125: Reset the parser for another parse routine.
! 126: After its use,
1.1 kristaps 127: .Fn mdoc_parseln
1.7 ! schwarze 128: behaves as if invoked for the first time.
! 129: If it returns 0, memory could not be allocated.
1.1 kristaps 130: .It Fn mdoc_free
1.7 ! schwarze 131: Free all resources of a parser.
! 132: The pointer is no longer valid after invocation.
1.1 kristaps 133: .It Fn mdoc_parseln
1.7 ! schwarze 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.
! 137: The input buffer
1.1 kristaps 138: .Fa buf
139: is modified by this function.
140: .It Fn mdoc_endparse
1.7 ! schwarze 141: Signals that the parse is complete.
! 142: Note that if
1.1 kristaps 143: .Fn mdoc_endparse
144: is called subsequent to
145: .Fn mdoc_node ,
1.7 ! schwarze 146: the resulting tree is incomplete.
! 147: Returns 0 on failure, 1 on success.
1.1 kristaps 148: .It Fn mdoc_node
1.7 ! schwarze 149: Returns the first node of the parse.
! 150: Note that if
1.1 kristaps 151: .Fn mdoc_parseln
152: or
153: .Fn mdoc_endparse
154: return 0, the tree will be incomplete.
155: .It Fn mdoc_meta
1.7 ! schwarze 156: Returns the document's parsed meta-data.
! 157: If this information has not yet been supplied or
1.1 kristaps 158: .Fn mdoc_parseln
159: or
160: .Fn mdoc_endparse
161: return 0, the data will be incomplete.
162: .El
163: .Ss Variables
164: The following variables are also defined:
1.6 schwarze 165: .Bl -ohang
1.1 kristaps 166: .It Va mdoc_macronames
167: An array of string-ified token names.
168: .It Va mdoc_argnames
169: An array of string-ified token argument names.
170: .El
171: .Ss Abstract Syntax Tree
1.4 schwarze 172: The
1.1 kristaps 173: .Nm
174: functions produce an abstract syntax tree (AST) describing input in a
1.7 ! schwarze 175: regular form.
! 176: It may be reviewed at any time with
1.1 kristaps 177: .Fn mdoc_nodes ;
178: however, if called before
179: .Fn mdoc_endparse ,
180: or after
1.4 schwarze 181: .Fn mdoc_endparse
1.1 kristaps 182: or
183: .Fn mdoc_parseln
1.4 schwarze 184: fail, it may be incomplete.
1.1 kristaps 185: .Pp
186: This AST is governed by the ontological
187: rules dictated in
188: .Xr mdoc 7
1.4 schwarze 189: and derives its terminology accordingly.
1.1 kristaps 190: .Qq In-line
191: elements described in
192: .Xr mdoc 7
1.4 schwarze 193: are described simply as
1.1 kristaps 194: .Qq elements .
195: .Pp
1.4 schwarze 196: The AST is composed of
1.1 kristaps 197: .Vt struct mdoc_node
198: nodes with block, head, body, element, root and text types as declared
199: by the
200: .Va type
1.7 ! schwarze 201: field.
! 202: Each node also provides its parse point (the
1.1 kristaps 203: .Va line ,
204: .Va sec ,
205: and
206: .Va pos
207: fields), its position in the tree (the
208: .Va parent ,
209: .Va child ,
1.4 schwarze 210: .Va next
1.1 kristaps 211: and
1.4 schwarze 212: .Va prev
1.1 kristaps 213: fields) and some type-specific data.
214: .Pp
215: The tree itself is arranged according to the following normal form,
216: where capitalised non-terminals represent nodes.
217: .Pp
1.6 schwarze 218: .Bl -tag -width "ELEMENTXX" -compact
1.1 kristaps 219: .It ROOT
220: \(<- mnode+
221: .It mnode
222: \(<- BLOCK | ELEMENT | TEXT
223: .It BLOCK
224: \(<- (HEAD [TEXT])+ [BODY [TEXT]] [TAIL [TEXT]]
225: .It BLOCK
226: \(<- BODY [TEXT] [TAIL [TEXT]]
227: .It ELEMENT
228: \(<- TEXT*
229: .It HEAD
230: \(<- mnode+
231: .It BODY
232: \(<- mnode+
233: .It TAIL
234: \(<- mnode+
235: .It TEXT
1.7 ! schwarze 236: \(<- [[:printable:],0x1e]*
1.1 kristaps 237: .El
238: .Pp
239: Of note are the TEXT nodes following the HEAD, BODY and TAIL nodes of
1.7 ! schwarze 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
1.1 kristaps 244: .Sq \&.Bd \-literal ,
245: an empty line will produce a zero-length string.
246: .Sh EXAMPLES
247: The following example reads lines from stdin and parses them, operating
1.4 schwarze 248: on the finished parse tree with
1.1 kristaps 249: .Fn parsed .
1.6 schwarze 250: This example does not error-check nor free memory upon failure.
251: .Bd -literal -offset indent
1.1 kristaps 252: struct mdoc *mdoc;
1.3 schwarze 253: const struct mdoc_node *node;
1.1 kristaps 254: char *buf;
255: size_t len;
256: int line;
257:
258: line = 1;
259: mdoc = mdoc_alloc(NULL, 0, NULL);
1.6 schwarze 260: buf = NULL;
261: alloc_len = 0;
1.1 kristaps 262:
1.6 schwarze 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");
268: line++;
1.1 kristaps 269: }
270:
271: if ( ! mdoc_endparse(mdoc))
1.6 schwarze 272: errx(1, "mdoc_endparse");
1.1 kristaps 273: if (NULL == (node = mdoc_node(mdoc)))
1.6 schwarze 274: errx(1, "mdoc_node");
1.1 kristaps 275:
276: parsed(mdoc, node);
277: mdoc_free(mdoc);
278: .Ed
1.7 ! schwarze 279: .Pp
! 280: Please see
! 281: .Pa main.c
! 282: in the source archive for a rigorous reference.
1.1 kristaps 283: .Sh SEE ALSO
284: .Xr mandoc 1 ,
285: .Xr mdoc 7
286: .Sh AUTHORS
287: The
288: .Nm
1.7 ! schwarze 289: library was written by
1.6 schwarze 290: .An Kristaps Dzonsons Aq kristaps@bsd.lv .