cs2lint

CubeScript linter for Cube2 engine
git clone git://deadbeef.fr/cs2lint.git
Log | Files | Refs | README | LICENSE

commit c7cb0025d5da5c7ef7f4925e621a2fe181e9e392
parent d8f787712270427ef36002ab0afb82817cecd8fa
Author: Morel BĂ©renger <berengermorel76@gmail.com>
Date:   Sun, 19 Jul 2020 02:27:08 +0200

make cs2lint usable in scripts

Diffstat:
Mmain.cpp | 55++++++++++++++++++++++++++++++++++++++++++++++---------
1 file changed, 46 insertions(+), 9 deletions(-)

diff --git a/main.cpp b/main.cpp @@ -1,18 +1,55 @@ +#include <assert.h> #include <stdio.h> #include "coco_build/Parser.h" -int main() +bool parse_file( char const* file_name ); + +int main( int argc, char** argv ) +{ + FILE* target = stdin; + bool failed = false; + assert( argv ); + + ++argv; + if( argc == 1 ) + { + return parse_file( *argv ) ? EXIT_FAILURE : EXIT_SUCCESS; + } + + do + { + failed = parse_file( *argv ) || failed; + ++argv; + } while( *argv ); + + return failed ? EXIT_FAILURE : EXIT_SUCCESS; +} + +bool parse_file( char const* file_name ) { bool failed = false; - Scanner scanner( stdin ); - Parser parser( &scanner ); - parser.Parse(); - failed = parser.errors && parser.errors->count > 0; - if( failed ) + FILE* target = file_name ? fopen( file_name, "r" ) : stdin; + try + { + Scanner scanner( target ); + Parser parser( &scanner ); + parser.Parse(); + failed = parser.errors && parser.errors->count > 0; + if( failed ) + { + wprintf( L"%d errors found in file %s\n", parser.errors->count, file_name ); + } + } + catch(...) + { + wprintf( L"Exception caught while analyzing %s\n", file_name ); + failed = true; + } + + if( file_name ) { - wprintf( L"%d errors found\n", parser.errors->count : 0 ); - return EXIT_FAILURE; + fclose( target ); } - return EXIT_SUCCESS; + return failed; }