| 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.cpp |
| 28 |
// desc: functions used in all phases of the compiler to give error messages |
| 29 |
// based on Andrew Appel's Tiger code |
| 30 |
// |
| 31 |
// author: Ge Wang ([email protected] | [email protected]) |
| 32 |
// based on code by: Andrew Appel ([email protected]) |
| 33 |
// date: Summer 2002 |
| 34 |
//----------------------------------------------------------------------------- |
| 35 |
#include "chuck_errmsg.h" |
| 36 |
#include "chuck_symbol.h" // for checked_malloc() |
| 37 |
#include "chuck.h" // for access to ChucK container object |
| 38 |
#include "util_platforms.h" |
| 39 |
#include "util_string.h" |
| 40 |
|
| 41 |
#ifndef __DISABLE_THREADS__ |
| 42 |
#include "util_thread.h" |
| 43 |
#endif |
| 44 |
|
| 45 |
#include <stdlib.h> |
| 46 |
#include <stdio.h> |
| 47 |
#include <stdarg.h> |
| 48 |
#include <string.h> |
| 49 |
#include <sstream> |
| 50 |
#include <iostream> |
| 51 |
using namespace std; |
| 52 |
|
| 53 |
|
| 54 |
// global |
| 55 |
t_CKINT EM_tokPos = 0; |
| 56 |
t_CKINT EM_lineNum = 1; |
| 57 |
|
| 58 |
// current per-file error message context | 1.5.4.0 (ge) |
| 59 |
static Chuck_CompileTarget * the_compileTarget = NULL; |
| 60 |
// current filename |
| 61 |
static const char * the_filename = ""; |
| 62 |
|
| 63 |
// file source info (for better error reporting) | 1.5.0.5 (ge) |
| 64 |
// static CompileFileSource g_currentFile; |
| 65 |
// whether to highlight | 1.5.0.5 (ge) |
| 66 |
static t_CKBOOL g_highlight_on_error = TRUE; |
| 67 |
|
| 68 |
// a local global string buffer for snprintf |
| 69 |
#define CK_ERR_BUF_LENGTH 2048 |
| 70 |
static char g_buffer[CK_ERR_BUF_LENGTH] = ""; |
| 71 |
// last error |
| 72 |
static std::string g_lasterror = ""; |
| 73 |
// for code snippet output | 1.5.2.0 |
| 74 |
static std::string g_codestr = ""; |
| 75 |
// for output error to str | 1.5.2.0 |
| 76 |
static std::string g_error2str = ""; |
| 77 |
|
| 78 |
// log globals |
| 79 |
t_CKINT g_loglevel = CK_LOG_CORE; |
| 80 |
t_CKINT g_logstack = 0; |
| 81 |
#ifndef __DISABLE_THREADS__ |
| 82 |
XMutex g_logmutex; |
| 83 |
#endif |
| 84 |
|
| 85 |
// more local globals |
| 86 |
std::stringstream g_stdout_stream; |
| 87 |
std::stringstream g_stderr_stream; |
| 88 |
// 1.5.0.5 (ge) increase g_buffer2_size from 1024 to 8192 |
| 89 |
// to accomodate longer, potentially multiline output strings |
| 90 |
static const size_t g_buffer2_size = 8192; |
| 91 |
static char g_buffer2[g_buffer2_size] = ""; |
| 92 |
|
| 93 |
// local global callbacks |
| 94 |
void (*g_stdout_callback)(const char *) = NULL; |
| 95 |
void (*g_stderr_callback)(const char *) = NULL; |
| 96 |
|
| 97 |
// name |
| 98 |
static const char * g_str[] = { |
| 99 |
"NONE", // 0 |
| 100 |
"CKCORE", // 1 |
| 101 |
"SYSTEM", // 2 |
| 102 |
"HERALD", // 3 <-- 1.5.1.3 changed from "SEVERE" to "HERALD" |
| 103 |
"WARN!!", // 4 |
| 104 |
"INFORM", // 5 |
| 105 |
"DEBUG!", // 6 <-- 1.5.0.5 changed from "CONFIG" to "DEBUG!" |
| 106 |
"FINE!!", // 7 |
| 107 |
"FINER!", // 8 |
| 108 |
"FINEST", // 9 |
| 109 |
"ALL!!!" // 10 |
| 110 |
}; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
//----------------------------------------------------------------------------- |
| 116 |
// called by lexer on new line |
| 117 |
// 1.5.0.5 (ge) updated to take a pos argument |
| 118 |
//----------------------------------------------------------------------------- |
| 119 |
void EM_newline( t_CKINT pos, t_CKINT lineNum ) |
| 120 |
{ |
| 121 |
// debug print |
| 122 |
// cerr << "pos: " << pos << " lineNum: " << lineNum |
| 123 |
// << " EM_lineNum: " << EM_lineNum << endl; |
| 124 |
|
| 125 |
// line num progress |
| 126 |
t_CKINT diff = lineNum - EM_lineNum; |
| 127 |
// this could happen, apparently, with single-line comments... |
| 128 |
if( diff < 1 ) |
| 129 |
{ |
| 130 |
// increment line num |
| 131 |
EM_lineNum++; the_compileTarget->lineNum++; |
| 132 |
// add to linked list |
| 133 |
the_compileTarget->the_linePos = intList( pos, the_compileTarget->the_linePos ); |
| 134 |
} |
| 135 |
else // diff == 1 (another line of code) or... |
| 136 |
// diff > 1 (multi-line string literal, does not call EM_newline until the end) |
| 137 |
{ |
| 138 |
// create nodes for one or more lines |
| 139 |
for( int i = EM_lineNum; i < lineNum; i++ ) |
| 140 |
the_compileTarget->the_linePos = intList( pos, the_compileTarget->the_linePos ); |
| 141 |
// set to new line num |
| 142 |
EM_lineNum = the_compileTarget->lineNum = lineNum; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
//----------------------------------------------------------------------------- |
| 150 |
// content after rightmost '/' |
| 151 |
//----------------------------------------------------------------------------- |
| 152 |
const char * mini( const char * str ) |
| 153 |
{ |
| 154 |
t_CKINT len = strlen( str ); |
| 155 |
const char * p = str + len; |
| 156 |
while( p != str && *p != '/' && *p != '\\' ) p--; |
| 157 |
return ( p == str || strlen(p+1) == 0 ? p : p+1 ); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
//----------------------------------------------------------------------------- |
| 164 |
// content after 'struct' |
| 165 |
//----------------------------------------------------------------------------- |
| 166 |
const char * mini_type( const char * str ) |
| 167 |
{ |
| 168 |
if( !strncmp( str, "struct ", 7 ) ) str += 7; |
| 169 |
const char * p = str; |
| 170 |
while( *p && *p >= '0' && *p <= '9' ) p++; |
| 171 |
return p; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
//----------------------------------------------------------------------------- |
| 178 |
// name: lastErrorCat() |
| 179 |
// desc: concatenate into last error string buffer |
| 180 |
//----------------------------------------------------------------------------- |
| 181 |
static void lastErrorCat( const std::string & str ) |
| 182 |
{ |
| 183 |
// concat |
| 184 |
g_lasterror += str; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
//----------------------------------------------------------------------------- |
| 191 |
// clear last error |
| 192 |
//----------------------------------------------------------------------------- |
| 193 |
void EM_reset_msg() |
| 194 |
{ |
| 195 |
g_lasterror = ""; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
//----------------------------------------------------------------------------- |
| 202 |
// name: EM_highlight_on_error() | 1.5.0.5 (ge) added |
| 203 |
// desc: whether to highligh code on compiler error |
| 204 |
// this defaults to true, but may be disabled when helpful |
| 205 |
// (e.g., to match output for automated testing) |
| 206 |
//----------------------------------------------------------------------------- |
| 207 |
t_CKBOOL EM_highlight_on_error() |
| 208 |
{ |
| 209 |
// if we have a chuck reference |
| 210 |
if( the_compileTarget && the_compileTarget->the_chuck ) |
| 211 |
{ |
| 212 |
// update |
| 213 |
g_highlight_on_error = the_compileTarget->the_chuck->getParamInt( CHUCK_PARAM_COMPILER_HIGHLIGHT_ON_ERROR ); |
| 214 |
} |
| 215 |
|
| 216 |
// return |
| 217 |
return g_highlight_on_error; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
//----------------------------------------------------------------------------- |
| 224 |
// name: EM_outputLineInCode() | 1.5.0.5 (ge) added |
| 225 |
// desc: output a particular line of code, with optional char pos for caret ^ |
| 226 |
//----------------------------------------------------------------------------- |
| 227 |
const char * EM_outputLineInCode( t_CKINT lineNumber, t_CKINT charNumber ) |
| 228 |
{ |
| 229 |
// check |
| 230 |
if( the_compileTarget == NULL ) return ""; |
| 231 |
|
| 232 |
// clear |
| 233 |
g_codestr = ""; |
| 234 |
|
| 235 |
// check |
| 236 |
if( !EM_highlight_on_error() || lineNumber <= 0 ) return ""; |
| 237 |
|
| 238 |
// get error line |
| 239 |
std::string line = the_compileTarget->fileSource.getLine(lineNumber); |
| 240 |
|
| 241 |
// detect empty file |
| 242 |
if( lineNumber == 1 && trim(line) == "" ) |
| 243 |
{ |
| 244 |
return "(empty file)\n"; |
| 245 |
} |
| 246 |
|
| 247 |
// spaces before caret |
| 248 |
t_CKINT spaces = charNumber - 1; |
| 249 |
|
| 250 |
// if not empty |
| 251 |
if( line != "" ) |
| 252 |
{ |
| 253 |
// replace tabs with a single space |
| 254 |
line = replace_tabs( line, ' ' ); |
| 255 |
|
| 256 |
// line position |
| 257 |
string posLine = ck_itoa(lineNumber); |
| 258 |
// spaces due to lineNumber + [] + space |
| 259 |
t_CKINT posLineIndent = posLine.length() + 3; |
| 260 |
|
| 261 |
// get terminal width; -2 for the indentation below |
| 262 |
t_CKUINT WIDTH = ck_ttywidth(); |
| 263 |
// check for 0; this one time, emscripten ioctl() ran amok... |
| 264 |
if( WIDTH == 0 ) WIDTH = ck_ttywidth_default(); |
| 265 |
// subtract |
| 266 |
WIDTH -= posLineIndent; |
| 267 |
// compute caret pos |
| 268 |
t_CKUINT CARET_POS = (t_CKUINT)(WIDTH/3.0); |
| 269 |
|
| 270 |
// check for some mininum |
| 271 |
if( WIDTH < posLineIndent + 4 ) |
| 272 |
{ |
| 273 |
// base defaults |
| 274 |
WIDTH = 80; CARET_POS = 28; |
| 275 |
// check if update |
| 276 |
if( the_compileTarget && the_compileTarget->the_chuck ) |
| 277 |
{ |
| 278 |
// get params stored in chuck; could have been set from host |
| 279 |
WIDTH = the_compileTarget->the_chuck->getParamInt( CHUCK_PARAM_TTY_WIDTH_HINT ); |
| 280 |
CARET_POS = (t_CKUINT)(WIDTH/3.0); |
| 281 |
} |
| 282 |
} |
| 283 |
// any padding on the right |
| 284 |
else{ WIDTH -= 1; CARET_POS -= 1; } |
| 285 |
|
| 286 |
// if line too long, snippet it |
| 287 |
if( line.length() > WIDTH || spaces > CARET_POS ) |
| 288 |
{ |
| 289 |
t_CKINT leftTrimmed = 0; |
| 290 |
t_CKINT rightTrimmed = 0; |
| 291 |
// quote the line |
| 292 |
line = snippet( line, WIDTH, CARET_POS, spaces, &leftTrimmed, &rightTrimmed ); |
| 293 |
|
| 294 |
// beyond certain length, replace end(s) with ... |
| 295 |
if( line.length() > 32 ) |
| 296 |
{ |
| 297 |
// left |
| 298 |
if( leftTrimmed >= 3 ) line.replace( 0, 3, "..." ); |
| 299 |
// right |
| 300 |
if( rightTrimmed >= 3 ) line.replace( line.length()-3, 3, "..." ); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
// account for lineNumber width + [] + space |
| 305 |
spaces += posLineIndent; |
| 306 |
|
| 307 |
// print the line with indentation e.g., [5] |
| 308 |
snprintf( g_buffer, CK_ERR_BUF_LENGTH, "[%s] %s\n", TC::blue(posLine,TRUE).c_str(), line.c_str() ); |
| 309 |
g_codestr += g_buffer; |
| 310 |
|
| 311 |
// if valid char position to print caret |
| 312 |
if( spaces >= 0 ) |
| 313 |
{ |
| 314 |
// print spaces |
| 315 |
for( t_CKINT i = 0; i < spaces; i++ ) g_codestr += " "; |
| 316 |
// print caret |
| 317 |
g_codestr += TC::green("^",TRUE); |
| 318 |
} |
| 319 |
|
| 320 |
// more space |
| 321 |
g_codestr += "\n"; |
| 322 |
} |
| 323 |
|
| 324 |
return g_codestr.c_str(); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
//----------------------------------------------------------------------------- |
| 331 |
// name: EM_printLineInCode() | 1.5.0.5 (ge) added |
| 332 |
// desc: output a particular line of code, with optional char pos for caret ^ |
| 333 |
//----------------------------------------------------------------------------- |
| 334 |
void EM_printLineInCode( t_CKINT lineNumber, t_CKINT charNumber ) |
| 335 |
{ |
| 336 |
CK_FPRINTF_STDERR( "%s", EM_outputLineInCode(lineNumber, charNumber) ); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
//----------------------------------------------------------------------------- |
| 342 |
// name: EM_printLineInCode() | 1.5.0.5 (ge) added |
| 343 |
// desc: overloaded version |
| 344 |
//----------------------------------------------------------------------------- |
| 345 |
void EM_printLineInCode( t_CKINT lineNumber ) |
| 346 |
{ |
| 347 |
EM_printLineInCode( lineNumber, -1 ); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
//----------------------------------------------------------------------------- |
| 354 |
// name: EM_error_prefix_begin() |
| 355 |
// desc: begin the error prefix, e.g, "[chuck" or "[FILE.ck" |
| 356 |
//----------------------------------------------------------------------------- |
| 357 |
static void EM_error_prefix_begin( t_CKBOOL colorTTY = TRUE, t_CKBOOL bold = FALSE ) |
| 358 |
{ |
| 359 |
// set color TTY mode |
| 360 |
if( colorTTY ) { TC::on(); } else { TC::off(); } |
| 361 |
|
| 362 |
// check filename |
| 363 |
t_CKBOOL hasFilename = (*the_filename) != '\0'; |
| 364 |
|
| 365 |
// prefix text |
| 366 |
std::string prefix = hasFilename ? mini(the_filename) : "[chuck"; |
| 367 |
|
| 368 |
// print prefix |
| 369 |
if( hasFilename ) CK_FPRINTF_STDERR( "%s", TC::orange(prefix, bold).c_str() ); |
| 370 |
else CK_FPRINTF_STDERR( "%s", prefix.c_str() ); |
| 371 |
|
| 372 |
// print to string |
| 373 |
snprintf( g_buffer, CK_ERR_BUF_LENGTH, "%s", prefix.c_str() ); |
| 374 |
// concat into last error |
| 375 |
lastErrorCat( g_buffer ); |
| 376 |
|
| 377 |
// if off reenable color TTY |
| 378 |
if( !colorTTY ) TC::on(); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
//----------------------------------------------------------------------------- |
| 385 |
// name: EM_error_prefix_end() |
| 386 |
// desc: close the error prefix, e.g, "]:" |
| 387 |
//----------------------------------------------------------------------------- |
| 388 |
static void EM_error_prefix_end( t_CKBOOL colorTTY = TRUE, t_CKBOOL bold = FALSE ) |
| 389 |
{ |
| 390 |
// set color TTY mode |
| 391 |
if( colorTTY ) { TC::on(); } else { TC::off(); } |
| 392 |
|
| 393 |
// check filename |
| 394 |
t_CKBOOL hasFilename = (*the_filename) != '\0'; |
| 395 |
|
| 396 |
// prefix text |
| 397 |
std::string prefix_end = hasFilename ? TC::orange(": ",bold).c_str() : "]: "; |
| 398 |
|
| 399 |
// print space |
| 400 |
CK_FPRINTF_STDERR( prefix_end.c_str() ); |
| 401 |
// buffer the above |
| 402 |
lastErrorCat( prefix_end.c_str() ); |
| 403 |
|
| 404 |
// if off reenable color TTY |
| 405 |
if( !colorTTY ) TC::on(); |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
//----------------------------------------------------------------------------- |
| 412 |
// name: EM_error() |
| 413 |
// format: [%s:%d:%d]: // was [%s].line(%d).char(%d): |
| 414 |
// desc: work horse of compiler and system error reporting |
| 415 |
//----------------------------------------------------------------------------- |
| 416 |
void EM_error( t_CKINT pos, const char * message, ... ) |
| 417 |
{ |
| 418 |
va_list ap; |
| 419 |
IntList lines = pos ? the_compileTarget->the_linePos : NULL; |
| 420 |
t_CKINT line = pos ? the_compileTarget->lineNum : 0; |
| 421 |
t_CKINT actualPos = -1; |
| 422 |
t_CKBOOL bold = TRUE; |
| 423 |
// declare each time to pick up any TC state |
| 424 |
string COLON = TC::orange(":",bold); |
| 425 |
|
| 426 |
if( pos > 0 ) |
| 427 |
{ |
| 428 |
// search for line containing pos |
| 429 |
while( lines && lines->i >= pos ) |
| 430 |
{ |
| 431 |
lines = lines->rest; |
| 432 |
line--; |
| 433 |
} |
| 434 |
// calculate actual pos on line |
| 435 |
if( lines ) actualPos = pos - lines->i; |
| 436 |
} |
| 437 |
|
| 438 |
// separate errmsgs with newlines |
| 439 |
if( g_lasterror != "" ) lastErrorCat( "\n" ); |
| 440 |
// debug print |
| 441 |
// cerr << "EM_error(): pos: " << pos << " actualPos:" << actualPos << " the_lineNum: " << the_lineNum << endl; |
| 442 |
|
| 443 |
// begin prefix, e.g, "FILE.ck" or "[chuck" |
| 444 |
EM_error_prefix_begin( TRUE, bold ); |
| 445 |
// if found |
| 446 |
if( pos > 0 && actualPos > 0 ) |
| 447 |
{ |
| 448 |
CK_FPRINTF_STDERR( "%s%s%s%s", COLON.c_str(), TC::blue(ck_itoa(line),bold).c_str(), COLON.c_str(), TC::green(ck_itoa(actualPos),bold).c_str() ); |
| 449 |
snprintf( g_buffer, CK_ERR_BUF_LENGTH, ":%d:%d", (int)line, (int)actualPos ); |
| 450 |
lastErrorCat( g_buffer ); |
| 451 |
} |
| 452 |
// end prefix, e.g, ": " or "]: " |
| 453 |
EM_error_prefix_end( TRUE, bold ); |
| 454 |
|
| 455 |
// print the message |
| 456 |
va_start(ap, message); |
| 457 |
CK_VFPRINTF_STDERR( TC::red(message, true).c_str(), ap ); |
| 458 |
va_end(ap); |
| 459 |
// buffer the message |
| 460 |
va_start(ap, message); |
| 461 |
vsnprintf( g_buffer, CK_ERR_BUF_LENGTH, message, ap ); |
| 462 |
va_end(ap); |
| 463 |
lastErrorCat( g_buffer ); |
| 464 |
|
| 465 |
// endl |
| 466 |
CK_FPRINTF_STDERR( "\n"); |
| 467 |
// flush |
| 468 |
CK_FFLUSH_STDERR(); |
| 469 |
|
| 470 |
// print code on error |
| 471 |
if( pos > 0 ) EM_printLineInCode( line, actualPos ); |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
//----------------------------------------------------------------------------- |
| 478 |
// name: EM_error2() |
| 479 |
// format: [%s:%d:%d]: // was [%s]:line(%d): |
| 480 |
// desc: error output (variant 2, with terminal colors) |
| 481 |
//----------------------------------------------------------------------------- |
| 482 |
void EM_error2( t_CKINT pos, const char * message, ... ) |
| 483 |
{ |
| 484 |
va_list ap; |
| 485 |
IntList lines = pos ? the_compileTarget->the_linePos : NULL; |
| 486 |
t_CKINT line = pos ? the_compileTarget->lineNum : 0; |
| 487 |
t_CKINT actualPos = -1; |
| 488 |
t_CKBOOL bold = TRUE; |
| 489 |
// declare each time to pick up any TC state |
| 490 |
string COLON = TC::orange(":",bold); |
| 491 |
|
| 492 |
// do if pos is positive |
| 493 |
if( pos > 0 ) |
| 494 |
{ |
| 495 |
// search for line containing pos |
| 496 |
while( lines && lines->i >= pos ) |
| 497 |
{ |
| 498 |
lines = lines->rest; |
| 499 |
line--; |
| 500 |
} |
| 501 |
// calculate actual pos on line |
| 502 |
if( lines ) actualPos = pos - lines->i; |
| 503 |
} |
| 504 |
|
| 505 |
// separate errmsgs with newlines |
| 506 |
if( g_lasterror != "" ) lastErrorCat( "\n" ); |
| 507 |
|
| 508 |
// begin prefix, e.g, "FILE.ck" or "[chuck" |
| 509 |
EM_error_prefix_begin( TRUE, bold ); |
| 510 |
// if given a valid line number |
| 511 |
if( pos ) |
| 512 |
{ |
| 513 |
CK_FPRINTF_STDERR( "%s%s%s%s", COLON.c_str(), TC::blue(ck_itoa(line),bold).c_str(), COLON.c_str(), TC::green(ck_itoa(actualPos),bold).c_str() ); |
| 514 |
snprintf( g_buffer, CK_ERR_BUF_LENGTH, ":%d:%d", (int)line, (int)actualPos ); |
| 515 |
lastErrorCat( g_buffer ); |
| 516 |
} |
| 517 |
// end prefix, e.g, ": " or "]: " |
| 518 |
EM_error_prefix_end( TRUE, bold ); |
| 519 |
|
| 520 |
if( pos ) |
| 521 |
{ |
| 522 |
// check for special message types | 1.5.1.3 |
| 523 |
const char * msgType = "error: "; |
| 524 |
// copy into c++ string |
| 525 |
string msg = message; |
| 526 |
// look for special strings |
| 527 |
if( msg.find("deprecated: ") == 0 ) |
| 528 |
{ |
| 529 |
// update msgType |
| 530 |
msgType = "deprecated: "; |
| 531 |
// skip the msgType |
| 532 |
message += string(msgType).length(); |
| 533 |
} |
| 534 |
|
| 535 |
// print error only if there is non-zero line number |
| 536 |
CK_FPRINTF_STDERR( "%s", TC::red(msgType,true).c_str() ); |
| 537 |
lastErrorCat( msgType ); |
| 538 |
} |
| 539 |
|
| 540 |
va_start( ap, message ); |
| 541 |
CK_VFPRINTF_STDERR( TC::orange(message,bold).c_str(), ap ); |
| 542 |
va_end( ap ); |
| 543 |
|
| 544 |
va_start( ap, message ); |
| 545 |
vsnprintf( g_buffer, CK_ERR_BUF_LENGTH, message, ap ); |
| 546 |
va_end( ap ); |
| 547 |
|
| 548 |
lastErrorCat( g_buffer ); |
| 549 |
CK_FPRINTF_STDERR( "\n" ); |
| 550 |
CK_FFLUSH_STDERR(); |
| 551 |
|
| 552 |
// print code on error |
| 553 |
if( pos > 0 ) EM_printLineInCode( line, actualPos ); |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
//----------------------------------------------------------------------------- |
| 560 |
// name: EM_error2b() |
| 561 |
// format: [%s:%d:%d]: // was [%s]:line(%d): |
| 562 |
// desc: error output -- like variant 2, but without color teriminal output |
| 563 |
//----------------------------------------------------------------------------- |
| 564 |
void EM_error2b( t_CKINT line, const char * message, ... ) |
| 565 |
{ |
| 566 |
va_list ap; |
| 567 |
|
| 568 |
// separate errmsgs with newlines |
| 569 |
if( g_lasterror != "" ) lastErrorCat( "\n" ); |
| 570 |
|
| 571 |
// begin prefix, e.g, "FILE.ck" or "[chuck" |
| 572 |
EM_error_prefix_begin( FALSE ); |
| 573 |
// if given a valid line number |
| 574 |
if( line > 0 ) |
| 575 |
{ |
| 576 |
CK_FPRINTF_STDERR( ":%d", (int)line ); |
| 577 |
snprintf( g_buffer, CK_ERR_BUF_LENGTH, ":%d", (int)line ); |
| 578 |
lastErrorCat( g_buffer ); |
| 579 |
} |
| 580 |
// end prefix, e.g, ": " or "]: " |
| 581 |
EM_error_prefix_end( FALSE ); |
| 582 |
|
| 583 |
va_start( ap, message ); |
| 584 |
CK_VFPRINTF_STDERR( message, ap ); |
| 585 |
va_end( ap ); |
| 586 |
|
| 587 |
va_start( ap, message ); |
| 588 |
vsnprintf( g_buffer, CK_ERR_BUF_LENGTH, message, ap ); |
| 589 |
va_end( ap ); |
| 590 |
|
| 591 |
lastErrorCat( g_buffer ); |
| 592 |
CK_FPRINTF_STDERR( "\n" ); |
| 593 |
CK_FFLUSH_STDERR(); |
| 594 |
|
| 595 |
// print |
| 596 |
if( line > 0 ) EM_printLineInCode( line ); |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
//----------------------------------------------------------------------------- |
| 603 |
// name: EM_error2str() | 1.5.2.0 |
| 604 |
// desc: generate error output to string (with terminal colors if enabled) |
| 605 |
//----------------------------------------------------------------------------- |
| 606 |
const char * EM_error2str( t_CKINT pos, t_CKBOOL outputPrefix, const char * message, ... ) |
| 607 |
{ |
| 608 |
va_list ap; |
| 609 |
IntList lines = pos ? the_compileTarget->the_linePos : NULL; |
| 610 |
t_CKINT line = pos ? the_compileTarget->lineNum : 0; |
| 611 |
t_CKINT actualPos = -1; |
| 612 |
t_CKBOOL bold = TRUE; |
| 613 |
// declare each time to pick up any TC state |
| 614 |
string COLON = TC::orange(":",bold); |
| 615 |
// reset |
| 616 |
g_error2str = ""; |
| 617 |
|
| 618 |
// do if pos is positive |
| 619 |
if( pos > 0 ) |
| 620 |
{ |
| 621 |
// search for line containing pos |
| 622 |
while( lines && lines->i >= pos ) |
| 623 |
{ |
| 624 |
lines = lines->rest; |
| 625 |
line--; |
| 626 |
} |
| 627 |
// calculate actual pos on line |
| 628 |
if( lines ) actualPos = pos - lines->i; |
| 629 |
} |
| 630 |
|
| 631 |
// begin prefix, e.g, "FILE.ck" or "[chuck" |
| 632 |
t_CKBOOL hasFilename = (*the_filename) != '\0'; |
| 633 |
if( outputPrefix ) g_error2str += hasFilename ? mini(the_filename) : "[chuck"; |
| 634 |
// if given a valid line number |
| 635 |
if( pos ) |
| 636 |
{ |
| 637 |
snprintf( g_buffer, CK_ERR_BUF_LENGTH, "%s%s%s%s", COLON.c_str(), TC::blue(ck_itoa(line),bold).c_str(), COLON.c_str(), TC::green(ck_itoa(actualPos),bold).c_str() ); |
| 638 |
g_error2str += g_buffer; |
| 639 |
} |
| 640 |
// end prefix, e.g, ": " or "]: " |
| 641 |
if( outputPrefix ) g_error2str += hasFilename ? TC::orange(": ",bold).c_str() : "]: "; |
| 642 |
|
| 643 |
va_start( ap, message ); |
| 644 |
vsnprintf( g_buffer, CK_ERR_BUF_LENGTH, message, ap ); |
| 645 |
va_end( ap ); |
| 646 |
g_error2str += g_buffer + string("\n"); |
| 647 |
|
| 648 |
// print code on error |
| 649 |
if( pos > 0 ) g_error2str += EM_outputLineInCode( line, actualPos ); |
| 650 |
|
| 651 |
// return |
| 652 |
return g_error2str.c_str(); |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
//----------------------------------------------------------------------------- |
| 659 |
// name: EM_error3() |
| 660 |
// desc: prints message, no prefix, no line number |
| 661 |
//----------------------------------------------------------------------------- |
| 662 |
void EM_error3( const char * message, ... ) |
| 663 |
{ |
| 664 |
va_list ap; |
| 665 |
|
| 666 |
// separate errmsgs with newlines |
| 667 |
if( g_lasterror != "" ) lastErrorCat( "\n" ); |
| 668 |
|
| 669 |
g_buffer[0] = '\0'; |
| 670 |
va_start( ap, message ); |
| 671 |
CK_VFPRINTF_STDERR( TC::orange(message,true).c_str(), ap ); |
| 672 |
va_end( ap ); |
| 673 |
|
| 674 |
va_start( ap, message ); |
| 675 |
vsnprintf( g_buffer, CK_ERR_BUF_LENGTH, message, ap ); |
| 676 |
va_end( ap ); |
| 677 |
|
| 678 |
lastErrorCat( g_buffer ); |
| 679 |
CK_FPRINTF_STDERR( "\n" ); |
| 680 |
CK_FFLUSH_STDERR(); |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
//----------------------------------------------------------------------------- |
| 687 |
// name: EM_error3b() |
| 688 |
// desc: prints message, no prefix, no line number, no color |
| 689 |
//----------------------------------------------------------------------------- |
| 690 |
void EM_error3b( const char * message, ... ) |
| 691 |
{ |
| 692 |
va_list ap; |
| 693 |
|
| 694 |
// separate errmsgs with newlines |
| 695 |
if( g_lasterror != "" ) lastErrorCat( "\n" ); |
| 696 |
|
| 697 |
g_buffer[0] = '\0'; |
| 698 |
va_start( ap, message ); |
| 699 |
CK_VFPRINTF_STDERR( message, ap ); |
| 700 |
va_end( ap ); |
| 701 |
|
| 702 |
va_start( ap, message ); |
| 703 |
vsnprintf( g_buffer, CK_ERR_BUF_LENGTH, message, ap ); |
| 704 |
va_end( ap ); |
| 705 |
|
| 706 |
lastErrorCat( g_buffer ); |
| 707 |
CK_FPRINTF_STDERR( "\n" ); |
| 708 |
CK_FFLUSH_STDERR(); |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
//----------------------------------------------------------------------------- |
| 715 |
// name: EM_exception() |
| 716 |
// desc: prints exception message |
| 717 |
//----------------------------------------------------------------------------- |
| 718 |
void EM_exception( const char * message, ... ) |
| 719 |
{ |
| 720 |
va_list ap; |
| 721 |
|
| 722 |
// preamble |
| 723 |
CK_FPRINTF_STDERR( "[%s]:(%s) ", |
| 724 |
TC::orange("chuck",true).c_str(), |
| 725 |
TC::red("EXCEPTION",true).c_str() ); |
| 726 |
|
| 727 |
// actual exception message text |
| 728 |
va_start( ap, message ); |
| 729 |
CK_VFPRINTF_STDERR( TC::orange(message,true).c_str(), ap ); |
| 730 |
va_end( ap ); |
| 731 |
|
| 732 |
// end line and flush to be extra sure |
| 733 |
CK_FPRINTF_STDERR( "\n" ); |
| 734 |
CK_FFLUSH_STDERR(); |
| 735 |
} |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
//----------------------------------------------------------------------------- |
| 741 |
// name: EM_print2vanilla() |
| 742 |
// format: like EM_error() minus the error and the line |
| 743 |
//----------------------------------------------------------------------------- |
| 744 |
void EM_print2vanilla( const char * message, ... ) |
| 745 |
{ |
| 746 |
va_list ap; |
| 747 |
|
| 748 |
// print |
| 749 |
CK_FPRINTF_STDERR( "[%s]:", *the_filename ? mini(the_filename) : "chuck" ); |
| 750 |
CK_FPRINTF_STDERR( " " ); |
| 751 |
|
| 752 |
// print |
| 753 |
va_start( ap, message ); |
| 754 |
CK_VFPRINTF_STDERR( message, ap ); |
| 755 |
va_end( ap ); |
| 756 |
|
| 757 |
// flush |
| 758 |
CK_FPRINTF_STDERR( "\n" ); |
| 759 |
CK_FFLUSH_STDERR(); |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
//----------------------------------------------------------------------------- |
| 766 |
// name: EM_print2green() |
| 767 |
// format: like EM_error() minus the error and the line |
| 768 |
//----------------------------------------------------------------------------- |
| 769 |
void EM_print2green( const char * message, ... ) |
| 770 |
{ |
| 771 |
va_list ap; |
| 772 |
|
| 773 |
// print |
| 774 |
CK_FPRINTF_STDERR( "[%s]:", *the_filename ? mini(the_filename) : "chuck" ); |
| 775 |
CK_FPRINTF_STDERR( " " ); |
| 776 |
|
| 777 |
// print |
| 778 |
va_start( ap, message ); |
| 779 |
CK_VFPRINTF_STDERR( TC::green(message,true).c_str(), ap ); |
| 780 |
va_end( ap ); |
| 781 |
|
| 782 |
// flush |
| 783 |
CK_FPRINTF_STDERR( "\n" ); |
| 784 |
CK_FFLUSH_STDERR(); |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
//----------------------------------------------------------------------------- |
| 791 |
// name: EM_print2blue() |
| 792 |
// format: like EM_error() minus the error and the line |
| 793 |
//----------------------------------------------------------------------------- |
| 794 |
void EM_print2blue( const char * message, ... ) |
| 795 |
{ |
| 796 |
va_list ap; |
| 797 |
|
| 798 |
// print |
| 799 |
CK_FPRINTF_STDERR( "[%s]:", *the_filename ? mini(the_filename) : "chuck" ); |
| 800 |
CK_FPRINTF_STDERR( " " ); |
| 801 |
|
| 802 |
// print |
| 803 |
va_start( ap, message ); |
| 804 |
CK_VFPRINTF_STDERR( TC::blue(message,true).c_str(), ap ); |
| 805 |
va_end( ap ); |
| 806 |
|
| 807 |
// flush |
| 808 |
CK_FPRINTF_STDERR( "\n" ); |
| 809 |
CK_FFLUSH_STDERR(); |
| 810 |
} |
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
//----------------------------------------------------------------------------- |
| 816 |
// name: EM_print2orange() |
| 817 |
// format: like EM_error() minus the error and the line |
| 818 |
//----------------------------------------------------------------------------- |
| 819 |
void EM_print2orange( const char * message, ... ) |
| 820 |
{ |
| 821 |
va_list ap; |
| 822 |
|
| 823 |
// print |
| 824 |
CK_FPRINTF_STDERR( "[%s]:", *the_filename ? mini(the_filename) : "chuck" ); |
| 825 |
CK_FPRINTF_STDERR( " " ); |
| 826 |
|
| 827 |
// print |
| 828 |
va_start( ap, message ); |
| 829 |
CK_VFPRINTF_STDERR( TC::orange(message,true).c_str(), ap ); |
| 830 |
va_end( ap ); |
| 831 |
|
| 832 |
// flush |
| 833 |
CK_FPRINTF_STDERR( "\n" ); |
| 834 |
CK_FFLUSH_STDERR(); |
| 835 |
} |
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
//----------------------------------------------------------------------------- |
| 841 |
// name: EM_print2magenta() |
| 842 |
// format: like EM_error() minus the error and the line |
| 843 |
//----------------------------------------------------------------------------- |
| 844 |
void EM_print2magenta( const char * message, ... ) |
| 845 |
{ |
| 846 |
va_list ap; |
| 847 |
|
| 848 |
// print |
| 849 |
CK_FPRINTF_STDERR( "[%s]:", *the_filename ? mini(the_filename) : "chuck" ); |
| 850 |
CK_FPRINTF_STDERR( " " ); |
| 851 |
|
| 852 |
// print |
| 853 |
va_start( ap, message ); |
| 854 |
CK_VFPRINTF_STDERR( TC::magenta(message,true).c_str(), ap ); |
| 855 |
va_end( ap ); |
| 856 |
|
| 857 |
// flush |
| 858 |
CK_FPRINTF_STDERR( "\n" ); |
| 859 |
CK_FFLUSH_STDERR(); |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
//----------------------------------------------------------------------------- |
| 866 |
// name: EM_log() |
| 867 |
// desc: output log message at a particular log level |
| 868 |
//----------------------------------------------------------------------------- |
| 869 |
void EM_log( t_CKINT level, const char * message, ... ) |
| 870 |
{ |
| 871 |
va_list ap; |
| 872 |
|
| 873 |
if( level > CK_LOG_ALL ) level = CK_LOG_ALL; |
| 874 |
else if( level <= CK_LOG_NONE ) level = CK_LOG_NONE + 1; |
| 875 |
|
| 876 |
// check level |
| 877 |
if( level > g_loglevel ) return; |
| 878 |
|
| 879 |
#ifndef __DISABLE_THREADS__ |
| 880 |
g_logmutex.acquire(); |
| 881 |
#endif |
| 882 |
|
| 883 |
TC::off(); |
| 884 |
CK_FPRINTF_STDERR( "[%s:%s:%s]: ", |
| 885 |
TC::blue("chuck", true).c_str(), |
| 886 |
TC::blue( ck_itoa(level), false ).c_str(), |
| 887 |
TC::blue( g_str[level] ).c_str() ); |
| 888 |
TC::on(); |
| 889 |
|
| 890 |
// if( g_logstack ) CK_FPRINTF_STDERR( " " ); |
| 891 |
for( int i = 0; i < g_logstack; i++ ) |
| 892 |
CK_FPRINTF_STDERR( " | " ); |
| 893 |
|
| 894 |
va_start( ap, message ); |
| 895 |
CK_VFPRINTF_STDERR( message, ap ); |
| 896 |
va_end( ap ); |
| 897 |
|
| 898 |
CK_FPRINTF_STDERR( "\n" ); |
| 899 |
CK_FFLUSH_STDERR(); |
| 900 |
|
| 901 |
#ifndef __DISABLE_THREADS__ |
| 902 |
g_logmutex.release(); |
| 903 |
#endif |
| 904 |
} |
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
//----------------------------------------------------------------------------- |
| 910 |
// name: EM_log_opts() |
| 911 |
// desc: output log message at a particular log level (no NL) |
| 912 |
//----------------------------------------------------------------------------- |
| 913 |
void EM_log_opts( t_CKINT level, enum em_LogOpts options, const char * message, ... ) |
| 914 |
{ |
| 915 |
va_list ap; |
| 916 |
|
| 917 |
if( level > CK_LOG_ALL ) level = CK_LOG_ALL; |
| 918 |
else if( level <= CK_LOG_NONE ) level = CK_LOG_NONE + 1; |
| 919 |
|
| 920 |
// check level |
| 921 |
if( level > g_loglevel ) return; |
| 922 |
|
| 923 |
// whether to print prefix |
| 924 |
t_CKBOOL prefix = !(options & EM_LOG_NO_PREFIX); |
| 925 |
// whether to print newline at end |
| 926 |
t_CKBOOL nl = !(options & EM_LOG_NO_NEWLINE); |
| 927 |
// whether to indent |
| 928 |
t_CKBOOL indent = !(options & EM_LOG_NO_INDENT); |
| 929 |
|
| 930 |
#ifndef __DISABLE_THREADS__ |
| 931 |
g_logmutex.acquire(); |
| 932 |
#endif |
| 933 |
|
| 934 |
// check option |
| 935 |
if( prefix ) |
| 936 |
{ |
| 937 |
TC::off(); |
| 938 |
CK_FPRINTF_STDERR( "[%s:%s:%s]: ", |
| 939 |
TC::blue("chuck", true).c_str(), |
| 940 |
TC::blue( ck_itoa(level), false ).c_str(), |
| 941 |
TC::blue( g_str[level] ).c_str() ); |
| 942 |
TC::on(); |
| 943 |
|
| 944 |
// if( g_logstack ) CK_FPRINTF_STDERR( " " ); |
| 945 |
if( indent ) |
| 946 |
{ for( int i = 0; i < g_logstack; i++ ) CK_FPRINTF_STDERR( " | " ); } |
| 947 |
} |
| 948 |
|
| 949 |
va_start( ap, message ); |
| 950 |
CK_VFPRINTF_STDERR( message, ap ); |
| 951 |
va_end( ap ); |
| 952 |
|
| 953 |
// print newline |
| 954 |
if( nl ) CK_FPRINTF_STDERR( "\n" ); |
| 955 |
|
| 956 |
// flush |
| 957 |
CK_FFLUSH_STDERR(); |
| 958 |
|
| 959 |
#ifndef __DISABLE_THREADS__ |
| 960 |
g_logmutex.release(); |
| 961 |
#endif |
| 962 |
} |
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
//----------------------------------------------------------------------------- |
| 968 |
// name: EM_setlog() |
| 969 |
// desc: set log level |
| 970 |
//----------------------------------------------------------------------------- |
| 971 |
void EM_setlog( t_CKINT level ) |
| 972 |
{ |
| 973 |
if( level > CK_LOG_ALL ) level = CK_LOG_ALL; |
| 974 |
else if( level < CK_LOG_NONE ) level = CK_LOG_NONE; |
| 975 |
g_loglevel = level; |
| 976 |
|
| 977 |
// log this |
| 978 |
EM_log( CK_LOG_INFO, "setting log level to: %li (%s)...", level, g_str[level] ); |
| 979 |
} |
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
//----------------------------------------------------------------------------- |
| 985 |
// name: EM_pushlog() |
| 986 |
// desc: push log indent |
| 987 |
//----------------------------------------------------------------------------- |
| 988 |
void EM_pushlog() |
| 989 |
{ |
| 990 |
g_logstack++; |
| 991 |
} |
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
//----------------------------------------------------------------------------- |
| 997 |
// name: EM_pushlog() |
| 998 |
// desc: pop log indent |
| 999 |
//----------------------------------------------------------------------------- |
| 1000 |
void EM_poplog() |
| 1001 |
{ |
| 1002 |
g_logstack--; |
| 1003 |
if( g_logstack < 0 ) g_logstack = 0; |
| 1004 |
} |
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
//----------------------------------------------------------------------------- |
| 1010 |
// name: EM_start_filename() |
| 1011 |
// desc: prepare new filename before chuck_parse() |
| 1012 |
//----------------------------------------------------------------------------- |
| 1013 |
void EM_start_filename( const char * fname ) |
| 1014 |
{ |
| 1015 |
the_filename = fname ? fname : (c_str)""; |
| 1016 |
EM_lineNum = 1; |
| 1017 |
} |
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
//----------------------------------------------------------------------------- |
| 1023 |
// name: EM_start_filename() |
| 1024 |
// desc: reset filename, usually after chuck_parse; this affects error output |
| 1025 |
//----------------------------------------------------------------------------- |
| 1026 |
void EM_reset_filename() |
| 1027 |
{ |
| 1028 |
// set |
| 1029 |
the_filename = (c_str)""; |
| 1030 |
// more set |
| 1031 |
EM_lineNum = 0; |
| 1032 |
} |
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
//----------------------------------------------------------------------------- |
| 1038 |
// name: EM_lasterror() |
| 1039 |
// desc: return last error |
| 1040 |
//----------------------------------------------------------------------------- |
| 1041 |
const char * EM_lasterror() |
| 1042 |
{ |
| 1043 |
return g_lasterror.c_str(); |
| 1044 |
} |
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
//----------------------------------------------------------------------------- |
| 1050 |
// name: EM_setCurrentTarget() |
| 1051 |
// desc: set current compile compilation target | 1.5.4.0 (ge) |
| 1052 |
//----------------------------------------------------------------------------- |
| 1053 |
void EM_setCurrentTarget( Chuck_CompileTarget * target ) |
| 1054 |
{ |
| 1055 |
// set |
| 1056 |
the_compileTarget = target; |
| 1057 |
|
| 1058 |
// if target |
| 1059 |
if( target != NULL ) |
| 1060 |
{ |
| 1061 |
// load in the information |
| 1062 |
EM_lineNum = target->lineNum; |
| 1063 |
the_filename = target->filename.c_str(); |
| 1064 |
} |
| 1065 |
else |
| 1066 |
{ |
| 1067 |
// reset |
| 1068 |
EM_lineNum = 0; |
| 1069 |
the_filename = (c_str)""; |
| 1070 |
} |
| 1071 |
} |
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
//----------------------------------------------------------------------------- |
| 1077 |
// name: EM_setCurrentFileSource() |
| 1078 |
// desc: set where current file being compile comes from; used for highlighting |
| 1079 |
// code to go with compiler error messages |
| 1080 |
//----------------------------------------------------------------------------- |
| 1081 |
void EM_setCurrentFileSource( const CompileFileSource & source ) |
| 1082 |
{ |
| 1083 |
// check (ignore if NULL) |
| 1084 |
if( the_compileTarget == NULL ) return; |
| 1085 |
// set |
| 1086 |
the_compileTarget->fileSource = source; |
| 1087 |
} |
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
//----------------------------------------------------------------------------- |
| 1093 |
// name: EM_cleanupCurrentFileSource() |
| 1094 |
// desc: clean up the current file source |
| 1095 |
//----------------------------------------------------------------------------- |
| 1096 |
void EM_cleanupCurrentFileSource() |
| 1097 |
{ |
| 1098 |
// check |
| 1099 |
if( the_compileTarget == NULL ) return; |
| 1100 |
// reset |
| 1101 |
the_compileTarget->fileSource.reset(); |
| 1102 |
} |
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
//----------------------------------------------------------------------------- |
| 1108 |
// name: getLineFromCode() |
| 1109 |
// desc: return a specific line from string of actual code |
| 1110 |
//----------------------------------------------------------------------------- |
| 1111 |
std::string getLineFromCode( const std::string & code, t_CKUINT n ) |
| 1112 |
{ |
| 1113 |
// stream stream |
| 1114 |
std::istringstream strin( code ); |
| 1115 |
std::string line; |
| 1116 |
t_CKBOOL found = FALSE; |
| 1117 |
t_CKUINT theLine = 0; |
| 1118 |
|
| 1119 |
// go through line by line |
| 1120 |
while( std::getline( strin, line ) ) |
| 1121 |
{ |
| 1122 |
theLine++; |
| 1123 |
if( theLine == n ) { found = TRUE; break; }; |
| 1124 |
} |
| 1125 |
|
| 1126 |
// make sure |
| 1127 |
return found ? line: ""; |
| 1128 |
} |
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
//----------------------------------------------------------------------------- |
| 1134 |
// name: getLineFromFile() |
| 1135 |
// desc: return a specific line from a presumed open FILE descriptor |
| 1136 |
// this will modify the position of the FILE descriptor; assume that |
| 1137 |
// place is not longer needed as this is usually called on error |
| 1138 |
//----------------------------------------------------------------------------- |
| 1139 |
std::string getLineFromFile( FILE * fd, t_CKUINT n ) |
| 1140 |
{ |
| 1141 |
// read lines using POSIX function getline |
| 1142 |
// this code won't work on Windows |
| 1143 |
char * line = NULL; |
| 1144 |
size_t len = 0; |
| 1145 |
t_CKBOOL found = FALSE; |
| 1146 |
std::string ret; |
| 1147 |
|
| 1148 |
// rewind the FILE descriptor |
| 1149 |
rewind( fd ); |
| 1150 |
|
| 1151 |
t_CKUINT theLine = 0; |
| 1152 |
// get line; c getline includes the delimiter (e.g., '\n'), unless EOF |
| 1153 |
// whereas c++ getline does not...so it goes |
| 1154 |
while( ck_getline( &line, &len, fd ) != -1 ) |
| 1155 |
{ |
| 1156 |
theLine++; |
| 1157 |
if( theLine == n ) { found = TRUE; break; }; |
| 1158 |
} |
| 1159 |
|
| 1160 |
// if we have a match |
| 1161 |
if( found ) |
| 1162 |
{ |
| 1163 |
// copy |
| 1164 |
ret = line; |
| 1165 |
t_CKINT i = ret.length()-1; |
| 1166 |
// go through any trail endline |
| 1167 |
while( ret[i] == '\n' || ret[i] == '\r' ) i--; |
| 1168 |
// move one back |
| 1169 |
i++; |
| 1170 |
// get rid of the trailing delimiter |
| 1171 |
if( i >= 0 ) ret[i] = '\0'; |
| 1172 |
} |
| 1173 |
|
| 1174 |
// free |
| 1175 |
CK_SAFE_FREE( line ); |
| 1176 |
|
| 1177 |
// return |
| 1178 |
return ret; |
| 1179 |
} |
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
//----------------------------------------------------------------------------- |
| 1185 |
// name: getLine() |
| 1186 |
// desc: get line from source |
| 1187 |
//----------------------------------------------------------------------------- |
| 1188 |
std::string CompileFileSource::getLine( t_CKUINT n ) |
| 1189 |
{ |
| 1190 |
std::string retval = ""; |
| 1191 |
|
| 1192 |
// which type? |
| 1193 |
switch( which ) |
| 1194 |
{ |
| 1195 |
case 2: |
| 1196 |
retval = getLineFromCode( code, n ); |
| 1197 |
break; |
| 1198 |
case 3: |
| 1199 |
retval = getLineFromFile( file, n ); |
| 1200 |
break; |
| 1201 |
default: |
| 1202 |
break; |
| 1203 |
} |
| 1204 |
|
| 1205 |
// empty? |
| 1206 |
// if( retval == "" ) |
| 1207 |
// CK_FPRINTF_STDERR( "[chuck]: (parser) cannot retrieve source to display!\n" ); |
| 1208 |
|
| 1209 |
return retval; |
| 1210 |
} |
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
//----------------------------------------------------------------------------- |
| 1216 |
// I/O redirect: stdout |
| 1217 |
//----------------------------------------------------------------------------- |
| 1218 |
void ck_fprintf_stdout( const char * format, ... ) |
| 1219 |
{ |
| 1220 |
// evaluate the format string |
| 1221 |
va_list args; |
| 1222 |
va_start( args, format ); |
| 1223 |
vsnprintf( g_buffer2, g_buffer2_size, format, args ); |
| 1224 |
va_end( args ); |
| 1225 |
|
| 1226 |
// send the formatted string to the buffer |
| 1227 |
g_stdout_stream << g_buffer2; |
| 1228 |
|
| 1229 |
// try flushing |
| 1230 |
ck_fflush_stdout(); |
| 1231 |
|
| 1232 |
// NOTE: currently the output size is limitd by g_buffer2_size, which |
| 1233 |
// will cut off longer strings (e.g., trying to print a deep stack trace) |
| 1234 |
// might look into vasprintf(), which dynamically allocates the |
| 1235 |
// final string buffer, accomodates arbitrarily long string; the downside |
| 1236 |
// is that vasprintf dynamically allocates memory each time |
| 1237 |
} |
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
//----------------------------------------------------------------------------- |
| 1243 |
// I/O redirect: stderr |
| 1244 |
//----------------------------------------------------------------------------- |
| 1245 |
void ck_fprintf_stderr( const char * format, ... ) |
| 1246 |
{ |
| 1247 |
// evaluate the format string |
| 1248 |
va_list args; |
| 1249 |
va_start( args, format ); |
| 1250 |
vsnprintf( g_buffer2, g_buffer2_size, format, args ); |
| 1251 |
va_end( args ); |
| 1252 |
|
| 1253 |
// send the formatted string to the buffer |
| 1254 |
g_stderr_stream << g_buffer2; |
| 1255 |
|
| 1256 |
// try flushing |
| 1257 |
ck_fflush_stderr(); |
| 1258 |
|
| 1259 |
// NOTE: currently the output size is limitd by g_buffer2_size, which |
| 1260 |
// will cut off longer strings (e.g., trying to print a deep stack trace) |
| 1261 |
// might look into vasprintf(), which dynamically allocates the |
| 1262 |
// final string buffer, accomodates arbitrarily long string; the downside |
| 1263 |
// is that vasprintf dynamically allocates memory each time |
| 1264 |
} |
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
//----------------------------------------------------------------------------- |
| 1270 |
// I/O flush stdout |
| 1271 |
//----------------------------------------------------------------------------- |
| 1272 |
void ck_fflush_stdout() |
| 1273 |
{ |
| 1274 |
// no callback? just flush it |
| 1275 |
if( g_stdout_callback == NULL ) |
| 1276 |
{ |
| 1277 |
// send to stdout |
| 1278 |
fprintf( stdout, "%s", g_stdout_stream.str().c_str() ); |
| 1279 |
fflush( stdout ); |
| 1280 |
|
| 1281 |
// and clear buffer |
| 1282 |
g_stdout_stream.str( std::string() ); |
| 1283 |
} |
| 1284 |
else |
| 1285 |
{ |
| 1286 |
// yes callback. |
| 1287 |
// check if stream contains a newline |
| 1288 |
if( g_stdout_stream.str().find( '\n' ) != std::string::npos ) |
| 1289 |
{ |
| 1290 |
// if so, emit to callback |
| 1291 |
g_stdout_callback( g_stdout_stream.str().c_str() ); |
| 1292 |
|
| 1293 |
// and clear buffer |
| 1294 |
g_stdout_stream.str( std::string() ); |
| 1295 |
} |
| 1296 |
} |
| 1297 |
} |
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
//----------------------------------------------------------------------------- |
| 1303 |
// I/O flush stderr |
| 1304 |
//----------------------------------------------------------------------------- |
| 1305 |
void ck_fflush_stderr() |
| 1306 |
{ |
| 1307 |
// no callback? just flush it |
| 1308 |
if( g_stderr_callback == NULL ) |
| 1309 |
{ |
| 1310 |
// send to stderr |
| 1311 |
fprintf( stderr, "%s", g_stderr_stream.str().c_str() ); |
| 1312 |
fflush( stderr ); |
| 1313 |
|
| 1314 |
// and clear buffer |
| 1315 |
g_stderr_stream.str( std::string() ); |
| 1316 |
} |
| 1317 |
else |
| 1318 |
{ |
| 1319 |
// yes callback. |
| 1320 |
// check if stream contains a newline |
| 1321 |
if( g_stderr_stream.str().find( '\n' ) != std::string::npos ) |
| 1322 |
{ |
| 1323 |
// if so, emit to callback |
| 1324 |
g_stderr_callback( g_stderr_stream.str().c_str() ); |
| 1325 |
|
| 1326 |
// and clear buffer |
| 1327 |
g_stderr_stream.str( std::string() ); |
| 1328 |
} |
| 1329 |
} |
| 1330 |
} |
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
//----------------------------------------------------------------------------- |
| 1336 |
// I/O redirect: stdout from format string output |
| 1337 |
//----------------------------------------------------------------------------- |
| 1338 |
void ck_vfprintf_stdout( const char * format, va_list args ) |
| 1339 |
{ |
| 1340 |
// evaluate the format string |
| 1341 |
vsnprintf( g_buffer2, g_buffer2_size, format, args ); |
| 1342 |
|
| 1343 |
// send the formatted string to the buffer |
| 1344 |
g_stdout_stream << g_buffer2; |
| 1345 |
|
| 1346 |
// try flushing |
| 1347 |
ck_fflush_stdout(); |
| 1348 |
} |
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
//----------------------------------------------------------------------------- |
| 1354 |
// I/O redirect: stderr from format string output |
| 1355 |
//----------------------------------------------------------------------------- |
| 1356 |
void ck_vfprintf_stderr( const char * format, va_list args ) |
| 1357 |
{ |
| 1358 |
// evaluate the format string |
| 1359 |
vsnprintf( g_buffer2, g_buffer2_size, format, args ); |
| 1360 |
|
| 1361 |
// send the formatted string to the buffer |
| 1362 |
g_stderr_stream << g_buffer2; |
| 1363 |
|
| 1364 |
// try flushing |
| 1365 |
ck_fflush_stderr(); |
| 1366 |
} |
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
//----------------------------------------------------------------------------- |
| 1372 |
// I/O redirect: set stdout callback; this is useful for redirecting output |
| 1373 |
// from chuck to miniAudicle, Chunity, or any other host system with its |
| 1374 |
// own way of outputing |
| 1375 |
//----------------------------------------------------------------------------- |
| 1376 |
void ck_set_stdout_callback( void (*callback)(const char *) ) |
| 1377 |
{ |
| 1378 |
g_stdout_callback = callback; |
| 1379 |
#ifdef __cplusplus |
| 1380 |
g_ck_stdoutstream.set_callback( callback ); |
| 1381 |
#endif |
| 1382 |
} |
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
//----------------------------------------------------------------------------- |
| 1388 |
// I/O redirect: set stderr callback; this is useful for redirecting output |
| 1389 |
// from chuck to miniAudicle, Chunity, or any other host system with its |
| 1390 |
// own way of outputing |
| 1391 |
//----------------------------------------------------------------------------- |
| 1392 |
void ck_set_stderr_callback( void (*callback)(const char *) ) |
| 1393 |
{ |
| 1394 |
g_stderr_callback = callback; |
| 1395 |
#ifdef __cplusplus |
| 1396 |
g_ck_stderrstream.set_callback( callback ); |
| 1397 |
#endif |
| 1398 |
} |
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
//----------------------------------------------------------------------------- |
| 1404 |
// implementation for ChuckOutStream | REFACTOR-2017 Jack Atherton |
| 1405 |
//----------------------------------------------------------------------------- |
| 1406 |
#ifdef __cplusplus |
| 1407 |
extern "C++" { |
| 1408 |
|
| 1409 |
ChuckOutStream::ChuckOutStream( bool isErr ) |
| 1410 |
{ |
| 1411 |
m_callback = NULL; |
| 1412 |
m_isErr = isErr; |
| 1413 |
} |
| 1414 |
|
| 1415 |
ChuckOutStream::~ChuckOutStream() |
| 1416 |
{ |
| 1417 |
} |
| 1418 |
|
| 1419 |
ChuckOutStream& ChuckOutStream::operator<<( const std::string & val ) |
| 1420 |
{ |
| 1421 |
m_stream << val; |
| 1422 |
if( m_isErr || (val == CK_STDENDL) |
| 1423 |
/* || val.find("\n") != std::string::npos */ ) |
| 1424 |
{ this->flush(); } |
| 1425 |
return *this; |
| 1426 |
} |
| 1427 |
|
| 1428 |
ChuckOutStream& ChuckOutStream::operator<<( const char * val ) |
| 1429 |
{ |
| 1430 |
m_stream << val; |
| 1431 |
string s = val; |
| 1432 |
if( m_isErr || s == CK_STDENDL |
| 1433 |
/* || s.find("\n") != std::string::npos */ ) |
| 1434 |
{ this->flush(); } |
| 1435 |
return *this; |
| 1436 |
} |
| 1437 |
|
| 1438 |
ChuckOutStream& ChuckOutStream::operator<<(const double val) |
| 1439 |
{ |
| 1440 |
m_stream << val; |
| 1441 |
if( m_isErr ) { this->flush(); } |
| 1442 |
return *this; |
| 1443 |
} |
| 1444 |
|
| 1445 |
ChuckOutStream& ChuckOutStream::operator<<( const float val ) |
| 1446 |
{ |
| 1447 |
m_stream << val; |
| 1448 |
if (m_isErr) { this->flush(); } |
| 1449 |
return *this; |
| 1450 |
} |
| 1451 |
|
| 1452 |
ChuckOutStream& ChuckOutStream::operator<<( const unsigned long long val ) |
| 1453 |
{ |
| 1454 |
m_stream << val; |
| 1455 |
if (m_isErr) { this->flush(); } |
| 1456 |
return *this; |
| 1457 |
} |
| 1458 |
|
| 1459 |
ChuckOutStream& ChuckOutStream::operator<<( const long long val ) |
| 1460 |
{ |
| 1461 |
m_stream << val; |
| 1462 |
if (m_isErr) { this->flush(); } |
| 1463 |
return *this; |
| 1464 |
} |
| 1465 |
|
| 1466 |
ChuckOutStream& ChuckOutStream::operator<<( const unsigned long val ) |
| 1467 |
{ |
| 1468 |
m_stream << val; |
| 1469 |
if (m_isErr) { this->flush(); } |
| 1470 |
return *this; |
| 1471 |
} |
| 1472 |
|
| 1473 |
ChuckOutStream& ChuckOutStream::operator<<( const long val ) |
| 1474 |
{ |
| 1475 |
m_stream << val; |
| 1476 |
if (m_isErr) { this->flush(); } |
| 1477 |
return *this; |
| 1478 |
} |
| 1479 |
|
| 1480 |
ChuckOutStream& ChuckOutStream::operator<<( const unsigned int val ) |
| 1481 |
{ |
| 1482 |
m_stream << val; |
| 1483 |
if (m_isErr) { this->flush(); } |
| 1484 |
return *this; |
| 1485 |
} |
| 1486 |
|
| 1487 |
ChuckOutStream& ChuckOutStream::operator<<( const int val ) |
| 1488 |
{ |
| 1489 |
m_stream << val; |
| 1490 |
if (m_isErr) { this->flush(); } |
| 1491 |
return *this; |
| 1492 |
} |
| 1493 |
|
| 1494 |
ChuckOutStream& ChuckOutStream::operator<<( const bool val ) |
| 1495 |
{ |
| 1496 |
m_stream << val; |
| 1497 |
if (m_isErr) { this->flush(); } |
| 1498 |
return *this; |
| 1499 |
} |
| 1500 |
|
| 1501 |
void ChuckOutStream::set_callback( void (*callback)( const char * ) ) |
| 1502 |
{ |
| 1503 |
m_callback = callback; |
| 1504 |
} |
| 1505 |
|
| 1506 |
void ChuckOutStream::flush() |
| 1507 |
{ |
| 1508 |
if( m_callback != NULL ) |
| 1509 |
{ |
| 1510 |
// send to callback |
| 1511 |
m_callback( m_stream.str().c_str() ); |
| 1512 |
} |
| 1513 |
else |
| 1514 |
{ |
| 1515 |
// print it |
| 1516 |
if( m_isErr ) |
| 1517 |
{ |
| 1518 |
// to cerr |
| 1519 |
std::cerr << m_stream.str().c_str(); |
| 1520 |
std::cerr.flush(); |
| 1521 |
} |
| 1522 |
else |
| 1523 |
{ |
| 1524 |
// to cout |
| 1525 |
std::cout << m_stream.str().c_str(); |
| 1526 |
std::cout.flush(); |
| 1527 |
} |
| 1528 |
} |
| 1529 |
// clear buffer |
| 1530 |
m_stream.str( std::string() ); |
| 1531 |
} |
| 1532 |
|
| 1533 |
ChuckOutStream g_ck_stdoutstream( FALSE ); |
| 1534 |
ChuckOutStream g_ck_stderrstream( TRUE ); |
| 1535 |
} |
| 1536 |
|
| 1537 |
#endif |