e82f7154 |
1 | /* -*-c-*- |
2 | * |
7bb7c50b |
3 | * $Id: scan.h,v 1.4 2002/02/22 23:44:16 mdw Exp $ |
e82f7154 |
4 | * |
5 | * Character scanners |
6 | * |
77d0e3dc |
7 | * (c) 1999 Straylight/Edgeware |
e82f7154 |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of the `fw' port forwarder. |
13 | * |
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. |
18 | * |
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. |
23 | * |
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. |
27 | */ |
28 | |
29 | /*----- Revision history --------------------------------------------------* |
30 | * |
31 | * $Log: scan.h,v $ |
7bb7c50b |
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. |
35 | * |
78111730 |
36 | * Revision 1.3 2002/01/13 14:50:07 mdw |
37 | * Make delimiters be a property of a scanner. |
38 | * |
77d0e3dc |
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. |
42 | * |
43 | * Revision 1.1.1.1 1999/07/01 08:56:23 mdw |
44 | * Initial revision. |
e82f7154 |
45 | * |
46 | */ |
47 | |
48 | #ifndef SCAN_H |
49 | #define SCAN_H |
50 | |
51 | #ifdef __cplusplus |
52 | extern "C" { |
53 | #endif |
54 | |
55 | /*----- Header files ------------------------------------------------------*/ |
56 | |
57 | #include <stdio.h> |
58 | |
59 | #include <mLib/dstr.h> |
60 | |
61 | /*----- Data structures ---------------------------------------------------*/ |
62 | |
77d0e3dc |
63 | /* --- A low-level scanner source --- */ |
64 | |
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 */ |
7bb7c50b |
70 | dstr pushback; /* Pushback characters */ |
77d0e3dc |
71 | char *tok; /* Token pushback */ |
72 | unsigned t; /* Token type pushback */ |
73 | } scansrc; |
74 | |
75 | /* --- Scanner source operations --- */ |
e82f7154 |
76 | |
77d0e3dc |
77 | typedef struct scansrc_ops { |
78 | int (*scan)(scansrc */*ss*/); /* Read another character */ |
79 | void (*destroy)(scansrc */*ss*/); /* Destroy an unwanted source */ |
80 | } scansrc_ops; |
81 | |
82 | /* --- A character scanner --- */ |
e82f7154 |
83 | |
84 | typedef struct scanner { |
77d0e3dc |
85 | scansrc *head, **tail; /* Scanner list head and tail */ |
e82f7154 |
86 | int t; /* Token type */ |
87 | dstr d; /* Current token value */ |
78111730 |
88 | const char *wbegin, *wcont; /* Parsing exception strings */ |
e82f7154 |
89 | } scanner; |
90 | |
77d0e3dc |
91 | /*----- Particular scanner types ------------------------------------------*/ |
92 | |
93 | /* --- @scan_file@ --- * |
94 | * |
95 | * Arguments: @FILE *fp@ = pointer to file descriptor |
7bb7c50b |
96 | * @const char *name@ = pointer to source file name |
77d0e3dc |
97 | * @unsigned f@ = flags |
98 | * |
99 | * Returns: A scanner source. |
100 | * |
101 | * Use: Creates a new scanner source for reading from a file. |
102 | */ |
103 | |
7bb7c50b |
104 | #define SCF_NOCLOSE 1u /* Don't close @fp@ when finished */ |
77d0e3dc |
105 | |
7bb7c50b |
106 | extern scansrc *scan_file(FILE */*fp*/, const char */*name*/, |
107 | unsigned /*f*/); |
77d0e3dc |
108 | |
109 | /* --- @scan_argv@ --- * |
110 | * |
111 | * Arguments: @char **av@ = pointer to argument array (null terminated) |
112 | * |
113 | * Returns: A scanner source. |
114 | * |
115 | * Use: Creates a new scanner source for reading from an @argv@ |
116 | * array. |
117 | */ |
118 | |
119 | extern scansrc *scan_argv(char **/*av*/); |
120 | |
121 | /*----- General scanner handling ------------------------------------------*/ |
122 | |
123 | /* --- @scan@ --- * |
124 | * |
125 | * Arguments: @scanner *sc@ = pointer to main scanner context |
126 | * |
127 | * Returns: Character read, or end-of-file. |
128 | * |
129 | * Use: Scans a character from a source of characters. |
130 | */ |
131 | |
132 | extern int scan(scanner */*sc*/); |
e82f7154 |
133 | |
77d0e3dc |
134 | /* --- @unscan@ --- * |
135 | * |
136 | * Arguments: @scanner *sc@ = pointer to main scanner context |
137 | * @int ch@ = character to unscan |
138 | * |
139 | * Returns: --- |
140 | * |
141 | * Use: Scans a character from a source of characters. |
142 | */ |
e82f7154 |
143 | |
77d0e3dc |
144 | extern void unscan(scanner */*sc*/, int /*ch*/); |
e82f7154 |
145 | |
77d0e3dc |
146 | /* --- @scan_push@ --- * |
147 | * |
148 | * Arguments: @scanner *sc@ = pointer to main scanner context |
149 | * @scansrc *ss@ = souorce to push |
150 | * |
151 | * Returns: --- |
152 | * |
153 | * Use: Pushes a scanner source onto the front of the queue. |
154 | */ |
e82f7154 |
155 | |
77d0e3dc |
156 | extern void scan_push(scanner */*sc*/, scansrc */*ss*/); |
e82f7154 |
157 | |
77d0e3dc |
158 | /* --- @scan_add@ --- * |
159 | * |
160 | * Arguments: @scanner *sc@ = pointer to main scanner context |
161 | * @scansrc *ss@ = souorce to push |
162 | * |
163 | * Returns: --- |
164 | * |
165 | * Use: Adds a scanner source onto the end of the queue. |
166 | */ |
167 | |
168 | extern void scan_add(scanner */*sc*/, scansrc */*ss*/); |
169 | |
170 | /* --- @scan_create@ --- * |
171 | * |
172 | * Arguments: @scanner *sc@ = scanner context to initialize |
173 | * |
174 | * Returns: --- |
175 | * |
176 | * Use: Initializes a scanner block ready for use. |
177 | */ |
178 | |
179 | extern void scan_create(scanner */*sc*/); |
180 | |
181 | /* --- @scan_destroy@ --- * |
182 | * |
183 | * Arguments: @scanner *sc@ = pointer to scanner context |
184 | * |
185 | * Returns: --- |
186 | * |
187 | * Use: Destroys a scanner and all the sources attached to it. |
188 | */ |
e82f7154 |
189 | |
77d0e3dc |
190 | extern void scan_destroy(scanner */*sc*/); |
e82f7154 |
191 | |
192 | /*----- That's all, folks -------------------------------------------------*/ |
193 | |
194 | #ifdef __cplusplus |
195 | } |
196 | #endif |
197 | |
198 | #endif |