| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Character scanners |
| 4 | * |
| 5 | * (c) 1999 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of the `fw' port forwarder. |
| 11 | * |
| 12 | * `fw' is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * `fw' is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with `fw'; if not, write to the Free Software Foundation, |
| 24 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | */ |
| 26 | |
| 27 | #ifndef SCAN_H |
| 28 | #define SCAN_H |
| 29 | |
| 30 | #ifdef __cplusplus |
| 31 | extern "C" { |
| 32 | #endif |
| 33 | |
| 34 | /*----- Header files ------------------------------------------------------*/ |
| 35 | |
| 36 | #include <stdio.h> |
| 37 | |
| 38 | #include <mLib/dstr.h> |
| 39 | |
| 40 | /*----- Data structures ---------------------------------------------------*/ |
| 41 | |
| 42 | /* --- A low-level scanner source --- */ |
| 43 | |
| 44 | typedef struct scansrc { |
| 45 | struct scansrc *next; /* Next one in the list */ |
| 46 | struct scansrc_ops *ops; /* Pointer to operations table */ |
| 47 | char *src; /* Name of this source */ |
| 48 | int line; /* Current line number */ |
| 49 | dstr pushback; /* Pushback characters */ |
| 50 | char *tok; /* Token pushback */ |
| 51 | unsigned t; /* Token type pushback */ |
| 52 | } scansrc; |
| 53 | |
| 54 | /* --- Scanner source operations --- */ |
| 55 | |
| 56 | typedef struct scansrc_ops { |
| 57 | int (*scan)(scansrc */*ss*/); /* Read another character */ |
| 58 | void (*destroy)(scansrc */*ss*/); /* Destroy an unwanted source */ |
| 59 | } scansrc_ops; |
| 60 | |
| 61 | /* --- A character scanner --- */ |
| 62 | |
| 63 | typedef struct scanner { |
| 64 | scansrc *head, **tail; /* Scanner list head and tail */ |
| 65 | int t; /* Token type */ |
| 66 | dstr d; /* Current token value */ |
| 67 | const char *wbegin, *wcont; /* Parsing exception strings */ |
| 68 | } scanner; |
| 69 | |
| 70 | /*----- Particular scanner types ------------------------------------------*/ |
| 71 | |
| 72 | /* --- @scan_file@ --- * |
| 73 | * |
| 74 | * Arguments: @FILE *fp@ = pointer to file descriptor |
| 75 | * @const char *name@ = pointer to source file name |
| 76 | * @unsigned f@ = flags |
| 77 | * |
| 78 | * Returns: A scanner source. |
| 79 | * |
| 80 | * Use: Creates a new scanner source for reading from a file. |
| 81 | */ |
| 82 | |
| 83 | #define SCF_NOCLOSE 1u /* Don't close @fp@ when finished */ |
| 84 | |
| 85 | extern scansrc *scan_file(FILE */*fp*/, const char */*name*/, |
| 86 | unsigned /*f*/); |
| 87 | |
| 88 | /* --- @scan_argv@ --- * |
| 89 | * |
| 90 | * Arguments: @char **av@ = pointer to argument array (null terminated) |
| 91 | * |
| 92 | * Returns: A scanner source. |
| 93 | * |
| 94 | * Use: Creates a new scanner source for reading from an @argv@ |
| 95 | * array. |
| 96 | */ |
| 97 | |
| 98 | extern scansrc *scan_argv(char **/*av*/); |
| 99 | |
| 100 | /*----- General scanner handling ------------------------------------------*/ |
| 101 | |
| 102 | /* --- @scan@ --- * |
| 103 | * |
| 104 | * Arguments: @scanner *sc@ = pointer to main scanner context |
| 105 | * |
| 106 | * Returns: Character read, or end-of-file. |
| 107 | * |
| 108 | * Use: Scans a character from a source of characters. |
| 109 | */ |
| 110 | |
| 111 | extern int scan(scanner */*sc*/); |
| 112 | |
| 113 | /* --- @unscan@ --- * |
| 114 | * |
| 115 | * Arguments: @scanner *sc@ = pointer to main scanner context |
| 116 | * @int ch@ = character to unscan |
| 117 | * |
| 118 | * Returns: --- |
| 119 | * |
| 120 | * Use: Scans a character from a source of characters. |
| 121 | */ |
| 122 | |
| 123 | extern void unscan(scanner */*sc*/, int /*ch*/); |
| 124 | |
| 125 | /* --- @scan_push@ --- * |
| 126 | * |
| 127 | * Arguments: @scanner *sc@ = pointer to main scanner context |
| 128 | * @scansrc *ss@ = souorce to push |
| 129 | * |
| 130 | * Returns: --- |
| 131 | * |
| 132 | * Use: Pushes a scanner source onto the front of the queue. |
| 133 | */ |
| 134 | |
| 135 | extern void scan_push(scanner */*sc*/, scansrc */*ss*/); |
| 136 | |
| 137 | /* --- @scan_add@ --- * |
| 138 | * |
| 139 | * Arguments: @scanner *sc@ = pointer to main scanner context |
| 140 | * @scansrc *ss@ = souorce to push |
| 141 | * |
| 142 | * Returns: --- |
| 143 | * |
| 144 | * Use: Adds a scanner source onto the end of the queue. |
| 145 | */ |
| 146 | |
| 147 | extern void scan_add(scanner */*sc*/, scansrc */*ss*/); |
| 148 | |
| 149 | /* --- @scan_create@ --- * |
| 150 | * |
| 151 | * Arguments: @scanner *sc@ = scanner context to initialize |
| 152 | * |
| 153 | * Returns: --- |
| 154 | * |
| 155 | * Use: Initializes a scanner block ready for use. |
| 156 | */ |
| 157 | |
| 158 | extern void scan_create(scanner */*sc*/); |
| 159 | |
| 160 | /* --- @scan_destroy@ --- * |
| 161 | * |
| 162 | * Arguments: @scanner *sc@ = pointer to scanner context |
| 163 | * |
| 164 | * Returns: --- |
| 165 | * |
| 166 | * Use: Destroys a scanner and all the sources attached to it. |
| 167 | */ |
| 168 | |
| 169 | extern void scan_destroy(scanner */*sc*/); |
| 170 | |
| 171 | /*----- That's all, folks -------------------------------------------------*/ |
| 172 | |
| 173 | #ifdef __cplusplus |
| 174 | } |
| 175 | #endif |
| 176 | |
| 177 | #endif |