ENH: Initial import of java parser

This commit is contained in:
Andy Cedilnik 2005-01-28 17:13:58 -05:00
parent 3a67582df8
commit ab475733e4
5 changed files with 10028 additions and 0 deletions

File diff suppressed because it is too large Load Diff

3226
Source/cmDependsJavaParser.y Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,433 @@
/*=========================================================================
Program: CMake - Cross-Platform Makefile Generator
Module: $RCSfile$
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "cmDependsJavaParserHelper.h"
#include "cmSystemTools.h"
#include "cmDependsJavaLexer.h"
int cmDependsJava_yyparse( yyscan_t yyscanner );
cmDependsJavaParserHelper::cmDependsJavaParserHelper()
{
this->CurrentDepth = 0;
this->UnionsAvailable = 0;
this->LastClassId = 0;
CurrentClass tl;
tl.Name = "*";
this->ClassStack.push_back(tl);
}
cmDependsJavaParserHelper::~cmDependsJavaParserHelper()
{
this->CleanupParser();
}
void cmDependsJavaParserHelper::CurrentClass::AddFileNamesForPrinting(std::vector<cmStdString> *files, const char* prefix, const char* sep)
{
cmStdString rname = "";
if ( prefix )
{
rname += prefix;
rname += sep;
}
rname += this->Name;
files->push_back(rname);
std::vector<CurrentClass>::iterator it;
for ( it = this->NestedClasses.begin();
it != this->NestedClasses.end();
++ it )
{
it->AddFileNamesForPrinting(files, rname.c_str(), sep);
}
}
void cmDependsJavaParserHelper::DeallocateParserType(char** pt)
{
if (!pt)
{
return;
}
if (!*pt)
{
//*pt = 0;
return;
}
// std::cout << (void*) *pt << " " << *pt << " this->DeallocateParserType" << std::endl;
//delete [] *pt;
*pt = 0;
this->UnionsAvailable --;
}
void cmDependsJavaParserHelper::AddClassFound(const char* sclass)
{
if( ! sclass )
{
return;
}
std::vector<cmStdString>::iterator it;
for ( it = this->ClassesFound.begin();
it != this->ClassesFound.end();
it ++ )
{
if ( *it == sclass )
{
return;
}
}
this->ClassesFound.push_back(sclass);
}
void cmDependsJavaParserHelper::AddPackagesImport(const char* sclass)
{
std::vector<cmStdString>::iterator it;
for ( it = this->PackagesImport.begin();
it != this->PackagesImport.end();
it ++ )
{
if ( *it == sclass )
{
return;
}
}
this->PackagesImport.push_back(sclass);
}
void cmDependsJavaParserHelper::SafePrintMissing(const char* str, int line, int cnt)
{
if ( str )
{
//std::cout << (void*) str << " JPSafePrintMissing" << std::endl;
std::cout << line << " String " << cnt << " exists: ";
unsigned int cc;
for ( cc = 0; cc < strlen(str); cc ++ )
{
unsigned char ch = str[cc];
if ( ch >= 32 && ch <= 126 )
{
std::cout << (char)ch;
}
else
{
std::cout << "<" << (int)ch << ">";
break;
}
}
std::cout << "- " << strlen(str) << std::endl;
}
}
void cmDependsJavaParserHelper::Print(const char* place, const char* str)
{
if ( this->Verbose )
{
std::cout << "[" << place << "=" << str << "]" << std::endl;
}
}
void cmDependsJavaParserHelper::CombineUnions(char** out, const char* in1, char** in2,
const char* sep)
{
int len = 1;
if ( in1 )
{
len += strlen(in1);
}
if ( *in2 )
{
len += strlen(*in2);
}
if ( sep )
{
len += strlen(sep);
}
*out = new char [ len ];
*out[0] = 0;
if ( in1 )
{
strcat(*out, in1);
}
if ( sep )
{
strcat(*out, sep);
}
if ( *in2 )
{
strcat(*out, *in2);
}
if ( *in2 )
{
this->DeallocateParserType(in2);
}
// std::cout << (void*) *out << " " << *out << " JPAllocateParserType" << std::endl;
this->UnionsAvailable ++;
}
void cmDependsJavaParserHelper::CheckEmpty(int line, int cnt, cmDependsJavaParserHelper::ParserType* pt)
{
int cc;
int kk = -cnt + 1;
for ( cc = 1; cc <= cnt; cc ++)
{
cmDependsJavaParserHelper::ParserType* cpt = pt + kk;
this->SafePrintMissing(cpt->str, line, cc);
kk ++;
}
}
void cmDependsJavaParserHelper::PrepareElement(cmDependsJavaParserHelper::ParserType* me)
{
// Inititalize self
me->str = 0;
}
void cmDependsJavaParserHelper::AllocateParserType(cmDependsJavaParserHelper::ParserType* pt,
const char* str, int len = 0)
{
pt->str = 0;
if ( len == 0 )
{
len = strlen(str);
}
if ( len == 0 )
{
return;
}
this->UnionsAvailable ++;
pt->str = new char[ len + 1 ];
strncpy(pt->str, str, len);
pt->str[len] = 0;
this->Allocates.push_back(pt->str);
// std::cout << (void*) pt->str << " " << pt->str << " JPAllocateParserType" << std::endl;
}
void cmDependsJavaParserHelper::StartClass(const char* cls)
{
CurrentClass cl;
cl.Name = cls;
this->ClassStack.push_back(cl);
this->CurrentDepth ++;
}
void cmDependsJavaParserHelper::EndClass()
{
CurrentClass* parent = 0;
CurrentClass* current = 0;
if ( this->ClassStack.size() > 0 )
{
current = &(*(this->ClassStack.end() - 1));
if ( this->ClassStack.size() > 1 )
{
parent = &(*(this->ClassStack.end() - 2));
}
}
if ( current == 0 )
{
std::cerr << "Error when parsing. Current class is null" << std::endl;
abort();
}
if ( parent == 0 )
{
std::cerr << "Error when parsing. Parent class is null" << std::endl;
abort();
}
this->CurrentDepth --;
parent->NestedClasses.push_back(*current);
this->ClassStack.erase(this->ClassStack.end()-1, this->ClassStack.end());
}
void cmDependsJavaParserHelper::PrintClasses()
{
if ( this->ClassStack.size() == 0 )
{
std::cerr << "Error when parsing. No classes on class stack" << std::endl;
abort();
}
std::vector<cmStdString> files = this->GetFilesProduced();
std::vector<cmStdString>::iterator sit;
for ( sit = files.begin();
sit != files.end();
++ sit )
{
std::cout << " " << sit->c_str() << ".class" << std::endl;
}
}
std::vector<cmStdString> cmDependsJavaParserHelper::GetFilesProduced()
{
std::vector<cmStdString> files;
CurrentClass* toplevel = &(*(this->ClassStack.begin()));
std::vector<CurrentClass>::iterator it;
for ( it = toplevel->NestedClasses.begin();
it != toplevel->NestedClasses.end();
++ it )
{
it->AddFileNamesForPrinting(&files, 0, "$");
}
return files;
}
int cmDependsJavaParserHelper::ParseString(const char* str, int verb)
{
if ( !str)
{
return 0;
}
//printf("Do some parsing: %s\n", str);
this->Verbose = verb;
this->InputBuffer = str;
this->InputBufferPos = 0;
this->CurrentLine = 0;
yyscan_t yyscanner;
cmDependsJava_yylex_init(&yyscanner);
cmDependsJava_yyset_extra(this, yyscanner);
int res = cmDependsJava_yyparse(yyscanner);
cmDependsJava_yylex_destroy(yyscanner);
if ( res != 0 )
{
std::cout << "JP_Parse returned: " << res << std::endl;
return 0;
}
if ( verb )
{
if ( this->CurrentPackage.size() > 0 )
{
std::cout << "Current package is: " << this->CurrentPackage.c_str() << std::endl;
}
std::cout << "Imports packages:";
if ( this->PackagesImport.size() > 0 )
{
std::vector<cmStdString>::iterator it;
for ( it = this->PackagesImport.begin();
it != this->PackagesImport.end();
++ it )
{
std::cout << " " << it->c_str();
}
}
std::cout << std::endl;
std::cout << "Depends on:";
if ( this->ClassesFound.size() > 0 )
{
std::vector<cmStdString>::iterator it;
for ( it = this->ClassesFound.begin();
it != this->ClassesFound.end();
++ it )
{
std::cout << " " << it->c_str();
}
}
std::cout << std::endl;
std::cout << "Generated files:" << std::endl;
this->PrintClasses();
if ( this->UnionsAvailable != 0 )
{
std::cout << "There are still " << this->UnionsAvailable << " unions available" << std::endl;
//return 0;
}
}
this->CleanupParser();
return 1;
}
void cmDependsJavaParserHelper::CleanupParser()
{
std::vector<char*>::iterator it;
for ( it = this->Allocates.begin();
it != this->Allocates.end();
++ it )
{
delete [] *it;
}
this->Allocates.erase(this->Allocates.begin(),
this->Allocates.end());
}
int cmDependsJavaParserHelper::LexInput(char* buf, int maxlen)
{
//std::cout << "JPLexInput ";
//std::cout.write(buf, maxlen);
//std::cout << std::endl;
if ( maxlen < 1 )
{
return 0;
}
if ( this->InputBufferPos < this->InputBuffer.size() )
{
buf[0] = this->InputBuffer[ this->InputBufferPos++ ];
if ( buf[0] == '\n' )
{
this->CurrentLine ++;
}
return(1);
}
else
{
buf[0] = '\n';
return( 0 );
}
}
void cmDependsJavaParserHelper::Error(const char* str)
{
fprintf(stderr, "JPError: %s (%d / Line: %d)\n", str, this->InputBufferPos, this->CurrentLine);
int cc;
std::cerr << "String: [";
for ( cc = 0; cc < 30 && *(this->InputBuffer.c_str() + this->InputBufferPos + cc);
cc ++ )
{
std::cerr << *(this->InputBuffer.c_str() + this->InputBufferPos + cc);
}
std::cerr << "]" << std::endl;
}
void cmDependsJavaParserHelper::UpdateCombine(const char* str1, const char* str2)
{
if ( this->CurrentCombine == "" && str1 != 0)
{
this->CurrentCombine = str1;
}
this->CurrentCombine += ".";
this->CurrentCombine += str2;
}
int cmDependsJavaParserHelper::ParseFile(const char* file)
{
if ( !cmSystemTools::FileExists(file))
{
return 0;
}
std::ifstream ifs(file);
if ( !ifs )
{
return 0;
}
cmStdString fullfile = "";
cmStdString line;
while ( cmSystemTools::GetLineFromStream(ifs, line) )
{
fullfile += line + "\n";
}
return this->ParseString(fullfile.c_str(), 0);
}

View File

@ -0,0 +1,108 @@
/*=========================================================================
Program: CMake - Cross-Platform Makefile Generator
Module: $RCSfile$
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef cmDependsJavaParserHelper_h
#define cmDependsJavaParserHelper_h
#include "cmStandardIncludes.h"
#define YYSTYPE cmDependsJavaParserHelper::ParserType
#define YYSTYPE_IS_DECLARED
#define YY_EXTRA_TYPE cmDependsJavaParserHelper*
#define YY_DECL int cmDependsJava_yylex(YYSTYPE* yylvalp, yyscan_t yyscanner)
/** \class cmDependsJavaParserHelper
* \brief Helper class for parsing java source files
*
* Finds dependencies for java file and list of outputs
*/
class cmDependsJavaParserHelper
{
public:
typedef struct {
char* str;
} ParserType;
cmDependsJavaParserHelper();
~cmDependsJavaParserHelper();
int ParseString(const char* str, int verb);
int ParseFile(const char* file);
// For the lexer:
void AllocateParserType(cmDependsJavaParserHelper::ParserType* pt,
const char* str, int len = 0);
int LexInput(char* buf, int maxlen);
void Error(const char* str);
// For yacc
void AddClassFound(const char* sclass);
void PrepareElement(ParserType* opt);
void DeallocateParserType(char** pt);
void CheckEmpty(int line, int cnt, ParserType* pt);
void StartClass(const char* cls);
void EndClass();
void AddPackagesImport(const char* sclass);
void SetCurrentPackage(const char* pkg) { this->CurrentPackage = pkg; }
const char* GetCurrentPackage() { return this->CurrentPackage.c_str(); }
void SetCurrentCombine(const char* cmb) { this->CurrentCombine = cmb; }
const char* GetCurrentCombine() { return this->CurrentCombine.c_str(); }
void UpdateCombine(const char* str1, const char* str2);
std::vector<cmStdString>& GetClassesFound() { return this->ClassesFound; }
std::vector<cmStdString> GetFilesProduced();
private:
class CurrentClass
{
public:
cmStdString Name;
std::vector<CurrentClass> NestedClasses;
CurrentClass() {}
void AddFileNamesForPrinting(std::vector<cmStdString> *files, const char* prefix, const char* sep);
};
cmStdString CurrentPackage;
cmStdString::size_type InputBufferPos;
cmStdString InputBuffer;
std::vector<char> OutputBuffer;
std::vector<cmStdString> ClassesFound;
std::vector<cmStdString> PackagesImport;
cmStdString CurrentCombine;
std::vector<CurrentClass> ClassStack;
int CurrentLine;
int UnionsAvailable;
int LastClassId;
int CurrentDepth;
int Verbose;
std::vector<char*> Allocates;
void PrintClasses();
void Print(const char* place, const char* str);
void CombineUnions(char** out, const char* in1, char** in2, const char* sep);
void SafePrintMissing(const char* str, int line, int cnt);
void CleanupParser();
};
#endif

View File

@ -0,0 +1,254 @@
/* A Bison parser, made by GNU Bison 1.875d. */
/* Skeleton parser for Yacc-like parsing with Bison,
Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
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, 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., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
/* As a special exception, when this file is copied by Bison into a
Bison output file, you may use that output file without restriction.
This special exception was added by the Free Software Foundation
in version 1.24 of Bison. */
/* Tokens. */
#ifndef YYTOKENTYPE
# define YYTOKENTYPE
/* Put the tokens into the symbol table, so that GDB and other debuggers
know about them. */
enum yytokentype {
ABSTRACT = 258,
ASSERT = 259,
BOOLEAN = 260,
BREAK = 261,
BYTE = 262,
CASE = 263,
CATCH = 264,
CHAR = 265,
CLASS = 266,
CONTINUE = 267,
DEFAULT = 268,
DO = 269,
DOUBLE = 270,
ELSE = 271,
EXTENDS = 272,
FINAL = 273,
FINALLY = 274,
FLOAT = 275,
FOR = 276,
IF = 277,
IMPLEMENTS = 278,
IMPORT = 279,
INSTANCEOF = 280,
INT = 281,
INTERFACE = 282,
LONG = 283,
NATIVE = 284,
NEW = 285,
PACKAGE = 286,
PRIVATE = 287,
PROTECTED = 288,
PUBLIC = 289,
RETURN = 290,
SHORT = 291,
STATIC = 292,
STRICTFP = 293,
SUPER = 294,
SWITCH = 295,
SYNCHRONIZED = 296,
THIS = 297,
THROW = 298,
THROWS = 299,
TRANSIENT = 300,
TRY = 301,
VOID = 302,
VOLATILE = 303,
WHILE = 304,
BOOLEANLITERAL = 305,
CHARACTERLITERAL = 306,
DECIMALINTEGERLITERAL = 307,
FLOATINGPOINTLITERAL = 308,
HEXINTEGERLITERAL = 309,
NULLLITERAL = 310,
STRINGLITERAL = 311,
NAME = 312,
AND = 313,
ANDAND = 314,
ANDEQUALS = 315,
BRACKETEND = 316,
BRACKETSTART = 317,
CARROT = 318,
CARROTEQUALS = 319,
COLON = 320,
COMMA = 321,
CURLYEND = 322,
CURLYSTART = 323,
DIVIDE = 324,
DIVIDEEQUALS = 325,
DOLLAR = 326,
DOT = 327,
EQUALS = 328,
EQUALSEQUALS = 329,
EXCLAMATION = 330,
EXCLAMATIONEQUALS = 331,
GREATER = 332,
GTEQUALS = 333,
GTGT = 334,
GTGTEQUALS = 335,
GTGTGT = 336,
GTGTGTEQUALS = 337,
LESLESEQUALS = 338,
LESSTHAN = 339,
LTEQUALS = 340,
LTLT = 341,
MINUS = 342,
MINUSEQUALS = 343,
MINUSMINUS = 344,
PAREEND = 345,
PARESTART = 346,
PERCENT = 347,
PERCENTEQUALS = 348,
PIPE = 349,
PIPEEQUALS = 350,
PIPEPIPE = 351,
PLUS = 352,
PLUSEQUALS = 353,
PLUSPLUS = 354,
QUESTION = 355,
SEMICOL = 356,
TILDE = 357,
TIMES = 358,
TIMESEQUALS = 359,
ERROR = 360
};
#endif
#define ABSTRACT 258
#define ASSERT 259
#define BOOLEAN 260
#define BREAK 261
#define BYTE 262
#define CASE 263
#define CATCH 264
#define CHAR 265
#define CLASS 266
#define CONTINUE 267
#define DEFAULT 268
#define DO 269
#define DOUBLE 270
#define ELSE 271
#define EXTENDS 272
#define FINAL 273
#define FINALLY 274
#define FLOAT 275
#define FOR 276
#define IF 277
#define IMPLEMENTS 278
#define IMPORT 279
#define INSTANCEOF 280
#define INT 281
#define INTERFACE 282
#define LONG 283
#define NATIVE 284
#define NEW 285
#define PACKAGE 286
#define PRIVATE 287
#define PROTECTED 288
#define PUBLIC 289
#define RETURN 290
#define SHORT 291
#define STATIC 292
#define STRICTFP 293
#define SUPER 294
#define SWITCH 295
#define SYNCHRONIZED 296
#define THIS 297
#define THROW 298
#define THROWS 299
#define TRANSIENT 300
#define TRY 301
#define VOID 302
#define VOLATILE 303
#define WHILE 304
#define BOOLEANLITERAL 305
#define CHARACTERLITERAL 306
#define DECIMALINTEGERLITERAL 307
#define FLOATINGPOINTLITERAL 308
#define HEXINTEGERLITERAL 309
#define NULLLITERAL 310
#define STRINGLITERAL 311
#define NAME 312
#define AND 313
#define ANDAND 314
#define ANDEQUALS 315
#define BRACKETEND 316
#define BRACKETSTART 317
#define CARROT 318
#define CARROTEQUALS 319
#define COLON 320
#define COMMA 321
#define CURLYEND 322
#define CURLYSTART 323
#define DIVIDE 324
#define DIVIDEEQUALS 325
#define DOLLAR 326
#define DOT 327
#define EQUALS 328
#define EQUALSEQUALS 329
#define EXCLAMATION 330
#define EXCLAMATIONEQUALS 331
#define GREATER 332
#define GTEQUALS 333
#define GTGT 334
#define GTGTEQUALS 335
#define GTGTGT 336
#define GTGTGTEQUALS 337
#define LESLESEQUALS 338
#define LESSTHAN 339
#define LTEQUALS 340
#define LTLT 341
#define MINUS 342
#define MINUSEQUALS 343
#define MINUSMINUS 344
#define PAREEND 345
#define PARESTART 346
#define PERCENT 347
#define PERCENTEQUALS 348
#define PIPE 349
#define PIPEEQUALS 350
#define PIPEPIPE 351
#define PLUS 352
#define PLUSEQUALS 353
#define PLUSPLUS 354
#define QUESTION 355
#define SEMICOL 356
#define TILDE 357
#define TIMES 358
#define TIMESEQUALS 359
#define ERROR 360
#if ! defined (YYSTYPE) && ! defined (YYSTYPE_IS_DECLARED)
typedef int YYSTYPE;
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
# define YYSTYPE_IS_DECLARED 1
# define YYSTYPE_IS_TRIVIAL 1
#endif