PageRenderTime 58ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/old-eval-stack/harbour/source/compiler/simplex.c

#
C | 1570 lines | 1248 code | 216 blank | 106 comment | 178 complexity | 9a73999419b382cf58a25c611458ec06 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
  1. /*
  2. * $Id: simplex.c 3518 2000-11-08 22:10:48Z ronpinkas $
  3. */
  4. /*
  5. * Copyright 2000 Ron Pinkas <ron@profit-master.com>
  6. * www - http://www.Profit-Master.com
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version, with one exception:
  12. *
  13. * The exception is that if you link this file with other files to produce
  14. * an executable, this does not by itself causes the resulting executable
  15. * to be covered by the GNU General Public License. Your use of that
  16. * executable is in no way restricted on account of linking this file.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA (or visit
  26. * their web site at http://www.gnu.org/).
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <limits.h>
  32. /* NOT overidable (yet). */
  33. #define MAX_MATCH 4
  34. #ifndef TOKEN_SIZE
  35. #define TOKEN_SIZE 64
  36. #endif
  37. /* Language Definitions Readability. */
  38. #define SELF_CONTAINED_WORDS_ARE LEX_WORD static aSelfs[] =
  39. #define LANGUAGE_KEY_WORDS_ARE LEX_WORD static aKeys[] =
  40. #define LANGUAGE_WORDS_ARE LEX_WORD static aWords[] =
  41. #define LANGUAGE_RULES_ARE static int aiRules[][ MAX_MATCH + 2 ] =
  42. #define ACCEPT_TOKEN_AND_DROP_DELIMITER_IF_ONE_OF_THESE(x) static char *szOmmit = x
  43. #define ACCEPT_TOKEN_AND_RETURN_DELIMITERS static LEX_DELIMITER aDelimiters[] =
  44. #define DELIMITER_BELONGS_TO_TOKEN_IF_ONE_OF_THESE(x) static char *szAppend = x
  45. #define START_NEW_LINE_IF_ONE_OF_THESE(x) static char *szNewLine = x
  46. #define IF_SEQUENCE_IS(a, b, c, d) {a, b, c, d
  47. #define REDUCE_TO(x, y) ,x, y }
  48. #define PASS_THROUGH() ,0, 0 }
  49. #define LEX_DELIMITER(x) {x
  50. #define LEX_WORD(x) {x
  51. #define AS_TOKEN(x) ,x }
  52. /* Streams ("Pairs"). */
  53. #define DEFINE_STREAM_AS_ONE_OF_THESE LEX_PAIR aPairs[] =
  54. #define START_WITH(x) { x,
  55. #define END_WITH(x) x,
  56. #define STOP_IF_ONE_OF_THESE(x) x,
  57. #define TEST_LEFT(x) x,
  58. #define AS_PAIR_TOKEN(x) x }
  59. #define STREAM_EXCEPTION( sPair, chrPair) \
  60. if( chrPair ) \
  61. { \
  62. printf( "Exception: %c for stream at: \"%s\"\n", chrPair, sPair ); \
  63. } \
  64. else \
  65. { \
  66. printf( "Exception: <EOF> for stream at: \"%s\"\n", chrPair, sPair ); \
  67. } \
  68. /* Pairs. */
  69. #ifndef MAX_STREAM
  70. #define MAX_STREAM 2048
  71. #endif
  72. #ifndef MAX_STREAM_STARTER
  73. #define MAX_STREAM_STARTER 2
  74. #endif
  75. #ifndef MAX_STREAM_TERMINATOR
  76. #define MAX_STREAM_TERMINATOR 2
  77. #endif
  78. #ifndef MAX_STREAM_EXCLUSIONS
  79. #define MAX_STREAM_EXCLUSIONS 2
  80. #endif
  81. static char sPair[ MAX_STREAM ];
  82. static char * sStart, * sTerm;
  83. static char * sExclude;
  84. static BOOL bTestLeft;
  85. static int iPairToken = 0;
  86. /* Self Contained Words. */
  87. static char sSelf[ TOKEN_SIZE ];
  88. typedef struct _LEX_DELIMITER
  89. {
  90. char cDelimiter;
  91. int iToken;
  92. } LEX_DELIMITER; /* support structure for KEYS and WORDS. */
  93. typedef struct _LEX_WORD
  94. {
  95. char sWord[ TOKEN_SIZE ];
  96. int iToken;
  97. } LEX_WORD; /* support structure for KEYS and WORDS. */
  98. typedef struct _LEX_PAIR
  99. {
  100. char sStart[MAX_STREAM_STARTER];
  101. char sTerm[MAX_STREAM_TERMINATOR];
  102. char sExclude[MAX_STREAM_EXCLUSIONS];
  103. BOOL bTestLeft;
  104. int iToken;
  105. } LEX_PAIR; /* support structure for Streams (Pairs). */
  106. #ifdef __cplusplus
  107. typedef struct yy_buffer_state *YY_BUFFER_STATE;
  108. #endif
  109. /* Above are NOT overidable !!! Need to precede the Language Definitions. */
  110. /* --------------------------------------------------------------------------------- */
  111. /* Overidables. */
  112. #define LEX_CUSTOM_ACTION -65
  113. #define DONT_REDUCE 1024
  114. #define YY_BUF_SIZE 16384
  115. #define YY_INPUT( a, b, c )
  116. /* Optional User Macros. */
  117. #define LEX_USER_SETUP()
  118. #define INIT_ACTION()
  119. #define INTERCEPT_ACTION(x)
  120. #define CUSTOM_ACTION(x)
  121. #define NEW_LINE_ACTION()
  122. #define ELEMENT_TOKEN(x,y) -1
  123. #define DEBUG_INFO(x)
  124. #define LEX_CASE(x)
  125. #define STREAM_OPEN(x)
  126. #define STREAM_APPEND(x) sPair[ iPairLen++ ] = x
  127. #define KEYWORD_ACTION()
  128. #define WORD_ACTION()
  129. #include SLX_RULES
  130. /* Declarations. */
  131. FILE *yyin; /* currently yacc parsed file */
  132. extern void yyerror( char * ); /* parsing error management function */
  133. #ifdef __cplusplus
  134. extern "C" int yywrap( void );
  135. #else
  136. extern int yywrap( void ); /* manages the EOF of current processed file */
  137. #endif
  138. /* Use prototypes in function declarations. */
  139. #define YY_USE_PROTOS
  140. #ifdef YY_USE_PROTOS
  141. #define YY_PROTO(proto) proto
  142. #else
  143. #define YY_PROTO(proto) ()
  144. #endif
  145. /* ---------------------------------------------------------------------------------------------- */
  146. #define LEX_RULE_SIZE ( sizeof( (int) iRet ) * ( MAX_MATCH + 2 ) )
  147. #define LEX_WORD_SIZE ( sizeof( LEX_WORD ) )
  148. #define LEX_PAIR_SIZE ( sizeof( LEX_PAIR ) )
  149. #define LEX_DELIMITER_SIZE ( sizeof( LEX_DELIMITER ) )
  150. /* Using statics when we could use locals to eliminate Allocations and Deallocations each time yylex is called and returns. */
  151. /* Look ahead Tokens. */
  152. static int iHold = 0;
  153. static int aiHold[4];
  154. /* Pre-Checked Tokens. */
  155. static int iReturn = 0;
  156. static int aiReturn[4];
  157. /* yylex */
  158. static char * tmpPtr;
  159. static char sToken[TOKEN_SIZE];
  160. static unsigned int iLen = 0;
  161. static char chr, cPrev = 0;
  162. static unsigned int iLastToken = 0;
  163. static char szLexBuffer[ YY_BUF_SIZE ];
  164. static char * s_szBuffer;
  165. static unsigned int iSize = 0;
  166. static int iRet;
  167. static BOOL bTmp, bIgnoreWords = FALSE, bRecursive = FALSE;
  168. /* Lex emulation */
  169. char * yytext;
  170. int yyleng;
  171. /* NewLine Support. */
  172. static BOOL bNewLine = TRUE, bStart = TRUE;
  173. #ifdef USE_KEYWORDS
  174. static unsigned int iKeys = (int) ( sizeof( aKeys ) / LEX_WORD_SIZE );
  175. #endif
  176. static unsigned int iWords = (int) ( sizeof( aWords ) / LEX_WORD_SIZE );
  177. static unsigned int iSelfs = (int) ( sizeof( aSelfs ) / LEX_WORD_SIZE );
  178. static unsigned int iPairs = (int) ( sizeof( aPairs ) / LEX_PAIR_SIZE );
  179. static unsigned int iDelimiters = (int) ( sizeof( aDelimiters ) / LEX_DELIMITER_SIZE );
  180. static unsigned int iRules = (int) ( sizeof( aiRules ) / LEX_RULE_SIZE );
  181. typedef struct _TREE_NODE
  182. {
  183. int iMin;
  184. int iMax;
  185. } TREE_NODE; /* support structure for Streams (Pairs). */
  186. /* Indexing System. */
  187. static TREE_NODE aPairNodes[256], aSelfNodes[256], aKeyNodes[256], aWordNodes[256], aRuleNodes[1024];
  188. static char acOmmit[256], acNewLine[256];
  189. static int acReturn[256];
  190. static int Reduce( int iToken );
  191. int SimpLex_GetNextToken( void );
  192. int SimpLex_CheckToken( void );
  193. void SimpLex_CheckWords( void );
  194. /* Indexing System. */
  195. static void GenTrees( void );
  196. static int rulecmp( const void * pLeft, const void * pRight );
  197. /* --------------------------------------------------------------------------------- */
  198. /* MACROS. */
  199. /* Readability Macros. */
  200. #define LEX_RULE_SIZE ( sizeof( (int) iRet ) * ( MAX_MATCH + 2 ) )
  201. #define LEX_WORD_SIZE ( sizeof( LEX_WORD ) )
  202. #define LEX_PAIR_SIZE ( sizeof( LEX_PAIR ) )
  203. #define IF_TOKEN_READY() if( iReturn )
  204. #define IF_TOKEN_ON_HOLD() if( iHold )
  205. #define RESET_LEX() { iLen = 0; iHold = 0; iReturn = 0; bNewLine = TRUE; bStart = TRUE; }
  206. #define FORCE_REDUCE() Reduce( 0 )
  207. #define HOLD_TOKEN(x) PUSH_TOKEN(x)
  208. #define IF_BEGIN_PAIR(chr) \
  209. if( aPairNodes[(int)chr].iMin == -1 ) \
  210. { \
  211. bTmp = FALSE; \
  212. } \
  213. else \
  214. { \
  215. register unsigned int i = aPairNodes[(int)chr].iMin, iMax = aPairNodes[(int)chr].iMax + 1, iStartLen; \
  216. register unsigned char chrStart; \
  217. unsigned int iLastPair = 0, iLastLen = 0; \
  218. \
  219. DEBUG_INFO( printf( "Checking %i Streams for %c At: >%s<\n", iPairs, chr, szBuffer - 1 ) ); \
  220. \
  221. while( i < iMax ) \
  222. { \
  223. iStartLen = 1; \
  224. \
  225. chrStart = LEX_CASE( *szBuffer ); \
  226. \
  227. while( aPairs[i].sStart[iStartLen] ) \
  228. { \
  229. if( chrStart != aPairs[i].sStart[iStartLen] ) \
  230. { \
  231. break; \
  232. } \
  233. \
  234. iStartLen++; \
  235. \
  236. chrStart = LEX_CASE( *( szBuffer + iStartLen - 1 ) ); \
  237. } \
  238. \
  239. /* Match */ \
  240. if( aPairs[i].sStart[iStartLen] == '\0' ) \
  241. { \
  242. if( iStartLen > iLastLen ) \
  243. { \
  244. iLastPair = i + 1; \
  245. iLastLen = iStartLen; \
  246. } \
  247. } \
  248. i++; \
  249. } \
  250. \
  251. bTmp = FALSE; \
  252. \
  253. if( iLastPair ) \
  254. { \
  255. iLastPair--; \
  256. STREAM_OPEN( aPairs[iLastPair].sStart )\
  257. { \
  258. bTmp = TRUE; \
  259. \
  260. /* Last charcter read. */\
  261. if( iStartLen > 1 ) chr = chrStart; \
  262. \
  263. /* Moving to next postion after the Stream Start position. */ \
  264. szBuffer += ( iLastLen - 1 ); \
  265. \
  266. sStart = (char *) aPairs[iLastPair].sStart; \
  267. sTerm = (char *) aPairs[iLastPair].sTerm; \
  268. sExclude = (char *) aPairs[iLastPair].sExclude; \
  269. bTestLeft = aPairs[iLastPair].bTestLeft; \
  270. iPairToken = aPairs[iLastPair].iToken; \
  271. \
  272. DEBUG_INFO( printf( "Looking for Stream Terminator: >%s< Exclusions >%s<\n", sTerm, sExclude ) ); \
  273. } \
  274. } \
  275. } \
  276. /* Begin New Pair. */ \
  277. if( bTmp )
  278. #define CHECK_SELF_CONTAINED(chr) \
  279. if( aSelfNodes[(int)chr].iMin != -1 ) \
  280. { \
  281. register unsigned int i = aSelfNodes[(int)chr].iMin, iMax = aSelfNodes[(int)chr].iMax + 1, iSelfLen; \
  282. register unsigned char chrSelf; \
  283. \
  284. DEBUG_INFO( printf( "Checking %i Selfs for %c At: >%s<\n", iSelfs, chr, szBuffer - 1 ) ); \
  285. \
  286. while( i < iMax ) \
  287. { \
  288. sSelf[0] = chr; \
  289. iSelfLen = 1; \
  290. chrSelf = LEX_CASE( *szBuffer ); \
  291. \
  292. while( aSelfs[i].sWord[iSelfLen] ) \
  293. { \
  294. if( aSelfs[i].sWord[iSelfLen] == chrSelf ) \
  295. { \
  296. sSelf[ iSelfLen ] = chrSelf; \
  297. } \
  298. else \
  299. { \
  300. break; \
  301. } \
  302. \
  303. iSelfLen++; \
  304. \
  305. chrSelf = LEX_CASE( *( szBuffer + iSelfLen - 1 ) ); \
  306. } \
  307. \
  308. /* Match */ \
  309. if( aSelfs[i].sWord[iSelfLen] == '\0' ) \
  310. { \
  311. /* Moving to next postion after the Self Contained Word. */ \
  312. szBuffer += ( iSelfLen - 1 ); \
  313. s_szBuffer = szBuffer; \
  314. \
  315. sSelf[ iSelfLen ] = '\0'; \
  316. iRet = aSelfs[i].iToken; \
  317. \
  318. if( iLen ) \
  319. { \
  320. DEBUG_INFO( printf( "Holding Self >%s<\n", sSelf ) ); \
  321. \
  322. HOLD_TOKEN( iRet ); \
  323. \
  324. /* Terminate current token and check it. */ \
  325. sToken[ iLen ] = '\0'; \
  326. \
  327. /* Last charcter read. */\
  328. chr = chrSelf;\
  329. \
  330. return SimpLex_CheckToken(); \
  331. } \
  332. else \
  333. { \
  334. DEBUG_INFO( printf( "Reducing Self >%s<\n", sSelf ) ); \
  335. bIgnoreWords = FALSE;\
  336. \
  337. if( bNewLine )\
  338. {\
  339. bNewLine = FALSE;\
  340. NEW_LINE_ACTION();\
  341. }\
  342. \
  343. /* Last charcter read. */\
  344. if( iSelfLen > 1 ) chr = chrSelf;\
  345. \
  346. return iRet; \
  347. } \
  348. } \
  349. \
  350. i++; \
  351. } \
  352. }
  353. #define IF_ABORT_PAIR(chrPair) \
  354. tmpPtr = sExclude; \
  355. while ( *tmpPtr && chrPair != *tmpPtr ) \
  356. { \
  357. tmpPtr++; \
  358. } \
  359. \
  360. /* Exception. */ \
  361. if( *tmpPtr )
  362. #define IF_APPEND_DELIMITER(chr) \
  363. /* Delimiter to Append? */ \
  364. tmpPtr = (char*) szAppend; \
  365. while ( *tmpPtr && chr != *tmpPtr ) tmpPtr++; \
  366. \
  367. /* Delimiter to Append found. */ \
  368. if( *tmpPtr )
  369. #ifndef IF_BELONG_LEFT
  370. #define IF_BELONG_LEFT(chr) \
  371. /* Give precedence to associate rules */ \
  372. DEBUG_INFO( printf( "Checking Left for: '%c' cPrev: '%c'\n", chr, cPrev ) ); \
  373. \
  374. if( 0 )
  375. #endif
  376. #define RETURN_READY_TOKEN() \
  377. \
  378. iReturn--; \
  379. iRet = aiReturn[iReturn]; \
  380. \
  381. DEBUG_INFO( printf( "Returning Ready: %i\n", iRet ) ); \
  382. \
  383. INTERCEPT_ACTION(iRet); \
  384. return iRet; \
  385. #define RELEASE_TOKEN() \
  386. \
  387. /* Last in First Out. */ \
  388. iHold--; \
  389. iRet = aiHold[iHold]; \
  390. \
  391. DEBUG_INFO( printf( "Released %i Now Holding %i Tokens: %i %i %i %i\n", iRet, iHold, aiHold[0], aiHold[1], aiHold[2], aiHold[3] ) ); \
  392. bIgnoreWords = FALSE;\
  393. \
  394. if( iRet < 256 ) \
  395. { \
  396. if( acNewLine[iRet] ) bNewLine = TRUE; \
  397. } \
  398. \
  399. DEBUG_INFO( printf( "Reducing Held: %i Pos: %i\n", iRet, iHold ) ); \
  400. LEX_RETURN( Reduce( iRet ) );
  401. #define LEX_RETURN(x) \
  402. \
  403. iRet = x;\
  404. \
  405. if( iRet < LEX_CUSTOM_ACTION ) \
  406. { \
  407. iRet = CUSTOM_ACTION(iRet); \
  408. } \
  409. \
  410. if( iRet ) \
  411. { \
  412. INTERCEPT_ACTION(iRet); \
  413. DEBUG_INFO( printf( "Returning: %i\n", iRet ) ); \
  414. return iRet; \
  415. } \
  416. else \
  417. { \
  418. goto Start; \
  419. }
  420. #define PUSH_TOKEN( iPushToken )\
  421. {\
  422. aiHold[ iHold++ ] = iPushToken;\
  423. DEBUG_INFO( printf("Now Holding %i Tokens: %i %i %i %i\n", iHold, aiHold[0], aiHold[1], aiHold[2], aiHold[3] ) ); \
  424. }
  425. #ifndef YY_DECL
  426. #define YY_DECL int yylex YY_PROTO(( void ))
  427. #endif
  428. YY_DECL
  429. {
  430. LEX_USER_SETUP();
  431. Start :
  432. IF_TOKEN_READY()
  433. {
  434. RETURN_READY_TOKEN();
  435. }
  436. IF_TOKEN_ON_HOLD()
  437. {
  438. RELEASE_TOKEN();
  439. }
  440. if( iSize == 0 )
  441. {
  442. /*
  443. if( szLexBuffer == NULL )
  444. {
  445. szLexBuffer = malloc( YY_BUF_SIZE );
  446. }
  447. if( yytext == NULL )
  448. {
  449. yytext = malloc( YY_BUF_SIZE );
  450. }
  451. */
  452. if( bStart )
  453. {
  454. bStart = FALSE;
  455. GenTrees();
  456. INIT_ACTION();
  457. }
  458. YY_INPUT( (char*) szLexBuffer, iSize, YY_BUF_SIZE );
  459. if( iSize )
  460. {
  461. s_szBuffer = (char*) szLexBuffer;
  462. DEBUG_INFO( printf( "New Buffer: >%s<\n", szLexBuffer ) );
  463. }
  464. else
  465. {
  466. RESET_LEX();
  467. DEBUG_INFO( printf( "Returning: <EOF>\n" ) );
  468. return -1; \
  469. }
  470. }
  471. LEX_RETURN( Reduce( SimpLex_GetNextToken() ) )
  472. }
  473. int SimpLex_GetNextToken( void )
  474. {
  475. register char * szBuffer = s_szBuffer;
  476. iLen = 0;
  477. while ( 1 )
  478. {
  479. if ( iSize && *szBuffer )
  480. {
  481. if( iPairToken )
  482. {
  483. goto ProcessStream;
  484. }
  485. cPrev = chr;
  486. /* Get next character. */
  487. iSize--;
  488. chr = (*szBuffer++);
  489. /* Not using LEX_CASE() yet (white space)!!! */
  490. if( acOmmit[(int)chr] )
  491. {
  492. while( acOmmit[(int)(*szBuffer)] )
  493. {
  494. iSize--; szBuffer++;
  495. }
  496. if ( iLen )
  497. {
  498. /* Terminate current token and check it. */
  499. sToken[ iLen ] = '\0';
  500. s_szBuffer = szBuffer;
  501. DEBUG_INFO( printf( "Token: \"%s\" Ommited: \'%c\'\n", sToken, chr ) );
  502. return SimpLex_CheckToken();
  503. }
  504. else
  505. {
  506. continue;
  507. }
  508. }
  509. chr = LEX_CASE(chr);
  510. CHECK_SELF_CONTAINED(chr);
  511. /* New Pair ? */
  512. IF_BEGIN_PAIR( chr )
  513. {
  514. if( iLen )
  515. {
  516. DEBUG_INFO( printf( "Holding Stream Mode: '%c' Buffer = >%s<\n", chr, szBuffer ) );
  517. /* Terminate and Check Token to the left. */
  518. sToken[ iLen ] = '\0';
  519. s_szBuffer = szBuffer;
  520. DEBUG_INFO( printf( "Token: \"%s\" before New Pair at: \'%c\'\n", sToken, chr ) );
  521. return SimpLex_CheckToken();
  522. }
  523. ProcessStream :
  524. bIgnoreWords = FALSE;
  525. if( bTestLeft )
  526. {
  527. IF_BELONG_LEFT( chr )
  528. {
  529. /* Resetting. */
  530. iPairToken = 0;
  531. s_szBuffer = szBuffer;
  532. DEBUG_INFO( printf( "Reducing Left '%c'\n", chr ) );
  533. return (int) chr ;
  534. }
  535. }
  536. { register int iPairLen = 0;
  537. register char chrPair;
  538. /* Look for the terminator. */
  539. while ( *szBuffer )
  540. {
  541. /* Next Character. */
  542. chrPair = *szBuffer++ ;
  543. /* Terminator ? */
  544. if( chrPair == sTerm[0] )
  545. {
  546. register int iTermLen = 1;
  547. if( sTerm[1] )
  548. {
  549. register char chrTerm = *szBuffer; /* Not using LEX_CASE() here !!! */
  550. while( sTerm[iTermLen] )
  551. {
  552. if( chrTerm != sTerm[iTermLen] )
  553. {
  554. /* Last charcter read. */
  555. chr = chrTerm;
  556. break;
  557. }
  558. iTermLen++;
  559. chrTerm = *( szBuffer + iTermLen - 1 ); /* Not using LEX_CASE() here !!! */
  560. }
  561. }
  562. /* Match */ \
  563. if( sTerm[iTermLen] == '\0' ) \
  564. { \
  565. /* Moving to next postion after the Stream Terminator. */ \
  566. szBuffer += ( iTermLen - 1 ); \
  567. sPair[ iPairLen ] = '\0';
  568. if( bNewLine )
  569. {
  570. bNewLine = FALSE;
  571. NEW_LINE_ACTION();
  572. }
  573. iRet = iPairToken;
  574. /* Resetting. */
  575. iPairToken = 0;
  576. s_szBuffer = szBuffer;
  577. DEBUG_INFO( printf( "Returning Pair = >%s<\n", sPair ) );
  578. return iRet;
  579. }
  580. }
  581. /* Check if exception. */
  582. IF_ABORT_PAIR( chrPair )
  583. {
  584. sPair[ iPairLen ] = '\0';
  585. /* Resetting. */
  586. iPairToken = 0;
  587. /* Last charcter read. */
  588. chr = chrPair;
  589. STREAM_EXCEPTION( sPair, chrPair );
  590. s_szBuffer = szBuffer;
  591. return iPairToken;
  592. }
  593. else
  594. {
  595. STREAM_APPEND( chrPair );
  596. }
  597. }
  598. }
  599. /* Resetting. */
  600. iPairToken = 0;
  601. /* EOF */
  602. STREAM_EXCEPTION( sPair, NULL );
  603. s_szBuffer = szBuffer;
  604. return iPairToken;
  605. }
  606. /* End Pairs. */
  607. /* NewLine ? */
  608. if( acNewLine[(int)chr] )
  609. {
  610. while( acNewLine[(int)(*szBuffer)] )
  611. {
  612. iSize--; szBuffer++;
  613. }
  614. s_szBuffer = szBuffer;
  615. if( iLen )
  616. {
  617. /* Will return NewLine on next call. */
  618. HOLD_TOKEN( chr );
  619. /* Terminate current token and check it. */
  620. sToken[ iLen ] = '\0';
  621. DEBUG_INFO( printf( "Token: \"%s\" at <NewLine> Holding: \'%c\'\n", sToken, chr ) );
  622. return SimpLex_CheckToken();
  623. }
  624. else
  625. {
  626. DEBUG_INFO( printf( "Reducing NewLine '%c'\n", chr ) );
  627. bIgnoreWords = FALSE;
  628. bNewLine = TRUE;
  629. return (int) chr;
  630. }
  631. }
  632. #ifdef USE_BELONGS
  633. IF_APPEND_DELIMITER( chr )
  634. {
  635. /* Append and Terminate current token and check it. */
  636. sToken[ iLen++ ] = chr;
  637. sToken[ iLen ] = '\0';
  638. s_szBuffer = szBuffer;
  639. DEBUG_INFO( printf( "Token: \"%s\" Appended: \'%c\'\n", sToken, chr ) );
  640. return SimpLex_CheckToken();
  641. }
  642. #endif
  643. if( acReturn[(int)chr] )
  644. {
  645. s_szBuffer = szBuffer;
  646. if( iLen )
  647. {
  648. /* Will be returned on next cycle. */
  649. HOLD_TOKEN( acReturn[(int)chr] );
  650. /* Terminate current token and check it. */
  651. sToken[ iLen ] = '\0';
  652. DEBUG_INFO( printf( "Token: \"%s\" Holding: \'%c\' As: %i \n", sToken, chr, iRet ) );
  653. return SimpLex_CheckToken();
  654. }
  655. else
  656. {
  657. bIgnoreWords = FALSE;
  658. if( bNewLine )
  659. {
  660. bNewLine = FALSE;
  661. NEW_LINE_ACTION();
  662. }
  663. DEBUG_INFO( printf( "Reducing Delimiter: '%c' As: %i\n", chr, iRet ) );
  664. return acReturn[(int)chr];
  665. }
  666. }
  667. /* Acumulate and scan next Charcter. */
  668. sToken[ iLen++ ] = chr;
  669. continue;
  670. }
  671. else
  672. {
  673. YY_INPUT( (char*) szLexBuffer, iSize, YY_BUF_SIZE );
  674. if( iSize )
  675. {
  676. szBuffer = (char*) szLexBuffer;
  677. continue;
  678. }
  679. else
  680. {
  681. if( iLen )
  682. {
  683. /* <EOF> */
  684. HOLD_TOKEN( -1 );
  685. /* Terminate current token and check it. */
  686. sToken[ iLen ] = '\0';
  687. s_szBuffer = szBuffer;
  688. DEBUG_INFO( printf( "Token: \"%s\" at: \'<EOF>\'\n", sToken ) );
  689. return SimpLex_CheckToken();
  690. }
  691. else
  692. {
  693. s_szBuffer = szBuffer;
  694. DEBUG_INFO( printf( "Returning: <EOF>\n", iRet ) ); \
  695. return -1; \
  696. }
  697. }
  698. }
  699. }
  700. }
  701. int SimpLex_CheckToken( void )
  702. {
  703. if( bRecursive )
  704. {
  705. return 0;
  706. }
  707. else
  708. {
  709. bRecursive = TRUE;
  710. }
  711. if( bNewLine )
  712. {
  713. bIgnoreWords = FALSE;
  714. NEW_LINE_ACTION();
  715. #ifdef USE_KEYWORDS
  716. SimpLex_CheckWords();
  717. if( iRet )
  718. {
  719. bRecursive = FALSE;
  720. /* bIgnoreWords and bNewLine were handled by SimpLex_CheckWords(). */
  721. return iRet;
  722. }
  723. #endif
  724. }
  725. if( bIgnoreWords )
  726. {
  727. DEBUG_INFO( printf( "Skiped Words for Word: %s\n", (char*) sToken ) );
  728. bIgnoreWords = FALSE;
  729. }
  730. else
  731. {
  732. SimpLex_CheckWords();
  733. if( iRet )
  734. {
  735. bRecursive = FALSE;
  736. return iRet;
  737. }
  738. }
  739. DEBUG_INFO( printf( "Reducing Element: \"%s\"\n", (char*) sToken ) );
  740. iRet = ELEMENT_TOKEN( (char*)sToken, iLen );
  741. bRecursive = FALSE;
  742. return iRet;
  743. }
  744. int Reduce( int iToken )
  745. {
  746. BeginReduce :
  747. if( iToken < LEX_CUSTOM_ACTION )
  748. {
  749. iToken = CUSTOM_ACTION( iToken ); \
  750. }
  751. if( iToken > DONT_REDUCE )
  752. {
  753. iLastToken = ( iToken - DONT_REDUCE );
  754. DEBUG_INFO( printf( "Returning Dont Reduce %i\n", iLastToken ) );
  755. return iLastToken;
  756. }
  757. else if( iToken == 0 )
  758. {
  759. DEBUG_INFO( printf( "Returning 0\n" ) );
  760. return 0;
  761. }
  762. iLastToken = iToken;
  763. /* No Rules for this token. */
  764. if( aRuleNodes[ iToken ].iMin == -1 )
  765. {
  766. DEBUG_INFO( printf( "Passing through: %i\n", iToken ) );
  767. return iToken;
  768. }
  769. else
  770. {
  771. register unsigned int i = (unsigned int)(aRuleNodes[ iToken ].iMin), iMax = (unsigned int)(aRuleNodes[ iToken ].iMax);
  772. register unsigned int iTentative = 0, iMatched = 1;
  773. DEBUG_INFO( printf( "Scaning Prospects %i-%i at Pos: 0 for Token: %i\n", i, iMax -1, iToken ) );
  774. {
  775. FoundProspect :
  776. DEBUG_INFO( printf( "Prospect of %i Tokens - Testing Token: %i\n", iMatched, iToken ) );
  777. if( iMatched == MAX_MATCH || aiRules[i][iMatched] == 0 )
  778. {
  779. DEBUG_INFO( printf( "Saving Tentative %i - Found match of %i Tokens at Token: %i\n", i, iMatched, iToken ) );
  780. iTentative = i;
  781. }
  782. else
  783. {
  784. DEBUG_INFO( printf( "Partial Match - Get next Token after Token: %i\n", iToken ) );
  785. if( iHold )
  786. {
  787. iHold--;
  788. iToken = aiHold[ iHold ];
  789. bIgnoreWords = FALSE;
  790. if( iToken < 256 )
  791. {
  792. if( acNewLine[iToken] ) bNewLine = TRUE;
  793. }
  794. }
  795. else
  796. {
  797. iToken = SimpLex_GetNextToken();
  798. }
  799. if( iToken < LEX_CUSTOM_ACTION )
  800. {
  801. iToken = CUSTOM_ACTION( iToken ); \
  802. }
  803. if( iToken > DONT_REDUCE )
  804. {
  805. DEBUG_INFO( printf( "Reduce Forced for Token: %i\n", iToken - DONT_REDUCE ) );
  806. aiHold[iHold++] = iToken;
  807. goto AfterScanRules;
  808. }
  809. else
  810. {
  811. iLastToken = iToken;
  812. }
  813. if( aiRules[i][iMatched] == iToken )
  814. {
  815. /* Continue... Still a prospect. */
  816. DEBUG_INFO( printf( "Accepted Token: %i - Continue with this Rule...\n", iToken ) );
  817. iMatched++;
  818. goto FoundProspect;
  819. }
  820. else if( aiRules[i][iMatched] > iToken )
  821. {
  822. DEBUG_INFO( printf( "Rejected Token: %i - Giving up...\n", iToken ) );
  823. aiHold[iHold++] = iToken;
  824. goto AfterScanRules;
  825. }
  826. else
  827. {
  828. DEBUG_INFO( printf( "Rejected Token: %i - Continue with next Rule...\n", iToken ) );
  829. aiHold[iHold++] = iToken;
  830. }
  831. }
  832. if( i < iMax )
  833. {
  834. register unsigned int j = 1;
  835. DEBUG_INFO( printf( "Checking if next rule is extension of last Rule\n" ) );
  836. while( j < iMatched )
  837. {
  838. if( aiRules[i][j] != aiRules[i+1][j] )
  839. {
  840. break;
  841. }
  842. j++;
  843. }
  844. if( j < iMatched )
  845. {
  846. DEBUG_INFO( printf( "Rejected Rule - Not an extension of previous - Giving up...\n" ) );
  847. }
  848. else
  849. {
  850. DEBUG_INFO( printf( "Accepted Next Rule...\n" ) );
  851. i++;
  852. goto FoundProspect;
  853. }
  854. }
  855. else
  856. {
  857. DEBUG_INFO( printf( "No More prospects...\n" ) );
  858. }
  859. }
  860. AfterScanRules :
  861. if( iTentative )
  862. {
  863. DEBUG_INFO( printf( "Processing Tentative: %i\n", iTentative ) );
  864. while( iMatched > 1 && aiRules[i][iMatched - 1] && aiRules[iTentative][iMatched - 1] == 0 )
  865. {
  866. DEBUG_INFO( printf( "Reclaimed Token: %i\n", aiRules[i][iMatched - 1] ) );
  867. aiHold[iHold++] = aiRules[i][iMatched - 1];
  868. iMatched--;
  869. }
  870. if( aiRules[iTentative][MAX_MATCH] )
  871. {
  872. DEBUG_INFO( printf( "Reducing Rule: %i Found %i Tokens\n", iTentative, iMatched ) );
  873. if( aiRules[iTentative][MAX_MATCH + 1] )
  874. {
  875. DEBUG_INFO( printf( "Pushing Reduction: %i\n", aiRules[iTentative][MAX_MATCH + 1] ) );
  876. aiHold[iHold++] = aiRules[iTentative][MAX_MATCH + 1];
  877. }
  878. DEBUG_INFO( printf( "Recycling Reduction: %i\n", aiRules[iTentative][MAX_MATCH] ) );
  879. iToken = aiRules[iTentative][MAX_MATCH];
  880. goto BeginReduce;
  881. }
  882. else
  883. {
  884. DEBUG_INFO( printf( "Passing Through %i Tokens\n", iMatched ) );
  885. while( iMatched > 1 )
  886. {
  887. iMatched--;
  888. DEBUG_INFO( printf( "Stacking Return: %i\n", aiRules[iTentative][iMatched] ) );
  889. aiReturn[iReturn++] = aiRules[iTentative][iMatched];
  890. }
  891. DEBUG_INFO( printf( "Returning: %i\n", aiRules[iTentative][0] ) );
  892. return aiRules[iTentative][0];
  893. }
  894. }
  895. else
  896. {
  897. while( iMatched > 1 )
  898. {
  899. iMatched--;
  900. DEBUG_INFO( printf( "Pushing: %i\n", aiRules[i][iMatched] ) );
  901. aiHold[iHold++] = aiRules[i][iMatched];
  902. }
  903. DEBUG_INFO( printf( "Returning Shifted Left: %i\n", aiRules[i][0] ) );
  904. return aiRules[i][0];
  905. }
  906. }
  907. }
  908. void SimpLex_CheckWords( void )
  909. {
  910. int iTentative = -1, iCompare;
  911. unsigned int i, iMax, iLenMatched, iBaseSize, iKeyLen;
  912. char *pNextSpacer, *sKeys2Match = NULL, *szBaseBuffer = s_szBuffer, cSpacer = chr;
  913. LEX_WORD *aCheck;
  914. #ifdef DEBUG_LEX
  915. char sKeyDesc[] = "Key", sWordDesc[] = "Word", *sDesc;
  916. #endif
  917. #ifdef LEX_ABBREVIATE
  918. unsigned int iLen2Match;
  919. #endif
  920. #ifdef USE_KEYWORDS
  921. if( bNewLine )
  922. {
  923. i = aKeyNodes[ (int)(sToken[0]) ].iMin;
  924. iMax = aKeyNodes[ (int)(sToken[0]) ].iMax + 1;
  925. aCheck = (LEX_WORD*) ( &(aKeys[0]) );
  926. #ifdef DEBUG_LEX
  927. sDesc = (char*) sKeyDesc;
  928. #endif
  929. }
  930. else
  931. #endif
  932. {
  933. i = aWordNodes[ (int)(sToken[0]) ].iMin;
  934. iMax = aWordNodes[ (int)(sToken[0]) ].iMax + 1;
  935. aCheck = (LEX_WORD*) ( &( aWords[0] ) );
  936. #ifdef DEBUG_LEX
  937. sDesc = (char*) sWordDesc;
  938. #endif
  939. }
  940. bNewLine = FALSE;
  941. DEBUG_INFO( printf( "Pre-Scaning %ss for Token: %s at Positions: %i-%i\n", sDesc, (char*) sToken, i, iMax -1 ) );
  942. while( i < iMax )
  943. {
  944. if( sToken[1] < aCheck[i].sWord[1] )
  945. {
  946. DEBUG_INFO( printf( "Gave-Up! Token [%s] < Pattern [%s]\n", sToken, aCheck[i].sWord ) );
  947. iRet = 0;
  948. return;
  949. }
  950. else if( sToken[1] > aCheck[i].sWord[1] )
  951. {
  952. DEBUG_INFO( printf( "Skip... %s [%s] < [%s]\n", sDesc, aCheck[i].sWord, sToken ) );
  953. i++;
  954. DEBUG_INFO( printf( "Continue with larger: [%s]\n", aCheck[i].sWord ) );
  955. continue;
  956. }
  957. else
  958. {
  959. break;
  960. }
  961. }
  962. while( i < iMax )
  963. {
  964. if( sKeys2Match )
  965. {
  966. pNextSpacer = strstr( sKeys2Match, "{WS}" );
  967. }
  968. else
  969. {
  970. sKeys2Match = aCheck[ i ].sWord;
  971. pNextSpacer = strstr( sKeys2Match, "{WS}" );
  972. }
  973. if( sToken[0] < sKeys2Match[0] )
  974. {
  975. DEBUG_INFO( printf( "Gave-Up! Token [%s] < Pattern [%s]\n", sToken, sKeys2Match ) );
  976. break;
  977. }
  978. else if( sToken[0] > sKeys2Match[0] )
  979. {
  980. DEBUG_INFO( printf( "Skip... %s [%s] < [%s]\n", sDesc, sKeys2Match, sToken ) );
  981. i++;
  982. if( ( iLenMatched = ( sKeys2Match - aCheck[i - 1].sWord ) ) == 0 )
  983. {
  984. sKeys2Match = NULL;
  985. DEBUG_INFO( printf( "Continue with larger: [%s]\n", aCheck[i].sWord ) );
  986. continue;
  987. }
  988. /* Is there a next potential Pattern. */
  989. if( i < iMax && strncmp( aCheck[i - 1].sWord, aCheck[i].sWord, iLenMatched ) == 0 )
  990. {
  991. /* Same relative position, in the next Pattern. */
  992. sKeys2Match = aCheck[i].sWord + iLenMatched;
  993. DEBUG_INFO( printf( "Continue with larger: [%s] at: [%s]\n", aCheck[i].sWord, sKeys2Match ) );
  994. continue;
  995. }
  996. else
  997. {
  998. DEBUG_INFO( printf( "Gave-Up! %i !< %i or Pattern [%s] not compatible with last match\n", i, iMax, aCheck[i].sWord ) );
  999. break;
  1000. }
  1001. }
  1002. if( pNextSpacer )
  1003. {
  1004. /* Token not followed by white space - can't match this [or any latter] pattern! */
  1005. if( ! acOmmit[(int)cSpacer] )
  1006. {
  1007. DEBUG_INFO( printf( "Skip... Pattern [%s] requires {WS}, cSpacer: %c\n", sKeys2Match, cSpacer ) );
  1008. i++;
  1009. if( ( iLenMatched = ( sKeys2Match - aCheck[i - 1].sWord ) ) == 0 )
  1010. {
  1011. sKeys2Match = NULL;
  1012. DEBUG_INFO( printf( "Continue with: [%s]\n", aCheck[i].sWord ) );
  1013. continue;
  1014. }
  1015. /* Is there a next potential Pattern. */
  1016. if( i < iMax && strncmp( aCheck[i - 1].sWord, aCheck[i].sWord, iLenMatched ) == 0 )
  1017. {
  1018. /* Same relative position, in the next Pattern. */
  1019. sKeys2Match = aCheck[i].sWord + iLenMatched;
  1020. DEBUG_INFO( printf( "Continue with: [%s] at: [%s]\n", aCheck[i].sWord, sKeys2Match ) );
  1021. continue;
  1022. }
  1023. else
  1024. {
  1025. DEBUG_INFO( printf( "Gave-Up! %i !< %i or Pattern [%s] not compatible with last match\n", i, iMax, aCheck[i].sWord ) );
  1026. break;
  1027. }
  1028. }
  1029. iKeyLen = pNextSpacer - sKeys2Match;
  1030. }
  1031. else
  1032. {
  1033. iKeyLen = strlen( sKeys2Match );
  1034. }
  1035. #ifdef LEX_ABBREVIATE
  1036. iLen2Match = iLen;
  1037. if( iLen2Match < LEX_ABBREVIATE && iLen2Match < iKeyLen )
  1038. {
  1039. iLen2Match = ( LEX_ABBREVIATE < iKeyLen ) ? LEX_ABBREVIATE : iKeyLen ;
  1040. }
  1041. if( iLen2Match > iKeyLen && i < iMax - 1 )
  1042. {
  1043. DEBUG_INFO( printf( "Trying Next... length mismatch - iKeyLen: %i iLen2Match: %i comparing: [%s] with: [%s]\n", iKeyLen, iLen2Match, sToken, sKeys2Match ) );
  1044. i++;
  1045. if( ( iLenMatched = ( sKeys2Match - aCheck[i - 1].sWord ) ) == 0 )
  1046. {
  1047. sKeys2Match = NULL;
  1048. DEBUG_INFO( printf( "Continue with: [%s]\n", aCheck[i].sWord ) );
  1049. continue;
  1050. }
  1051. /* Is there a next potential Pattern. */
  1052. if( i < iMax && strncmp( aCheck[i - 1].sWord, aCheck[i].sWord, iLenMatched ) == 0 )
  1053. {
  1054. /* Same relative position, in the next Pattern. */
  1055. sKeys2Match = aCheck[i].sWord + iLenMatched;
  1056. DEBUG_INFO( printf( "Continue with: [%s] at: [%s]\n", aCheck[i].sWord, sKeys2Match ) );
  1057. continue;
  1058. }
  1059. else
  1060. {
  1061. DEBUG_INFO( printf( "Gave-Up! %i !< %i or Pattern [%s] not compatible with last match\n", i, iMax, aCheck[i].sWord ) );
  1062. break;
  1063. }
  1064. }
  1065. DEBUG_INFO( printf( "iKeyLen: %i iLen2Match: %i comparing: [%s] with: [%s]\n", iKeyLen, iLen2Match, sToken, sKeys2Match ) );
  1066. iCompare = strncmp( (char*) sToken, sKeys2Match, iLen2Match );
  1067. #else
  1068. iCompare = strcmp( (char*) sToken, sKeys2Match );
  1069. #endif
  1070. if( iCompare == 0 ) /* Match found */
  1071. {
  1072. if( pNextSpacer == NULL ) /* Full Match! */
  1073. {
  1074. DEBUG_INFO( printf( "Saving Tentative %s [%s] == [%s]\n", sDesc, sToken, sKeys2Match ) );
  1075. iTentative = i;
  1076. iLenMatched = strlen( aCheck[i].sWord );
  1077. /* Saving this pointer of the input stream, we might have to get here again. */
  1078. szBaseBuffer = s_szBuffer; iBaseSize = iSize;
  1079. DEBUG_INFO( printf( "Saved Buffer Postion: %i at: [%s]\n", iBaseSize, szBaseBuffer ) );
  1080. /* No White Space after last Token! */
  1081. if( iHold || iPairToken )
  1082. {
  1083. DEBUG_INFO( printf( "No White space after [%s] Holding: %i\n", sToken, aiHold[0] ) );
  1084. break;
  1085. }
  1086. IsExtendedMatch :
  1087. i++;
  1088. /* Is there a next potential Pattern, that is an extended version of the current Pattern. */
  1089. if( i < iMax && strncmp( aCheck[i - 1].sWord, aCheck[i].sWord, iLenMatched ) == 0 )
  1090. {
  1091. if( strlen( aCheck[i].sWord ) > ( iLenMatched + 4 ) && ( pNextSpacer = strstr( aCheck[i].sWord + iLenMatched, "{WS}" ) ) != NULL )
  1092. {
  1093. /* Same relative position, in the next Pattern. */
  1094. sKeys2Match = pNextSpacer + 4;
  1095. DEBUG_INFO( printf( "Continue with: [%s] at: [%s]\n", aCheck[i].sWord, sKeys2Match ) );
  1096. }
  1097. else
  1098. {
  1099. DEBUG_INFO( printf( "Skip... - Not Extended: [%s]\n", aCheck[i].sWord ) );
  1100. goto IsExtendedMatch;
  1101. }
  1102. }
  1103. else
  1104. {
  1105. DEBUG_INFO( printf( "Gave-Up! %i !< %i or Pattern [%s] not extension of Pattern [%s]\n", i, iMax, aCheck[i].sWord, aCheck[iTentative].sWord ) );
  1106. break;
  1107. }
  1108. }
  1109. else
  1110. {
  1111. sKeys2Match = pNextSpacer + 4;
  1112. DEBUG_INFO( printf( "Partial %s Match! [%s] == [%s] - Looking for: [%s]\n", sDesc, sToken, aCheck[i].sWord, sKeys2Match ) );
  1113. /* Saving this pointer of the input stream, we might have to get here again. */
  1114. szBaseBuffer = s_szBuffer; iBaseSize = iSize;
  1115. DEBUG_INFO( printf( "Saved Buffer Postion: %i at: [%s]\n", iBaseSize, szBaseBuffer ) );
  1116. }
  1117. /* i may have been increased above - don't want to read next token if it won't get used! */
  1118. if( i < iMax )
  1119. {
  1120. bRecursive = TRUE;
  1121. cSpacer = chr;
  1122. DEBUG_INFO( printf( "Getting next Token...\n" ) );
  1123. SimpLex_GetNextToken();
  1124. continue;
  1125. }
  1126. }
  1127. else if( iCompare > 0 )
  1128. {
  1129. DEBUG_INFO( printf( "Trying Next %s Pattern... [%s] > [%s]\n", sDesc, sToken, sKeys2Match ) );
  1130. i++;
  1131. if( ( iLenMatched = ( sKeys2Match - aCheck[i - 1].sWord ) ) == 0 )
  1132. {
  1133. sKeys2Match = NULL;
  1134. DEBUG_INFO( printf( "Continue with: [%s]\n", aCheck[i].sWord ) );
  1135. continue;
  1136. }
  1137. /* Is there a next potential Pattern. */
  1138. if( i < iMax && strncmp( aCheck[i - 1].sWord, aCheck[i].sWord, iLenMatched ) == 0 )
  1139. {
  1140. /* Same relative position, in the next Pattern. */
  1141. sKeys2Match = aCheck[i].sWord + iLenMatched;
  1142. DEBUG_INFO( printf( "Continue with: [%s] at: [%s]\n", aCheck[i].sWord, sKeys2Match ) );
  1143. continue;
  1144. }
  1145. else
  1146. {
  1147. DEBUG_INFO( printf( "Gave-Up! %i !< %i or Pattern [%s] not compatible with previous match.\n", i, iMax, aCheck[i].sWord ) );
  1148. break;
  1149. }
  1150. }
  1151. else
  1152. {
  1153. DEBUG_INFO( printf( "Gave-Up! [%s] < [%s]\n", sToken, sKeys2Match ) );
  1154. break;
  1155. }
  1156. }
  1157. if( s_szBuffer != szBaseBuffer )
  1158. {
  1159. s_szBuffer = szBaseBuffer; iSize = iBaseSize; iHold = 0; iReturn = 0; iPairToken = 0;
  1160. DEBUG_INFO( printf( "Partial Match - Restored position: %i at: [%s]\n", iSize, s_szBuffer ) );
  1161. }
  1162. if( iTentative > -1 )
  1163. {
  1164. DEBUG_INFO( printf( "Reducing %s Pattern: %i [%s]\n", sDesc, iTentative, aCheck[iTentative].sWord ) );
  1165. bIgnoreWords = TRUE;
  1166. KEYWORD_ACTION()
  1167. iRet = aCheck[ iTentative ].iToken;
  1168. if( iRet < LEX_CUSTOM_ACTION )
  1169. {
  1170. iRet = CUSTOM_ACTION( iRet );
  1171. }
  1172. }
  1173. else
  1174. {
  1175. iRet = 0;
  1176. }
  1177. }
  1178. #ifdef __cplusplus
  1179. YY_BUFFER_STATE yy_create_buffer( FILE * pFile, int iBufSize )
  1180. #else
  1181. void * yy_create_buffer( FILE * pFile, int iBufSize )
  1182. #endif
  1183. {
  1184. HB_SYMBOL_UNUSED( pFile );
  1185. HB_SYMBOL_UNUSED( iBufSize );
  1186. iSize = 0;
  1187. #ifdef __cplusplus
  1188. return (YY_BUFFER_STATE) szLexBuffer;
  1189. #else
  1190. return (void*) szLexBuffer;
  1191. #endif
  1192. }
  1193. #ifdef __cplusplus
  1194. void yy_switch_to_buffer( YY_BUFFER_STATE pBuffer )
  1195. #else
  1196. void yy_switch_to_buffer( void * pBuffer )
  1197. #endif
  1198. {
  1199. HB_SYMBOL_UNUSED( pBuffer );
  1200. FORCE_REDUCE();
  1201. iSize = 0;
  1202. }
  1203. #ifdef __cplusplus
  1204. void yy_delete_buffer( YY_BUFFER_STATE pBuffer )
  1205. #else
  1206. void yy_delete_buffer( void * pBuffer )
  1207. #endif
  1208. {
  1209. HB_SYMBOL_UNUSED( pBuffer );
  1210. FORCE_REDUCE();
  1211. iSize = 0;
  1212. }
  1213. void * yy_bytes_buffer( char * pBuffer, int iBufSize )
  1214. {
  1215. s_szBuffer = pBuffer;
  1216. iSize = iBufSize;
  1217. if( bStart )
  1218. {
  1219. bStart = FALSE;
  1220. GenTrees();
  1221. INIT_ACTION();
  1222. }
  1223. return s_szBuffer;
  1224. }
  1225. static void GenTrees( void )
  1226. {
  1227. register unsigned int i;
  1228. register unsigned int iIndex;
  1229. i = 0;
  1230. while( i < 256 )
  1231. {
  1232. acOmmit[i] = 0;
  1233. acNewLine[i] = 0;
  1234. acReturn[i] = 0;
  1235. aPairNodes[i].iMin = -1;
  1236. aPairNodes[i].iMax = -1;
  1237. aSelfNodes[i].iMin = -1;
  1238. aSelfNodes[i].iMax = -1;
  1239. aKeyNodes[i].iMin = -1;
  1240. aKeyNodes[i].iMax = -1;
  1241. aWordNodes[i].iMin = -1;
  1242. aWordNodes[i].iMax = -1;
  1243. aRuleNodes[i].iMin = -1;
  1244. aRuleNodes[i].iMax = -1;
  1245. i++;
  1246. }
  1247. while( i < 1024 )
  1248. {
  1249. aRuleNodes[i].iMin = -1;
  1250. aRuleNodes[i].iMax = -1;
  1251. i++;
  1252. }
  1253. i = 0;
  1254. while ( szOmmit[i] )
  1255. {
  1256. acOmmit[ (int)(szOmmit[i]) ] = 1;
  1257. i++;
  1258. }
  1259. i = 0;
  1260. while ( szNewLine[i] )
  1261. {
  1262. acNewLine[ (int)(szNewLine[i]) ] = 1;
  1263. i++;
  1264. }
  1265. i = 0;
  1266. while ( i < iDelimiters )
  1267. {
  1268. acReturn[ (int)(aDelimiters[i].cDelimiter) ] = aDelimiters[i].iToken;
  1269. i++;
  1270. }
  1271. i = 0;
  1272. while ( i < iPairs )
  1273. {
  1274. iIndex = aPairs[i].sStart[0];
  1275. if( aPairNodes[ iIndex ].iMin == -1 )
  1276. {
  1277. aPairNodes[ iIndex ].iMin = i;
  1278. }
  1279. aPairNodes[ iIndex ].iMax = i;
  1280. i++;
  1281. }
  1282. i = 0;
  1283. while ( i < iSelfs )
  1284. {
  1285. iIndex = aSelfs[i].sWord[0];
  1286. if( aSelfNodes[ iIndex ].iMin == -1 )
  1287. {
  1288. aSelfNodes[ iIndex ].iMin = i;
  1289. }
  1290. aSelfNodes[ iIndex ].iMax = i;
  1291. i++;
  1292. }
  1293. #ifdef USE_KEYWORDS
  1294. i = 0;
  1295. while ( i < iKeys )
  1296. {
  1297. iIndex = aKeys[i].sWord[0];
  1298. if( aKeyNodes[ iIndex ].iMin == -1 )
  1299. {
  1300. aKeyNodes[ iIndex ].iMin = i;
  1301. }
  1302. aKeyNodes[ iIndex ].iMax = i;
  1303. i++;
  1304. }
  1305. #endif
  1306. i = 0;
  1307. while ( i < iWords )
  1308. {
  1309. iIndex = aWords[i].sWord[0];
  1310. if( aWordNodes[ iIndex ].iMin == -1 )
  1311. {
  1312. aWordNodes[ iIndex ].iMin = i;
  1313. }
  1314. aWordNodes[ iIndex ].iMax = i;
  1315. i++;
  1316. }
  1317. /* Reduce logic excpects the Rules to be sorted. */
  1318. qsort( ( void * ) aiRules, iRules, LEX_RULE_SIZE, rulecmp );
  1319. i = 0;
  1320. while ( i < iRules )
  1321. {
  1322. iIndex = (unsigned int) aiRules[i][0];
  1323. if( iIndex > 1023 )
  1324. {
  1325. printf( "ERROR! Primary Token: %i out of range.\n", (int) iIndex );
  1326. exit( EXIT_FAILURE );
  1327. }
  1328. if( aRuleNodes[ iIndex ].iMin == -1 )
  1329. {
  1330. aRuleNodes[ iIndex ].iMin = i;
  1331. }
  1332. aRuleNodes[ iIndex ].iMax = i;
  1333. i++;
  1334. }
  1335. }
  1336. static int rulecmp( const void * pLeft, const void * pRight )
  1337. {
  1338. int *iLeftRule = (int*)( pLeft );
  1339. int *iRightRule = (int*)( pRight );
  1340. register unsigned int i = 0;
  1341. while( iLeftRule[i] == iRightRule[i] )
  1342. {
  1343. i++;
  1344. }
  1345. if( iLeftRule[i] < iRightRule[i] )
  1346. {
  1347. return -1;
  1348. }
  1349. else
  1350. {
  1351. return 1;
  1352. }
  1353. }