📄 scan.h
字号:
/* scan.h -- some scanner related functions *//* This is part of cdg - a C-source Documentation Generator. Copyright (C) 1995, 1996 Peter Knoppers. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*//* * This code is included only in "scan.c", which is the scanner that is * generated from the lex-file "scan.l". * By keeping the self-written C-code of the scanner separate we can use the * cross reference generator on itself without having to cross reference and * list the unreadable (and uninteresting) lex-generated C-code. */#include <errno.h>#ifndef YYLMAX#define YYLMAX 8192#endifint yywrap(void){ return(1);}/* * nextinputfile: Open a file for scanning (optionally closing the * current one). * arguments: * char * filename: Name of the next input file. * returns: Int, 0 on success, nonzero if an error occurred. */int nextinputfile (char * filename){ static FILE * inputfp = (FILE *) 0; if (inputfp != (FILE *) 0) fclose (inputfp); if ((inputfp = fopen (filename, "r")) == (FILE *) 0) { fprintf (stderr, "Cannot open file %s: %s\n", filename, sys_errlist[errno]); return (errno); } yyin = inputfp; YY_NEW_FILE; totalinsertednewlines += yylineno - 1; yylineno = 1; return (0);}/* States for macro; the cpp-command handling function: */enum{ FINDNONWHITE, /* skip white space until cpp-command */ FINDTYPE, /* find end of cpp-command */ SKIPREST, /* eatup rest of cpp-command */ FINDDEFINED, /* find identifier that is being defined */ FINDREFS /* find referenced identifiers */};/* * macro: Handle a C preprocessor command. * This function tries to recognize those words that get * #define'd and those that are referenced in a macro. * For a #define'd word emit a tag of type 'M'. * For each referenced word emit a tag of type 'm'. * It is probably possible to create macros that will * confuse this function... * arguments: None. * returns: Nothing. * remark: This is not a very readable function... */void macro(void){ char c; char * cp = yytext; int state = FINDNONWHITE; int lineno = yylineno + totalinsertednewlines; update_insertednewlines (); copyout ('#'); while ((c = input()) >= 0) { copyout (*cp++ = c); if (c == '\\') { copyout (*cp++ = c = input()); if (c == '\r') /* if reading MS-DOS file... */ copyout (*cp++ = c = input()); if (c == '\n') /* escaped newline */ *--cp = c = ' '; /* replace by space */ } else if (c == '/') { if ((c = input()) == '*') /* comment inside a macro */ { /* * I'm not really sure what happens if a comment starts inside * a macro and extends onto the next line... */ copyout (c); while ((c = input()) >= 0) { copyout (c); if (c == '*') { if ((c = input()) == '/') break; if (c != 0) unput (c); } } copyout (c); continue; } else { /* it's not a comment */ unput (c); c = '/'; } } if (state == FINDNONWHITE) if (isspace (c)) cp = yytext; /* eatup white space */ else state = FINDTYPE; if ((isalnum (c)) || (c == '_')) { /* Identifiers are never split over several input lines. */ lineno = yylineno + totalinsertednewlines; continue; /* continue until end of word */ } if (c == '\'') /* literal char */ { if ((c = input()) == '\\') { copyout (c); c = input(); } copyout (c); copyout (c = input()); continue; } if (c == '\"') /* found a string... */ { /* eatup whole string */ while ((c = input ()) >= 0) { copyout (c); if (c == '\\') copyout (c = input()); else if (c == '\"') break; } update_insertednewlines (); cp = yytext; continue; } /* * cp is pointing to first non-alphanumeric char in yytext. */ if (cp > yytext) cp--; /* backup one position */ *cp = '\0'; /* add termination */ /* * See what we've got... */ if (state == FINDTYPE) { if (strncasecmp (yytext, "if", 2) == 0) state = FINDREFS; /* #ifdef, #ifndef, or #if */ else if (strncasecmp (yytext, "define", 6) == 0) state = FINDDEFINED; /* #define */ else state = SKIPREST; /* other cpp-command: ignore */ } else if ((state == FINDDEFINED) && (cp > yytext)) { maketag (lineno, yytext, 'M'); state = FINDREFS; } else if ((state == FINDREFS) && (cp > yytext) && (isalpha (*yytext) || (*yytext == '_'))) maketag (lineno, yytext, 'm'); /* else ignore */ cp = yytext; /* reset */ update_insertednewlines (); if (c == '\n') break; /* end of cpp-command */ }}/* * string: Parse a string-literal and copy it to the outputfile * and yytext and update yyleng. * arguments: None. * returns: Nothing. * remark: If a string (almost) exceeds the size of yytext, than * the program is aborted with an explanation. */void string(void){ char c; char * cp = yytext; update_insertednewlines (); copyout ('\"'); while ((c = input()) >= 0) { if (cp - yytext >= YYLMAX - 2) { fprintf (stderr, "Line %d: string too long. %s %d\n", yylineno, "Recompile with YYLMAX >", YYLMAX); exit(1); } if (c == '\n') { fprintf (stderr, "Line %d: un-escaped newline in string constant\n", yylineno); unput (c); /* should eventually recover */ break; } if (c == '\\') { copyout (c); copyout (c = input()); if (c == '\r') /* if reading MS-DOS file... */ copyout (c = input()); if (c != '\n') { /* escaped non-newline */ *cp++ = '\\'; *cp++ = c; } } else if (c == '\"') { copyout (c); break; /* closing quote */ } else copyout (*cp++ = c); } *cp = '\0'; yyleng = cp - yytext;}/* * comment: Parse one comment and copy it to the outputfile. * arguments: None. * returns: Nothing. */void comment(void){ char c; update_insertednewlines (); copyout ('/'); copyout ('*'); while ((c = input()) >= 0) { copyout (c); if (c == '*') { if ((c = input()) == '/') break; if (c != 0) unput (c); } } copyout (c);}/* * count: Copy contents of yytext to outputfile. * arguments: None. * returns: Nothing. * remark: The name of this function no longer explains what it * actually does... */void count(void){ char * cp; update_insertednewlines (); for (cp = yytext; *cp != '\0'; cp++) copyout (*cp);}char identifierstring[MAXIDENTIFIER + 1];/* * Anything that looks like an identifier and is not a keyword is assumed * to be an identifier; that includes all typedef'd, and other user-made types. *//* * identifier: Copy contents of yytext into identifierstring * arguments: None. * returns: Int, the (constant) IDENTIFIER. */int identifier (void){ strncpy (identifierstring, yytext, MAXIDENTIFIER); identifierstring[MAXIDENTIFIER] = '\0'; /* make sure */ return (IDENTIFIER);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -