3 * $Id: scan.h,v 1.3 2002/01/13 14:50:07 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.3 2002/01/13 14:50:07 mdw
33 * Make delimiters be a property of a scanner.
35 * Revision 1.2 1999/07/26 23:24:33 mdw
36 * Complete rewrite. Allow a list of character sources to enable changes
37 * during parsing of syntactic constructs.
39 * Revision 1.1.1.1 1999/07/01 08:56:23 mdw
51 /*----- Header files ------------------------------------------------------*/
55 #include <mLib/dstr.h>
57 /*----- Data structures ---------------------------------------------------*/
59 /* --- A low-level scanner source --- */
61 typedef struct scansrc {
62 struct scansrc *next; /* Next one in the list */
63 struct scansrc_ops *ops; /* Pointer to operations table */
64 char *src; /* Name of this source */
65 int line; /* Current line number */
66 int pushback; /* Pushback character */
67 char *tok; /* Token pushback */
68 unsigned t; /* Token type pushback */
71 /* --- Scanner source operations --- */
73 typedef struct scansrc_ops {
74 int (*scan)(scansrc */*ss*/); /* Read another character */
75 void (*destroy)(scansrc */*ss*/); /* Destroy an unwanted source */
78 /* --- A character scanner --- */
80 typedef struct scanner {
81 scansrc *head, **tail; /* Scanner list head and tail */
82 int t; /* Token type */
83 dstr d; /* Current token value */
84 const char *wbegin, *wcont; /* Parsing exception strings */
87 /*----- Particular scanner types ------------------------------------------*/
89 /* --- @scan_file@ --- *
91 * Arguments: @FILE *fp@ = pointer to file descriptor
92 * @char *name@ = pointer to source file name
93 * @unsigned f@ = flags
95 * Returns: A scanner source.
97 * Use: Creates a new scanner source for reading from a file.
100 extern scansrc *scan_file(FILE */*fp*/, char */*name*/, unsigned /*f*/);
102 #define SCF_NOCLOSE 1u
103 #define SCF_FREENAME 2u
105 /* --- @scan_argv@ --- *
107 * Arguments: @char **av@ = pointer to argument array (null terminated)
109 * Returns: A scanner source.
111 * Use: Creates a new scanner source for reading from an @argv@
115 extern scansrc *scan_argv(char **/*av*/);
117 /*----- General scanner handling ------------------------------------------*/
121 * Arguments: @scanner *sc@ = pointer to main scanner context
123 * Returns: Character read, or end-of-file.
125 * Use: Scans a character from a source of characters.
128 extern int scan(scanner */*sc*/);
130 /* --- @unscan@ --- *
132 * Arguments: @scanner *sc@ = pointer to main scanner context
133 * @int ch@ = character to unscan
137 * Use: Scans a character from a source of characters.
140 extern void unscan(scanner */*sc*/, int /*ch*/);
142 /* --- @scan_push@ --- *
144 * Arguments: @scanner *sc@ = pointer to main scanner context
145 * @scansrc *ss@ = souorce to push
149 * Use: Pushes a scanner source onto the front of the queue.
152 extern void scan_push(scanner */*sc*/, scansrc */*ss*/);
154 /* --- @scan_add@ --- *
156 * Arguments: @scanner *sc@ = pointer to main scanner context
157 * @scansrc *ss@ = souorce to push
161 * Use: Adds a scanner source onto the end of the queue.
164 extern void scan_add(scanner */*sc*/, scansrc */*ss*/);
166 /* --- @scan_create@ --- *
168 * Arguments: @scanner *sc@ = scanner context to initialize
172 * Use: Initializes a scanner block ready for use.
175 extern void scan_create(scanner */*sc*/);
177 /* --- @scan_destroy@ --- *
179 * Arguments: @scanner *sc@ = pointer to scanner context
183 * Use: Destroys a scanner and all the sources attached to it.
186 extern void scan_destroy(scanner */*sc*/);
188 /*----- That's all, folks -------------------------------------------------*/