PageRenderTime 60ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/tags/build31/harbour/source/pp/hbpp.c

#
C | 2100 lines | 1847 code | 178 blank | 75 comment | 900 complexity | 578635696f779bb96b68c665448bf475 MD5 | raw file
Possible License(s): AGPL-1.0, BSD-3-Clause, CC-BY-SA-3.0, LGPL-3.0, GPL-2.0, LGPL-2.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * $Id: hbpp.c 2019 2000-01-21 15:01:27Z alkresin $
  3. */
  4. /*
  5. * Harbour Project source code:
  6. * Preprocessor core module
  7. *
  8. * Copyright 1999 Alexander S.Kresin <alex@belacy.belgorod.su>
  9. * www - http://www.harbour-project.org
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version, with one exception:
  15. *
  16. * The exception is that if you link the Harbour Runtime Library (HRL)
  17. * and/or the Harbour Virtual Machine (HVM) with other files to produce
  18. * an executable, this does not by itself cause the resulting executable
  19. * to be covered by the GNU General Public License. Your use of that
  20. * executable is in no way restricted on account of linking the HRL
  21. * and/or HVM code into it.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program; if not, write to the Free Software
  30. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA (or visit
  31. * their web site at http://www.gnu.org/).
  32. *
  33. */
  34. /*
  35. * The following parts are Copyright of the individual authors.
  36. * www - http://www.harbour-project.org
  37. *
  38. * Copyright 1999 Jose Lalin <dezac@corevia.com>
  39. * Support for #pragma directive and related functions
  40. * See doc/pragma.txt
  41. *
  42. * See doc/license.txt for licensing terms.
  43. *
  44. */
  45. /*
  46. * Avoid tracing in preprocessor/compiler.
  47. */
  48. #if ! defined(HB_TRACE_UTILS)
  49. #if defined(HB_TRACE_LEVEL)
  50. #undef HB_TRACE_LEVEL
  51. #endif
  52. #endif
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <string.h>
  56. #include <ctype.h>
  57. #include "hbpp.h"
  58. #include "hberrors.h"
  59. #include "compiler.h"
  60. static COMMANDS * AddCommand( char * ); /* Add new #command to an array */
  61. static COMMANDS * AddTranslate( char * ); /* Add new #translate to an array */
  62. static DEFINES * DefSearch( char *, BOOL * );
  63. static COMMANDS * ComSearch( char *, COMMANDS * );
  64. static COMMANDS * TraSearch( char *, COMMANDS * );
  65. static int ParseDefine( char * ); /* Process #define directive */
  66. static int ParseUndef( char * ); /* Process #undef directive */
  67. static int ParseIfdef( char *, int ); /* Process #ifdef directive */
  68. static void ParseCommand( char *, BOOL, BOOL ); /* Process #command or #translate directive */
  69. static void ConvertPatterns( char *, int, char *, int ); /* Converting result pattern in #command and #translate */
  70. static int WorkDefine( char **, char *, DEFINES * ); /* Replace fragment of code with a #defined result text */
  71. static int WorkPseudoF( char **, char *, DEFINES * ); /* Replace pseudofunction with a #defined result text */
  72. static int WorkCommand( char *, char *, COMMANDS * );
  73. static int WorkTranslate( char *, char *, COMMANDS *, int * );
  74. static int CommandStuff( char *, char *, char *, int *, BOOL, BOOL );
  75. static int RemoveSlash( char * );
  76. static int WorkMarkers( char **, char **, char *, int *, BOOL );
  77. static int getExpReal( char *, char **, BOOL, int );
  78. static BOOL isExpres( char * );
  79. static BOOL TestOptional( char *, char * );
  80. static BOOL CheckOptional( char *, char *, char *, int *, BOOL, BOOL );
  81. static void SkipOptional( char ** );
  82. static void SearnRep( char *, char *, int, char *, int * );
  83. static int ReplacePattern( char, char *, int, char *, int );
  84. static void pp_rQuotes( char *, char * );
  85. static int md_strAt( char *, int, char *, BOOL, BOOL );
  86. static char * PrevSquare( char * , char *, int * );
  87. static int IsInStr( char, char * );
  88. static int stroncpy( char *, char *, int );
  89. static int strincpy( char *, char * );
  90. static BOOL truncmp( char **, char **, BOOL );
  91. static BOOL strincmp( char *, char **, BOOL );
  92. static int strotrim( char * );
  93. static int NextWord( char **, char *, BOOL );
  94. static int NextName( char **, char * );
  95. static int NextParm( char **, char * );
  96. static BOOL OpenInclude( char *, PATHNAMES *, PHB_FNAME, FILE **, BOOL bStandardOnly, char * );
  97. /* These are related to pragma support */
  98. static int ParsePragma( char * );
  99. static BOOL StringToBool( char *, BOOL );
  100. static int StringToInt( char *, int );
  101. static BOOL IsOnOffSwitch( char *, BOOL );
  102. static void DebugPragma( char *, int, BOOL );
  103. static BOOL s_bTracePragma = FALSE;
  104. #define ISNAME( c ) ( isalnum( c ) || ( c ) == '_' || ( c ) > 0x7E )
  105. #define MAX_NAME 255
  106. #define MAX_EXP 1024
  107. #define PATTERN_SIZE 2048
  108. #define STATE_INIT 0
  109. #define STATE_NORMAL 1
  110. #define STATE_COMMENT 2
  111. #define STATE_QUOTE1 3
  112. #define STATE_QUOTE2 4
  113. #define STATE_QUOTE3 5
  114. #define STATE_ID_END 6
  115. #define STATE_ID 7
  116. #define STATE_EXPRES 8
  117. #define STATE_EXPRES_ID 9
  118. #define STATE_BRACKET 10
  119. #define IT_EXPR 1
  120. #define IT_ID 2
  121. #define IT_COMMA 3
  122. #define IT_ID_OR_EXPR 4
  123. static int s_kolAddDefs = 0;
  124. static int s_ParseState = 0;
  125. static int s_maxCondCompile = 5;
  126. static int s_aIsRepeate[ 5 ];
  127. static int s_Repeate;
  128. static BOOL s_bReplacePat = TRUE;
  129. static int s_numBrackets;
  130. static char s_groupchar;
  131. static char s_prevchar = 'A';
  132. int hb_pp_lInclude = 0;
  133. int * hb_pp_aCondCompile;
  134. int hb_pp_nCondCompile = 0;
  135. /* Table with parse errors */
  136. char * hb_pp_szErrors[] =
  137. {
  138. "Can\'t open #include file: \'%s\'",
  139. "#else does not match #ifdef",
  140. "#endif does not match #ifdef",
  141. "Bad filename in #include",
  142. "#define without parameters",
  143. "Missing => in #translate/#command",
  144. "Error in pattern definition",
  145. "Cycled #define",
  146. "Invalid name follows #: \'%s\'",
  147. "#error: \'%s\'",
  148. "Memory allocation error",
  149. "Memory reallocation error",
  150. "Freeing a NULL memory pointer",
  151. "Value out of range in #pragma directive"
  152. };
  153. /* Table with parse warnings */
  154. /* NOTE: The first character stores the warning's level that triggers this
  155. * warning. The warning's level is set by -w<n> command line option.
  156. */
  157. char * hb_pp_szWarnings[] =
  158. {
  159. "3Non directive in include file %s(%s)"
  160. };
  161. int hb_pp_ParseDirective( char * sLine )
  162. {
  163. char sDirective[MAX_NAME];
  164. char szInclude[_POSIX_PATH_MAX];
  165. int i;
  166. FILE* handl_i;
  167. HB_TRACE(HB_TR_DEBUG, ("hb_pp_ParseDirective(%s)", sLine));
  168. i = NextName( &sLine, sDirective );
  169. hb_strupr( sDirective );
  170. HB_SKIPTABSPACES(sLine);
  171. if( i == 4 && memcmp( sDirective, "ELSE", 4 ) == 0 )
  172. { /* --- #else --- */
  173. if( hb_pp_nCondCompile == 0 )
  174. hb_compGenError( hb_pp_szErrors, 'F', ERR_DIRECTIVE_ELSE, NULL, NULL );
  175. else if( hb_pp_nCondCompile == 1 || hb_pp_aCondCompile[hb_pp_nCondCompile-2] )
  176. hb_pp_aCondCompile[hb_pp_nCondCompile-1] = 1 - hb_pp_aCondCompile[hb_pp_nCondCompile-1];
  177. }
  178. else if( i >= 4 && i <= 5 && memcmp( sDirective, "ENDIF", i ) == 0 )
  179. { /* --- #endif --- */
  180. if( hb_pp_nCondCompile == 0 )
  181. hb_compGenError( hb_pp_szErrors, 'F', ERR_DIRECTIVE_ENDIF, NULL, NULL );
  182. else hb_pp_nCondCompile--;
  183. }
  184. else if( i >= 4 && i <= 5 && memcmp( sDirective, "IFDEF", i ) == 0 )
  185. ParseIfdef( sLine, TRUE ); /* --- #ifdef --- */
  186. else if( i >= 4 && i <= 6 && memcmp( sDirective, "IFNDEF", i ) == 0 )
  187. ParseIfdef( sLine, FALSE ); /* --- #ifndef --- */
  188. else if( hb_pp_nCondCompile==0 || hb_pp_aCondCompile[hb_pp_nCondCompile-1])
  189. {
  190. if( i >= 4 && i <= 7 && memcmp( sDirective, "INCLUDE", i ) == 0 )
  191. { /* --- #include --- */
  192. char cDelimChar;
  193. if( *sLine != '\"' && *sLine != '\'' && *sLine != '<' )
  194. hb_compGenError( hb_pp_szErrors, 'F', ERR_WRONG_NAME, NULL, NULL );
  195. cDelimChar = *sLine;
  196. if( cDelimChar == '<' )
  197. cDelimChar = '>';
  198. else if( cDelimChar == '`' )
  199. cDelimChar = '\'';
  200. sLine++; i = 0;
  201. while( *(sLine+i) != '\0' && *(sLine+i) != cDelimChar ) i++;
  202. if( *(sLine+i) != cDelimChar )
  203. hb_compGenError( hb_pp_szErrors, 'F', ERR_WRONG_NAME, NULL, NULL );
  204. *(sLine+i) = '\0';
  205. /* if((handl_i = fopen(sLine, "r")) == NULL) */
  206. if( OpenInclude( sLine, hb_comp_pIncludePath, hb_comp_pFileName, &handl_i, ( cDelimChar == '>' ), szInclude ) )
  207. {
  208. hb_pp_lInclude++;
  209. hb_pp_Parse(handl_i, 0, szInclude );
  210. hb_pp_lInclude--;
  211. fclose(handl_i);
  212. }
  213. else
  214. hb_compGenError( hb_pp_szErrors, 'F', ERR_CANNOT_OPEN, sLine, NULL );
  215. }
  216. else if( i >= 4 && i <= 6 && memcmp( sDirective, "DEFINE", i ) == 0 )
  217. ParseDefine( sLine ); /* --- #define --- */
  218. else if( i >= 4 && i <= 5 && memcmp( sDirective, "UNDEF", i ) == 0 )
  219. ParseUndef( sLine ); /* --- #undef --- */
  220. else if( (i >= 4 && i <= 7 && memcmp( sDirective, "COMMAND", i ) == 0) ||
  221. (i >= 4 && i <= 8 && memcmp( sDirective, "XCOMMAND", i ) == 0) )
  222. /* --- #command --- */
  223. ParseCommand( sLine, (i==7)? FALSE:TRUE, TRUE );
  224. else if( (i >= 4 && i <= 9 && memcmp( sDirective, "TRANSLATE", i ) == 0) ||
  225. (i >= 4 && i <= 10 && memcmp( sDirective, "XTRANSLATE", i ) == 0) )
  226. /* --- #translate --- */
  227. ParseCommand( sLine, (i==9)? FALSE:TRUE, FALSE );
  228. else if( i >= 4 && i <= 6 && memcmp( sDirective, "STDOUT", i ) == 0 )
  229. printf( "%s\n", sLine ); /* --- #stdout --- */
  230. else if( i >= 4 && i <= 5 && memcmp( sDirective, "ERROR", i ) == 0 )
  231. /* --- #error --- */
  232. hb_compGenError( hb_pp_szErrors, 'E', ERR_EXPLICIT, sLine, NULL );
  233. else if( i == 4 && memcmp( sDirective, "LINE", 4 ) == 0 )
  234. return -1;
  235. else if( i == 6 && memcmp( sDirective, "PRAGMA", 6 ) == 0 )
  236. ParsePragma( sLine ); /* --- #pragma --- */
  237. else
  238. hb_compGenError( hb_pp_szErrors, 'F', ERR_WRONG_DIRECTIVE, sDirective, NULL );
  239. }
  240. return 0;
  241. }
  242. static int ParseDefine( char * sLine )
  243. {
  244. char defname[MAX_NAME], pars[MAX_NAME];
  245. int i, npars = -1;
  246. DEFINES * lastdef;
  247. HB_TRACE(HB_TR_DEBUG, ("ParseDefine(%s)", sLine));
  248. HB_SKIPTABSPACES( sLine );
  249. if( isalpha( *sLine ) || *sLine == '_' || *sLine > 0x7e )
  250. {
  251. NextName( &sLine, defname );
  252. if( *sLine == '(' ) /* If pseudofunction was found */
  253. {
  254. sLine++; i = 0;
  255. npars = 0;
  256. while( *sLine != '\0' && *sLine != ')')
  257. {
  258. if( *sLine == ',' ) npars++;
  259. if( *sLine != ' ' && *sLine != '\t' ) *(pars+i++) = *sLine;
  260. sLine++;
  261. }
  262. if( i > 0 ) npars++;
  263. *(pars+i) = '\0';
  264. sLine++;
  265. }
  266. HB_SKIPTABSPACES(sLine);
  267. lastdef = hb_pp_AddDefine( defname, ( *sLine == '\0' )? NULL : sLine );
  268. lastdef->npars = npars;
  269. lastdef->pars = ( npars <= 0 )? NULL : hb_strdup( pars );
  270. }
  271. else
  272. hb_compGenError( hb_pp_szErrors, 'F', ERR_DEFINE_ABSENT, NULL, NULL );
  273. return 0;
  274. }
  275. DEFINES * hb_pp_AddDefine( char * defname, char * value )
  276. {
  277. BOOL isNew;
  278. DEFINES* stdef = DefSearch( defname, &isNew );
  279. HB_TRACE(HB_TR_DEBUG, ("hb_pp_AddDefine(%s, %s)", defname, value));
  280. if( stdef != NULL )
  281. {
  282. if( isNew )
  283. {
  284. if( stdef->pars ) hb_xfree( stdef->pars );
  285. if( stdef->value ) hb_xfree( stdef->value );
  286. }
  287. }
  288. else
  289. {
  290. stdef = ( DEFINES * ) hb_xgrab( sizeof( DEFINES ) );
  291. stdef->last = hb_pp_topDefine;
  292. hb_pp_topDefine = stdef;
  293. stdef->name = hb_strdup( defname );
  294. s_kolAddDefs++;
  295. }
  296. stdef->value = ( value == NULL )? NULL : hb_strdup( value );
  297. return stdef;
  298. }
  299. static int ParseUndef( char * sLine )
  300. {
  301. char defname[MAX_NAME];
  302. DEFINES* stdef;
  303. BOOL isNew;
  304. HB_TRACE(HB_TR_DEBUG, ("ParseUndef(%s)", sLine));
  305. NextWord( &sLine, defname, FALSE );
  306. if( ( stdef = DefSearch(defname, &isNew ) ) != NULL )
  307. {
  308. if( isNew )
  309. {
  310. if( stdef->pars ) hb_xfree( stdef->pars );
  311. if( stdef->value ) hb_xfree( stdef->value );
  312. hb_xfree( stdef->name );
  313. }
  314. stdef->name = NULL;
  315. }
  316. return 0;
  317. }
  318. static int ParseIfdef( char * sLine, int usl )
  319. {
  320. char defname[ MAX_NAME ];
  321. DEFINES * stdef;
  322. HB_TRACE(HB_TR_DEBUG, ("ParseIfdef(%s, %d)", sLine, usl));
  323. if( hb_pp_nCondCompile==0 || hb_pp_aCondCompile[hb_pp_nCondCompile-1])
  324. {
  325. NextWord( &sLine, defname, FALSE );
  326. if( *defname == '\0' )
  327. hb_compGenError( hb_pp_szErrors, 'F', ERR_DEFINE_ABSENT, NULL, NULL );
  328. }
  329. if( hb_pp_nCondCompile == s_maxCondCompile )
  330. {
  331. s_maxCondCompile += 5;
  332. hb_pp_aCondCompile = (int*)hb_xrealloc( hb_pp_aCondCompile, sizeof( int ) * s_maxCondCompile );
  333. }
  334. if( hb_pp_nCondCompile==0 || hb_pp_aCondCompile[hb_pp_nCondCompile-1])
  335. {
  336. if( ( (stdef = DefSearch(defname,NULL)) != NULL && usl )
  337. || ( stdef == NULL && !usl ) ) hb_pp_aCondCompile[hb_pp_nCondCompile] = 1;
  338. else hb_pp_aCondCompile[hb_pp_nCondCompile] = 0;
  339. }
  340. else
  341. hb_pp_aCondCompile[ hb_pp_nCondCompile ] = 0;
  342. hb_pp_nCondCompile++;
  343. return 0;
  344. }
  345. static DEFINES * DefSearch( char * defname, BOOL * isNew )
  346. {
  347. int kol = 0,j;
  348. DEFINES * stdef = hb_pp_topDefine;
  349. HB_TRACE(HB_TR_DEBUG, ("DefSearch(%s)", defname));
  350. while( stdef != NULL )
  351. {
  352. kol++;
  353. if( stdef->name != NULL )
  354. {
  355. for( j=0; *(stdef->name+j) == *(defname+j) &&
  356. *(stdef->name+j) != '\0'; j++ );
  357. if( *(stdef->name+j) == *(defname+j) )
  358. {
  359. if( isNew ) *isNew = ( s_kolAddDefs >= kol );
  360. return stdef;
  361. }
  362. }
  363. stdef = stdef->last;
  364. }
  365. return NULL;
  366. }
  367. static COMMANDS * ComSearch( char * cmdname, COMMANDS * stcmdStart )
  368. {
  369. COMMANDS * stcmd = ( stcmdStart ) ? stcmdStart : hb_pp_topCommand;
  370. HB_TRACE(HB_TR_DEBUG, ("ComSearch(%s, %p)", cmdname, stcmdStart));
  371. while( stcmd != NULL )
  372. {
  373. int j;
  374. for( j=0; (*(stcmd->name+j)==toupper(*(cmdname+j))) &&
  375. (*(stcmd->name+j)!='\0') &&
  376. ((stcmd->com_or_xcom)? 1:(j<4 || ISNAME(*(cmdname+j+1)))); j++ );
  377. if( (*(stcmd->name+j)==toupper(*(cmdname+j))) ||
  378. ( !stcmd->com_or_xcom && j >= 4 && *(stcmd->name+j)!='\0'
  379. && *(cmdname+j) == '\0' ) )
  380. break;
  381. stcmd = stcmd->last;
  382. }
  383. return stcmd;
  384. }
  385. static COMMANDS * TraSearch( char * cmdname, COMMANDS * sttraStart )
  386. {
  387. int j;
  388. COMMANDS *sttra = ( sttraStart ) ? sttraStart : hb_pp_topTranslate;
  389. HB_TRACE(HB_TR_DEBUG, ("TraSearch(%s, %p)", cmdname, sttraStart));
  390. while( sttra != NULL )
  391. {
  392. for( j=0; *(sttra->name+j)==toupper(*(cmdname+j)) &&
  393. *(sttra->name+j)!='\0' &&
  394. ((sttra->com_or_xcom)? 1:(j<4 || ISNAME(*(cmdname+j+1)))); j++ );
  395. if( *(sttra->name+j)==toupper(*(cmdname+j)) ||
  396. ( !sttra->com_or_xcom && j >= 4 &&
  397. *(sttra->name+j)!='\0' && *(cmdname+j) == '\0' ) )
  398. break;
  399. sttra = sttra->last;
  400. }
  401. return sttra;
  402. }
  403. static void ParseCommand( char * sLine, BOOL com_or_xcom, BOOL com_or_tra )
  404. {
  405. static char mpatt[ PATTERN_SIZE ];
  406. static char rpatt[ PATTERN_SIZE ];
  407. char cmdname[ MAX_NAME ];
  408. COMMANDS * stcmd;
  409. int mlen,rlen;
  410. int ipos;
  411. HB_TRACE(HB_TR_DEBUG, ("ParseCommand(%s, $d, $d)", sLine, com_or_xcom, com_or_tra));
  412. NextWord( &sLine, cmdname, FALSE );
  413. hb_strupr( cmdname );
  414. HB_SKIPTABSPACES(sLine);
  415. if( (ipos = hb_strAt( "=>", 2, sLine, strlen(sLine) )) > 0 )
  416. {
  417. stroncpy( mpatt, sLine, ipos-1 );
  418. RemoveSlash( mpatt );
  419. mlen = strotrim( mpatt );
  420. sLine += ipos + 1;
  421. HB_SKIPTABSPACES(sLine);
  422. hb_pp_strocpy( rpatt, sLine );
  423. rlen = strotrim( rpatt );
  424. ConvertPatterns( mpatt, mlen, rpatt, rlen );
  425. if( com_or_tra )
  426. stcmd = AddCommand( cmdname );
  427. else
  428. stcmd = AddTranslate( cmdname );
  429. stcmd->com_or_xcom = com_or_xcom;
  430. stcmd->mpatt = hb_strdup( mpatt );
  431. stcmd->value = ( rlen > 0 ) ? hb_strdup( rpatt ) : NULL;
  432. }
  433. else
  434. hb_compGenError( hb_pp_szErrors, 'F', ERR_COMMAND_DEFINITION, NULL, NULL );
  435. }
  436. /* ConvertPatterns()
  437. * Converts result pattern in #command and #translate to inner format
  438. */
  439. static void ConvertPatterns( char * mpatt, int mlen, char * rpatt, int rlen )
  440. {
  441. int i = 0, ipos, ifou;
  442. int explen, rmlen;
  443. char exppatt[ MAX_NAME ], expreal[ 5 ] = "\1 0";
  444. char lastchar = '@', exptype;
  445. char * ptr;
  446. HB_TRACE(HB_TR_DEBUG, ("ConvertPatterns(%s, $d, %s, $d)", mpatt, mlen, rpatt, rlen));
  447. while( *(mpatt+i) != '\0' )
  448. {
  449. if( *(mpatt+i) == '<' )
  450. { /* Drag match marker, determine it type */
  451. explen = 0; ipos = i; i++; exptype = '0';
  452. while( *(mpatt+i) == ' ' || *(mpatt+i) == '\t' ) i++;
  453. if( *(mpatt+i) == '*' ) /* Wild match marker */
  454. { exptype = '3'; i++; }
  455. else if( *(mpatt+i) == '(' ) /* Extended expression match marker */
  456. { exptype = '4'; i++; }
  457. while( *(mpatt+i) != '>' )
  458. {
  459. if( *(mpatt+i) == ',' ) /* List match marker */
  460. {
  461. exptype = '1';
  462. while( *(mpatt+i) != '>' ) i++;
  463. break;
  464. }
  465. else if( *(mpatt+i) == ':' ) /* Restricted match marker */
  466. {
  467. exptype = '2';
  468. *(mpatt+i--) = ' ';
  469. break;
  470. }
  471. if( *(mpatt+i) != ' ' && *(mpatt+i) != '\t' )
  472. *(exppatt+explen++) = *(mpatt+i);
  473. i++;
  474. }
  475. if( exptype == '3' )
  476. {
  477. if( *(exppatt+explen-1) == '*' ) explen--;
  478. else
  479. hb_compGenError( hb_pp_szErrors, 'F', ERR_PATTERN_DEFINITION, NULL, NULL );
  480. }
  481. else if( exptype == '4' )
  482. {
  483. if( *(exppatt+explen-1) == ')' ) explen--;
  484. else
  485. hb_compGenError( hb_pp_szErrors, 'F', ERR_PATTERN_DEFINITION, NULL, NULL );
  486. }
  487. rmlen = i - ipos + 1;
  488. /* Convert match marker into inner format */
  489. lastchar = (lastchar!='Z') ? ( (char) ( (unsigned int)lastchar + 1 ) ):
  490. 'a';
  491. expreal[1] = lastchar;
  492. expreal[2] = exptype;
  493. hb_pp_Stuff( expreal, mpatt+ipos, 4, rmlen, mlen );
  494. mlen += 4 - rmlen;
  495. i += 4 - rmlen;
  496. /* Look for appropriate result markers */
  497. ptr = rpatt;
  498. while( (ifou = hb_strAt( exppatt, explen, ptr, rlen-(ptr-rpatt) )) > 0 )
  499. {
  500. /* Convert result marker into inner format */
  501. ptr += ifou;
  502. if( *(ptr-2) == '<' && *(ptr+explen-1) == '>' &&
  503. *(ptr-3) != '\\' && *(ptr+explen-2) != '\\' ) /* <...> */
  504. {
  505. if( *(ptr-3) == '#' && *(ptr-4) != '\\' ) /* #<...> */
  506. { exptype = '1'; ptr -= 3; rmlen = explen+3; }
  507. else
  508. { exptype = '0'; ptr -= 2; rmlen = explen+2; }
  509. }
  510. else if( *(ptr-3) == '<' && *(ptr+explen) == '>' &&
  511. *(ptr-4) != '\\' && *(ptr+explen-1) != '\\' ) /* < ... > */
  512. {
  513. ptr -= 2;
  514. if( *ptr == '\"' ) exptype = '2';
  515. else if( *ptr == '(' ) exptype = '3';
  516. else if( *ptr == '{' ) exptype = '4';
  517. else if( *ptr == '.' ) exptype = '5';
  518. ptr--;
  519. rmlen = explen+4;
  520. }
  521. else continue;
  522. expreal[2] = exptype;
  523. hb_pp_Stuff( expreal, ptr, 4, rmlen, rlen );
  524. rlen += 4 - rmlen;
  525. }
  526. }
  527. i++;
  528. }
  529. }
  530. static COMMANDS * AddCommand( char * cmdname )
  531. {
  532. COMMANDS * stcmd;
  533. HB_TRACE(HB_TR_DEBUG, ("AddCommand(%s)", cmdname));
  534. stcmd = ( COMMANDS * ) hb_xgrab( sizeof( COMMANDS ) );
  535. stcmd->last = hb_pp_topCommand;
  536. hb_pp_topCommand = stcmd;
  537. stcmd->name = hb_strdup( cmdname );
  538. return stcmd;
  539. }
  540. static COMMANDS* AddTranslate( char * traname )
  541. {
  542. COMMANDS * sttra;
  543. HB_TRACE(HB_TR_DEBUG, ("AddTranslate(%s)", traname));
  544. sttra = ( COMMANDS * ) hb_xgrab( sizeof( COMMANDS ) );
  545. sttra->last = hb_pp_topTranslate;
  546. hb_pp_topTranslate = sttra;
  547. sttra->name = hb_strdup( traname );
  548. return sttra;
  549. }
  550. int hb_pp_ParseExpression( char * sLine, char * sOutLine )
  551. {
  552. char sToken[MAX_NAME];
  553. char * ptri, * ptro, * ptrb;
  554. int lenToken, i, ipos, isdvig, lens;
  555. int ifou;
  556. int rezDef, rezTra, rezCom, kolpass = 0;
  557. DEFINES * stdef;
  558. COMMANDS * stcmd;
  559. HB_TRACE(HB_TR_DEBUG, ("hb_pp_ParseExpression(%s, %s)", sLine, sOutLine));
  560. do
  561. {
  562. strotrim( sLine );
  563. rezDef = 0; rezTra = 0; rezCom = 0;
  564. isdvig = 0;
  565. do
  566. {
  567. ptro = sOutLine;
  568. ptri = sLine + isdvig;
  569. ipos = md_strAt( ";", 1, ptri, TRUE, FALSE );
  570. if( ipos > 0 ) *(ptri+ipos-1) = '\0';
  571. HB_SKIPTABSPACES( ptri );
  572. if( *ptri == '#' )
  573. {
  574. hb_pp_ParseDirective( ptri+1 );
  575. if( ipos > 0 ) *( sLine + isdvig + ipos - 1 ) = ';';
  576. lens = strlen( sLine+isdvig );
  577. hb_pp_Stuff( " ", sLine+isdvig, 0, (ipos)? ipos:lens, lens );
  578. if( ipos > 0 ) ipos = 1;
  579. }
  580. else
  581. { /* Look for macros from #define */
  582. while( ( lenToken = NextName( &ptri, sToken ) ) > 0 )
  583. if( (stdef=DefSearch(sToken,NULL)) != NULL )
  584. {
  585. ptrb = ptri - lenToken;
  586. if( ( i = WorkDefine( &ptri, ptro, stdef ) ) >= 0 )
  587. {
  588. rezDef++;
  589. lens = strlen( ptrb );
  590. if( ipos > 0 )
  591. {
  592. *(ptrb+lens) = ';';
  593. lens += strlen( ptrb+lens+1 );
  594. }
  595. hb_pp_Stuff( ptro, ptrb, i, ptri-ptrb, lens+1 );
  596. if( ipos > 0 )
  597. {
  598. ipos += i - (ptri-ptrb);
  599. *(sLine + isdvig + ipos - 1) = '\0';
  600. }
  601. ptri += i - (ptri-ptrb);
  602. }
  603. }
  604. /* Look for definitions from #translate */
  605. stcmd = hb_pp_topTranslate;
  606. while( stcmd != NULL )
  607. {
  608. ptri = sLine + isdvig;
  609. lenToken = strlen(stcmd->name);
  610. while( ( ifou = md_strAt( stcmd->name, lenToken,
  611. ptri, TRUE, FALSE )) > 0 )
  612. {
  613. ptri += ifou -1;
  614. if( (i = WorkTranslate( ptri+lenToken, ptro, stcmd, &lens )) >= 0 )
  615. {
  616. lens += lenToken;
  617. while( lens > 0 &&
  618. (*(ptri+lens-1)==' ' || *(ptri+lens-1)=='\t') )
  619. lens--;
  620. if( ipos > 0 ) *(sLine+isdvig+ipos-1) = ';';
  621. hb_pp_Stuff( ptro, ptri, i, lens, strlen(ptri) );
  622. rezTra = 1;
  623. if( ipos > 0 )
  624. {
  625. ipos += i - lens;
  626. *(sLine+isdvig+ipos-1) = '\0';
  627. }
  628. ptri += i;
  629. }
  630. else
  631. ptri += lenToken;
  632. }
  633. stcmd = stcmd->last;
  634. }
  635. /* Look for definitions from #command */
  636. if( kolpass < 3 )
  637. {
  638. ptri = sLine + isdvig;
  639. HB_SKIPTABSPACES( ptri );
  640. if( ISNAME( *ptri ) )
  641. NextName( &ptri, sToken );
  642. else
  643. {
  644. i = 0;
  645. while( *ptri != ' ' && *ptri != '\t' && *ptri != '\0' &&
  646. *ptri != '\"' && *ptri != '\'' && *ptri != '(' &&
  647. !ISNAME(*ptri) )
  648. {
  649. *(sToken+i) = *ptri++;
  650. i++;
  651. }
  652. *(sToken+i) = '\0';
  653. }
  654. HB_SKIPTABSPACES( ptri );
  655. if( ( *ptri == '\0' || ( *ptri != '=' &&
  656. (!IsInStr(*ptri,":/*+-") || *(ptri+1) != '=') &&
  657. ( *ptri != '-' || *(ptri+1) != '>' ) ) )
  658. && ( stcmd = ComSearch(sToken,NULL) ) != NULL )
  659. {
  660. ptro = sOutLine;
  661. i = WorkCommand( ptri, ptro, stcmd );
  662. ptri = sLine + isdvig;
  663. if( ipos > 0 ) *(ptri+ipos-1) = ';';
  664. if( i >= 0 )
  665. {
  666. if( isdvig + ipos > 0 )
  667. {
  668. lens = strlen( sLine+isdvig );
  669. hb_pp_Stuff( ptro, sLine+isdvig, i, (ipos)? ipos-1:lens, lens );
  670. if( ipos > 0 ) ipos = i + 1;
  671. }
  672. else
  673. memcpy( sLine, sOutLine, i+1);
  674. }
  675. rezCom = 1;
  676. }
  677. else if( ipos > 0 ) *(sLine+isdvig+ipos-1) = ';';
  678. }
  679. else if( ipos > 0 )
  680. *(sLine+isdvig+ipos-1) = ';';
  681. }
  682. isdvig += ipos;
  683. }
  684. while( ipos != 0 );
  685. kolpass++;
  686. if( kolpass > 20 && rezDef )
  687. {
  688. hb_compGenError( hb_pp_szErrors, 'F', ERR_RECURSE, NULL, NULL );
  689. break;
  690. }
  691. }
  692. while( rezDef || rezTra || rezCom );
  693. return 0;
  694. }
  695. static int WorkDefine( char ** ptri, char * ptro, DEFINES * stdef )
  696. {
  697. int npars, lens;
  698. char * ptr;
  699. HB_TRACE(HB_TR_DEBUG, ("WorkDefine(%p, %s, %p)", ptri, ptro, stdef));
  700. if( stdef->npars < 0 )
  701. lens = hb_pp_strocpy( ptro,stdef->value );
  702. else
  703. {
  704. HB_SKIPTABSPACES( *ptri );
  705. if( **ptri == '(' )
  706. {
  707. npars = 0; ptr = *ptri;
  708. do
  709. {
  710. ptr++;
  711. if( NextParm( &ptr, NULL ) > 0 ) npars++;
  712. }
  713. while( *ptr != ')' && *ptr != '\0' );
  714. if( *ptr == ')' && stdef->npars == npars )
  715. lens = WorkPseudoF( ptri, ptro, stdef );
  716. else return -1;
  717. }
  718. else return -1;
  719. }
  720. return lens;
  721. }
  722. static int WorkPseudoF( char ** ptri, char * ptro, DEFINES * stdef )
  723. {
  724. char parfict[ MAX_NAME ], * ptrreal;
  725. char * ptrb;
  726. int ipos, ifou, ibeg;
  727. int lenfict, lenreal, lenres;
  728. HB_TRACE(HB_TR_DEBUG, ("WorkPseudoF(%p, %s, %p)", ptri, ptro, stdef));
  729. lenres = hb_pp_strocpy( ptro, stdef->value ); /* Copying value of macro to destination string */
  730. if( stdef->pars )
  731. {
  732. ipos = 0; ibeg = 0;
  733. do /* Parsing through parameters */
  734. { /* in macro definition */
  735. if( *(stdef->pars+ipos) == ',' || *(stdef->pars+ipos) == '\0' )
  736. {
  737. *(parfict+ipos-ibeg) = '\0';
  738. lenfict = ipos - ibeg;
  739. if( **ptri != ')' )
  740. {
  741. (*ptri)++; /* Get next real parameter */
  742. HB_SKIPTABSPACES( *ptri );
  743. ptrreal = *ptri;
  744. lenreal = NextParm( ptri, NULL);
  745. ptrb = ptro;
  746. while( (ifou = hb_strAt( parfict, lenfict, ptrb, lenres-(ptrb-ptro) )) > 0 )
  747. {
  748. ptrb = ptrb+ifou-1;
  749. if( !ISNAME(*(ptrb-1)) && !ISNAME(*(ptrb+lenfict)) )
  750. {
  751. hb_pp_Stuff( ptrreal, ptrb, lenreal, lenfict, lenres );
  752. lenres += lenreal - lenfict;
  753. ptrb += lenreal;
  754. }
  755. else ptrb++;
  756. }
  757. ibeg = ipos+1;
  758. }
  759. }
  760. else *(parfict+ipos-ibeg) = *(stdef->pars+ipos);
  761. if( *(stdef->pars+ipos) == '\0' ) break;
  762. ipos++;
  763. }
  764. while( 1 );
  765. }
  766. else while( **ptri != ')' ) (*ptri)++;
  767. (*ptri)++;
  768. return lenres;
  769. }
  770. static int WorkCommand( char * ptri, char * ptro, COMMANDS * stcmd )
  771. {
  772. int rez;
  773. int lenres;
  774. char * ptrmp;
  775. char * sToken = stcmd->name;
  776. HB_TRACE(HB_TR_DEBUG, ("WorkCommand(%s, %s, %p)", ptri, ptro, stcmd));
  777. do
  778. {
  779. lenres = hb_pp_strocpy( ptro, stcmd->value ); /* Copying result pattern */
  780. ptrmp = stcmd->mpatt; /* Pointer to a match pattern */
  781. s_Repeate = 0;
  782. s_groupchar = '@';
  783. rez = CommandStuff( ptrmp, ptri, ptro, &lenres, TRUE, stcmd->com_or_xcom );
  784. stcmd = stcmd->last;
  785. if( rez < 0 && stcmd != NULL ) stcmd = ComSearch(sToken, stcmd);
  786. }
  787. while( rez < 0 && stcmd != NULL );
  788. *(ptro+lenres) = '\0';
  789. if( rez >= 0 ) return lenres;
  790. return -1;
  791. }
  792. static int WorkTranslate( char * ptri, char * ptro, COMMANDS * sttra, int * lens )
  793. {
  794. int rez;
  795. int lenres;
  796. char * ptrmp;
  797. char * sToken = sttra->name;
  798. HB_TRACE(HB_TR_DEBUG, ("WorkTranslate(%s, %s, %p, %p)", ptri, ptro, sttra, lens));
  799. do
  800. {
  801. lenres = hb_pp_strocpy( ptro, sttra->value );
  802. ptrmp = sttra->mpatt;
  803. s_Repeate = 0;
  804. s_groupchar = '@';
  805. rez = CommandStuff( ptrmp, ptri, ptro, &lenres, FALSE, sttra->com_or_xcom );
  806. sttra = sttra->last;
  807. if( rez < 0 && sttra != NULL ) sttra = TraSearch(sToken, sttra);
  808. }
  809. while( rez < 0 && sttra != NULL );
  810. *(ptro+lenres) = '\0';
  811. if( rez >= 0 )
  812. {
  813. *lens = rez;
  814. return lenres;
  815. }
  816. return -1;
  817. }
  818. static int CommandStuff( char * ptrmp, char * inputLine, char * ptro, int * lenres, BOOL com_or_tra, BOOL com_or_xcom )
  819. {
  820. BOOL endTranslation = FALSE;
  821. int ipos;
  822. char * lastopti[ 3 ], * strtopti = NULL, * strtptri = NULL;
  823. char * ptri = inputLine, * ptr, tmpname[ MAX_NAME ];
  824. HB_TRACE(HB_TR_DEBUG, ("CommandStuff(%s, %s, %s, %p, %d, %d)", ptrmp, inputLine, ptro, lenres, com_or_tra, com_or_xcom));
  825. s_numBrackets = 0;
  826. HB_SKIPTABSPACES( ptri );
  827. if( ptrmp == NULL ) { if( *ptri != '\0' ) return -1; }
  828. else
  829. while( *ptri != '\0' && !endTranslation )
  830. {
  831. HB_SKIPTABSPACES( ptrmp );
  832. if( *ptrmp == '[' && !s_numBrackets && !strtopti )
  833. strtopti = ptrmp;
  834. if( !s_numBrackets && strtopti && strtptri != ptri &&
  835. ( ISNAME( *ptri ) || *ptri=='&' ) )
  836. {
  837. strtptri = ptri;
  838. ptrmp = strtopti;
  839. ptr = ptri;
  840. ipos = NextName( &ptr, tmpname );
  841. ipos = md_strAt( tmpname, ipos, strtopti, TRUE, TRUE );
  842. if( ipos && TestOptional( strtopti, strtopti+ipos-2 ) )
  843. {
  844. ptr = strtopti+ipos-2;
  845. ptr = PrevSquare( ptr, strtopti, NULL );
  846. if( ptr )
  847. ptrmp = ptr;
  848. }
  849. }
  850. switch( *ptrmp ) {
  851. case '[':
  852. s_numBrackets++;
  853. s_aIsRepeate[ s_Repeate ] = 0;
  854. lastopti[s_Repeate++] = ptrmp;
  855. ptrmp++;
  856. if( !CheckOptional( ptrmp, ptri, ptro, lenres, com_or_tra, com_or_xcom ) )
  857. SkipOptional( &ptrmp );
  858. break;
  859. case ']':
  860. if( s_Repeate )
  861. {
  862. s_Repeate--;
  863. if( s_aIsRepeate[ s_Repeate ] )
  864. {
  865. if( ISNAME(*ptri) )
  866. {
  867. ptr = ptri;
  868. ipos = NextName( &ptr, tmpname );
  869. ipos = md_strAt( tmpname, ipos, ptrmp, TRUE, TRUE );
  870. if( ipos && TestOptional( ptrmp+1, ptrmp+ipos-2 ) )
  871. {
  872. ptr = PrevSquare( ptrmp+ipos-2, ptrmp+1, NULL );
  873. if( !ptr || CheckOptional( ptrmp+1, ptri, ptro, lenres, com_or_tra, com_or_xcom ) )
  874. {
  875. ptrmp = lastopti[s_Repeate];
  876. ptrmp++;
  877. s_Repeate++;
  878. SkipOptional( &ptrmp );
  879. s_numBrackets++;
  880. ptrmp++;
  881. strtptri = ptri;
  882. }
  883. else
  884. ptrmp = lastopti[s_Repeate];
  885. }
  886. else
  887. ptrmp = lastopti[s_Repeate];
  888. }
  889. else
  890. ptrmp = lastopti[s_Repeate];
  891. }
  892. else ptrmp++;
  893. s_numBrackets--;
  894. }
  895. else { s_numBrackets--; ptrmp++; }
  896. break;
  897. case ',':
  898. if( !s_numBrackets ) strtopti = NULL;
  899. if( *ptri == ',' ) { ptrmp++; ptri++; }
  900. else
  901. {
  902. if( s_numBrackets )
  903. {
  904. SkipOptional( &ptrmp );
  905. }
  906. else return -1;
  907. }
  908. break;
  909. case '\1': /* Match marker */
  910. if( !s_numBrackets ) strtopti = NULL;
  911. if( !WorkMarkers( &ptrmp, &ptri, ptro, lenres, com_or_xcom ) )
  912. {
  913. if( s_numBrackets )
  914. {
  915. SkipOptional( &ptrmp );
  916. }
  917. else return -1;
  918. }
  919. break;
  920. case '\0':
  921. if( com_or_tra )
  922. return -1;
  923. else endTranslation = TRUE;
  924. break;
  925. default: /* Key word */
  926. if( !s_numBrackets ) strtopti = NULL;
  927. ptr = ptri;
  928. if( *ptri == ',' || truncmp( &ptri, &ptrmp, !com_or_xcom ) )
  929. {
  930. ptri = ptr;
  931. if( s_numBrackets )
  932. {
  933. SkipOptional( &ptrmp );
  934. }
  935. else return -1;
  936. }
  937. }
  938. HB_SKIPTABSPACES( ptri );
  939. };
  940. if( *ptrmp != '\0' )
  941. {
  942. if( s_Repeate ) { s_Repeate = 0; ptrmp = lastopti[0]; }
  943. s_numBrackets = 0;
  944. do
  945. {
  946. HB_SKIPTABSPACES( ptrmp );
  947. if( *ptrmp != '\0' )
  948. switch( *ptrmp ) {
  949. case '[':
  950. ptrmp++;
  951. SkipOptional( &ptrmp );
  952. ptrmp++;
  953. break;
  954. case ']': ptrmp++; break;
  955. default:
  956. return -1;
  957. }
  958. }
  959. while( *ptrmp != '\0' );
  960. }
  961. SearnRep( "\1","",0,ptro,lenres);
  962. *(ptro + *lenres) = '\0';
  963. *lenres = RemoveSlash( ptro ); /* Removing '\' from result string */
  964. if( com_or_tra ) return 1; else return (ptri-inputLine);
  965. }
  966. static int RemoveSlash( char * stroka )
  967. {
  968. char *ptr = stroka;
  969. int State = STATE_INIT;
  970. BOOL bDirective = FALSE;
  971. int lenres = strlen( stroka );
  972. HB_TRACE(HB_TR_DEBUG, ("RemoveSlash(%s)", stroka));
  973. while( *ptr != '\0' )
  974. {
  975. switch( State ) {
  976. case STATE_INIT:
  977. if( *ptr != ' ' && *ptr != '\t' ) State = STATE_NORMAL;
  978. if( *ptr == '#' ) bDirective = TRUE;
  979. case STATE_NORMAL:
  980. if( *ptr == '\'' ) State = STATE_QUOTE1;
  981. else if( *ptr == '\"' ) State = STATE_QUOTE2;
  982. else if( *ptr == ';' )
  983. {
  984. State = STATE_INIT;
  985. bDirective = FALSE;
  986. }
  987. else if( !bDirective )
  988. {
  989. if( *ptr == '\\' && ( *(ptr+1) == '[' || *(ptr+1) == ']' ||
  990. *(ptr+1) == '{' || *(ptr+1) == '}' || *(ptr+1) == '<' ||
  991. *(ptr+1) == '>' || *(ptr+1) == '\'' || *(ptr+1) == '\"' ) )
  992. {
  993. hb_pp_Stuff( "", ptr, 0, 1, lenres - (ptr - stroka) );
  994. lenres--;
  995. ptr++;
  996. }
  997. }
  998. break;
  999. case STATE_QUOTE1:
  1000. if( *ptr == '\'' ) State = STATE_NORMAL;
  1001. break;
  1002. case STATE_QUOTE2:
  1003. if( *ptr == '\"' ) State = STATE_NORMAL;
  1004. break;
  1005. }
  1006. ptr++;
  1007. }
  1008. return lenres;
  1009. }
  1010. static int WorkMarkers( char ** ptrmp, char ** ptri, char * ptro, int * lenres, BOOL com_or_xcom )
  1011. {
  1012. static char expreal[ MAX_EXP ];
  1013. char exppatt[ MAX_NAME ];
  1014. int lenreal = 0, maxlenreal = HB_PP_STR_SIZE, lenpatt;
  1015. int rezrestr, ipos;
  1016. char * ptr, * ptrtemp;
  1017. HB_TRACE(HB_TR_DEBUG, ("WorkMarkers(%p, %p, %s, %p)", ptrmp, ptri, ptro, lenres));
  1018. /* Copying a match pattern to 'exppatt' */
  1019. lenpatt = stroncpy( exppatt, *ptrmp, 4 );
  1020. *ptrmp += 4;
  1021. HB_SKIPTABSPACES( *ptrmp );
  1022. if( **ptri == ',' )
  1023. {
  1024. if( s_numBrackets )
  1025. {
  1026. return 0;
  1027. }
  1028. }
  1029. ptrtemp = *ptrmp;
  1030. if( *(exppatt+2) != '2' && *ptrtemp == ']' )
  1031. {
  1032. ptrtemp++;
  1033. HB_SKIPTABSPACES( ptrtemp );
  1034. }
  1035. if( *(exppatt+2) != '2' && *ptrtemp != '\1' && *ptrtemp != ',' &&
  1036. *ptrtemp != '[' && *ptrtemp != ']' && *ptrtemp != '\0' )
  1037. {
  1038. lenreal = strincpy( expreal, ptrtemp );
  1039. if( (ipos = md_strAt( expreal, lenreal, *ptri, TRUE, TRUE )) > 0 )
  1040. {
  1041. if( ptrtemp > *ptrmp )
  1042. {
  1043. if( ipos == 1 )
  1044. {
  1045. if( s_numBrackets )
  1046. {
  1047. return 0;
  1048. }
  1049. }
  1050. else
  1051. {
  1052. maxlenreal = ipos - 1;
  1053. lenreal = 0;
  1054. }
  1055. }
  1056. else
  1057. {
  1058. lenreal = stroncpy( expreal, *ptri, ipos-1 );
  1059. if( ipos > 1 && isExpres( expreal ) )
  1060. *ptri += lenreal;
  1061. else
  1062. {
  1063. if( s_numBrackets )
  1064. {
  1065. return 0;
  1066. }
  1067. else lenreal = 0;
  1068. }
  1069. }
  1070. }
  1071. else
  1072. {
  1073. if( s_numBrackets )
  1074. {
  1075. return 0;
  1076. }
  1077. else lenreal = 0;
  1078. }
  1079. }
  1080. if( *(exppatt+2) == '4' ) /* ---- extended match marker */
  1081. {
  1082. if( !lenreal ) lenreal = getExpReal( expreal, ptri, FALSE, maxlenreal );
  1083. SearnRep( exppatt,expreal,lenreal,ptro,lenres);
  1084. }
  1085. else if( *(exppatt+2) == '3' ) /* ---- wild match marker */
  1086. {
  1087. lenreal = hb_pp_strocpy( expreal, *ptri );
  1088. *ptri += lenreal;
  1089. SearnRep( exppatt,expreal,lenreal,ptro,lenres);
  1090. }
  1091. else if( *(exppatt+2) == '2' ) /* ---- restricted match marker */
  1092. {
  1093. while( **ptrmp != '>' ) *(exppatt+lenpatt++) = *((*ptrmp)++);
  1094. *(exppatt+lenpatt) = '\0';
  1095. (*ptrmp)++;
  1096. ptr = exppatt + 4;
  1097. rezrestr = 0;
  1098. while( *ptr != '\0' )
  1099. {
  1100. if( *ptr == '&' )
  1101. {
  1102. if( **ptri == '&' )
  1103. {
  1104. rezrestr = 1;
  1105. /* (*ptri)++; */
  1106. lenreal = getExpReal( expreal, ptri, FALSE, maxlenreal );
  1107. SearnRep( exppatt,expreal,lenreal,ptro,lenres);
  1108. break;
  1109. }
  1110. else ptr++;
  1111. }
  1112. else
  1113. {
  1114. HB_SKIPTABSPACES( ptr );
  1115. /* Comparing real parameter and restriction value */
  1116. ptrtemp = ptr;
  1117. if( !strincmp( *ptri, &ptr, !com_or_xcom ) )
  1118. {
  1119. lenreal = stroncpy( expreal, *ptri, (ptr-ptrtemp) );
  1120. *ptri += lenreal;
  1121. SearnRep( exppatt,expreal,lenreal,ptro,lenres);
  1122. rezrestr = 1;
  1123. break;
  1124. }
  1125. else
  1126. {
  1127. while( *ptr != ',' && *ptr != '\0' ) ptr++;
  1128. if( *ptr == ',' ) ptr++;
  1129. }
  1130. }
  1131. }
  1132. if( rezrestr == 0 )
  1133. { /* If restricted match marker doesn't correspond to real parameter */
  1134. if( s_numBrackets )
  1135. {
  1136. return 0;
  1137. }
  1138. else return 0;
  1139. }
  1140. }
  1141. else if( *(exppatt+2) == '1' ) /* ---- list match marker */
  1142. {
  1143. if( !lenreal ) lenreal = getExpReal( expreal, ptri, TRUE, maxlenreal );
  1144. SearnRep( exppatt,expreal,lenreal,ptro,lenres);
  1145. }
  1146. else /* ---- regular match marker */
  1147. {
  1148. /* Copying a real expression to 'expreal' */
  1149. if( !lenreal ) lenreal = getExpReal( expreal, ptri, FALSE, maxlenreal );
  1150. SearnRep( exppatt,expreal,lenreal,ptro,lenres);
  1151. }
  1152. return 1;
  1153. }
  1154. static int getExpReal( char * expreal, char ** ptri, BOOL prlist, int maxrez )
  1155. {
  1156. int lens = 0;
  1157. char * sZnaki = "+-=><*/$.:#%!^";
  1158. int State;
  1159. int StBr1 = 0, StBr2 = 0, StBr3 = 0;
  1160. BOOL rez = FALSE;
  1161. HB_TRACE(HB_TR_DEBUG, ("getExpReal(%s, %p, %d, %d)", expreal, ptri, prlist, maxrez));
  1162. HB_SKIPTABSPACES( *ptri );
  1163. State = (**ptri=='\'' || **ptri=='\"')? STATE_EXPRES:STATE_ID;
  1164. while( **ptri != '\0' && !rez && lens < maxrez )
  1165. {
  1166. switch( State ) {
  1167. case STATE_QUOTE1:
  1168. if(**ptri=='\'')
  1169. State = (StBr1==0 && StBr2==0 && StBr3==0)? STATE_ID_END:STATE_BRACKET;
  1170. break;
  1171. case STATE_QUOTE2:
  1172. if(**ptri=='\"')
  1173. State = (StBr1==0 && StBr2==0 && StBr3==0)? STATE_ID_END:STATE_BRACKET;
  1174. break;
  1175. case STATE_BRACKET:
  1176. if( **ptri == '\'' ) State = STATE_QUOTE1;
  1177. else if( **ptri == '\"' ) State = STATE_QUOTE2;
  1178. else if( **ptri == '(' ) StBr1++;
  1179. else if( **ptri == '[' ) StBr2++;
  1180. else if( **ptri == '{' ) StBr3++;
  1181. else if( **ptri == ')' )
  1182. { StBr1--; if (StBr1==0 && StBr2==0 && StBr3==0) State = STATE_ID_END; }
  1183. else if( **ptri == ']' )
  1184. { StBr2--; if (StBr1==0 && StBr2==0 && StBr3==0) State = STATE_ID_END; }
  1185. else if( **ptri == '}' )
  1186. { StBr3--; if (StBr1==0 && StBr2==0 && StBr3==0) State = STATE_ID_END; }
  1187. break;
  1188. case STATE_ID:
  1189. case STATE_ID_END:
  1190. if( ( (ISNAME(**ptri) || **ptri=='\\' || **ptri=='&') && State == STATE_ID_END ) ||
  1191. **ptri==',' || **ptri=='\'' || **ptri=='\"' || **ptri==')' )
  1192. {
  1193. if( **ptri == ',' )
  1194. {
  1195. if( !prlist ) rez = TRUE;
  1196. State = STATE_EXPRES;
  1197. }
  1198. else rez = TRUE;
  1199. }
  1200. else if( IsInStr( **ptri, sZnaki ) )
  1201. {
  1202. State = STATE_EXPRES;
  1203. }
  1204. else if( **ptri == '(' )
  1205. {
  1206. State = STATE_BRACKET;
  1207. StBr1 = 1;
  1208. }
  1209. else if( **ptri == '[' )
  1210. {
  1211. State = STATE_BRACKET;
  1212. StBr2 = 1;
  1213. }
  1214. else if( **ptri == '{' )
  1215. {
  1216. State = STATE_BRACKET;
  1217. StBr3 = 1;
  1218. }
  1219. else if( **ptri == ' ' ) State = STATE_ID_END;
  1220. break;
  1221. case STATE_EXPRES:
  1222. case STATE_EXPRES_ID:
  1223. if( **ptri == '\'' ) State = STATE_QUOTE1;
  1224. else if( **ptri == '\"' ) State = STATE_QUOTE2;
  1225. else if( ISNAME(**ptri) ) State = STATE_EXPRES_ID;
  1226. else if( **ptri == ' ' )
  1227. {
  1228. if( State == STATE_EXPRES_ID ) State = STATE_ID_END;
  1229. else if( lens > 2 && ( ( *(*ptri-2)=='+' && *(*ptri-1)=='+' ) ||
  1230. ( *(*ptri-2)=='-' && *(*ptri-1)=='-' ) ) )
  1231. State = STATE_ID_END;
  1232. }
  1233. else if( **ptri == '(' ) { StBr1++; State = STATE_BRACKET; }
  1234. else if( **ptri == '[' ) { StBr2++; State = STATE_BRACKET; }
  1235. else if( **ptri == '{' ) { StBr3++; State = STATE_BRACKET; }
  1236. else if( **ptri == ',' ) { if ( !prlist ) rez = TRUE; State = STATE_EXPRES; }
  1237. else if( **ptri == '.' && *(*ptri-2) == '.' &&
  1238. ( *(*ptri-1) == 'T' || *(*ptri-1) == 'F' ||
  1239. *(*ptri-1) == 't' || *(*ptri-1) == 'f' ) )
  1240. State = STATE_ID_END;
  1241. else State = STATE_EXPRES;
  1242. break;
  1243. }
  1244. if( !rez )
  1245. {
  1246. if( expreal != NULL ) *expreal++ = **ptri;
  1247. (*ptri)++;
  1248. lens++;
  1249. }
  1250. }
  1251. if( expreal != NULL )
  1252. {
  1253. if( *(expreal-1) == ' ' ) { expreal--; lens--; };
  1254. *expreal = '\0';
  1255. }
  1256. return lens;
  1257. }
  1258. static BOOL isExpres( char * stroka )
  1259. {
  1260. int l1,l2;
  1261. HB_TRACE(HB_TR_DEBUG, ("isExpres(%s)", stroka));
  1262. l1 = strlen( stroka );
  1263. l2 = getExpReal( NULL, &stroka, FALSE, HB_PP_STR_SIZE );
  1264. return ( l1 <= l2 );
  1265. }
  1266. static BOOL TestOptional( char *ptr1, char *ptr2 )
  1267. {
  1268. int nbr = 0;
  1269. BOOL flagname = FALSE;
  1270. int statevar = 0;
  1271. HB_TRACE(HB_TR_DEBUG, ("TestOptional(%s, %s)", ptr1, ptr2));
  1272. while( ptr1 <= ptr2 )
  1273. {
  1274. if( *ptr1 == '[' ) nbr++;
  1275. else if( *ptr1 == ']' )
  1276. {
  1277. if( nbr )
  1278. {
  1279. nbr--;
  1280. flagname = FALSE;
  1281. }
  1282. else return 0;
  1283. }
  1284. else if( *ptr1 == '\1' && *(ptr1+2) == '2' && nbr ) statevar = 1;
  1285. else if( *ptr1 == '>' && statevar ) statevar = 0;
  1286. else if( *ptr1 != ' ' && *ptr1 != '\t' && !statevar )
  1287. {
  1288. if( nbr ) flagname = TRUE;
  1289. else return 0;
  1290. }
  1291. ptr1++;
  1292. }
  1293. /* if( !flagname )
  1294. while( *ptr1 != ']' )
  1295. {
  1296. if( *ptr1 == '[' || *ptr1 == '\0' ) return 0;
  1297. ptr1++;
  1298. } */
  1299. return !flagname;
  1300. }
  1301. static BOOL CheckOptional( char * ptrmp, char * ptri, char * ptro, int * lenres, BOOL com_or_tra, BOOL com_or_xcom )
  1302. {
  1303. int save_numBr = s_numBrackets, save_Repeate = s_Repeate;
  1304. BOOL endTranslation = FALSE;
  1305. BOOL bResult = TRUE;
  1306. char * lastInputptr[ 5 ];
  1307. char * lastopti[ 3 ], *ptr;
  1308. HB_SYMBOL_UNUSED( com_or_tra );
  1309. HB_TRACE(HB_TR_DEBUG, ("CheckOptional(%s, %s, %s, %p, %d, %d)", ptrmp, ptri, ptro, lenres, com_or_tra, com_or_xcom));
  1310. s_bReplacePat = FALSE;
  1311. lastInputptr[s_Repeate] = ptri;
  1312. while( *ptri != '\0' && !endTranslation && bResult )
  1313. {
  1314. HB_SKIPTABSPACES( ptrmp );
  1315. switch( *ptrmp ) {
  1316. case '[':
  1317. s_numBrackets++;
  1318. s_aIsRepeate[ s_Repeate ] = 0;
  1319. lastInputptr[s_Repeate] = ptri;
  1320. lastopti[s_Repeate++] = ptrmp;
  1321. ptrmp++;
  1322. break;
  1323. case ']':
  1324. if( s_numBrackets == save_numBr )
  1325. endTranslation = TRUE;
  1326. else
  1327. {
  1328. if( s_Repeate )
  1329. {
  1330. s_Repeate--;
  1331. ptrmp = lastopti[s_Repeate];
  1332. }
  1333. else ptrmp++;
  1334. s_numBrackets--;
  1335. }
  1336. break;
  1337. case ',':
  1338. if( *ptri == ',' ) { ptrmp++; ptri++; }
  1339. else
  1340. {
  1341. if( s_numBrackets - save_numBr > 0 )
  1342. {
  1343. SkipOptional( &ptrmp );
  1344. ptri = lastInputptr[s_Repeate];
  1345. }
  1346. else bResult = FALSE;
  1347. }
  1348. break;
  1349. case '\1': /* Match marker */
  1350. if( !WorkMarkers( &ptrmp, &ptri, ptro, lenres, com_or_xcom ) )
  1351. {
  1352. if( s_numBrackets - save_numBr > 0 )
  1353. {
  1354. SkipOptional( &ptrmp );
  1355. ptri = lastInputptr[s_Repeate];
  1356. }
  1357. else bResult = FALSE;
  1358. }
  1359. break;
  1360. case '\0':
  1361. bResult = FALSE;
  1362. default: /* Key word */
  1363. ptr = ptri;
  1364. if( *ptri == ',' || truncmp( &ptri, &ptrmp, !com_or_xcom ) )
  1365. {
  1366. ptri = ptr;
  1367. if( s_numBrackets - save_numBr > 0 )
  1368. {
  1369. SkipOptional( &ptrmp );
  1370. ptri = lastInputptr[s_Repeate];
  1371. }
  1372. else bResult = FALSE;
  1373. }
  1374. }
  1375. HB_SKIPTABSPACES( ptri );
  1376. };
  1377. if( *ptri == '\0' )
  1378. {
  1379. do
  1380. {
  1381. HB_SKIPTABSPACES( ptrmp );
  1382. if( *ptrmp == '[' )
  1383. {
  1384. ptrmp++;
  1385. SkipOptional( &ptrmp );
  1386. }
  1387. else if( *ptrmp == ']' )
  1388. break;
  1389. else
  1390. {
  1391. bResult = 0;
  1392. break;
  1393. }
  1394. }
  1395. while( 1 );
  1396. }
  1397. s_Repeate = save_Repeate;
  1398. s_numBrackets = save_numBr;
  1399. s_bReplacePat = TRUE;
  1400. return bResult;
  1401. }
  1402. static void SkipOptional( char ** ptri )
  1403. {
  1404. int nbr = 0;
  1405. HB_TRACE(HB_TR_DEBUG, ("SkipOptional(%p)", ptri));
  1406. while( **ptri != ']' || nbr )
  1407. {
  1408. switch( **ptri ) {
  1409. case '[': nbr++; break;
  1410. case ']': nbr--; break;
  1411. case '\1':
  1412. (*ptri) += 3;
  1413. if( *(*ptri-1) == '2' )
  1414. while( **ptri != '>' ) (*ptri)++;
  1415. break;
  1416. }
  1417. (*ptri)++;
  1418. }
  1419. if( **ptri == ']' && s_numBrackets > 0 )
  1420. {
  1421. if( s_Repeate ) s_Repeate--;
  1422. s_numBrackets--; (*ptri)++;
  1423. }
  1424. }
  1425. static void SearnRep( char * exppatt, char * expreal, int lenreal, char * ptro, int * lenres )
  1426. {
  1427. static char expnew[ MAX_EXP ];
  1428. int ifou, isdvig = 0;
  1429. BOOL rezs;
  1430. int kolmarkers;
  1431. int lennew, i;
  1432. char lastchar = '0';
  1433. char *ptr, *ptr2, *ptrOut = ptro;
  1434. HB_TRACE(HB_TR_DEBUG, ("SearnRep(%s, %s, %d, %s, %p)", exppatt, expreal, lenreal, ptro, lenres));
  1435. if( *(exppatt+1) == '\0' ) *( ptro + *lenres ) = '\0';
  1436. while( (ifou = md_strAt( exppatt, (*(exppatt+1))? 2:1, ptrOut, FALSE, FALSE )) > 0 )
  1437. {
  1438. rezs = FALSE;
  1439. ptr = ptrOut + ifou - 2;
  1440. kolmarkers = 0;
  1441. ptr = PrevSquare( ptr, ptrOut, &kolmarkers );
  1442. if( ptr )
  1443. {
  1444. if( s_Repeate ) s_aIsRepeate[ s_Repeate - 1 ]++;
  1445. if( !s_bReplacePat ) return;
  1446. ptr2 = ptrOut + ifou + 3;
  1447. while( *ptr2 != ']' || *(ptr2-1) == '\\' )
  1448. {

Large files files are truncated, but you can click here to view the full file