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