Lhogho  0.0.028
 All Data Structures Files Functions Variables Typedefs Macros Pages
errors.c File Reference

Go to the source code of this file.

Macros

#define LEFT_DIST   10
 
#define RIGHT_DIST   40
 
#define DUMP_BUF_SIZE   128
 

Functions

void init_errors ()
 initializes errors More...
 
void finit_errors ()
 finalizes errors More...
 
atom_t new_error (uint_t code, atom_t data)
 creates a new error atom More...
 
atom_t new_os_error (chars_t filename)
 creates error atom for OS error More...
 
atom_t new_os_error_atom (atom_t filename)
 
atom_t new_parse_error (uint_t code, int position, atom_t source)
 creates error atom for parser's error More...
 
void add_error_source (atom_t error, atom_t source)
 adds new error position to an error More...
 
void delete_error (atom_t a)
 deletes error atom More...
 
int get_error_position (atom_t data, chars_t *source)
 gets error position More...
 
void dump_neighbourhood (chars_t source, int position)
 dumps source around some position More...
 
void dump_error (atom_t a, int level)
 dumps error atom More...
 
void clear_all_errors ()
 clears all errors More...
 

Variables

atom_t error_texts [LAST_ERROR_CODE+1]
 Texts for error messages. More...
 
int last_os_error
 records last OS error code More...
 
atom_t all_errors = NULL
 list of all occurred errors More...
 

Macro Definition Documentation

#define LEFT_DIST   10

Definition at line 358 of file errors.c.

#define RIGHT_DIST   40

Definition at line 359 of file errors.c.

#define DUMP_BUF_SIZE   128

Function Documentation

void init_errors ( )

Initializes the table with error messages taken from TR_ERRORS.

Definition at line 91 of file errors.c.

92 {
93  error_texts[0] = unbound;
94 
95  atom_t names = new_word( TR_ERRORS, UNKNOWN );
96  atom_t tokens = tokenize( names, TOKENIZE_DATA );
97  atom_t t = tokens;
98 
99  // create error texts
100  int i;
101  for( i = 1; i<=LAST_ERROR_CODE; i++, t=CDR(t) )
102  {
103  #ifdef SAFEMODE
104  assert( IS_NOT_EMPTY(t) ); // too few words in TR_PRIMITIVES
105  #endif
106 
107  error_texts[i] = USE(CAR(t));
108  }
109 
110  #ifdef SAFEMODE
111  assert( IS_EMPTY(t) ); // too many words in TR_PRIMITIVES
112  #endif
113 
114  DEUSE( names );
115  DEUSE( tokens );
116 }
void finit_errors ( )

Frees atoms holding error texts.

Definition at line 127 of file errors.c.

128 {
129  int i;
130  for( i = 1; i<=LAST_ERROR_CODE; i++ )
131  {
132  DEUSE(error_texts[i]);
133  }
134 }
atom_t new_error ( uint_t  code,
atom_t  data 
)
Parameters
codeerror code
dataerror data
Returns
error atom

Creates an error atom with reference count 1. The reference count of data is increased. Initializes the error position to be a list containing one element, which is the data parameter itself.

Definition at line 152 of file errors.c.

153 {
154  //printf("-----------\n");
155  //printf("new error code=%d\n",code);
156  //printf("new error data="); dumpln(data);
157  //printf("-----------\n");
158 
160 
161  #ifdef SAFEMODE
162  assert( data );
163  #endif //SAFEMODE
164 
165  REF(a) = 1;
166  ID(a) = ERROR_ID;
167  ERRCODE(a) = code;
168  ERRPOS(a) = new_list( USE(data), empty_list );
169  ERRDATA(a) = USE(data);
170 
171  #ifdef DEBUG_ATOM
172  printf("<ATOM> [%08x] error=[code=%d]\n",(int)a,code);
173  #endif //DEBUG_ATOM
174 
175  #ifdef ADVANCED
176  stats[ID(a)].allocs++;
177  if( stats[ID(a)].max<(stats[ID(a)].allocs-stats[ID(a)].deallocs) )
178  stats[ID(a)].max=(stats[ID(a)].allocs-stats[ID(a)].deallocs);
179  stats_free--;
180  #endif //ADVANCED
181 
182  #ifdef DEBUG_RUNTIME_ATOMS
184  {
185  outter( TEXT("<RUNTIME> new "), -1 );
186  dump_atom_address( a );
187  dump_atom( a, 1 );
188  outter( TEXT("\n"), -1 );
189  }
190  #endif
191  #ifdef DEBUG_COMPILETIME_ATOMS
192  if( compiling_code )
193  {
194  outter( TEXT("<COMPILETIME> new "), -1 );
195  dump_atom_address( a );
196  dump_atom( a, 1 );
197  outter( TEXT("\n"), -1 );
198  }
199  #endif
200 
203 
204  return a;
205 }
atom_t new_os_error ( chars_t  filename)
Parameters
filenamefile name associated with the error
Returns
error atom

Creates error atom describing OS error.

Definition at line 217 of file errors.c.

218 {
219  atom_t wrd = new_word( filename, UNKNOWN );
220  atom_t res = new_os_error_atom( wrd );
221  DEUSE( wrd );
222  return res;
223  //return new_error( ERROR_OS_ERROR, wrd );
224 }
atom_t new_os_error_atom ( atom_t  filename)

Definition at line 225 of file errors.c.

226 {
227  last_os_error = errno;
228  return new_error( ERROR_OS_ERROR, filename );
229 }
atom_t new_parse_error ( uint_t  code,
int  position,
atom_t  source 
)
Parameters
codeerror code
positioncharacter position within the source
source(sub)word containing the source
Returns
error atom

Creates error atom describing an error generated by the parser.

Definition at line 245 of file errors.c.

246 {
247  atom_t errsrc = new_subword( source, STRING(source)+position, 1 );
248  atom_t error = new_error( code, unbound );
249  CAR(ERRPOS(error)) = errsrc;
250  return error;
251 }
void add_error_source ( atom_t  error,
atom_t  source 
)
Parameters
errorerror to which new position will be added
sourceerror source

Adds a new error position to an already existing error atom. The error position is simply an atom which may have been extracted from the actual source. The new error position is inserted in the first place of the list of error positions.

Definition at line 268 of file errors.c.

269 {
270  //printf("register error pos>>>---------------\n");
271  //printf("register error src>>>"); dumpln(source);
272  //printf("register error err>>>"); dumpln(error);
273  //printf("register error pos>>>---------------\n");
274  //printf("errpos old ref=%d\n",REF(ERRPOS(error)));
275  ERRPOS(error) = new_list( USE(source), ERRPOS(error) );
276  //printf("errpos new ref=%d\n",REF(ERRPOS(error)));
277 }
void delete_error ( atom_t  a)
Parameters
aatom to delete

Deletes error atom by returning it back to the data pool. The source atom is dereferenced.

Definition at line 290 of file errors.c.

291 {
292  //printf("****** DELETING ERROR ****** [%x]\n",(int)a);
293  DEUSE( ERRPOS(a) );
294  DEUSE( ERRDATA(a) );
295  return_to_pool( &data_pool, a );
296 }
int get_error_position ( atom_t  data,
chars_t source 
)
Parameters
datadata to search for
sourcesource where the data is found
Returns
position of data within found source

Tries to identify the position of the first (sub)word in the data in the original source. This function returns the character position and the source. If the source cannot be identified, then return -1 and set source to NULL.

Definition at line 314 of file errors.c.

315 {
316  // dig in a list to find the first (sub)word
317  while( IS_LIST(data) & IS_NOT_EMPTY(data) ) data = CAR(data);
318 
319  // if it is a word then return we have found something
320  if( IS_WORD(data) )
321  {
322  *source = STRING(data);
323  return 0;
324  }
325 
326  // if it is a subword then return we have found something
327  if( IS_SUBWORD(data) )
328  {
329  *source = STRING(WORD(data));
330  return STRING(data)-STRING(WORD(data));
331  }
332 
333  // no, could not find the error position
334  *source = NULL;
335  return -1;
336 }
void dump_neighbourhood ( chars_t  source,
int  position 
)
Parameters
sourcesource containing the neighbourhood
positionposition marking the neighbourhood

Dumps a piece of source code containing given position. The dumped neighbourhood starts before the position and end after it in a way that: (1) at most LEFT_DIST chars before the position are included (2) at most RIGHT_DIST chars after the position are included (3) the neightbourhood contains characters from a single line only Dumped neighbourhood is output as a single line with an arrow anove it pointing the exact position.

Definition at line 360 of file errors.c.

361 {
362  if( !source || position<0 ) return;
363 
364  int left;
365  int right;
366  int ellipses = 1;
367  for( left=0; left<LEFT_DIST; left++ )
368  {
369  // reached the beginning of the source?
370  if( position-left == 0 ) {ellipses=0; break;}
371 
372  // reached the beginning of the line?
373  char_t ch = *(source+(position-left-1));
374  if( ch==TEXT('\n') || ch==TEXT('\r') ) {ellipses=0; break;}
375  }
376  while( left && ((*(source+position-left))<=TEXT(' ')) ) left--;
377 
378  for( right=0; right<RIGHT_DIST; right++ )
379  {
380  // reached the end of the line/file?
381  char_t ch = *(source+(position+right+1));
382  if( ch=='\n' || ch=='\r' || ch=='\0') break;
383  }
384 
385  int i;
386  chars_t s;
387  if( ellipses ) outter( TEXT("..."), 3 );
388  for( i=0,s=source+position-left; i<left+right+1; i++,s++ ) outter( s, 1 );
389  if( *(source+position+right+1)>=TEXT(' ') )
390  outter( TEXT("..."), 3 );
391  outter( TEXT("\n"), -1 );
392  for( i=0; i<left+3*ellipses; i++ ) outter( TEXT(" "), 1 );
393  outter( TEXT("^"), -1 );
394 }
void dump_error ( atom_t  a,
int  level 
)
Parameters
aatom to dump
leveldump level

Dumps error atom through the current outter function.

Definition at line 411 of file errors.c.

412 {
413  #define DUMP_BUF_SIZE 128
414  char_t buf[DUMP_BUF_SIZE];
415  int n;
416 
417  //printf("<<err ref=%d>> ",REF(a));
418  //printf("temporary dump of an error\n");
419  //printf(">>>>>>error ref = %d\n",REF(a));
420  //printf(">>>>>>error code = %d\n",ERRCODE(a));
421  //printf(">>>>>>error posn = "); dumpln(ERRPOS(a));
422  //printf(">>>>>>error data = "); dumpln(ERRDATA(a));
423  //return;
424 
425  int errpos = -1;
426  int errpos2 = -1;
427  int errpos3 = -1;
428  chars_t errsrc = NULL;
429  chars_t errsrc2 = NULL;
430  chars_t errsrc3 = NULL;
431  atom_t p;
432  //printf("errpos="); dump_atom(ERRPOS(a),1); printf("\n------------\n");
433  for( p=ERRPOS(a); IS_NOT_EMPTY(p); p=CDR(p) )
434  {
435  //printf("ERRPOS=");dumpln(CAR(p));
436  chars_t src;
437  int pos = get_error_position( CAR(p), &src );
438  if( pos>-1 )
439  {
440  errpos3 = errpos2;
441  errsrc3 = errsrc2;
442  errpos2 = errpos;
443  errsrc2 = errsrc;
444  errpos = pos;
445  errsrc = src;
446  }
447  //printf("pos=%d source=",pos); dumpln(CAR(p));
448  }
449 
451  {
452  errpos = errpos3;
453  errsrc = errsrc3;
454  }
455 
456  // print error code
457  n = SPRINTF( buf, DUMP_BUF_SIZE, FORMAT_ERR_CODE, ERRCODE(a) );
458  outter( buf, n );
459 
460  // print error position
461  n = SPRINTF( buf, DUMP_BUF_SIZE, FORMAT_ERR_POS, errpos );
462  outter( buf, n );
463 
464  // print error message
466  {
467  outter( TEXT(" - "), 3 );
468  dump( ERRDATA(a) );
469  }
470  else
471  {
472  #ifdef SAFEMODE
473  assert( ERRCODE(a)>0 );
474  assert( ERRCODE(a)<=LAST_ERROR_CODE );
475  #endif
476  outter( TEXT(" - "), 3 );
477  dump( error_texts[ERRCODE(a)] );
478  }
479 
480  if( ERRCODE(a)==ERROR_OS_ERROR )
481  {
482  chars_t msg = UNFILENAME(strerror(last_os_error));
483  outter( TEXT(": "), 2 );
484  outter( msg, -1 );
485  DEALLOC( msg );
486  }
487  outter( TEXT("\n"), -1 );
488 
489  // print error source
490  dump_neighbourhood( errsrc, errpos );
491 }
void clear_all_errors ( )

Clears all errors recorder in all_errors except for the one stored in last_error

Definition at line 503 of file errors.c.

504 {
505  atom_t err = all_errors;
506  if( err==NULL ) return;
507 
508  //printf("===INSIDE CLEAR_ALL_ERRORS===\n");
509  //printf("===EXCEPTION(%d): ",REF(last_error)); dumpln(last_error);
510  //printf("===ALL ERRORS (BEFORE): "); dumpln(all_errors);
511 
512  //printf("===START ERROR SCANNING===\n");
513  while( IS_NOT_EMPTY(err) )
514  {
515  //printf("===ERROR: [%x]\n",CAR(err));
516  if( CAR(err)!=last_error ) REF(CAR(err)) = 1;
517  err = CDR(err);
518  }
519  //printf("===END ERROR SCANNING===\n");
520 
521  USE( last_error );
522  //printf("AEREF=%d !!!!!!!!!!!!!!!!\n",REF(all_errors));
523  DEUSE( all_errors );
524  //printf("AEREF=%d !!!!!!!!!!!!!!!!\n",REF(all_errors));
525  DEUSE( last_error );
526 
528  //printf("===END OF CLEAR_ALL_ERRORS===\n\n");
529 }

Variable Documentation

atom_t error_texts[LAST_ERROR_CODE+1]

Definition at line 74 of file errors.c.

int last_os_error

Definition at line 79 of file errors.c.

atom_t all_errors = NULL

Definition at line 80 of file errors.c.


[ HOME | INDEX | ATOMS | VARS | REFERENCE ]
Lhogho Developer's Documentation
Wed Jul 10 2013