3 * $Id: scan.h,v 1.4 2002/02/22 23:44:16 mdw Exp $
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the `fw' port forwarder.
14 * `fw' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * `fw' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with `fw'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.4 2002/02/22 23:44:16 mdw
33 * Miscellaneous tidying up, to make this code independent of `fw'. It
34 * might end up in a library somewhere.
36 * Revision 1.3 2002/01/13 14:50:07 mdw
37 * Make delimiters be a property of a scanner.
39 * Revision 1.2 1999/07/26 23:24:33 mdw
40 * Complete rewrite. Allow a list of character sources to enable changes
41 * during parsing of syntactic constructs.
43 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
55 /*----- Header files ------------------------------------------------------*/
59 #include <mLib/dstr.h>
61 /*----- Data structures ---------------------------------------------------*/
63 /* --- A low-level scanner source --- */
65 typedef struct scansrc
{
66 struct scansrc
*next
; /* Next one in the list */
67 struct scansrc_ops
*ops
; /* Pointer to operations table */
68 char *src
; /* Name of this source */
69 int line
; /* Current line number */
70 dstr pushback
; /* Pushback characters */
71 char *tok
; /* Token pushback */
72 unsigned t
; /* Token type pushback */
75 /* --- Scanner source operations --- */
77 typedef struct scansrc_ops
{
78 int (*scan
)(scansrc */
*ss*/
); /* Read another character */
79 void (*destroy
)(scansrc */
*ss*/
); /* Destroy an unwanted source */
82 /* --- A character scanner --- */
84 typedef struct scanner
{
85 scansrc
*head
, **tail
; /* Scanner list head and tail */
86 int t
; /* Token type */
87 dstr d
; /* Current token value */
88 const char *wbegin
, *wcont
; /* Parsing exception strings */
91 /*----- Particular scanner types ------------------------------------------*/
93 /* --- @scan_file@ --- *
95 * Arguments: @FILE *fp@ = pointer to file descriptor
96 * @const char *name@ = pointer to source file name
97 * @unsigned f@ = flags
99 * Returns: A scanner source.
101 * Use: Creates a new scanner source for reading from a file.
104 #define SCF_NOCLOSE 1u /* Don't close @fp@ when finished */
106 extern scansrc
*scan_file(FILE */
*fp*/
, const char */
*name*/
,
109 /* --- @scan_argv@ --- *
111 * Arguments: @char **av@ = pointer to argument array (null terminated)
113 * Returns: A scanner source.
115 * Use: Creates a new scanner source for reading from an @argv@
119 extern scansrc
*scan_argv(char **/
*av*/
);
121 /*----- General scanner handling ------------------------------------------*/
125 * Arguments: @scanner *sc@ = pointer to main scanner context
127 * Returns: Character read, or end-of-file.
129 * Use: Scans a character from a source of characters.
132 extern int scan(scanner */
*sc*/
);
134 /* --- @unscan@ --- *
136 * Arguments: @scanner *sc@ = pointer to main scanner context
137 * @int ch@ = character to unscan
141 * Use: Scans a character from a source of characters.
144 extern void unscan(scanner */
*sc*/
, int /*ch*/);
146 /* --- @scan_push@ --- *
148 * Arguments: @scanner *sc@ = pointer to main scanner context
149 * @scansrc *ss@ = souorce to push
153 * Use: Pushes a scanner source onto the front of the queue.
156 extern void scan_push(scanner */
*sc*/
, scansrc */
*ss*/
);
158 /* --- @scan_add@ --- *
160 * Arguments: @scanner *sc@ = pointer to main scanner context
161 * @scansrc *ss@ = souorce to push
165 * Use: Adds a scanner source onto the end of the queue.
168 extern void scan_add(scanner */
*sc*/
, scansrc */
*ss*/
);
170 /* --- @scan_create@ --- *
172 * Arguments: @scanner *sc@ = scanner context to initialize
176 * Use: Initializes a scanner block ready for use.
179 extern void scan_create(scanner */
*sc*/
);
181 /* --- @scan_destroy@ --- *
183 * Arguments: @scanner *sc@ = pointer to scanner context
187 * Use: Destroys a scanner and all the sources attached to it.
190 extern void scan_destroy(scanner */
*sc*/
);
192 /*----- That's all, folks -------------------------------------------------*/