Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

905 rindas
23KB

  1. %{
  2. /*===========================================================================
  3. Copyright (c) 1998-2000, The Santa Cruz Operation
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are met:
  7. *Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. *Redistributions in binary form must reproduce the above copyright notice,
  10. this list of conditions and the following disclaimer in the documentation
  11. and/or other materials provided with the distribution.
  12. *Neither name of The Santa Cruz Operation nor the names of its contributors
  13. may be used to endorse or promote products derived from this software
  14. without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
  16. IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT falseT LIMITED TO,
  17. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
  19. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. CONSEQUENTIAL DAMAGES (INCLUDING, BUT falseT LIMITED TO, PROCUREMENT OF
  21. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. INTERRUPTION)
  23. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. DAMAGE.
  27. =========================================================================*/
  28. /* cscope - interactive C symbol cross-reference
  29. *
  30. * C symbol scanner
  31. */
  32. #include "global.h"
  33. #include "scanner.h"
  34. #include "lookup.h"
  35. #include <assert.h>
  36. /* the line counting has been moved from character reading for speed */
  37. /* comments are discarded */
  38. #ifndef FLEX_SCANNER
  39. # error Sorry, this scanner needs flex. It is not usable with AT&T Lex.
  40. #endif
  41. #define IFLEVELINC 5 /* #if nesting level size increment */
  42. #define YY_false_TOP_STATE 1
  43. int first; /* buffer index for first char of symbol */
  44. int last; /* buffer index for last char of symbol */
  45. int lineno; /* symbol line number */
  46. int myylineno = 1;
  47. /* HBB 20001007: new variables, emulating yytext in a way that allows
  48. * the yymore() simulation, my_yymore(), to be used even in the presence of
  49. * yyless(). */
  50. size_t my_yyleng = 0;
  51. char *my_yytext = NULL;
  52. static bool arraydimension; /* inside array dimension declaration */
  53. static bool bplisting; /* breakpoint listing */
  54. static int braces; /* unmatched left brace count */
  55. static bool classdef; /* c++ class definition */
  56. static bool elseelif; /* #else or #elif found */
  57. static bool esudef; /* enum/struct/union global definition */
  58. static bool external; /* external definition */
  59. static int externalbraces; /* external definition outer brace count */
  60. static bool fcndef; /* function definition */
  61. static bool global; /* file global scope (outside functions) */
  62. static size_t iflevel; /* #if nesting level */
  63. static bool initializer; /* data initializer */
  64. static int initializerbraces; /* data initializer outer brace count */
  65. static bool lex; /* lex file */
  66. static size_t miflevel = IFLEVELINC; /* maximum #if nesting level */
  67. static int *maxifbraces; /* maximum brace count within #if */
  68. static int *preifbraces; /* brace count before #if */
  69. static int parens; /* unmatched left parenthesis count */
  70. static bool ppdefine; /* preprocessor define statement */
  71. static bool pseudoelif; /* pseudo-#elif */
  72. static bool oldtype; /* next identifier is an old type */
  73. static bool rules; /* lex/yacc rules */
  74. static bool sdl; /* sdl file */
  75. static bool structfield; /* structure field declaration */
  76. static int tagdef; /* class/enum/struct/union tag definition */
  77. static bool template; /* function template */
  78. static int templateparens; /* function template outer parentheses count */
  79. static int typedefbraces = -1; /* initial typedef brace count */
  80. static int token; /* token found */
  81. static int ident_start; /* begin of preceding identifier */
  82. static void my_yymore(void);
  83. %}
  84. identifier [a-zA-Z_$][a-zA-Z_0-9$]*
  85. number \.?[0-9][.0-9a-fA-FlLuUxX]*
  86. comment "/*"([^*]*("*"+[^/])?)*"*/"|"//"[^\n]*\n
  87. ws [ \t\r\v\f]
  88. wsnl [ \t\r\v\f\n]|{comment}
  89. /* flex options: stack of start conditions, and don't use yywrap() */
  90. %option stack
  91. %option noyywrap
  92. %start SDL
  93. %a 4000
  94. %o 7000
  95. /* exclusive start conditions. not available in AT&T lex -> use flex! */
  96. %x IN_PREPROC WAS_ENDIF WAS_IDENTIFIER WAS_ESU IN_DQUOTE IN_SQUOTE COMMENT
  97. %%
  98. %\{ { /* lex/yacc C declarations/definitions */
  99. global = true;
  100. goto more;
  101. /* NOTREACHED */
  102. }
  103. %\} {
  104. global = false;
  105. goto more;
  106. /* NOTREACHED */
  107. }
  108. ^%% { /* lex/yacc rules delimiter */
  109. braces = 0;
  110. if (rules == false) {
  111. /* this %% starts the section containing the rules */
  112. rules = true;
  113. /* Copy yytext to private buffer, to be able to add further
  114. * content following it: */
  115. my_yymore();
  116. /* simulate a yylex() or yyparse() definition */
  117. (void) strcat(my_yytext, " /* ");
  118. first = strlen(my_yytext);
  119. if (lex == true) {
  120. (void) strcat(my_yytext, "yylex");
  121. } else {
  122. /* yacc: yyparse implicitly calls yylex */
  123. char *s = " yylex()";
  124. char *cp = s + strlen(s);
  125. while (--cp >= s) {
  126. unput(*cp);
  127. }
  128. (void) strcat(my_yytext, "yyparse");
  129. }
  130. last = strlen(my_yytext);
  131. (void) strcat(my_yytext, " */");
  132. my_yyleng = strlen(my_yytext);
  133. return(FCNDEF);
  134. } else {
  135. /* were in the rules section, now comes the closing one */
  136. rules = false;
  137. global = true;
  138. last = first;
  139. my_yymore();
  140. return(FCNEND);
  141. /* NOTREACHED */
  142. }
  143. }
  144. <SDL>STATE[ \t]+({identifier}|\*) { /* sdl state, treat as function def */
  145. braces = 1;
  146. fcndef = true;
  147. token = FCNDEF;
  148. goto findident;
  149. /* NOTREACHED */
  150. }
  151. <SDL>ENDSTATE[ \t] { /* end of an sdl state, treat as end of a function */
  152. goto endstate;
  153. /* NOTREACHED */
  154. }
  155. \{ { /* count unmatched left braces for fcn def detection */
  156. ++braces;
  157. /* mark an untagged enum/struct/union so its beginning
  158. can be found */
  159. if (tagdef) {
  160. if (braces == 1) {
  161. esudef = true;
  162. }
  163. token = tagdef;
  164. tagdef = '\0';
  165. last = first;
  166. my_yymore();
  167. return(token);
  168. }
  169. goto more;
  170. /* NOTREACHED */
  171. }
  172. \#{ws}* { /* start a preprocessor line */
  173. if (rules == false) /* don't consider CPP for lex/yacc rules */
  174. BEGIN(IN_PREPROC);
  175. yyleng = 1; /* get rid of the blanks, if any */
  176. goto more;
  177. /* NOTREACHED */
  178. }
  179. <IN_PREPROC>endif([^a-zA-Z0-9_$\n].*)? { /* #endif */
  180. /* delay treatment of #endif depending on whether an
  181. * #if comes right after it, or not */
  182. /* HBB 20010619: new pattern allows trailing garbage
  183. * after the #endif */
  184. BEGIN(WAS_ENDIF);
  185. goto more;
  186. /* NOTREACHED */
  187. }
  188. <WAS_ENDIF>\n{wsnl}*#{ws}*if(ndef|def)?{ws}+ {
  189. /* attempt to correct erroneous brace count caused by:
  190. *
  191. * #if ...
  192. * ... {
  193. * #endif
  194. * #if ...
  195. * ... {
  196. * #endif
  197. */
  198. /* the current #if must not have an #else or #elif */
  199. if (elseelif == true) {
  200. goto endif;
  201. /* NOTREACHED */
  202. }
  203. pseudoelif = true;
  204. BEGIN(INITIAL);
  205. yyless(1); /* rescan all but the line ending */
  206. yy_set_bol(1);
  207. goto eol;
  208. /* NOTREACHED */
  209. }
  210. <WAS_ENDIF>\n{wsnl}* { /* an #endif with no #if right after it */
  211. endif:
  212. if (iflevel > 0) {
  213. /* get the maximum brace count for this #if */
  214. if (braces < maxifbraces[--iflevel]) {
  215. braces = maxifbraces[iflevel];
  216. }
  217. }
  218. BEGIN(INITIAL);
  219. yyless(1);
  220. yy_set_bol(1);
  221. goto eol;
  222. /* NOTREACHED */
  223. }
  224. <IN_PREPROC>ifndef{ws}+ |
  225. <IN_PREPROC>ifdef{ws}+ |
  226. <IN_PREPROC>if{ws}+ { /* #if directive */
  227. elseelif = false;
  228. if (pseudoelif == true) {
  229. pseudoelif = false;
  230. goto elif;
  231. /* NOTREACHED */
  232. }
  233. /* make sure there is room for the current brace count */
  234. if (iflevel == miflevel) {
  235. miflevel += IFLEVELINC;
  236. maxifbraces = realloc(maxifbraces, miflevel * sizeof(*maxifbraces));
  237. preifbraces = realloc(preifbraces, miflevel * sizeof(*preifbraces));
  238. }
  239. /* push the current brace count */
  240. preifbraces[iflevel] = braces;
  241. maxifbraces[iflevel++] = 0;
  242. BEGIN(INITIAL);
  243. goto more;
  244. /* NOTREACHED */
  245. }
  246. <IN_PREPROC>else({ws}.*)? { /* #else --- eat up whole line */
  247. elseelif = true;
  248. if (iflevel > 0) {
  249. /* save the maximum brace count for this #if */
  250. if (braces > maxifbraces[iflevel - 1]) {
  251. maxifbraces[iflevel - 1] = braces;
  252. }
  253. /* restore the brace count to before the #if */
  254. braces = preifbraces[iflevel - 1];
  255. }
  256. BEGIN(INITIAL);
  257. goto more;
  258. /* NOTREACHED */
  259. }
  260. <IN_PREPROC>elif{ws}+ { /* #elif */
  261. /* elseelif = true; --- HBB I doubt this is correct */
  262. elif:
  263. if (iflevel > 0) {
  264. /* save the maximum brace count for this #if */
  265. if (braces > maxifbraces[iflevel - 1]) {
  266. maxifbraces[iflevel - 1] = braces;
  267. }
  268. /* restore the brace count to before the #if */
  269. braces = preifbraces[iflevel - 1];
  270. }
  271. BEGIN(INITIAL);
  272. goto more;
  273. /* NOTREACHED */
  274. }
  275. <IN_PREPROC>include{ws}*\"[^"\n]+\" |
  276. <IN_PREPROC>include{ws}*<[^>\n]+> { /* #include file */
  277. char *s;
  278. char remember = yytext[yyleng-1];
  279. my_yymore();
  280. s = strpbrk(my_yytext, "\"<");
  281. if (!s)
  282. return(LEXERR);
  283. my_yytext[my_yyleng-1] = '\0';
  284. incfile(s + 1, s);
  285. my_yytext[my_yyleng-1] = remember;
  286. first = s - my_yytext;
  287. last = my_yyleng - 1;
  288. if (compress == true) {
  289. my_yytext[0] = '\2'; /* compress the keyword */
  290. }
  291. BEGIN(INITIAL);
  292. return(INCLUDE);
  293. /* NOTREACHED */
  294. }
  295. \} {
  296. /* could be the last enum member initializer */
  297. if (braces == initializerbraces) {
  298. initializerbraces = -1;
  299. initializer = false;
  300. }
  301. if (--braces <= 0) {
  302. endstate:
  303. braces = 0;
  304. classdef = false;
  305. }
  306. if (braces == 0 || (braces == 1 && classdef == true)) {
  307. /* if the end of an enum/struct/union definition */
  308. if (esudef == true) {
  309. esudef = false;
  310. }
  311. /* if the end of the function */
  312. else if (fcndef == true) {
  313. fcndef = false;
  314. last = first;
  315. my_yymore();
  316. return(FCNEND);
  317. }
  318. }
  319. goto more;
  320. /* NOTREACHED */
  321. }
  322. \( { /* count unmatched left parentheses for function templates */
  323. ++parens;
  324. goto more;
  325. /* NOTREACHED */
  326. }
  327. \) {
  328. if (--parens <= 0) {
  329. parens = 0;
  330. }
  331. /* if the end of a function template */
  332. if (parens == templateparens) {
  333. templateparens = -1;
  334. template = false;
  335. }
  336. goto more;
  337. /* NOTREACHED */
  338. }
  339. = { /* if a global definition initializer */
  340. if (!my_yytext)
  341. return(LEXERR);
  342. if (global == true && ppdefine == false && my_yytext[0] != '#') {
  343. initializerbraces = braces;
  344. initializer = true;
  345. }
  346. goto more;
  347. /* NOTREACHED */
  348. }
  349. : { /* a if global structure field */
  350. if (!my_yytext)
  351. return(LEXERR);
  352. if (global == true && ppdefine == false && my_yytext[0] != '#') {
  353. structfield = true;
  354. }
  355. goto more;
  356. /* NOTREACHED */
  357. }
  358. \, {
  359. if (braces == initializerbraces) {
  360. initializerbraces = -1;
  361. initializer = false;
  362. }
  363. structfield = false;
  364. goto more;
  365. /* NOTREACHED */
  366. }
  367. ; { /* if the enum/struct/union was not a definition */
  368. if (braces == 0) {
  369. esudef = false;
  370. }
  371. /* if the end of a typedef */
  372. if (braces == typedefbraces) {
  373. typedefbraces = -1;
  374. }
  375. /* if the end of a external definition */
  376. if (braces == externalbraces) {
  377. externalbraces = -1;
  378. external = false;
  379. }
  380. structfield = false;
  381. initializer = false;
  382. goto more;
  383. /* NOTREACHED */
  384. }
  385. <IN_PREPROC>define{ws}+{identifier} {
  386. /* preprocessor macro or constant definition */
  387. ppdefine = true;
  388. token = DEFINE;
  389. if (compress == true) {
  390. my_yytext[0] = '\1'; /* compress the keyword */
  391. }
  392. findident:
  393. /* search backwards through yytext[] to find the identifier */
  394. /* falseTE: this had better be left to flex, by use of
  395. * yet another starting condition */
  396. my_yymore();
  397. first = my_yyleng - 1;
  398. while (my_yytext[first] != ' ' && my_yytext[first] != '\t') {
  399. --first;
  400. }
  401. ++first;
  402. last = my_yyleng;
  403. BEGIN(INITIAL);
  404. goto definition;
  405. /* NOTREACHED */
  406. }
  407. <IN_PREPROC>\n { /* unknown preprocessor line */
  408. BEGIN(INITIAL);
  409. ++myylineno;
  410. goto more;
  411. /* NOTREACHED */
  412. }
  413. <IN_PREPROC>. |
  414. <IN_PREPROC>{identifier} { /* unknown preprocessor line */
  415. BEGIN(INITIAL);
  416. goto more;
  417. /* NOTREACHED */
  418. }
  419. class{wsnl}+{identifier}({wsnl}|{identifier}|[():])*\{ { /* class definition */
  420. classdef = true;
  421. tagdef = 'c';
  422. yyless(5); /* eat up 'class', and re-scan */
  423. yy_set_bol(0);
  424. goto more;
  425. /* NOTREACHED */
  426. }
  427. ("enum"|"struct"|"union") {
  428. ident_start = first;
  429. BEGIN(WAS_ESU);
  430. goto more;
  431. }
  432. <WAS_ESU>{
  433. ({wsnl}+{identifier}){wsnl}*\{ { /* e/s/u definition */
  434. tagdef = my_yytext[ident_start];
  435. BEGIN(WAS_IDENTIFIER);
  436. goto ident;
  437. }
  438. {wsnl}*\{ { /* e/s/u definition without a tag */
  439. tagdef = my_yytext[ident_start];
  440. BEGIN(INITIAL);
  441. if (braces == 0) {
  442. esudef = true;
  443. }
  444. last = first;
  445. yyless(0); /* re-scan all this as normal text */
  446. tagdef = '\0';
  447. goto more;
  448. }
  449. ({wsnl}+{identifier})?{wsnl}* |
  450. .|\n { /* e/s/u usage */
  451. BEGIN(WAS_IDENTIFIER);
  452. goto ident;
  453. }
  454. }
  455. if{wsnl}*\( { /* ignore 'if' */
  456. yyless(2);
  457. yy_set_bol(0);
  458. goto more;
  459. }
  460. {identifier} { /* identifier found: do nothing, yet. (!) */
  461. BEGIN(WAS_IDENTIFIER);
  462. ident_start = first;
  463. goto more;
  464. /* NOTREACHED */
  465. }
  466. <WAS_IDENTIFIER>{
  467. {ws}*\(({wsnl}|{identifier}|{number}|[*&[\]=,.:])*\)([()]|{wsnl})*[:a-zA-Z_#{] {
  468. /* a function definition */
  469. /* note: "#define a (b) {" and "#if defined(a)\n#"
  470. * are not fcn definitions! */
  471. /* warning: "if (...)" must not overflow yytext,
  472. * so the content of function argument definitions
  473. * is restricted, in particular parentheses are
  474. * not allowed */
  475. /* FIXME HBB 20001003: the above 'not allowed' may well be the
  476. * reason for the parsing bug concerning function pointer usage,
  477. * I suspect. --- I think my new special-case rule for 'if'
  478. * could be helpful in removing that limitation */
  479. if ((braces == 0 && ppdefine == false && my_yytext[0] != '#' && rules == false) ||
  480. (braces == 1 && classdef == true)) {
  481. fcndef = true;
  482. token = FCNDEF;
  483. goto fcn;
  484. /* NOTREACHED */
  485. }
  486. goto fcncal;
  487. /* NOTREACHED */
  488. }
  489. {ws}*\(([*&[\]=,.]|{identifier}|{number}|{wsnl})* { /* function call */
  490. fcncal: if (fcndef == true || ppdefine == true || rules == true) {
  491. token = FCNCALL;
  492. goto fcn;
  493. /* NOTREACHED */
  494. }
  495. if (template == false) {
  496. templateparens = parens;
  497. template = true;
  498. }
  499. goto ident;
  500. /* NOTREACHED */
  501. }
  502. ("*"|{wsnl})+{identifier} { /* typedef name or modifier use */
  503. goto ident;
  504. /* NOTREACHED */
  505. }
  506. .|\n { /* general identifer usage */
  507. char *s;
  508. if (global == true && ppdefine == false && my_yytext[0] != '#' &&
  509. external == false && initializer == false &&
  510. arraydimension == false && structfield == false &&
  511. template == false && fcndef == false) {
  512. if (esudef == true) {
  513. /* if enum/struct/union */
  514. token = MEMBERDEF;
  515. } else {
  516. token = GLOBALDEF;
  517. }
  518. } else {
  519. ident:
  520. token = IDENT;
  521. }
  522. fcn:
  523. if (YYSTATE == WAS_IDENTIFIER) {
  524. /* Position back to the actual identifier: */
  525. last = first;
  526. first = ident_start;
  527. yyless(0);
  528. /* HBB 20001008: if the anti-backup-pattern above matched,
  529. * and the matched context ended with a \n, then the scanner
  530. * believes it's at the start of a new line. But the yyless()
  531. * should feeds that \n back into the input, so that's
  532. * wrong. --> force 'beginning-of-line' status off. */
  533. yy_set_bol(0);
  534. BEGIN(INITIAL);
  535. } else {
  536. my_yymore();
  537. last = my_yyleng;
  538. }
  539. definition:
  540. /* if a long line */
  541. if (yyleng > STMTMAX) {
  542. int c;
  543. /* skip to the end of the line */
  544. warning("line too long");
  545. while ((c = input()) > LEXEOF) {
  546. if (c == '\n') {
  547. unput(c);
  548. break;
  549. }
  550. }
  551. }
  552. /* truncate a long symbol */
  553. if (yyleng > PATLEN) {
  554. warning("symbol too long");
  555. my_yyleng = first + PATLEN;
  556. my_yytext[my_yyleng] = '\0';
  557. }
  558. /* if found word was a keyword: */
  559. if ((s = lookup(my_yytext + first)) != NULL) {
  560. first = my_yyleng;
  561. /* if the start of a typedef */
  562. if (s == typedeftext) {
  563. typedefbraces = braces;
  564. oldtype = true;
  565. }
  566. /* if an enum/struct/union */
  567. /* (needed for "typedef struct tag name;" so
  568. tag isn't marked as the typedef name) */
  569. else if (s == enumtext || s == structtext || s == uniontext) {
  570. /* do nothing */
  571. } else if (s == externtext) {
  572. /* if an external definition */
  573. externalbraces = braces;
  574. external = true;
  575. } else if (templateparens == parens && template == true) {
  576. /* keyword doesn't start a function
  577. * template */
  578. templateparens = -1;
  579. template = false;
  580. } else {
  581. /* identifier after typedef was a
  582. * keyword */
  583. oldtype = false;
  584. }
  585. } else {
  586. /* not a keyword --> found an identifier */
  587. /* last = yyleng; */
  588. /* if a class/enum/struct/union tag definition */
  589. /* FIXME HBB 20001001: why reject "class"? */
  590. if (tagdef && strnotequal(my_yytext + first, "class")) {
  591. token = tagdef;
  592. tagdef = '\0';
  593. if (braces == 0) {
  594. esudef = true;
  595. }
  596. } else if (braces == typedefbraces && oldtype == false &&
  597. arraydimension == false) {
  598. /* if a typedef name */
  599. token = TYPEDEF;
  600. } else {
  601. oldtype = false;
  602. }
  603. /* my_yymore(); */
  604. return(token);
  605. /* NOTREACHED */
  606. }
  607. }
  608. }
  609. \[ { /* array dimension (don't worry or about subscripts) */
  610. arraydimension = true;
  611. goto more;
  612. /* NOTREACHED */
  613. }
  614. \] {
  615. arraydimension = false;
  616. goto more;
  617. /* NOTREACHED */
  618. }
  619. \\\n { /* preprocessor statement is continued on next line */
  620. /* save the '\\' to the output file, but not the '\n': */
  621. yyleng = 1;
  622. my_yymore();
  623. goto eol;
  624. /* NOTREACHED */
  625. }
  626. \n { /* end of the line */
  627. if (ppdefine == true) { /* end of a #define */
  628. ppdefine = false;
  629. yyless(yyleng - 1);
  630. last = first;
  631. my_yymore();
  632. return(DEFINEEND);
  633. }
  634. /* skip the first 8 columns of a breakpoint listing line */
  635. /* and skip the file path in the page header */
  636. if (bplisting == true) {
  637. int c, i;
  638. /* FIXME HBB 20001007: should call input() instead */
  639. switch (input()) { /* tab and EOF just fall through */
  640. case ' ': /* breakpoint number line */
  641. case '[':
  642. for (i = 1; i < 8 && input() > LEXEOF; ++i)
  643. ;
  644. break;
  645. case '.': /* header line */
  646. case '/':
  647. /* skip to the end of the line */
  648. while ((c = input()) > LEXEOF) {
  649. if (c == '\n') {
  650. unput(c);
  651. break;
  652. }
  653. }
  654. break;
  655. case '\n': /* empty line */
  656. unput('\n');
  657. break;
  658. }
  659. }
  660. eol:
  661. ++myylineno;
  662. first = 0;
  663. last = 0;
  664. if (symbols > 0) {
  665. /* no my_yymore(): \n doesn't need to be in my_yytext */
  666. return(NEWLINE);
  667. }
  668. /* line ended --> flush my_yytext */
  669. if (my_yytext)
  670. *my_yytext = '\0';
  671. my_yyleng = 0;
  672. lineno = myylineno;
  673. }
  674. \' { /* character constant */
  675. if (sdl == false)
  676. BEGIN(IN_SQUOTE);
  677. goto more;
  678. /* NOTREACHED */
  679. }
  680. <IN_SQUOTE>\' {
  681. BEGIN(INITIAL);
  682. goto more;
  683. /* NOTREACHED */
  684. }
  685. \" { /* string constant */
  686. BEGIN(IN_DQUOTE);
  687. goto more;
  688. /* NOTREACHED */
  689. }
  690. <IN_DQUOTE>\" {
  691. BEGIN(INITIAL);
  692. goto more;
  693. /* NOTREACHED */
  694. }
  695. <IN_DQUOTE,IN_SQUOTE>{
  696. \n { /* syntax error: unexpected EOL */
  697. BEGIN(INITIAL);
  698. goto eol;
  699. /* NOTREACHED */
  700. }
  701. \\. |
  702. . {
  703. goto more;
  704. /* NOTREACHED */
  705. }
  706. \\\n { /* line continuation inside a string! */
  707. myylineno++;
  708. goto more;
  709. /* NOTREACHED */
  710. }
  711. }
  712. ^{ws}+ { /* don't save leading white space */
  713. }
  714. {ws}+\n { /* eat whitespace at end of line */
  715. unput('\n');
  716. }
  717. [\t\r\v\f]+ { /* eat non-blank whitespace sequences, replace
  718. * by single blank */
  719. unput(' ');
  720. }
  721. {ws}{2,} { /* compress sequential whitespace here, not in putcrossref() */
  722. unput(' ');
  723. }
  724. "/*" yy_push_state(COMMENT);
  725. <COMMENT>{
  726. [^*\n]* |
  727. "*"+[^*/\n]* ; /* do nothing */
  728. [^*\n]*\n |
  729. "*"+[^*/\n]*\n {
  730. if (ppdefine == false) {
  731. goto eol;
  732. } else {
  733. ++myylineno;
  734. }
  735. /* NOTREACHED */
  736. }
  737. "*"+"/" {
  738. /* replace the comment by a single blank */
  739. unput(' ');
  740. yy_pop_state();
  741. }
  742. }
  743. "//".*\n? {
  744. /* C++-style one-line comment */
  745. goto eol;
  746. /* NOTREACHED */
  747. }
  748. {number} | /* number */
  749. <SDL>STATE[ \t]+ | /* ... and other syntax error catchers... */
  750. . { /* punctuation and operators */
  751. more:
  752. my_yymore();
  753. first = my_yyleng;
  754. }
  755. %%
  756. void
  757. initscanner(char *srcfile)
  758. {
  759. char *s;
  760. if (maxifbraces == NULL) {
  761. maxifbraces = malloc(miflevel * sizeof(*maxifbraces));
  762. preifbraces = malloc(miflevel * sizeof(*preifbraces));
  763. }
  764. first = 0; /* buffer index for first char of symbol */
  765. last = 0; /* buffer index for last char of symbol */
  766. lineno = 1; /* symbol line number */
  767. myylineno = 1; /* input line number */
  768. arraydimension = false; /* inside array dimension declaration */
  769. bplisting = false; /* breakpoint listing */
  770. braces = 0; /* unmatched left brace count */
  771. classdef = false; /* c++ class definition */
  772. elseelif = false; /* #else or #elif found */
  773. esudef = false; /* enum/struct/union global definition */
  774. external = false; /* external definition */
  775. externalbraces = -1; /* external definition outer brace count */
  776. fcndef = false; /* function definition */
  777. global = true; /* file global scope (outside functions) */
  778. iflevel = 0; /* #if nesting level */
  779. initializer = false; /* data initializer */
  780. initializerbraces = -1; /* data initializer outer brace count */
  781. lex = false; /* lex file */
  782. parens = 0; /* unmatched left parenthesis count */
  783. ppdefine = false; /* preprocessor define statement */
  784. pseudoelif = false; /* pseudo-#elif */
  785. oldtype = false; /* next identifier is an old type */
  786. rules = false; /* lex/yacc rules */
  787. sdl = false; /* sdl file */
  788. structfield = false; /* structure field declaration */
  789. tagdef = '\0'; /* class/enum/struct/union tag definition */
  790. template = false; /* function template */
  791. templateparens = -1; /* function template outer parentheses count */
  792. typedefbraces = -1; /* initial typedef braces count */
  793. ident_start = 0; /* start of previously found identifier */
  794. if (my_yytext)
  795. *my_yytext = '\0';
  796. my_yyleng = 0;
  797. BEGIN(INITIAL);
  798. /* if this is not a C file */
  799. if ((s = strrchr(srcfile, '.')) != NULL) {
  800. switch (*++s) { /* this switch saves time on C files */
  801. case 'b':
  802. if (strcmp(s, "bp") == 0) { /* breakpoint listing */
  803. bplisting = true;
  804. }
  805. break;
  806. case 'l':
  807. if (strcmp(s, "l") == 0) { /* lex */
  808. lex = true;
  809. global = false;
  810. }
  811. break;
  812. case 's':
  813. if (strcmp(s, "sd") == 0) { /* sdl */
  814. sdl = true;
  815. BEGIN(SDL);
  816. }
  817. break;
  818. case 'y':
  819. if (strcmp(s, "y") == 0) { /* yacc */
  820. global = false;
  821. }
  822. break;
  823. }
  824. }
  825. }
  826. #define MY_YY_ALLOCSTEP 1000
  827. static void
  828. my_yymore(void)
  829. {
  830. static size_t yytext_size = 0;
  831. /* my_yytext is an ever-growing buffer. It will not ever
  832. * shrink, nor will it be freed at end of program, for now */
  833. while (my_yyleng + yyleng + 1 >= yytext_size) {
  834. my_yytext = realloc(my_yytext, yytext_size += MY_YY_ALLOCSTEP);
  835. }
  836. strncpy (my_yytext + my_yyleng, yytext, yyleng+1);
  837. my_yyleng += yyleng;
  838. }