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