00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef CODEPARSER_H
00019 #define CODEPARSER_H
00020
00021 #include <iostream>
00022 #include <sstream>
00023 #include <string>
00024 #include <iomanip>
00025
00026
00027
00028 #include <wctype.h>
00029
00030 #include "languagedefinition.h"
00031 #include "documentstyle.h"
00032 #include "astyle/ASFormatter.h"
00033 #include "preformatter.h"
00034 #include "enums.h"
00035
00036
00037 #define NUMBER_BUILTIN_STYLES 10
00038 #define DEFAULT_LINE_NR_WIDTH 5
00039
00043 namespace highlight {
00044
00045 class ReGroup;
00046
00057 class CodeGenerator
00058 {
00059
00060 public:
00061
00062 virtual ~CodeGenerator();
00063
00068 static CodeGenerator* getInstance(OutputType type);
00069
00070
00074 static void deleteInstance();
00075
00082 ParseError generateFile(const string &inFileName, const string &outFileName);
00083
00090 string generateString(const string &input, const string &title="");
00091
00098 const char * generateCString(const string &input, const string &title="");
00099
00101 bool styleFound();
00102
00108 virtual bool initStyle(const string& stylePath);
00109
00111 bool formattingDisabled();
00112
00114 bool formattingIsPossible();
00115
00119 LoadResult initLanguage(const string& langDefPath);
00120
00122 LanguageDefinition &getLanguage();
00123
00128 void setPrintLineNumbers(bool flag, unsigned int startCnt=0);
00129
00131 bool getPrintLineNumbers();
00132
00136 void setPrintZeroes(bool flag);
00137
00139 bool getPrintZeroes();
00140
00144 void setFragmentCode(bool flag);
00145
00149 void setLineNumberWidth(int w);
00150
00152 int getLineNumberWidth();
00153
00155 bool getFragmentCode();
00156
00158 const string& getStyleName();
00159
00160
00161
00165 void setBaseFont(const string& s);
00166
00168 const string getBaseFont() const ;
00169
00173 void setBaseFontSize(const string& s);
00174
00176 const string& getBaseFontSize() ;
00177
00178
00179
00185 void setPreformatting(WrapMode lineWrappingStyle, unsigned int lineLength,int numberSpaces);
00186
00187
00188
00189
00193 void setIncludeStyle(bool flag);
00194
00203 void setSpecialOptions(bool htmlAttachAnchors,bool htmlOrderedList,
00204 bool latexReplQuotes, bool latexDisableShorthands,
00205 bool xslFopCompatible, const string&rtfPageSize);
00206
00210 bool printExternalStyle(const string &outFile);
00211
00216 virtual bool printIndexFile(const vector<string> & fileList,
00217 const string &outPath);
00218
00223 bool initIndentationScheme(const string&indentSchemePath);
00224
00228 void setStyleInputPath(const string& path);
00229
00233 void setStyleOutputPath(const string& path);
00234
00238 void setEncoding(const string& encodingName);
00239
00241 const string& getStyleInputPath();
00242
00244 const string& getStyleOutputPath();
00245
00246 protected:
00247 CodeGenerator(highlight::OutputType type);
00248 CodeGenerator() {};
00249
00252 virtual string maskCharacter(unsigned char c) = 0;
00253
00256 string maskString(const string &s ) ;
00257
00261 State getState(const string &s, unsigned int searchPos);
00262
00264 virtual void insertLineNumber(bool insertNewLine=true);
00265
00267 virtual string getFooter() = 0;
00268
00270 virtual void printBody() = 0;
00271
00275 virtual string getHeader(const string &title) = 0;
00276
00279 unsigned int getLineNumber();
00280
00282 vector <string> styleTagOpen, styleTagClose;
00283
00285 DocumentStyle docStyle;
00286
00288 LanguageDefinition langInfo;
00289
00291 string newLineTag;
00292
00294 string spacer;
00295
00297 istream *in;
00298
00300 ostream *out;
00301
00303 string maskWsBegin, maskWsEnd;
00304
00306 string styleCommentOpen, styleCommentClose;
00307
00309 string encoding;
00310
00312 bool maskWs;
00313
00315 bool excludeWs;
00316
00318 bool fragmentOutput;
00319
00321 bool showLineNumbers;
00322
00324 bool lineNumberFillZeroes;
00325
00326
00328 string baseFont ;
00329
00331 string baseFontSize ;
00332
00333
00335 string line;
00336
00338 unsigned int lineNumber;
00339
00340 unsigned int lineNumberOffset;
00341
00343 State currentState;
00344
00346 unsigned int currentKeywordClass;
00347
00349 void processRootState();
00350
00352 virtual string getNewLine();
00353
00359 unsigned int getStyleID(State s, unsigned int kwClassID = 0);
00360
00362 unsigned int getLineIndex();
00363
00365 void flushWs();
00366
00368 string readUserStyleDef();
00369
00371 virtual string getStyleDefinition() {return "";};
00372
00374 bool encodingDefined() {return StringTools::lowerCase(encoding)!="none";}
00375
00377 string wsBuffer;
00378
00380 bool includeStyleDef;
00381
00382 private:
00383
00384 CodeGenerator(const CodeGenerator&){cerr<< "Doh!\n";}
00385
00386 CodeGenerator& operator=(CodeGenerator&){ return *this;}
00387
00388 static CodeGenerator* generator;
00389
00391 virtual string getMatchingOpenTag(unsigned int) = 0;
00392 virtual string getMatchingCloseTag(unsigned int) = 0;
00393
00397
00398
00399
00401 void openTag(State s);
00402
00404 void closeTag(State s);
00405
00406 void closeTag(unsigned int styleID);
00407
00408 void openTag(unsigned int styleID);
00409
00410
00411 string stylePath;
00412
00413
00414 unsigned int lineIndex;
00415
00416 unsigned int lineNumberWidth;
00417
00419 unsigned char terminatingChar;
00420
00422 astyle::ASFormatter *formatter;
00423
00425 PreFormatter *preFormatter;
00426
00428 bool formattingEnabled;
00429
00430
00432 bool formattingPossible;
00433
00435 string token;
00436
00437 string styleInputPath, styleOutputPath;
00438
00440 void reset();
00441
00443 bool readNewLine(string &newLine);
00444
00446 unsigned char getInputChar();
00447
00448 OutputType outputType;
00449
00451 State getCurrentState ( bool lastStateWasNumber=false);
00452
00454 bool processKeywordState(State myState) ;
00455 bool processNumberState() ;
00456 bool processMultiLineCommentState();
00457 bool processSingleLineCommentState();
00458 bool processStringState(State oldState);
00459 bool processEscapeCharState();
00460 bool processDirectiveState();
00461 bool processTagState();
00462 bool processSymbolState();
00463 void processWsState();
00464
00466 bool isFirstNonWsChar() ;
00467
00469 void printMaskedToken(bool flushWhiteSpace=true);
00470
00471 void closeKWTag(unsigned int styleID);
00472
00473 void openKWTag(unsigned int styleID);
00474
00478 bool checkSpecialCmd();
00479
00480 map <int, ReGroup*> regexGroups;
00481
00482 void matchRegex(const string &);
00483
00484 };
00485
00486 class ReGroup {
00487 public:
00488
00489 ReGroup(State s, unsigned int l , unsigned int c ): length(l), state(s), kwClass(c) {
00490
00491 }
00492 ~ReGroup(){}
00493
00494 unsigned int length;
00495 State state;
00496 unsigned int kwClass;
00497 };
00498
00499 }
00500
00501 #endif