gendata.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright 2005-2007 Adrian Thurston <thurston@complang.org>
  3. */
  4. /* This file is part of Ragel.
  5. *
  6. * Ragel is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Ragel is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Ragel; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #ifndef _GENDATA_H
  21. #define _GENDATA_H
  22. #include <iostream>
  23. #include "config.h"
  24. #include "redfsm.h"
  25. #include "common.h"
  26. using std::ostream;
  27. extern bool generateDot;
  28. struct NameInst;
  29. typedef DList<GenAction> GenActionList;
  30. typedef unsigned long ulong;
  31. extern int gblErrorCount;
  32. struct CodeGenData;
  33. typedef AvlMap<char *, CodeGenData*, CmpStr> CodeGenMap;
  34. typedef AvlMapEl<char *, CodeGenData*> CodeGenMapEl;
  35. void cdLineDirective( ostream &out, const char *fileName, int line );
  36. void javaLineDirective( ostream &out, const char *fileName, int line );
  37. void goLineDirective( ostream &out, const char *fileName, int line );
  38. void rubyLineDirective( ostream &out, const char *fileName, int line );
  39. void csharpLineDirective( ostream &out, const char *fileName, int line );
  40. void ocamlLineDirective( ostream &out, const char *fileName, int line );
  41. void genLineDirective( ostream &out );
  42. void lineDirective( ostream &out, const char *fileName, int line );
  43. string itoa( int i );
  44. /*********************************/
  45. struct CodeGenData
  46. {
  47. /*
  48. * The interface to the code generator.
  49. */
  50. virtual void finishRagelDef() {}
  51. /* These are invoked by the corresponding write statements. */
  52. virtual void writeData() {};
  53. virtual void writeInit() {};
  54. virtual void writeExec() {};
  55. virtual void writeExports() {};
  56. virtual void writeStart() {};
  57. virtual void writeFirstFinal() {};
  58. virtual void writeError() {};
  59. /* This can also be overwridden to modify the processing of write
  60. * statements. */
  61. virtual bool writeStatement( InputLoc &loc, int nargs, char **args );
  62. /********************/
  63. CodeGenData( ostream &out );
  64. virtual ~CodeGenData() {}
  65. /*
  66. * Collecting the machine.
  67. */
  68. const char *sourceFileName;
  69. const char *fsmName;
  70. ostream &out;
  71. RedFsmAp *redFsm;
  72. GenAction *allActions;
  73. RedAction *allActionTables;
  74. Condition *allConditions;
  75. GenCondSpace *allCondSpaces;
  76. RedStateAp *allStates;
  77. NameInst **nameIndex;
  78. int startState;
  79. int errState;
  80. GenActionList actionList;
  81. ConditionList conditionList;
  82. CondSpaceList condSpaceList;
  83. GenInlineList *getKeyExpr;
  84. GenInlineList *accessExpr;
  85. GenInlineList *prePushExpr;
  86. GenInlineList *postPopExpr;
  87. /* Overriding variables. */
  88. GenInlineList *pExpr;
  89. GenInlineList *peExpr;
  90. GenInlineList *eofExpr;
  91. GenInlineList *csExpr;
  92. GenInlineList *topExpr;
  93. GenInlineList *stackExpr;
  94. GenInlineList *actExpr;
  95. GenInlineList *tokstartExpr;
  96. GenInlineList *tokendExpr;
  97. GenInlineList *dataExpr;
  98. KeyOps thisKeyOps;
  99. bool wantComplete;
  100. EntryIdVect entryPointIds;
  101. EntryNameVect entryPointNames;
  102. bool hasLongestMatch;
  103. ExportList exportList;
  104. /* Write options. */
  105. bool noEnd;
  106. bool noPrefix;
  107. bool noFinal;
  108. bool noError;
  109. bool noEntry;
  110. bool noCS;
  111. void createMachine();
  112. void initActionList( unsigned long length );
  113. void newAction( int anum, const char *name, const InputLoc &loc, GenInlineList *inlineList );
  114. void initActionTableList( unsigned long length );
  115. void initStateList( unsigned long length );
  116. void setStartState( unsigned long startState );
  117. void setErrorState( unsigned long errState );
  118. void addEntryPoint( char *name, unsigned long entryState );
  119. void setId( int snum, int id );
  120. void setFinal( int snum );
  121. void initTransList( int snum, unsigned long length );
  122. void newTrans( int snum, int tnum, Key lowKey, Key highKey,
  123. long targ, long act );
  124. void finishTransList( int snum );
  125. void setStateActions( int snum, long toStateAction,
  126. long fromStateAction, long eofAction );
  127. void setEofTrans( int snum, long targ, long eofAction );
  128. void setForcedErrorState()
  129. { redFsm->forcedErrorState = true; }
  130. void initCondSpaceList( ulong length );
  131. void condSpaceItem( int cnum, long condActionId );
  132. void newCondSpace( int cnum, int condSpaceId, Key baseKey );
  133. void initStateCondList( int snum, ulong length );
  134. void addStateCond( int snum, Key lowKey, Key highKey, long condNum );
  135. GenCondSpace *findCondSpace( Key lowKey, Key highKey );
  136. Condition *findCondition( Key key );
  137. bool setAlphType( const char *data );
  138. void resolveTargetStates( GenInlineList *inlineList );
  139. Key findMaxKey();
  140. /* Gather various info on the machine. */
  141. void analyzeActionList( RedAction *redAct, GenInlineList *inlineList );
  142. void analyzeAction( GenAction *act, GenInlineList *inlineList );
  143. void findFinalActionRefs();
  144. void analyzeMachine();
  145. void closeMachine();
  146. void setValueLimits();
  147. void assignActionIds();
  148. ostream &source_warning( const InputLoc &loc );
  149. ostream &source_error( const InputLoc &loc );
  150. void write_option_error( InputLoc &loc, char *arg );
  151. };
  152. CodeGenData *makeCodeGen( const char *sourceFileName,
  153. const char *fsmName, ostream &out );
  154. #endif