| 1 | /*---------------------------------------------------------------------------- |
| 2 | ChucK Strongly-timed Audio Programming Language |
| 3 | Compiler, Virtual Machine, and Synthesis Engine |
| 4 | |
| 5 | Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved. |
| 6 | http://chuck.stanford.edu/ |
| 7 | http://chuck.cs.princeton.edu/ |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the dual-license terms of EITHER the MIT License OR the GNU |
| 11 | General Public License (the latter as published by the Free Software |
| 12 | Foundation; either version 2 of the License or, at your option, any |
| 13 | later version). |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful and/or |
| 16 | interesting, but WITHOUT ANY WARRANTY; without even the implied warranty |
| 17 | of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | MIT Licence and/or the GNU General Public License for details. |
| 19 | |
| 20 | You should have received a copy of the MIT License and the GNU General |
| 21 | Public License (GPL) along with this program; a copy of the GPL can also |
| 22 | be obtained by writing to the Free Software Foundation, Inc., 59 Temple |
| 23 | Place, Suite 330, Boston, MA 02111-1307 U.S.A. |
| 24 | -----------------------------------------------------------------------------*/ |
| 25 | |
| 26 | //----------------------------------------------------------------------------- |
| 27 | // file: errmsg.h |
| 28 | // desc: functions used in all phases of the compiler to give error messages |
| 29 | // 2002 version by Ge Wang, based on Andrew Appel's Tiger code |
| 30 | // 2017 addition by Jacy Atherton |
| 31 | // 2023 refactor and code highlight by Ge Wang |
| 32 | // |
| 33 | // author: Ge Wang ([email protected] | [email protected]) |
| 34 | // date: Summer 2002 to 2022 and ... |
| 35 | //----------------------------------------------------------------------------- |
| 36 | #ifndef __CHUCK_ERRORMSG_H__ |
| 37 | #define __CHUCK_ERRORMSG_H__ |
| 38 | |
| 39 | #include "chuck_def.h" |
| 40 | #include <stdio.h> |
| 41 | |
| 42 | |
| 43 | // log levels |
| 44 | #define CK_LOG_ALL 10 // set this to log everything |
| 45 | #define CK_LOG_FINEST 9 |
| 46 | #define CK_LOG_FINER 8 |
| 47 | #define CK_LOG_FINE 7 |
| 48 | #define CK_LOG_DEBUG 6 // 1.5.0.5 was: CK_LOG_CONFIG |
| 49 | #define CK_LOG_INFO 5 |
| 50 | #define CK_LOG_WARNING 4 |
| 51 | #define CK_LOG_HERALD 3 |
| 52 | #define CK_LOG_SYSTEM 2 |
| 53 | #define CK_LOG_CORE 1 |
| 54 | #define CK_LOG_NONE 0 // set this to log nothing |
| 55 | |
| 56 | // bitwise options for EM_log_opts |
| 57 | enum em_LogOpts |
| 58 | { |
| 59 | EM_LOG_NONE = 0, |
| 60 | EM_LOG_NO_NEWLINE = 0x1, |
| 61 | EM_LOG_NO_PREFIX = 0x10, |
| 62 | EM_LOG_NO_INDENT = 0x100 |
| 63 | }; |
| 64 | |
| 65 | // C linkage |
| 66 | #if defined(_cplusplus) || defined(__cplusplus) |
| 67 | extern "C" { |
| 68 | #endif |
| 69 | |
| 70 | //----------------------------------------------------------------------------- |
| 71 | // the many ways to report errors; tools for an error prone world |
| 72 | //----------------------------------------------------------------------------- |
| 73 | // log macros -- generally more efficient than calling EM_log() directly... |
| 74 | // since this macro conditionally computes args based on log level |
| 75 | #define CK_LOG( level, ... ) do{ if(level <= g_loglevel) \ |
| 76 | { EM_log( level, __VA_ARGS__ ); } }while(0) |
| 77 | |
| 78 | |
| 79 | // output log message |
| 80 | void EM_log( t_CKINT, c_constr, ... ); |
| 81 | // output log message (with options) |
| 82 | void EM_log_opts( t_CKINT level, enum em_LogOpts options, c_constr, ... ); |
| 83 | // set log level [CK_LOG_NONE, CK_LOG_ALL] |
| 84 | void EM_setlog( t_CKINT level ); |
| 85 | // push the log indentation |
| 86 | void EM_pushlog(); |
| 87 | // pop log indentation |
| 88 | void EM_poplog(); |
| 89 | // actual level |
| 90 | extern t_CKINT g_loglevel; |
| 91 | |
| 92 | // [%s]:line(%d).char(%d): |
| 93 | void EM_error( t_CKINT, c_constr, ... ); |
| 94 | // [%s]:line(%d): (terminal colors mode) |
| 95 | void EM_error2( t_CKINT, c_constr, ... ); |
| 96 | // [%s]:line(%d): (classic mode) |
| 97 | void EM_error2b( t_CKINT, c_constr, ... ); |
| 98 | // prints message, no line number |
| 99 | void EM_error3( c_constr, ... ); |
| 100 | // prints message, no line number, no color |
| 101 | void EM_error3b( c_constr, ... ); |
| 102 | // prints exception message |
| 103 | void EM_exception( c_constr, ... ); |
| 104 | // generate and return error string | 1.5.2.0 (ge) |
| 105 | c_constr EM_error2str( t_CKINT, t_CKBOOL, c_constr, ... ); |
| 106 | |
| 107 | // like EM_error2() minus line arg and error |
| 108 | void EM_print2vanilla( c_constr, ... ); |
| 109 | // green edition |
| 110 | void EM_print2green( c_constr, ... ); |
| 111 | // blue edition |
| 112 | void EM_print2blue( c_constr, ... ); |
| 113 | // orange edition |
| 114 | void EM_print2orange( c_constr, ... ); |
| 115 | // magenta edition |
| 116 | void EM_print2magenta( c_constr, ... ); |
| 117 | |
| 118 | // clear last error message |
| 119 | void EM_reset_msg(); |
| 120 | // prepare for new file before a file |
| 121 | void EM_start_filename( c_constr filename ); |
| 122 | // clear state after a file |
| 123 | void EM_reset_filename(); |
| 124 | // get last erorr |
| 125 | const char * EM_lasterror(); |
| 126 | |
| 127 | // get filename portion of path; e.g., mini("foo/bar.ck") -> "bar.ck" |
| 128 | const char * mini( const char * path ); |
| 129 | const char * mini_type( const char * str ); |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | //----------------------------------------------------------------------------- |
| 135 | // things connected with lexer and parser |
| 136 | //----------------------------------------------------------------------------- |
| 137 | // variables |
| 138 | extern t_CKINT EM_tokPos; |
| 139 | extern t_CKINT EM_lineNum; |
| 140 | |
| 141 | // advance state when new line is encountered |
| 142 | void EM_newline( t_CKINT pos, t_CKINT line ); |
| 143 | |
| 144 | |
| 145 | //----------------------------------------------------------------------------- |
| 146 | // REFACTOR-2017: (jack) -- callback printing |
| 147 | //----------------------------------------------------------------------------- |
| 148 | // printing |
| 149 | void ck_fprintf_stdout( const char * format, ... ); |
| 150 | void ck_fprintf_stderr( const char * format, ... ); |
| 151 | void ck_fflush_stdout(); |
| 152 | void ck_fflush_stderr(); |
| 153 | void ck_vfprintf_stdout( const char * format, va_list args ); |
| 154 | void ck_vfprintf_stderr( const char * format, va_list args ); |
| 155 | |
| 156 | // callbacks |
| 157 | void ck_set_stdout_callback( void (*callback)(const char *) ); |
| 158 | void ck_set_stderr_callback( void (*callback)(const char *) ); |
| 159 | |
| 160 | // macros |
| 161 | #define CK_FPRINTF_STDOUT(...) ck_fprintf_stdout(__VA_ARGS__) |
| 162 | #define CK_FPRINTF_STDERR(...) ck_fprintf_stderr(__VA_ARGS__) |
| 163 | #define CK_FFLUSH_STDOUT() ck_fflush_stdout() |
| 164 | #define CK_FFLUSH_STDERR() ck_fflush_stderr() |
| 165 | #define CK_VFPRINTF_STDOUT(message, ap) ck_vfprintf_stdout(message, ap) |
| 166 | #define CK_VFPRINTF_STDERR(message, ap) ck_vfprintf_stderr(message, ap) |
| 167 | |
| 168 | #if defined(_cplusplus) || defined(__cplusplus) |
| 169 | } // end C linkage |
| 170 | #endif |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | //----------------------------------------------------------------------------- |
| 176 | // begin the callback printing (c++) |
| 177 | //----------------------------------------------------------------------------- |
| 178 | #if defined(_cplusplus) || defined(__cplusplus) |
| 179 | extern "C++" |
| 180 | { |
| 181 | #include <sstream> |
| 182 | |
| 183 | // class forward reference | 1.5.0.5 (ge) added |
| 184 | class ChucK; |
| 185 | // struct forward reference | 1.5.4.0 (ge) added |
| 186 | struct Chuck_CompileTarget; |
| 187 | |
| 188 | // set current compile compilation target; used for state | 1.5.4.0 (ge) |
| 189 | void EM_setCurrentTarget( Chuck_CompileTarget * target ); |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | //----------------------------------------------------------------------------- |
| 195 | // c++: custom stream-like thing | REFACTOR-2017 (jack) |
| 196 | //----------------------------------------------------------------------------- |
| 197 | class ChuckOutStream |
| 198 | { |
| 199 | public: |
| 200 | ChuckOutStream( bool isErr ); |
| 201 | ~ChuckOutStream(); |
| 202 | |
| 203 | // there's probably a way to do this with templates or something |
| 204 | // this is not that way |
| 205 | ChuckOutStream& operator<<( const std::string & val ); |
| 206 | ChuckOutStream& operator<<( const char * val ); |
| 207 | ChuckOutStream& operator<<( const double val ); |
| 208 | ChuckOutStream& operator<<( const float val ); |
| 209 | ChuckOutStream& operator<<( const unsigned long long val ); |
| 210 | ChuckOutStream& operator<<( const long long val ); |
| 211 | ChuckOutStream& operator<<( const unsigned long val ); |
| 212 | ChuckOutStream& operator<<( const long val ); |
| 213 | ChuckOutStream& operator<<( const unsigned int val ); |
| 214 | ChuckOutStream& operator<<( const int val ); |
| 215 | ChuckOutStream& operator<<( const bool val ); |
| 216 | |
| 217 | // set callback to direct this object's output |
| 218 | void set_callback( void (*callback)(const char *) ); |
| 219 | // flush | moved from private to public | 1.5.1.1 |
| 220 | void flush(); |
| 221 | |
| 222 | private: |
| 223 | // the string stream |
| 224 | std::stringstream m_stream; |
| 225 | // the callback |
| 226 | void (*m_callback)(const char *); |
| 227 | // am I an error stream? |
| 228 | bool m_isErr; |
| 229 | }; |
| 230 | |
| 231 | // stdout ("buffered") |
| 232 | extern ChuckOutStream g_ck_stdoutstream; |
| 233 | // stderr ("unbuffered") |
| 234 | extern ChuckOutStream g_ck_stderrstream; |
| 235 | |
| 236 | #define CK_STDCOUT g_ck_stdoutstream |
| 237 | #define CK_STDCERR g_ck_stderrstream |
| 238 | #define CK_STDENDL std::string("\n") |
| 239 | |
| 240 | } // end C++ |
| 241 | |
| 242 | #else // if not C++ |
| 243 | |
| 244 | // c: oh well |
| 245 | #define CK_STDCOUT std::cout |
| 246 | #define CK_STDCERR std::cerr |
| 247 | #define CK_STDENDL std::endl |
| 248 | |
| 249 | #endif // #if defined(_cplusplus) || defined(__cplusplus) |
| 250 | //----------------------------------------------------------------------------- |
| 251 | // here endeth the callback printing |
| 252 | //----------------------------------------------------------------------------- |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | //----------------------------------------------------------------------------- |
| 258 | // more c++ in a mix c/c++ land |
| 259 | //----------------------------------------------------------------------------- |
| 260 | #if defined(_cplusplus) || defined(__cplusplus) |
| 261 | extern "C++" { |
| 262 | //----------------------------------------------------------------------------- |
| 263 | // 1.5.0.5 (ge) added |
| 264 | // struct CompileFileSource |
| 265 | // desc: information about file currently being compiled |
| 266 | // this is so we can load the file and give better compiler feedback |
| 267 | //----------------------------------------------------------------------------- |
| 268 | struct CompileFileSource |
| 269 | { |
| 270 | // which of the following should we use? |
| 271 | t_CKINT which; // 1:filepath, 2:codeLiteral, 3:FILE |
| 272 | // file path |
| 273 | std::string path; |
| 274 | // pointer to code literal |
| 275 | const char * code; |
| 276 | // file descriptor |
| 277 | FILE * file; |
| 278 | |
| 279 | // constructor |
| 280 | CompileFileSource( int w = 0, const std::string & p = "", const char * c = NULL, FILE * f = NULL ) |
| 281 | : which(w), path(p), code(c), file(f) |
| 282 | { } |
| 283 | |
| 284 | // reset |
| 285 | void reset() { which = 0; path = ""; code = NULL; file = NULL; } |
| 286 | // set |
| 287 | void setPath( const std::string & f ) { path = f; which = 1;} |
| 288 | void setCode( const char * c ) { code = c; which = 2;} |
| 289 | void setFile( FILE * f ) { file = f; which = 3;} |
| 290 | |
| 291 | // get line from source |
| 292 | std::string getLine( t_CKUINT n ); |
| 293 | }; |
| 294 | |
| 295 | // set |
| 296 | void EM_setCurrentFileSource( const CompileFileSource & info ); |
| 297 | void EM_cleanupCurrentFileSource(); |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | //----------------------------------------------------------------------------- |
| 303 | // name: class SmartPushLog (added 1.4.1.0 spencer) |
| 304 | // desc: scoped EM_pushlog for convenience and auto-balancing of push/pop logs |
| 305 | // usage: create an instance of SmartPushLog when you want to push the |
| 306 | // log level; the log level will be automatically popped when the instance goes |
| 307 | // out of scope and the SmartPushLog is deconstructed. |
| 308 | //----------------------------------------------------------------------------- |
| 309 | class SmartPushLog |
| 310 | { |
| 311 | public: |
| 312 | SmartPushLog() |
| 313 | { |
| 314 | EM_pushlog(); |
| 315 | } |
| 316 | |
| 317 | ~SmartPushLog() |
| 318 | { |
| 319 | EM_poplog(); |
| 320 | } |
| 321 | }; |
| 322 | |
| 323 | |
| 324 | } |
| 325 | #endif // #if defined(_cplusplus) || defined(__cplusplus) |
| 326 | |
| 327 | |
| 328 | #endif // __CHUCK_ERRORMSG_H__ |