| 1 | /* |
| 2 | * Copyright (c) 2002, 2002 Michael J Roberts. All Rights Reserved. |
| 3 | * |
| 4 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 5 | * on using and copying this software. |
| 6 | */ |
| 7 | /* |
| 8 | Name |
| 9 | tclibprs.h - tads compiler: library parser |
| 10 | Function |
| 11 | Parses .tl files, which are text files that reference source files for |
| 12 | inclusion in a compilation. A .tl file can be used in a compilation as |
| 13 | though it were a source file, and stands for the set of source files it |
| 14 | references. A .tl file can reference other .tl files. |
| 15 | Notes |
| 16 | |
| 17 | Modified |
| 18 | 01/08/02 MJRoberts - Creation |
| 19 | */ |
| 20 | |
| 21 | #ifndef TCLIBPRS_H |
| 22 | #define TCLIBPRS_H |
| 23 | |
| 24 | #include "os.h" |
| 25 | |
| 26 | /* |
| 27 | * Library parser class. This class calls virtual methods to process the |
| 28 | * particular items found in a library file; we do nothing with the |
| 29 | * information we find in a library by default, so subclasses are expected |
| 30 | * to override the scan_xxx methods to carry out the desired processing. |
| 31 | */ |
| 32 | class CTcLibParser |
| 33 | { |
| 34 | public: |
| 35 | CTcLibParser(const char *lib_name); |
| 36 | virtual ~CTcLibParser(); |
| 37 | |
| 38 | /* |
| 39 | * Parse a library file. We'll open the file and parse its contents. |
| 40 | * If any errors occur, we'll call one of the err_xxx methods to |
| 41 | * indicate the error. |
| 42 | */ |
| 43 | void parse_lib(); |
| 44 | |
| 45 | /* get the error count */ |
| 46 | int get_err_cnt() const { return err_cnt_; } |
| 47 | |
| 48 | protected: |
| 49 | /* error: could not open the library */ |
| 50 | virtual void err_open_file() |
| 51 | { |
| 52 | err_msg("unable to open library file \"%s\"", lib_name_); |
| 53 | } |
| 54 | |
| 55 | /* error: line of text in file is too long */ |
| 56 | virtual void err_line_too_long() |
| 57 | { |
| 58 | src_err_msg("line is too long"); |
| 59 | } |
| 60 | |
| 61 | /* error: line of text is too long after symbol substitution */ |
| 62 | virtual void err_expanded_line_too_long() |
| 63 | { |
| 64 | src_err_msg("line is too long after symbol substitution"); |
| 65 | } |
| 66 | |
| 67 | /* error: missing colon */ |
| 68 | virtual void err_missing_colon() |
| 69 | { |
| 70 | src_err_msg("missing colon"); |
| 71 | } |
| 72 | |
| 73 | /* error: unknown field definition (the "xxx" in "xxx: val") */ |
| 74 | virtual void err_unknown_var(const char *name, const char *val) |
| 75 | { |
| 76 | src_err_msg("unknown definition \"%s\"", name); |
| 77 | } |
| 78 | |
| 79 | /* error: undefined preprocessor symbol */ |
| 80 | virtual void err_undef_sym(const char *sym_name, size_t sym_len) |
| 81 | { |
| 82 | src_err_msg("undefined symbol \"%.*s\"", (int)sym_len, sym_name); |
| 83 | } |
| 84 | |
| 85 | /* error: invalid '$' sequence */ |
| 86 | virtual void err_invalid_dollar() |
| 87 | { |
| 88 | src_err_msg("invalid '$' sequence - use '$$' for a literal " |
| 89 | "dollar sign character"); |
| 90 | } |
| 91 | |
| 92 | /* log an error message */ |
| 93 | virtual void err_msg(const char *msg, ...) |
| 94 | { |
| 95 | /* do nothing by default */ |
| 96 | } |
| 97 | |
| 98 | /* log an error in a source line */ |
| 99 | virtual void src_err_msg(const char *msg, ...); |
| 100 | |
| 101 | /* |
| 102 | * Scan a variable. This is the main scanning dispatcher: we'll call |
| 103 | * one of the specific scan_xxx routines based on the variable name. |
| 104 | * Most subclasses will want to override the specific variable scan |
| 105 | * routines rather than this one, but some subclasses might wish to |
| 106 | * override this routine in order to write a catch-all that applies |
| 107 | * some operation uniformly to all variable names. |
| 108 | */ |
| 109 | virtual void scan_var(const char *name, const char *val); |
| 110 | |
| 111 | /* |
| 112 | * Scan the "name" variable - this gives a human-readable name for the |
| 113 | * library that can be used to describe the library in a user |
| 114 | * interface. |
| 115 | */ |
| 116 | virtual void scan_name(const char *val) { } |
| 117 | |
| 118 | /* |
| 119 | * Scan a full source filename - this is the high-level scanner for |
| 120 | * the "source" variable, which receives the fully-expanded name of |
| 121 | * the referenced file. |
| 122 | */ |
| 123 | virtual void scan_full_source(const char *val, const char *full_fname) |
| 124 | { } |
| 125 | |
| 126 | /* |
| 127 | * Scan a full library filename - this is the high-level scanner for |
| 128 | * the "library" variable, which receives the fully-expanded name of |
| 129 | * the referenced library file. |
| 130 | */ |
| 131 | virtual void scan_full_library(const char *val, const char *fname) { } |
| 132 | |
| 133 | /* |
| 134 | * Scan a full resource filename - this is the high-level scanner for |
| 135 | * the "resource" variable, which receives the fully-expanded name of |
| 136 | * the referenced resource file. |
| 137 | */ |
| 138 | virtual void scan_full_resource(const char *val, const char *fname) { } |
| 139 | |
| 140 | /* |
| 141 | * Scan a "needmacro" definition. This is the high-level scanner that |
| 142 | * subclasses will normally override. By default, we'll simply show a |
| 143 | * suitable warning message. Note that the macro name is not |
| 144 | * necessarily null-terminated (macro_len gives the number of bytes in |
| 145 | * the name), but the warning text is always null-terminated. |
| 146 | */ |
| 147 | virtual void scan_parsed_needmacro(const char *macro_name, |
| 148 | size_t macro_len, |
| 149 | const char *warning_text); |
| 150 | |
| 151 | /* |
| 152 | * Scan a "source" variable - this gives the name of a file that the |
| 153 | * library includes. This variable can appear multiple times. |
| 154 | * |
| 155 | * This is the low-level scanner for the "source" variable. Our |
| 156 | * default implementation builds the full filename by combining the |
| 157 | * path to the library itself with the value of this variable, then |
| 158 | * calls scan_full_source(). Normally, subclasses will not override |
| 159 | * this routine, but will override scan_full_source() instead. |
| 160 | */ |
| 161 | virtual void scan_source(const char *val); |
| 162 | |
| 163 | /* |
| 164 | * Scan a "library" variable - this gives the name of a sub-library |
| 165 | * that the library includes. This variable can appear multiple times. |
| 166 | * |
| 167 | * This is the low-level scanner for the "library" variable. Our |
| 168 | * default implementation builds the full filename by combining the |
| 169 | * path to the enclosing library with the name of the sublibrary file, |
| 170 | * then calls scan_full_library(). Normally, subclasses will override |
| 171 | * scan_full_library() instead of this routine. |
| 172 | */ |
| 173 | virtual void scan_library(const char *val); |
| 174 | |
| 175 | /* |
| 176 | * Scan a "resource" variable - this gives the name of a resource or |
| 177 | * resource directory to bundle into the build. This variable can |
| 178 | * appear multiple times. |
| 179 | * |
| 180 | * This is the low-level scanner for the "resource" variable. Our |
| 181 | * default implementation builds the full filename by combining the |
| 182 | * path to the enclosing library with the name of the resource file or |
| 183 | * directory, then calls scan_full_resource(). Normally, subclasses |
| 184 | * will override scan_full_resource() instead of this routine. |
| 185 | */ |
| 186 | virtual void scan_resource(const char *val); |
| 187 | |
| 188 | /* |
| 189 | * Scan a "needmacro" variable - this gives the name of a required |
| 190 | * macro symbol, and a warning message to display if the symbol isn't |
| 191 | * defined. |
| 192 | * |
| 193 | * This is the low-level scanner for this definition. Our default |
| 194 | * implementation parses out the macro name (which is the first token, |
| 195 | * where whitespace is the token separator) and the warning message |
| 196 | * (which is the literal text of the rest of the line after the token |
| 197 | * separator), then calls scan_parsed_needmacro(). In most cases, |
| 198 | * subclasses will override scan_parsed_needmacro() instead of this |
| 199 | * routine. |
| 200 | */ |
| 201 | virtual void scan_needmacro(const char *val); |
| 202 | |
| 203 | /* |
| 204 | * Look up a preprocessor symbol by name. Copy the value to the given |
| 205 | * result buffer, and return the length of the value (not including any |
| 206 | * null termination). Null termination is NOT required on the result. |
| 207 | * If the value is too long for the result buffer, then DO NOT copy the |
| 208 | * result into the buffer, but simply return the required length; the |
| 209 | * caller will realize that nothing's been copied when it sees the |
| 210 | * overflowing length. |
| 211 | * |
| 212 | * Note that the symbol name string that 'sym_name' points to might not |
| 213 | * be null-terminated, as its length in bytes is given by 'sym_len'. |
| 214 | * |
| 215 | * By default, we return failure, since we don't provide any symbol |
| 216 | * management generically. Subclasses should override if they provide |
| 217 | * symbols. |
| 218 | */ |
| 219 | virtual int get_pp_symbol(char *dst, size_t dst_len, |
| 220 | const char *sym_name, size_t sym_len) |
| 221 | { |
| 222 | /* by default, we don't have any symbols defined */ |
| 223 | return -1; |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * scan a "nodef" flag - this indicates that the library is a |
| 228 | * replacement for the compiler's default library inclusions, and thus |
| 229 | * the compiler should exclude those files it would otherwise include |
| 230 | * by default |
| 231 | */ |
| 232 | virtual void scan_nodef() |
| 233 | { |
| 234 | /* do nothing by default */ |
| 235 | } |
| 236 | |
| 237 | /* our library name */ |
| 238 | char *lib_name_; |
| 239 | |
| 240 | /* the path to our library */ |
| 241 | char *lib_path_; |
| 242 | |
| 243 | /* error count */ |
| 244 | int err_cnt_; |
| 245 | |
| 246 | /* current line number in source file */ |
| 247 | unsigned long linenum_; |
| 248 | }; |
| 249 | |
| 250 | #endif /* TCLIBPRS_H */ |
| 251 |