• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

C:/CVUT/diplomka/Automata_editor/sources/parser.h

Go to the documentation of this file.
00001 #ifndef PARSER_H_1467785685834255
00002 #define PARSER_H_1467785685834255
00003 
00004 #include <QString>
00005 #include <QStringList>
00006 #include <QRect>
00007 
00008 // logging enabled/disabled
00009 #ifdef TESTING_LEXER
00010 #   define DBGLOG_LEX(x) DBGLOG_("LEXER", x)
00011 #else
00012 #   define DBGLOG_LEX(x)
00013 #endif
00014 #ifdef TESTING_PARSER
00015 #   define DBGLOG_PAR(x) DBGLOG_("PARSER", x)
00016 #else
00017 #   define DBGLOG_PAR(x)
00018 #endif
00019 
00020 class QFile;
00021 class Editor;
00022 class Transition;
00023 class State;
00024 
00025 namespace VaucansonKeywords
00026 {
00027 
00028 enum Token {IDENT, COMMENT, NUMB, FLOAT, MINUS, EQUAL, DOT,
00029             COMMA, LPAR, RPAR, LBRACE, RBRACE, LBRACKET, RBRACKET,
00030             UNDERLINE, SPECIAL, OTHER,
00031             eFirstKw,
00032             kwShowGrid = eFirstKw, kwShowFrame, kwHideGrid, kwHideFrame,
00033             kwVCPut,
00034             kwBeginVCPicture, kwBegin, kwEnd, kwState, kwFinalState, kwStateVar, kwFinalStateVar,
00035             kwInitial, kwFinal,
00036             kwLoopN, kwLoopS, kwLoopE, kwLoopW, kwLoopNE, kwLoopNW, kwLoopSE, kwLoopSW,
00037             kwCLoopN, kwCLoopS, kwCLoopE, kwCLoopW, kwCLoopNE, kwCLoopNW, kwCLoopSE, kwCLoopSW,
00038             kwLoopVarN, kwLoopVarS,
00039             kwVarLoopOn, kwVarLoopOff,
00040             kwEdgeL, kwEdgeR, kwArcL, kwArcR, kwLArcL, kwLArcR,
00041             kwVArcL, kwVArcR, kwVCurveL, kwVCurveR,
00042             kwLabelL, kwLabelR,
00043             kwDimState, kwRstState, kwDimEdge, kwRstEdge, kwEdgeBorder, kwEdgeBorderOff, 
00044             kwStateLineDouble, kwStateLineSimple, kwEdgeLineDouble, kwEdgeLineSimple,
00045             setStateLineStyle, setStateLineWidth, setStateLineColor, setStateLabelColor, setStateLabelScale,
00046             setStateFillStatus, setStateFillColor,
00047             setEdgeLineStyle, setEdgeLineWidth, setEdgeLineColor, setEdgeLabelColor, setEdgeLabelScale,
00048             chgStateLineStyle, chgStateLineWidth, chgStateLineColor, chgStateLabelColor, chgStateLabelScale,
00049             chgStateFillStatus, chgStateFillColor,
00050             chgEdgeLineStyle, chgEdgeLineWidth, chgEdgeLineColor, chgEdgeLabelColor, chgEdgeLabelScale,
00051             rstStateLineStyle, rstStateLineWidth, rstStateLineColor, rstStateLabelColor, rstStateLabelScale,
00052             rstStateFillStatus, rstStateFillColor,
00053             rstEdgeLineStyle, rstEdgeLineWidth, rstEdgeLineColor, rstEdgeLabelColor, rstEdgeLabelScale,
00054             fixDimState, fixStateLineDouble, fixDimEdge, fixEdgeBorder, fixEdgeLineDouble,
00055             eLastKw = fixEdgeLineDouble,
00056             ERR,EOI};
00057 };
00058 
00059 
00060 /*!
00061  * Lexical analyzer used for splitting lexical symbols from input 'stream'.
00062  */
00063 class LexAn
00064 {
00065 public:
00066     typedef QMap<QString, VaucansonKeywords::Token>    TKeywordMap;    
00067     static TKeywordMap keywordMap;
00068     static void initialize();
00069     
00070     LexAn();
00071     ~LexAn();
00072   
00073     //! opens input file, returns false if opening fails
00074     bool openFile(const QString &fn);
00075 
00076     //! reads string between characters pair, if allowMatching is false,
00077     //! first closing character stops reading, otherwise balances matching is used
00078     //! never call matching on opening character, but ever call matching to closing character!
00079     QString readStringBetween(const QChar &opening, const QChar &closing, bool allowMatching = true);
00080     //! reads single new token
00081     VaucansonKeywords::Token readToken();
00082     //! returns last readed token
00083     VaucansonKeywords::Token getToken();
00084     //! returns token data
00085     QString getTokenData();
00086     //! returns current line number
00087     int getLineNumber();
00088   
00089 private:
00090     QFile *inFile;
00091     bool fileOpened;
00092   
00093     char input; // type of input character (n - number, c - char, e - EOI, ...)
00094     unsigned char character;  // read character
00095     int       lineNumber;
00096     char      line[1024];     // read line
00097     int       lineIndex;      // index of currently read character
00098     int       lineLength;     // currentyl read line length  
00099     VaucansonKeywords::Token token;
00100     QString tokenData;
00101   
00102     char readChar();
00103     VaucansonKeywords::Token keyWordTest(const QString &tokenData);    
00104     bool isAllowed(char character);
00105 };
00106 
00107 
00108 
00109 /*!
00110  *   Parser implemented by recursive descending method.
00111  */
00112 class Parser
00113 {  
00114 public: 
00115     Parser(Editor *editor, bool canChangeEditor = true);
00116     ~Parser();
00117     
00118     //! fill created states (with assigned transitions) -> editor will add it all to scene or
00119     //! do with it something other
00120     bool run(const QString &filename, QList<State*> &stateList, QRect &gridRect);
00121 
00122     inline QString getReport() const
00123     {
00124         return report.join("<br>");
00125     }
00126 
00127     //! Returns true if name is in correct form, otherwise returns false
00128     //! \param error - sets error string if not NULL
00129     //! \sa Parser::Name, NameAcceptDialog::myAccept
00130     static bool checkNameCorrectness(const QString &name, QString *error);
00131     
00132 protected:
00133     Editor *editor;
00134     LexAn *lexan;    
00135 
00136     bool    m_canChangeEditor;
00137     Transition *lastTr; // for Label element addition
00138     
00139     QMap<QString, State*>    m_stateMap;
00140     QRect                    m_gridRect;    
00141 
00142     QStringList report;
00143     int err_count, war_count;
00144 
00145     bool loaded;
00146   
00147     // switches
00148     bool dimState;
00149     bool dimEdge;
00150     bool edgeBorder;
00151     bool stateLineDouble;
00152     bool varLoop;
00153     
00154     void resetParams();
00155     
00156     // parameters which can be changed
00157     // state parameters
00158     Qt::PenStyle stateLineStyle;
00159     float stateLineWidth;
00160     QString stateLineColor;
00161     QString stateLabelColor;
00162     float stateLabelScale;
00163     Qt::BrushStyle stateFillStatus;
00164     QString stateFillColor;
00165     // edge parameters
00166     Qt::PenStyle edgeLineStyle;
00167     float edgeLineWidth;
00168     QString edgeLineColor;
00169     QString edgeLabelColor;
00170     float edgeLabelScale;
00171     bool edgeLineDblStatus;
00172   
00173     // state parametres preset
00174     float stateLineDoubleCoef;
00175     float stateLineDoubleSep;
00176     // transition parametres preset
00177     float edgeLineBorderCoef;
00178     QString edgeLineBorderColor;
00179     float edgeLineDblCoef;
00180     float edgeLineDblSep;
00181   
00182     // state parametres dimmed
00183     Qt::PenStyle dimStateLineStyle;
00184     QString dimStateLineColor;
00185     float dimStateLineCoef;
00186     QString dimStateLabelColor;
00187     QString dimStateFillColor;
00188     // transition parametres dimmed
00189     Qt::PenStyle dimEdgeLineStyle;
00190     QString dimEdgeLineColor;
00191     float dimEdgeLineCoef;
00192     QString dimEdgeLabelColor;
00193   
00194     // object params
00195     QString param;
00196   
00197     struct CurveParam{
00198         int angleA; bool isAngleA;
00199         int angleB; bool isAngleB;
00200         float ncurv; bool isNCurv;
00201     } cParam;
00202   
00203     void matching(VaucansonKeywords::Token token, bool end = false);
00204 
00205     void checkTrParam(Transition *tr);
00206     bool kwTest(VaucansonKeywords::Token token);
00207 
00208     void resetStateParams();
00209     void resetTrParams();
00210 
00211     // functions of recurcive descent
00212     void File();
00213     void Predeclaration();
00214     void VCPicture();
00215     void VCPut();
00216     bool Element();
00217     void Switch();
00218     void ChgParam();
00219     Qt::BrushStyle FillStatus();
00220     Qt::PenStyle LineStyle();
00221     QString Color();
00222     void State_r();
00223     void StateDeclaration(const QString &typeName);
00224     void StateLabel();
00225     void Transition_r();
00226     void Label_r();
00227     void OneStateTransition();
00228     int Loop();
00229     void TwoStateTransition();
00230     bool Edge();
00231     bool Arc(bool &small);
00232     bool VArc();
00233     bool VCurve();
00234     void VArcParam();
00235     void VArcParamNext();
00236     void VArcParamIdent();
00237     void VCurveParam();
00238     void VCurveParamNext();
00239     void VCurveParamIdent();
00240     void EndTransitionDeclaration(const QString &typeName, bool left, float pos, bool isPosParam);
00241     void TransitionLabel();
00242     void NextLabel(); // next transition label
00243     float PosParam(bool &isParam);
00244     float Pos(bool &isParam);
00245     int DirParam(bool &isParam);
00246     int Dir(bool &isParam);
00247     float Number();
00248     float NumberNextPart();
00249     int Numb();
00250     bool Sign();
00251     void Name();
00252     void NameNextPart();
00253     void NameAllowedToken();
00254     bool NoBracesToken();
00255     bool NoBracketsToken();
00256 
00257     inline void errorReportUTOKEN()
00258     {
00259         report << QString("%1: Error %2 - Unexpected token '%3'")
00260             .arg(lexan->getLineNumber())
00261             .arg(++err_count)
00262             .arg(lexan->getTokenData());
00263     }
00264 };
00265 
00266 #endif

Generated on Tue Jan 4 2011 03:03:23 for Autoamata editor by  doxygen 1.7.0