main.cpp (1466B)
1 #include <errno.h> 2 #include <string.h> 3 #include <assert.h> 4 #include <stdio.h> 5 6 #include "b/coco_gen/Parser.h" 7 8 bool parse_file( char const* file_name ); 9 10 enum { QUIET, FATAL, NORMAL, VERBOSE } verbosity = NORMAL; 11 12 int main( int argc, char** argv ) 13 { 14 bool failed = false; 15 assert( argv ); 16 17 ++argv; 18 if( argc == 1 ) 19 { 20 return parse_file( *argv ) ? EXIT_FAILURE : EXIT_SUCCESS; 21 } 22 23 do 24 { 25 failed = parse_file( *argv ) || failed; 26 ++argv; 27 } while( *argv ); 28 29 return failed ? EXIT_FAILURE : EXIT_SUCCESS; 30 } 31 32 bool parse_file( char const* file_name ) 33 { 34 if( verbosity >= VERBOSE ) 35 { 36 fwprintf( stderr, L"parsing file %s\n", file_name ); 37 } 38 bool failed = false; 39 FILE* target = file_name ? fopen( file_name, "r" ) : stdin; 40 if( !target && verbosity >= FATAL ) 41 { 42 fwprintf( stderr, L"Unable to open file %s: %s(%02x)\n", file_name, strerror( errno ), errno ); 43 return true; 44 } 45 try 46 { 47 Scanner scanner( target ); 48 Parser parser( &scanner ); 49 parser.Parse(); 50 failed = parser.errors && parser.errors->count > 0; 51 if( failed && verbosity >= NORMAL ) 52 { 53 fwprintf( stderr, L"%d errors found in file %s\n", parser.errors->count, file_name ); 54 } 55 } 56 catch(...) 57 { 58 if( verbosity >= FATAL ) 59 { 60 fwprintf( stderr, L"Exception caught while analyzing %s\n", file_name ); 61 } 62 failed = true; 63 } 64 65 if( file_name ) 66 { 67 fclose( target ); 68 } 69 if( verbosity >= VERBOSE ) 70 { 71 fwprintf( stderr, L"parsing file %s completed\n", file_name ); 72 } 73 return failed; 74 }