00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef CODEPARSER_H
00029 #define CODEPARSER_H
00030
00031 #include <iostream>
00032 #include <sstream>
00033 #include <string>
00034 #include <iomanip>
00035
00036 #include "languagedefinition.h"
00037 #include "documentstyle.h"
00038 #include "ctagsreader.h"
00039 #include "astyle/astyle.h"
00040 #include "preformatter.h"
00041 #include "enums.h"
00042 #include "stringtools.h"
00043
00045
00046 namespace highlight
00047 {
00054 class ReGroup
00055 {
00056 public:
00057
00059 ReGroup() : length ( 0 ), state ( STANDARD ), kwClass ( 0 ), name()
00060 {
00061 }
00062
00064 ReGroup ( State s, unsigned int l , unsigned int c, const string&n ) :
00065 length ( l ), state ( s ), kwClass ( c ), name(n)
00066 {
00067 }
00068
00070 ReGroup ( const ReGroup& other )
00071 {
00072 length = other.length;
00073 state = other.state;
00074 kwClass = other.kwClass;
00075 name=other.name;
00076 }
00077
00079 ReGroup& operator= ( const ReGroup & other )
00080 {
00081 length = other.length;
00082 state = other.state;
00083 kwClass = other.kwClass;
00084 name=other.name;
00085 return *this;
00086 }
00087
00088 ~ReGroup()
00089 {
00090 }
00091
00092 unsigned int length;
00093 State state;
00094 unsigned int kwClass;
00095 string name;
00096 };
00097
00098
00113 class CodeGenerator
00114 {
00115
00116 public:
00117
00118 virtual ~CodeGenerator();
00119
00125 static CodeGenerator* getInstance ( OutputType type );
00126
00132 static void deleteInstance ( CodeGenerator* inst ) {if ( inst ) delete inst;}
00133
00140 bool initTheme ( const string& themePath );
00141
00147 bool initIndentationScheme ( const string&indentScheme );
00148
00154 bool initTagInformation ( const string& ctagsPath );
00155
00161 LoadResult loadLanguage ( const string& langDefPath );
00162
00169 ParseError generateFile ( const string &inFileName, const string &outFileName );
00170
00176 string generateString ( const string &input );
00177
00183 string generateStringFromFile ( const string &inFileName );
00184
00189 bool printExternalStyle ( const string &outFile );
00190
00196 virtual bool printIndexFile ( const vector<string> & fileList,
00197 const string &outPath );
00198
00199
00206 void setPreformatting ( WrapMode lineWrappingStyle, unsigned int lineLength,int numberSpaces );
00207
00210 bool styleFound();
00211
00213 bool formattingDisabled();
00214
00216 bool formattingIsPossible();
00217
00224 LoadResult initLanguage ( const string& langDefPath ) { return loadLanguage ( langDefPath );}
00225
00227 const LanguageDefinition &getLanguage();
00228
00233 void setPrintLineNumbers ( bool flag, unsigned int startCnt=1 );
00234
00236 bool getPrintLineNumbers();
00237
00241 void setPrintZeroes ( bool flag );
00242
00244 bool getPrintZeroes();
00245
00249 void setFragmentCode ( bool flag );
00250
00252 bool getFragmentCode();
00253
00257 void setLineNumberWidth ( int w );
00258
00260 int getLineNumberWidth();
00261
00265 void setValidateInput ( bool flag );
00266
00268 bool getValidateInput();
00269
00271 const string& getStyleName();
00272
00276 void setBaseFont ( const string& s );
00277
00279 const string getBaseFont() const ;
00280
00284 void setBaseFontSize ( const string& s );
00285
00287 const string getBaseFontSize();
00288
00292 void setIncludeStyle ( bool flag );
00293
00294
00298 void disableTrailingNL ( bool flag );
00299
00303 void setStyleInputPath ( const string& path );
00304
00308 void setStyleOutputPath ( const string& path );
00309
00313 void setEncoding ( const string& encodingName );
00314
00316 const string& getStyleInputPath();
00317
00319 const string& getStyleOutputPath();
00320
00322 void setTitle ( const string & title );
00323
00325 string getTitle();
00326
00328 void setMaxInputLineCnt ( unsigned int cnt );
00329
00331 bool hasWhiteBGColour();
00332
00334 void setKeyWordCase ( StringTools::KeywordCase keyCase );
00335
00338 void addMarkedLine ( int lineNo, string &helpTxt );
00339
00342 void setEOLDelimiter(char delim);
00343
00348 void setStartingNestedLang(const string &langName);
00349
00350
00353 virtual void setHTMLAttachAnchors ( bool ) {};
00354
00357 virtual void setHTMLOrderedList ( bool ) {};
00358
00361 virtual void setHTMLInlineCSS ( bool ) {};
00362
00365 virtual void setHTMLEnclosePreTag ( bool ) {};
00366
00369 virtual void setHTMLAnchorPrefix ( const string& ) {};
00370
00373 virtual void setHTMLClassName ( const string& ) {};
00374
00377 virtual void setLATEXReplaceQuotes ( bool ) {};
00378
00381 virtual void setLATEXNoShorthands ( bool ) {};
00382
00385 virtual void setLATEXPrettySymbols ( bool ) {};
00386
00389 virtual void setRTFPageSize ( const string& ) {};
00390
00393 virtual void setRTFCharStyles ( bool ) {};
00394
00397 virtual void setSVGSize ( const string&, const string& ) {};
00398
00399
00400 protected:
00401
00402 static const unsigned int NUMBER_BUILTIN_STATES;
00403
00404 static const string STY_NAME_STD;
00405 static const string STY_NAME_STR;
00406 static const string STY_NAME_NUM;
00407 static const string STY_NAME_SLC;
00408 static const string STY_NAME_COM;
00409 static const string STY_NAME_ESC;
00410 static const string STY_NAME_DIR;
00411 static const string STY_NAME_DST;
00412 static const string STY_NAME_LIN;
00413 static const string STY_NAME_SYM;
00414
00416 CodeGenerator ( highlight::OutputType type );
00417 CodeGenerator() {};
00418
00421 virtual string maskCharacter ( unsigned char c ) = 0;
00422
00425 void maskString ( ostream& ss, const string &s ) ;
00426
00430 State getState ( const string &s, unsigned int searchPos );
00431
00434 unsigned int getLineNumber();
00435
00436 vector <string> openTags,
00437 closeTags;
00438
00440 DocumentStyle docStyle;
00441
00443 LanguageDefinition langInfo;
00444
00446 CTagsReader metaInfo;
00447
00449 string newLineTag;
00450
00452 string spacer;
00453
00455 istream *in;
00456
00458 ostream *out;
00459
00460 string maskWsBegin,
00461 maskWsEnd;
00462
00463 string styleCommentOpen,
00464 styleCommentClose;
00465
00466 string embedBlockOpen,
00467 embedBlockClose;
00468
00470 string encoding;
00471
00473 string docTitle;
00474
00475 string inFile,
00476 outFile;
00477
00479 bool maskWs;
00480
00482 bool excludeWs;
00483
00485 bool fragmentOutput;
00486
00488 bool showLineNumbers;
00489
00491 bool lineNumberFillZeroes;
00492
00494 bool printNewLines;
00495
00497 string baseFont ;
00498
00500 string baseFontSize ;
00501
00503 string line;
00504
00506 unsigned int lineNumber;
00507
00509 int lineNumberOffset;
00510
00512 State currentState;
00513
00515 unsigned int currentKeywordClass;
00516
00518 void processRootState();
00519
00521 virtual string getNewLine();
00522
00528 unsigned int getStyleID ( State s, unsigned int kwClassID = 0 );
00529
00531 unsigned int getLineIndex();
00532
00534 void flushWs();
00535
00537 string readUserStyleDef();
00538
00540 virtual string getStyleDefinition() {return "";};
00541
00543 bool encodingDefined() {return StringTools::change_case ( encoding ) !="none";}
00544
00546 string wsBuffer;
00547
00549 bool includeStyleDef;
00550
00552 map <int, string> markLines;
00553
00555 PreFormatter preFormatter;
00556
00557 private:
00558
00559 CodeGenerator ( const CodeGenerator& ) {}
00560
00561 CodeGenerator& operator= ( CodeGenerator& ) { return *this;}
00562
00564 virtual void insertLineNumber ( bool insertNewLine=true );
00565
00568 virtual string getFooter() = 0;
00569
00571 virtual void printBody() = 0;
00572
00576 virtual string getHeader() = 0;
00577
00579 virtual void initOutputTags() = 0;
00580
00583 virtual string getKeywordOpenTag ( unsigned int ) = 0;
00584
00587 virtual string getKeywordCloseTag ( unsigned int ) = 0;
00588
00593 virtual string getMetaInfoOpenTag ( const TagInfo& info ) {return "";}
00594
00598 virtual string getMetaInfoCloseTag() {return "";}
00599
00601 void openTag ( State s );
00602
00604 void closeTag ( State s );
00605
00607 void closeKWTag ( unsigned int styleID );
00608
00610 void openKWTag ( unsigned int styleID );
00611
00612
00613
00614
00615
00616
00617
00619 string themePath;
00620
00622 string hostLangDefPath;
00623
00625 string embedLangDefPath;
00626
00628 string embedLangStart;
00629
00631 unsigned int lineIndex;
00632
00634 unsigned int lineNumberWidth;
00635
00637 unsigned int maxLineCnt;
00638
00640 unsigned char terminatingChar;
00641
00643 astyle::ASFormatter *formatter;
00644
00646 bool formattingEnabled;
00647
00649 bool formattingPossible;
00650
00652 bool validateInput;
00653
00655 bool tagsEnabled;
00656
00658 bool noTrailingNewLine;
00659
00661 StringTools::KeywordCase keywordCase;
00662
00664 string token;
00665
00666 string styleInputPath,
00667 styleOutputPath;
00668
00670 char eolDelimiter;
00671
00673 void reset();
00674
00676 bool readNewLine ( string &newLine );
00677
00679 unsigned char getInputChar();
00680
00682 OutputType outputType;
00683
00685 State getCurrentState ();
00686
00687
00688 bool processKeywordState ( State myState );
00689 bool processNumberState() ;
00690 bool processMultiLineCommentState();
00691 bool processSingleLineCommentState();
00692 bool processStringState ( State oldState );
00693 bool processEscapeCharState();
00694 bool processDirectiveState();
00695 bool processTagState();
00696 bool processSymbolState();
00697 void processWsState();
00698 bool processSyntaxChangeState(State myState );
00699
00705 void printMaskedToken ( bool addMetaInfo = false, bool flushWhiteSpace = true,
00706 StringTools::KeywordCase tcase = StringTools::CASE_UNCHANGED );
00707
00711 bool checkSpecialCmd();
00712
00714 map <int, ReGroup> regexGroups;
00715
00718 void matchRegex ( const string &line );
00719
00721 bool validateInputStream();
00722
00723 void loadEmbeddedLang(const string&embedLangDefPath);
00724
00725 };
00726
00727 }
00728
00729 #endif