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