📄 fscanner.l
字号:
%{/*=========================================================================== Copyright (c) 1998-2000, The Santa Cruz Operation All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: *Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. *Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. *Neither name of The Santa Cruz Operation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. =========================================================================*//* cscope - interactive C symbol cross-reference * * C symbol scanner */#include "global.h"#include "scanner.h"#include "lookup.h"#include <assert.h>/* the line counting has been moved from character reading for speed *//* comments are discarded */#ifndef FLEX_SCANNER# error Sorry, this scanner needs flex. It is not usable with AT&T Lex.#endif#define IFLEVELINC 5 /* #if nesting level size increment */static char const rcsid[] = "$Id: fscanner.l,v 1.9 2002/06/20 16:25:33 broeker Exp $";int first; /* buffer index for first char of symbol */int last; /* buffer index for last char of symbol */int lineno; /* symbol line number */int myylineno = 1;/* HBB 20001007: new variables, emulating yytext in a way that allows * the yymore() simulation, my_yymore(), to be used even in the presence of * yyless(). */size_t my_yyleng = 0;char *my_yytext = NULL;static BOOL arraydimension; /* inside array dimension declaration */static BOOL bplisting; /* breakpoint listing */static int braces; /* unmatched left brace count */static BOOL classdef; /* c++ class definition */static BOOL elseelif; /* #else or #elif found */static BOOL esudef; /* enum/struct/union global definition */static BOOL external; /* external definition */static int externalbraces; /* external definition outer brace count */static BOOL fcndef; /* function definition */static BOOL global; /* file global scope (outside functions) */static int iflevel; /* #if nesting level */static BOOL initializer; /* data initializer */static int initializerbraces; /* data initializer outer brace count */static BOOL lex; /* lex file */static int miflevel = IFLEVELINC; /* maximum #if nesting level */static int *maxifbraces; /* maximum brace count within #if */static int *preifbraces; /* brace count before #if */static int parens; /* unmatched left parenthesis count */static BOOL ppdefine; /* preprocessor define statement */static BOOL pseudoelif; /* pseudo-#elif */static BOOL oldtype; /* next identifier is an old type */static BOOL rules; /* lex/yacc rules */static BOOL sdl; /* sdl file */static BOOL structfield; /* structure field declaration */static int tagdef; /* class/enum/struct/union tag definition */static BOOL template; /* function template */static int templateparens; /* function template outer parentheses count */static int typedefbraces = -1; /* initial typedef brace count */static int token; /* token found */static int ident_start; /* begin of preceding identifier *//* If this is defined to 1, use flex rules rather than the input * function to discard comments. The scanner gains quite a bit of * speed this way, because of a large reduction of the number of I/O * system/library calls. The original skipcomment_input() called * getc() so often that the call overhead of shared libraries * vs. static linking, alone, already caused a sizeable performance * hit (up to 40% gross gain on a cscope -cub of its own source * dir). */#define COMMENTS_BY_FLEX 1#if !COMMENTS_BY_FLEXstatic int skipcomment_input(void);static int comment(void);static int insidestring_input(int);#endifstatic void my_yymore(void);#if COMMENTS_BY_FLEX# define skipcomment_input input#else# define YY_INPUT(buf,result,max_size) \{ \ int c = skipcomment_input (); \ result = (c == EOF) ? YY_NULL : (buf[0] = c, 1); \}#endif /* !COMMENTS_BY_FLEX*/%}identifier [a-zA-Z_$][a-zA-Z_0-9$]*number \.?[0-9][.0-9a-fA-FlLuUxX]*comment "/*"([^*]*("*"+[^/])?)*"*/"|"//"[^\n]*\nws [ \t\r\v\f]wsnl [ \t\n]|{comment}/* flex options: stack of start conditions, and don't use yywrap() */%option stack%option noyywrap%start SDL%a 4000%o 7000/* exclusive start conditions. not available in AT&T lex -> use flex! */%x IN_PREPROC WAS_ENDIF WAS_IDENTIFIER WAS_ESU IN_DQUOTE IN_SQUOTE COMMENT%%%\{ { /* lex/yacc C declarations/definitions */ global = YES; goto more; /* NOTREACHED */ }%\} { global = NO; goto more; /* NOTREACHED */ }^%% { /* lex/yacc rules delimiter */ braces = 0; if (rules == NO) { /* this %% starts the section containing the rules */ rules = YES; /* Copy yytext to private buffer, to be able to add further * content following it: */ my_yymore(); /* simulate a yylex() or yyparse() definition */ (void) strcat(my_yytext, " /* "); first = strlen(my_yytext); if (lex == YES) { (void) strcat(my_yytext, "yylex"); } else { /* yacc: yyparse implicitly calls yylex */ char *s = " yylex()"; char *cp = s + strlen(s); while (--cp >= s) { unput(*cp); } (void) strcat(my_yytext, "yyparse"); } last = strlen(my_yytext); (void) strcat(my_yytext, " */"); my_yyleng = strlen(my_yytext); return(FCNDEF); } else { /* were in the rules section, now comes the closing one */ rules = NO; global = YES; last = first; my_yymore(); return(FCNEND); /* NOTREACHED */ } }<SDL>STATE[ \t]+({identifier}|\*) { /* sdl state, treat as function def */ braces = 1; fcndef = YES; token = FCNDEF; goto findident; /* NOTREACHED */ }<SDL>ENDSTATE[ \t] { /* end of an sdl state, treat as end of a function */ goto endstate; /* NOTREACHED */ }\{ { /* count unmatched left braces for fcn def detection */ ++braces; /* mark an untagged enum/struct/union so its beginning can be found */ if (tagdef) { if (braces == 1) { esudef = YES; } token = tagdef; tagdef = '\0'; last = first; my_yymore(); return(token); } goto more; /* NOTREACHED */ }\#{ws}* { /* start a preprocessor line */ if (rules == NO) /* don't consider CPP for lex/yacc rules */ BEGIN(IN_PREPROC); yyleng = 1; /* get rid of the blanks, if any */ goto more; /* NOTREACHED */ }<IN_PREPROC>endif([^a-zA-Z0-9_$\n].*)? { /* #endif */ /* delay treatment of #endif depending on whether an * #if comes right after it, or not */ /* HBB 20010619: new pattern allows trailing garbage * after the #endif */ BEGIN(WAS_ENDIF); goto more; /* NOTREACHED */ }<WAS_ENDIF>\n{wsnl}*#{ws}*if(ndef|def)?{ws}+ { /* attempt to correct erroneous brace count caused by: * * #if ... * ... { * #endif * #if ... * ... { * #endif */ /* the current #if must not have an #else or #elif */ if (elseelif == YES) { goto endif; /* NOTREACHED */ } pseudoelif = YES; BEGIN(INITIAL); yyless(1); /* rescan all but the line ending */ yy_set_bol(1); goto eol; /* NOTREACHED */ }<WAS_ENDIF>\n{wsnl}* { /* an #endif with no #if right after it */ endif: if (iflevel > 0) { /* get the maximum brace count for this #if */ if (braces < maxifbraces[--iflevel]) { braces = maxifbraces[iflevel]; } } BEGIN(INITIAL); yyless(1); yy_set_bol(1); goto eol; /* NOTREACHED */ }<IN_PREPROC>ifndef{ws}+ |<IN_PREPROC>ifdef{ws}+ |<IN_PREPROC>if{ws}+ { /* #if directive */ elseelif = NO; if (pseudoelif == YES) { pseudoelif = NO; goto elif; /* NOTREACHED */ } /* make sure there is room for the current brace count */ if (iflevel == miflevel) { miflevel += IFLEVELINC; maxifbraces = myrealloc(maxifbraces, miflevel * sizeof(int)); preifbraces = myrealloc(preifbraces, miflevel * sizeof(int)); } /* push the current brace count */ preifbraces[iflevel] = braces; maxifbraces[iflevel++] = 0; BEGIN(INITIAL); goto more; /* NOTREACHED */ }<IN_PREPROC>else({ws}.*)? { /* #else --- eat up whole line */ elseelif = YES; if (iflevel > 0) { /* save the maximum brace count for this #if */ if (braces > maxifbraces[iflevel - 1]) { maxifbraces[iflevel - 1] = braces; } /* restore the brace count to before the #if */ braces = preifbraces[iflevel - 1]; } BEGIN(INITIAL); goto more; /* NOTREACHED */ }<IN_PREPROC>elif{ws}+ { /* #elif */ /* elseelif = YES; --- HBB I doubt this is correct */ elif: if (iflevel > 0) { /* save the maximum brace count for this #if */ if (braces > maxifbraces[iflevel - 1]) { maxifbraces[iflevel - 1] = braces; } /* restore the brace count to before the #if */ braces = preifbraces[iflevel - 1]; } BEGIN(INITIAL); goto more; /* NOTREACHED */ }<IN_PREPROC>include{ws}*\"[^"\n]+\" |<IN_PREPROC>include{ws}*<[^>\n]+> { /* #include file */ char *s; char remember = yytext[yyleng-1]; my_yymore(); s = strpbrk(my_yytext, "\"<"); my_yytext[my_yyleng-1] = '\0'; incfile(s + 1, s); my_yytext[my_yyleng-1] = remember; first = s - my_yytext; last = my_yyleng - 1; if (compress == YES) { my_yytext[0] = '\2'; /* compress the keyword */ } BEGIN(INITIAL); return(INCLUDE); /* NOTREACHED */ }\} { /* could be the last enum member initializer */ if (braces == initializerbraces) { initializerbraces = -1; initializer = NO; } if (--braces <= 0) { endstate: braces = 0; classdef = NO; } if (braces == 0 || (braces == 1 && classdef == YES)) { /* if the end of an enum/struct/union definition */ if (esudef == YES) { esudef = NO; } /* if the end of the function */ else if (fcndef == YES) { fcndef = NO; last = first; my_yymore(); return(FCNEND); } } goto more; /* NOTREACHED */ }\( { /* count unmatched left parentheses for function templates */ ++parens; goto more; /* NOTREACHED */ }\) { if (--parens <= 0) { parens = 0; } /* if the end of a function template */ if (parens == templateparens) { templateparens = -1; template = NO; } goto more; /* NOTREACHED */ }= { /* if a global definition initializer */ if (global == YES && ppdefine == NO && my_yytext[0] != '#') { initializerbraces = braces; initializer = YES; } goto more; /* NOTREACHED */ }: { /* a if global structure field */ if (global == YES && ppdefine == NO && my_yytext[0] != '#') { structfield = YES; } goto more; /* NOTREACHED */ }\, { if (braces == initializerbraces) { initializerbraces = -1; initializer = NO; } structfield = NO; goto more; /* NOTREACHED */ }; { /* if the enum/struct/union was not a definition */ if (braces == 0) { esudef = NO; } /* if the end of a typedef */ if (braces == typedefbraces) { typedefbraces = -1; } /* if the end of a external definition */ if (braces == externalbraces) { externalbraces = -1; external = NO; } structfield = NO; initializer = NO; goto more; /* NOTREACHED */ }<IN_PREPROC>define{ws}+{identifier} { /* preprocessor macro or constant definition */ ppdefine = YES; token = DEFINE; if (compress == YES) { my_yytext[0] = '\1'; /* compress the keyword */ } findident: /* search backwards through yytext[] to find the identifier */ /* NOTE: this had better be left to flex, by use of * yet another starting condition */ my_yymore(); first = my_yyleng - 1; while (my_yytext[first] != ' ' && my_yytext[first] != '\t') { --first; } ++first; last = my_yyleng; BEGIN(INITIAL); goto definition; /* NOTREACHED */ }<IN_PREPROC>.|\n |<IN_PREPROC>{identifier} { /* unknown preprocessor line */ BEGIN(INITIAL); goto more; /* NOTREACHED */ }class{wsnl}+{identifier}({wsnl}|{identifier}|[():])*\{ { /* class definition */ classdef = YES; tagdef = 'c'; yyless(5); /* eat up 'class', and re-scan */ yy_set_bol(0); goto more; /* NOTREACHED */ }("enum"|"struct"|"union") { ident_start = first; BEGIN(WAS_ESU); goto more; }<WAS_ESU>{({wsnl}+{identifier}){wsnl}*\{ { /* e/s/u definition */ tagdef = my_yytext[ident_start]; BEGIN(WAS_IDENTIFIER); goto ident; }{wsnl}*\{ { /* e/s/u definition without a tag */ tagdef = my_yytext[ident_start]; BEGIN(INITIAL); if (braces == 0) { esudef = YES; } last = first; yyless(0); /* re-scan all this as normal text */ tagdef = '\0'; goto more; }({wsnl}+{identifier})?{wsnl}* | .|\n { /* e/s/u usage */ BEGIN(WAS_IDENTIFIER); goto ident; }}if{wsnl}*\( { /* ignore 'if' */ yyless(2); yy_set_bol(0); goto more;} {identifier} { /* identifier found: do nothing, yet. (!) */ BEGIN(WAS_IDENTIFIER); ident_start = first; goto more; /* NOTREACHED */ }<WAS_IDENTIFIER>{ {ws}*\(({wsnl}|{identifier}|[*&[\]=,.])*\)([()]|{wsnl})*[:a-zA-Z_#{] { /* a function definition */ /* note: "#define a (b) {" and "#if defined(a)\n#"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -