home *** CD-ROM | disk | FTP | other *** search
- /*
- GIK/2 1.0.1 EWYBAPA.C 5621-432 (C) COPYRIGHT IBM CORP 1993. ALL RIGHTS RESERVED. LICENSED MATERIALS - PROPERTY OF IBM.
- */
- /**********************************************************************/
- /* */
- /* MODULE PROLOGUE */
- /* */
- /* COMPONENT NAME: C++ CLASS BROWSER EXAMPLE */
- /* */
- /* MODULE NAME: EWYBAPA.C */
- /* */
- /* DESCRIPTIVE NAME: C++ Parser */
- /* */
- /* PURPOSE: */
- /* */
- /* This module contains a small parser for c++ header files */
- /* */
- /* The parser should work for most c++ header files, even if not */
- /* syntactically correct. */
- /* */
- /* COPYRIGHT: (C) 1993 IBM Corporation */
- /* */
- /* DISCLAIMER OF WARRANTIES. The following [enclosed] code is */
- /* sample code created by IBM Corporation. This sample code is not */
- /* part of any standard or IBM product and is provided to you solely */
- /* for the purpose of assisting you in the development of your */
- /* applications. The code is provided "AS IS", without */
- /* warranty of any kind. IBM shall not be liable for any damages */
- /* arising out of your use of the sample code, even if they have been */
- /* advised of the possibility of such damages. */
- /**********************************************************************/
-
- #define INCL_DOS /* OS/2 definitions */
- #define INCL_PM /* PM definitions */
-
- /*--------------------------------------------------------------------*/
- /* HEADER FILES */
- /*--------------------------------------------------------------------*/
-
- #include <os2.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <malloc.h>
- #include <ewyga.h>
- #include "ewybapa.h"
- #include "ewybacc.h"
-
- /*--------------------------------------------------------------------*/
- /* CONSTANTS */
- /*--------------------------------------------------------------------*/
-
- #define MAX_IDENT_LEN 33
- #define MAX_PARENTS 32
- #define TOKEN_EOF 0
- #define TOKEN_BLANK 1
- #define TOKEN_CLASS 2
- #define TOKEN_IDENT 3
- #define TOKEN_LEFT_PARENTHESIS 4
- #define TOKEN_RIGHT_PARENTHESIS 5
- #define TOKEN_LEFT_BRACE 6
- #define TOKEN_RIGHT_BRACE 7
- #define TOKEN_SEMICOLON 8
- #define TOKEN_OTHER 9
-
- /*--------------------------------------------------------------------*/
- /* LOCAL VARIABLES */
- /*--------------------------------------------------------------------*/
-
- static int M_nLineNumber = 1;
- static int M_nNestParentheses = 0;
- static int M_nNestBraces = 0;
- static char M_szIdentifier[MAX_IDENT_LEN] = "";
- static char M_szClassName[MAX_IDENT_LEN] = "";
- static char M_szParentName[MAX_PARENTS][MAX_IDENT_LEN];
-
- /*--------------------------------------------------------------------*/
- /* LOCAL FUNCTIONS */
- /*--------------------------------------------------------------------*/
-
- static void ProcessFile(DHND hDiag,FILE *hFile);
- static void FillBuffer(char *pszBuffer,FILE *hFile,int nLen);
- static int GetToken(FILE *hFile);
- static void ReadString(int c,FILE *hFile);
- static void ReadBlockComment(FILE *hFile);
- static void ReadLineComment(FILE *hFile);
- static void ReadIdentifier(int c,FILE *hFile);
- static void ReadPreprocessor(FILE *hFile);
- static int IsIdentifierLetter(int c);
- static int IsKeyword(char *pszString);
-
- /*--------------------------------------------------------------------*/
- /* CODE */
- /*--------------------------------------------------------------------*/
- /**********************************************************************/
- /*** parse the c++ header file ** */
- /**********************************************************************/
-
- void ParseFile(DHND hDiag,char *pszFile)
- {
- FILE *hFile;
-
- hFile = fopen(pszFile, "r");
- if (hFile == NULL)
- return ;
- setvbuf(hFile, NULL, _IOFBF, 32000);
- ProcessFile(hDiag, hFile);
- fclose(hFile);
- return ;
- }
-
- /**********************************************************************/
- /*** read the c++ header file ** */
- /**********************************************************************/
-
- static void ProcessFile(DHND hDiag,FILE *hFile)
- {
- char *pszBuffer = NULL;
- char *pszClassParents[MAX_PARENTS];
- int nToken;
- int nNestBracesOld;
- int nParentCount;
- int p1,p2,pt,l;
- int i;
- int fSkip;
-
- nToken = GetToken(hFile);
- while (nToken != TOKEN_EOF)
- {
-
- /******************************************************************/
- /* ** continue until we have a class ** */
- /******************************************************************/
-
- if (nToken != TOKEN_CLASS)
- nToken = GetToken(hFile);
- else
- {
- while (TOKEN_IDENT != (nToken = GetToken(hFile)))
- {
- continue;
- }
- strcpy(M_szClassName, M_szIdentifier);
- nNestBracesOld = M_nNestBraces;
- for (i = 0; i < MAX_PARENTS; i++)
- pszClassParents[i] = NULL;
- nParentCount = 0;
- fSkip = 1;
- do
- {
- nToken = GetToken(hFile);
- if (nToken == TOKEN_IDENT)
- {
- strcpy(M_szParentName[nParentCount], M_szIdentifier);
- pszClassParents[nParentCount] = M_szParentName[nParentCount];
- nParentCount++;
- }
- if (nToken == TOKEN_SEMICOLON)
- {
- fSkip = 0;
- break;
- }
- } while (nToken != TOKEN_LEFT_BRACE);
- p1 = ftell(hFile);
- if (fSkip)
- {
-
- /**************************************************************/
- /* ** skip to } ** */
- /**************************************************************/
-
- while (M_nNestBraces > nNestBracesOld)
- {
- while (TOKEN_RIGHT_BRACE != (nToken = GetToken(hFile)))
- {
- continue;
- }
- }
- p2 = ftell(hFile)-2;
- l = p2-p1+1;
- pszBuffer = malloc(l+1);
- memset(pszBuffer, 0, l+1);
- pt = ftell(hFile);
- fseek(hFile, p1, SEEK_SET);
- FillBuffer(pszBuffer, hFile, l-1);
- fseek(hFile, pt, SEEK_SET);
- }
-
- /****************************************************************/
- /* ** all info ready, insert class now ** */
- /****************************************************************/
-
- InsertClass(hDiag, M_szClassName, pszClassParents, pszBuffer);
- }
- }
- }
-
- /**********************************************************************/
- /*** fill textbuffer from file ** */
- /**********************************************************************/
-
- static void FillBuffer(char *pszBuffer,FILE *hFile,int nLen)
- {
- char c;
- int i = 0;
-
- while (nLen > 0)
- {
- c = fgetc(hFile);
- pszBuffer[i] = c;
- if (c == '\n')
- nLen = nLen-2;
- else
- nLen = nLen-1;
- i++;
- }
- pszBuffer[i] = '\0';
- return ;
- }
-
- /**********************************************************************/
- /*** tokenizer ** */
- /**********************************************************************/
-
- static int GetToken(FILE *hFile)
- {
- int c;
-
- c = fgetc(hFile);
- while ((c == ' ') || (c == '\t'))
- c = fgetc(hFile);
- switch (c)
- {
- case '{' :
- M_nNestBraces++;
- return (TOKEN_LEFT_BRACE);
- case '}' :
- M_nNestBraces--;
- return (TOKEN_RIGHT_BRACE);
- case '(' :
- M_nNestParentheses++;
- return (TOKEN_LEFT_PARENTHESIS);
- case ')' :
- M_nNestParentheses--;
- return (TOKEN_RIGHT_PARENTHESIS);
- case ';' :
- return (TOKEN_SEMICOLON);
- case EOF :
- return (TOKEN_EOF);
- case '\n' :
- M_nLineNumber++;
- return (TOKEN_BLANK);
- case '#' :
- ReadPreprocessor(hFile);
- return (TOKEN_BLANK);
- case '\'' :
- case '"' :
- ReadString(c, hFile);
- return (TOKEN_OTHER);
- case '/' :
- c = fgetc(hFile);
- if (c == '*')
- {
- ReadBlockComment(hFile);
- return (TOKEN_BLANK);
- }
- if (c == '/')
- {
- ReadLineComment(hFile);
- return (TOKEN_BLANK);
- }
- ungetc(c, hFile);
- return (TOKEN_OTHER);
- default :
- if (IsIdentifierLetter(c))
- {
- ReadIdentifier(c, hFile);
- if (!strcmp(M_szIdentifier, "class"))
- return (TOKEN_CLASS);
- if (IsKeyword(M_szIdentifier))
- return (TOKEN_OTHER);
- return (TOKEN_IDENT);
- }
- return (TOKEN_OTHER);
- }
- return (TOKEN_OTHER);
- }
-
- /**********************************************************************/
- /*** read a string ** */
- /**********************************************************************/
-
- static void ReadString(int c,FILE *hFile)
- {
- int cStop;
-
- cStop = c;
- while (cStop != (c = fgetc(hFile)))
- {
- if (c == '\\')
- fgetc(hFile);
- if (c == EOF)
- break;
- }
- return ;
- }
-
- /**********************************************************************/
- /*** read a C comment ** */
- /**********************************************************************/
-
- static void ReadBlockComment(FILE *hFile)
- {
- int c;
-
- c = fgetc(hFile);
- while (c != EOF)
- {
- if (c == '*')
- {
- c = fgetc(hFile);
- if (c == '/')
- return ;
- }
- else
- {
- if (c == '\n')
- M_nLineNumber++;
- c = fgetc(hFile);
- }
- }
- return ;
- }
-
- /**********************************************************************/
- /*** read a C++ comment ** */
- /**********************************************************************/
-
- static void ReadLineComment(FILE *hFile)
- {
- int c;
-
- c = fgetc(hFile);
- while (c != EOF)
- {
- if (c == '\n')
- {
- M_nLineNumber++;
- return ;
- }
- c = fgetc(hFile);
- }
- return ;
- }
-
- /**********************************************************************/
- /*** read an identifier ** */
- /**********************************************************************/
-
- static void ReadIdentifier(int c,FILE *hFile)
- {
- int nLen = 0;
-
- M_szIdentifier[nLen++] = (char)c;
- while (IsIdentifierLetter(c = fgetc(hFile)))
- {
- if (nLen > MAX_IDENT_LEN)
- break;
- M_szIdentifier[nLen++] = (char)c;
- }
- M_szIdentifier[nLen] = '\0';
- ungetc(c, hFile);
- }
-
- /**********************************************************************/
- /*** read a preprocessor statement ** */
- /**********************************************************************/
-
- static void ReadPreprocessor(FILE *hFile)
- {
- int c;
-
- c = fgetc(hFile);
- while (c != EOF)
- {
- if (c == '\\')
- {
- c = fgetc(hFile);
- if (c != '\n')
- continue;
- M_nLineNumber++;
- c = fgetc(hFile);
- continue;
- }
- else
- if (c == '\n')
- {
- M_nLineNumber++;
- return ;
- }
- else
- {
- c = fgetc(hFile);
- }
- }
- return ;
- }
-
- /**********************************************************************/
- /*** check, if valid char in identifier ** */
- /**********************************************************************/
-
- static int IsIdentifierLetter(int c)
- {
- return (isalnum(c) || (c == '_'));
- }
-
- /**********************************************************************/
- /*** check, if valid keyword ** */
- /**********************************************************************/
-
- static int IsKeyword(char *pszString)
- {
- if (strcmp(pszString, "auto") == 0)
- return 1;
- if (strcmp(pszString, "char") == 0)
- return 1;
- if (strcmp(pszString, "class") == 0)
- return 1;
- if (strcmp(pszString, "const") == 0)
- return 1;
- if (strcmp(pszString, "double") == 0)
- return 1;
- if (strcmp(pszString, "enum") == 0)
- return 1;
- if (strcmp(pszString, "extern") == 0)
- return 1;
- if (strcmp(pszString, "float") == 0)
- return 1;
- if (strcmp(pszString, "int") == 0)
- return 1;
- if (strcmp(pszString, "long") == 0)
- return 1;
- if (strcmp(pszString, "register") == 0)
- return 1;
- if (strcmp(pszString, "short") == 0)
- return 1;
- if (strcmp(pszString, "signed") == 0)
- return 1;
- if (strcmp(pszString, "static") == 0)
- return 1;
- if (strcmp(pszString, "struct") == 0)
- return 1;
- if (strcmp(pszString, "union") == 0)
- return 1;
- if (strcmp(pszString, "unsigned") == 0)
- return 1;
- if (strcmp(pszString, "void") == 0)
- return 1;
- if (strcmp(pszString, "volatile") == 0)
- return 1;
- if (strcmp(pszString, "break") == 0)
- return 1;
- if (strcmp(pszString, "case") == 0)
- return 1;
- if (strcmp(pszString, "continue") == 0)
- return 1;
- if (strcmp(pszString, "default") == 0)
- return 1;
- if (strcmp(pszString, "do") == 0)
- return 1;
- if (strcmp(pszString, "else") == 0)
- return 1;
- if (strcmp(pszString, "for") == 0)
- return 1;
- if (strcmp(pszString, "goto") == 0)
- return 1;
- if (strcmp(pszString, "if") == 0)
- return 1;
- if (strcmp(pszString, "private") == 0)
- return 1;
- if (strcmp(pszString, "protected") == 0)
- return 1;
- if (strcmp(pszString, "public") == 0)
- return 1;
- if (strcmp(pszString, "return") == 0)
- return 1;
- if (strcmp(pszString, "sizeof") == 0)
- return 1;
- if (strcmp(pszString, "switch") == 0)
- return 1;
- if (strcmp(pszString, "typedef") == 0)
- return 1;
- if (strcmp(pszString, "while") == 0)
- return 1;
- return 0;
- }