diff --git a/harbour/ChangeLog b/harbour/ChangeLog index 893827720a..bae409d4b9 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -1,3 +1,13 @@ +20000217-23:00 GMT+3 Alexander Kresin + * source/pp/hbpp.c + * source/pp/hbppint.c + * source/pp/hbpplib.c + * source/pp/stdalone/hbpp.c + * source/compiler/harbour.y + * include/compiler.h + * accepting of statements in include files implemented + * pp now generate #line statements + 20000217-09:00 GMT-3 Luiz Rafael Culik *doc/vm.txt doc/harbext.txt diff --git a/harbour/include/compiler.h b/harbour/include/compiler.h index 974c3b2871..232b017bc4 100644 --- a/harbour/include/compiler.h +++ b/harbour/include/compiler.h @@ -67,7 +67,9 @@ typedef enum typedef struct { FILE * handle; /* handle of the opened file */ - void * pBuffer; /* buffer used by yacc */ + void * pBuffer; /* file buffer */ + int iBuffer; /* current position in file buffer */ + int lenBuffer; /* current length of data in file buffer */ char * szFileName; /* name of the file */ void * pPrev; /* pointer to the previous opened file */ void * pNext; /* pointer to the next opened file */ diff --git a/harbour/source/compiler/harbour.y b/harbour/source/compiler/harbour.y index 2eb006a87e..b216914193 100644 --- a/harbour/source/compiler/harbour.y +++ b/harbour/source/compiler/harbour.y @@ -1522,6 +1522,8 @@ BOOL hb_compInclude( char * szFileName, PATHNAMES * pSearch ) pFile = ( PFILE ) hb_xgrab( sizeof( _FILE ) ); pFile->handle = yyin; + pFile->pBuffer = hb_xgrab( HB_PP_BUFF_SIZE ); + pFile->iBuffer = pFile->lenBuffer = 10; pFile->szFileName = szFileName; pFile->pPrev = NULL; @@ -1558,7 +1560,10 @@ int yywrap( void ) /* handles the EOF of the currently processed file */ void * pLast; if( hb_comp_files.iFiles == 1 ) + { + hb_xfree( hb_comp_files.pLast->pBuffer ); return 1; /* we have reached the main EOF */ + } /* else { diff --git a/harbour/source/pp/hbpp.c b/harbour/source/pp/hbpp.c index 2e7621f549..d047c7ad3e 100644 --- a/harbour/source/pp/hbpp.c +++ b/harbour/source/pp/hbpp.c @@ -100,7 +100,7 @@ static int strotrim( char * ); static int NextWord( char **, char *, BOOL ); static int NextName( char **, char * ); static int NextParm( char **, char * ); -static BOOL OpenInclude( char *, PATHNAMES *, PHB_FNAME, FILE **, BOOL bStandardOnly, char * ); +static BOOL OpenInclude( char *, PATHNAMES *, PHB_FNAME, BOOL bStandardOnly, char * ); /* These are related to pragma support */ static int ParsePragma( char * ); @@ -143,7 +143,7 @@ static int s_numBrackets; static char s_groupchar; static char s_prevchar = 'A'; -int hb_pp_lInclude = 0; +extern int hb_comp_iLine; /* currently parsed file line number */ int * hb_pp_aCondCompile; int hb_pp_nCondCompile = 0; @@ -180,7 +180,6 @@ int hb_pp_ParseDirective( char * sLine ) char sDirective[ MAX_NAME ]; char szInclude[ _POSIX_PATH_MAX ]; int i; - FILE * handl_i; HB_TRACE(HB_TR_DEBUG, ("hb_pp_ParseDirective(%s)", sLine)); @@ -232,15 +231,8 @@ int hb_pp_ParseDirective( char * sLine ) *(sLine+i) = '\0'; /* if((handl_i = fopen(sLine, "r")) == NULL) */ - if( OpenInclude( sLine, hb_comp_pIncludePath, hb_comp_pFileName, &handl_i, ( cDelimChar == '>' ), szInclude ) ) - { - hb_pp_lInclude++; - hb_pp_Parse(handl_i, 0, szInclude ); - hb_pp_lInclude--; - fclose(handl_i); - } - else - hb_compGenError( hb_pp_szErrors, 'F', ERR_CANNOT_OPEN, sLine, NULL ); + if( !OpenInclude( sLine, hb_comp_pIncludePath, hb_comp_pFileName, ( cDelimChar == '>' ), szInclude ) ) + hb_compGenError( hb_pp_szErrors, 'F', ERR_CANNOT_OPEN, sLine, NULL ); } else if( i >= 4 && i <= 6 && memcmp( sDirective, "DEFINE", i ) == 0 ) @@ -2276,15 +2268,17 @@ static int NextParm( char ** sSource, char * sDest ) return lenName; } -static BOOL OpenInclude( char * szFileName, PATHNAMES * pSearch, PHB_FNAME pMainFileName, FILE ** fptr, BOOL bStandardOnly, char * szInclude ) +static BOOL OpenInclude( char * szFileName, PATHNAMES * pSearch, PHB_FNAME pMainFileName, BOOL bStandardOnly, char * szInclude ) { + FILE * fptr; PHB_FNAME pFileName; + PFILE pFile; HB_TRACE(HB_TR_DEBUG, ("OpenInclude(%s, %p, %p, %p, %d)", szFileName, pSearch, pMainFileName, fptr, (int) bStandardOnly)); if( bStandardOnly ) { - *fptr = 0; + fptr = 0; szInclude[ 0 ] = '\0'; } else @@ -2293,26 +2287,40 @@ static BOOL OpenInclude( char * szFileName, PATHNAMES * pSearch, PHB_FNAME pMain if( pFileName->szPath == NULL || *(pFileName->szPath) == '\0' ) pFileName->szPath = pMainFileName->szPath; hb_fsFNameMerge( szInclude, pFileName ); - *fptr = fopen( szInclude, "r" ); + fptr = fopen( szInclude, "r" ); hb_xfree( pFileName ); } - if( !*fptr && pSearch ) + if( !fptr && pSearch ) { pFileName = hb_fsFNameSplit( szFileName ); pFileName->szName = szFileName; pFileName->szExtension = NULL; - while( pSearch && !*fptr ) + while( pSearch && !fptr ) { pFileName->szPath = pSearch->szPath; hb_fsFNameMerge( szInclude, pFileName ); - *fptr = fopen( szInclude, "r" ); + fptr = fopen( szInclude, "r" ); pSearch = pSearch->pNext; } hb_xfree( pFileName ); } - return ( *fptr ? TRUE : FALSE ); + if( fptr ) + { + pFile = ( PFILE ) hb_xgrab( sizeof( _FILE ) ); + pFile->handle = fptr; + pFile->pBuffer = hb_xgrab( HB_PP_BUFF_SIZE ); + pFile->iBuffer = pFile->lenBuffer = 10; + pFile->szFileName = szFileName; + hb_comp_files.pLast->iLine = hb_comp_iLine; + hb_comp_iLine = 1; + pFile->pPrev = hb_comp_files.pLast; + hb_comp_files.pLast = pFile; + hb_comp_files.iFiles++; + return TRUE; + } + return ( FALSE ); } /* Size of abreviated pragma commands */ diff --git a/harbour/source/pp/hbppint.c b/harbour/source/pp/hbppint.c index 9e02cb2f65..7f2dd48d6e 100644 --- a/harbour/source/pp/hbppint.c +++ b/harbour/source/pp/hbppint.c @@ -6,7 +6,7 @@ * Harbour Project source code: * Preprocessor & Compiler integration module * - * Copyright 1999 Alexander S.Kresin + * Copyright 1999 Alexander S.Kresin * www - http://www.harbour-project.org * * This program is free software; you can redistribute it and/or modify @@ -50,9 +50,8 @@ #include "compiler.h" extern FILES hb_comp_files; +extern int hb_comp_iLine; /* currently parsed file line number */ -static int s_iBuffer; -static int s_lenBuffer; static char s_szLine[ HB_PP_STR_SIZE ]; static char s_szOutLine[ HB_PP_STR_SIZE ]; @@ -60,75 +59,89 @@ void hb_pp_Init( void ) { HB_TRACE(HB_TR_DEBUG, ("Hbpp_init()")); - s_lenBuffer = 10; - s_iBuffer = 10; hb_pp_aCondCompile = ( int * ) hb_xgrab( sizeof( int ) * 5 ); } int hb_pp_Internal( FILE * handl_o, char * sOut ) { - static char sBuffer[ HB_PP_BUFF_SIZE ]; /* File read buffer */ - char * ptr, * ptrOut = sOut; - int lContinue = 0; - int lens = 0, rdlen; - int rezParse; - int nline = 0; - FILE * handl_i = hb_comp_files.pLast->handle; + char *sBuffer; /* File read buffer */ + char * ptr, * ptrOut; + int lContinue; + int lens, rdlen; + FILE * handl_i; HB_TRACE(HB_TR_DEBUG, ("PreProcess(%p, %p, %s)", handl_i, handl_o, sOut)); - while( ( rdlen = hb_pp_RdStr( handl_i, s_szLine + lens, HB_PP_STR_SIZE - lens, lContinue, - sBuffer, &s_lenBuffer, &s_iBuffer ) ) >= 0 ) - { - if( ! hb_pp_lInclude ) - nline++; - lens += rdlen; + handl_i = hb_comp_files.pLast->handle; + sBuffer = hb_comp_files.pLast->pBuffer; + lContinue = lens = 0; + ptrOut = sOut; + while( ( rdlen = hb_pp_RdStr( handl_i, s_szLine + lens, HB_PP_STR_SIZE - + lens, lContinue, sBuffer, &(hb_comp_files.pLast->lenBuffer), + &(hb_comp_files.pLast->iBuffer) ) ) >= 0 ) + { + lens += rdlen; - if( s_szLine[ lens - 1 ] == ';' ) - { - lContinue = 1; - lens--; - lens--; - while( s_szLine[ lens ] == ' ' || s_szLine[ lens ] == '\t' ) lens--; - s_szLine[ ++lens ] = ' '; - s_szLine[ ++lens ] = '\0'; + if( s_szLine[ lens - 1 ] == ';' ) + { + lContinue = 1; + lens--; + lens--; + while( s_szLine[ lens ] == ' ' || s_szLine[ lens ] == '\t' ) lens--; + s_szLine[ ++lens ] = ' '; + s_szLine[ ++lens ] = '\0'; - *ptrOut++ = '\n'; - } - else - { - lContinue = 0; - lens = 0; - } + *ptrOut++ = '\n'; + } + else + { + lContinue = 0; + lens = 0; + } - if( !lContinue ) - { - if( *s_szLine != '\0' ) - { - ptr = s_szLine; - HB_SKIPTABSPACES( ptr ); - if( *ptr == '#' ) - { - if( ( rezParse = hb_pp_ParseDirective( ptr + 1 ) ) == 0 ) - *s_szLine = '\0'; - } - else - { - if( hb_pp_nCondCompile == 0 || hb_pp_aCondCompile[ hb_pp_nCondCompile - 1 ] ) - { - if( ( rezParse = hb_pp_ParseExpression( ptr, s_szOutLine ) ) > 0 ) - { - printf( "\nError number %u in line %u\n", rezParse, nline ); - } - } - else - *s_szLine = '\0'; - } - } - break; - } - } - if( rdlen < 0 ) return 0; + if( !lContinue ) + { + if( *s_szLine != '\0' ) + { + ptr = s_szLine; + HB_SKIPTABSPACES( ptr ); + if( *ptr == '#' ) + { + hb_pp_ParseDirective( ptr + 1 ); + if( sBuffer != hb_comp_files.pLast->pBuffer ) + sprintf( s_szLine, "#line 1 \"%s\"", + hb_comp_files.pLast->szFileName ); + else + *s_szLine = '\0'; + } + else + { + if( hb_pp_nCondCompile == 0 || hb_pp_aCondCompile[ hb_pp_nCondCompile - 1 ] ) + hb_pp_ParseExpression( ptr, s_szOutLine ); + else + *s_szLine = '\0'; + } + } + break; + } + } + if( rdlen < 0 ) + { + if( hb_comp_files.iFiles == 1 ) + return 0; /* we have reached the main EOF */ + else + { /* we close the currently include file and continue */ + PFILE pFile; + fclose( hb_comp_files.pLast->handle ); + hb_xfree( hb_comp_files.pLast->pBuffer ); + pFile = ( PFILE ) ( ( PFILE ) hb_comp_files.pLast )->pPrev; + hb_xfree( hb_comp_files.pLast ); + hb_comp_files.pLast = pFile; + hb_comp_iLine = hb_comp_files.pLast->iLine; + hb_comp_files.iFiles--; + sprintf( s_szLine, "#line %d \"%s\"",hb_comp_iLine+2,hb_comp_files.pLast->szFileName ); + } + } lens = hb_pp_strocpy( ptrOut, s_szLine ) + ( ptrOut - sOut ); *( sOut + lens++ ) = '\n'; @@ -139,62 +152,3 @@ int hb_pp_Internal( FILE * handl_o, char * sOut ) return lens; } - -int hb_pp_Parse( FILE * handl_i, FILE * handl_o, char * szSource ) -{ - char * sBuffer = ( char * ) hb_xgrab( HB_PP_BUFF_SIZE ); /* File read buffer */ - char * ptr; - char szLine[ 16 ]; - int lContinue = 0; - int iBuffer = 10, lenBuffer = 10; - int lens = 0, rdlen, iLine = 0; - - HB_SYMBOL_UNUSED( handl_o ); - - HB_TRACE(HB_TR_DEBUG, ("hb_pp_Parse(%p, %p)", handl_i, handl_o)); - - while( ( rdlen = hb_pp_RdStr( handl_i, s_szLine + lens, HB_PP_STR_SIZE - lens, lContinue, - sBuffer, &lenBuffer, &iBuffer ) ) >= 0 ) - { - lens += rdlen; - iLine++; - - if( s_szLine[ lens - 1 ] == ';' ) - { - lContinue = 1; - lens--; - lens--; - while( s_szLine[ lens ] == ' ' || s_szLine[ lens ] == '\t' ) lens--; - s_szLine[ ++lens ] = ' '; - s_szLine[ ++lens ] = '\0'; - } - else - { - lContinue = 0; - lens = 0; - } - - if( !lContinue ) - { - if( *s_szLine != '\0' ) - { - ptr = s_szLine; - HB_SKIPTABSPACES( ptr ); - if( *ptr == '#' ) - { - hb_pp_ParseDirective( ptr + 1 ); - *s_szLine = '\0'; - } - else - { - sprintf( szLine, "%d", iLine ); - hb_compGenWarning( hb_pp_szWarnings, 'I', WARN_NONDIRECTIVE, szSource, szLine ); - } - } - } - } - - hb_xfree( sBuffer ); - - return 0; -} diff --git a/harbour/source/pp/hbpplib.c b/harbour/source/pp/hbpplib.c index 8b3faa7126..0e4f0f7674 100644 --- a/harbour/source/pp/hbpplib.c +++ b/harbour/source/pp/hbpplib.c @@ -55,6 +55,7 @@ PATHNAMES * hb_comp_pIncludePath = NULL; PHB_FNAME hb_comp_pFileName = NULL; FILES hb_comp_files; +int hb_comp_iLine; /* currently parsed file line number */ /* These are need for the PP #pragma support */ BOOL hb_comp_bPPO = FALSE; /* flag indicating, is ppo output needed */ diff --git a/harbour/source/pp/stdalone/hbpp.c b/harbour/source/pp/stdalone/hbpp.c index 0d3cabaa2f..1238516bc5 100644 --- a/harbour/source/pp/stdalone/hbpp.c +++ b/harbour/source/pp/stdalone/hbpp.c @@ -56,13 +56,14 @@ static void AddSearchPath( char * szPath, PATHNAMES * * pSearchList ); static void OutTable( DEFINES * endDefine, COMMANDS * endCommand ); -static int s_iline = 0; static char s_szLine[ HB_PP_STR_SIZE ]; static char s_szOutLine[ HB_PP_STR_SIZE ]; static int s_iWarnings = 0; PATHNAMES * hb_comp_pIncludePath = NULL; PHB_FNAME hb_comp_pFileName = NULL; +FILES hb_comp_files; +int hb_comp_iLine = 0; /* currently parsed file line number */ /* These are need for the PP #pragma support */ BOOL hb_comp_bPPO = FALSE; /* flag indicating, is ppo output needed */ @@ -239,7 +240,7 @@ int hb_pp_Parse( FILE * handl_i, FILE * handl_o, char * szSource ) while( ( rdlen = hb_pp_RdStr( handl_i, s_szLine + lens, HB_PP_STR_SIZE - lens, lContinue, sBuffer, &lenBuffer, &iBuffer ) ) >= 0 ) { - if( ! hb_pp_lInclude ) s_iline++; + if( hb_comp_files.iFiles == 1 ) hb_comp_iLine++; lens += rdlen; if( s_szLine[ lens - 1 ] == ';' ) @@ -259,7 +260,7 @@ int hb_pp_Parse( FILE * handl_i, FILE * handl_o, char * szSource ) if( *s_szLine != '\0' && !lContinue ) { - printf( "\r line %i", s_iline ); + printf( "\r line %i", hb_comp_iLine ); ptr = s_szLine; HB_SKIPTABSPACES( ptr ); if( *ptr == '#' ) @@ -276,7 +277,7 @@ int hb_pp_Parse( FILE * handl_i, FILE * handl_o, char * szSource ) } } - if( ! hb_pp_lInclude ) + if( hb_comp_files.iFiles == 1 ) { if( lContinue ) hb_pp_WrStr( handl_o, "\n" ); else hb_pp_WrStr( handl_o, s_szLine ); @@ -470,7 +471,7 @@ void hb_compGenError( char * _szErrors[], char cPrefix, int iError, char * szErr { HB_TRACE(HB_TR_DEBUG, ("hb_compGenError(%p, %c, %d, %s, %s)", _szErrors, cPrefix, iError, szError1, szError2)); - printf( "\r(%i) ", s_iline ); + printf( "\r(%i) ", hb_comp_iLine ); printf( "Error %c%04i ", cPrefix, iError ); printf( _szErrors[ iError - 1 ], szError1, szError2 ); printf( "\n\n" ); @@ -488,7 +489,7 @@ void hb_compGenWarning( char* _szWarnings[], char cPrefix, int iWarning, char * if( (szText[ 0 ] - '0') <= s_iWarnings ) { - printf( "\r(%i) ", s_iline ); + printf( "\r(%i) ", hb_comp_iLine ); printf( "Warning %c%04i ", cPrefix, iWarning ); printf( szText + 1, szWarning1, szWarning2 ); printf( "\n" );