Started on the move back to a single executable. The backend programs are now a
[ragel.git] / redfsm / gendata.h
blob7c68c6ed3416211c5c589121a855ec7df3f6eb3e
1 /*
2 * Copyright 2005-2007 Adrian Thurston <thurston@cs.queensu.ca>
3 */
5 /* This file is part of Ragel.
7 * Ragel is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Ragel is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Ragel; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #ifndef _GENDATA_H
23 #define _GENDATA_H
25 #include <iostream>
26 #include "config.h"
27 #include "redfsm.h"
28 #include "common.h"
30 using std::ostream;
32 struct NameInst;
33 typedef DList<Action> ActionList;
35 typedef unsigned long ulong;
37 extern int gblErrorCount;
39 struct CodeGenData;
41 typedef AvlMap<char *, CodeGenData*, CmpStr> CodeGenMap;
42 typedef AvlMapEl<char *, CodeGenData*> CodeGenMapEl;
45 * The interface to the parser
48 /* These functions must be implemented by the code generation executable.
49 * The openOutput function is invoked when the root element is opened. The
50 * makeCodeGen function is invoked when a ragel_def element is opened. */
51 std::ostream *cdOpenOutput( char *inputFile );
52 std::ostream *javaOpenOutput( char *inputFile );
53 std::ostream *rubyOpenOutput( char *inputFile );
54 std::ostream *csharpOpenOutput( char *inputFile );
56 CodeGenData *cdMakeCodeGen( char *sourceFileName,
57 char *fsmName, ostream &out, bool wantComplete );
58 CodeGenData *javaMakeCodeGen( char *sourceFileName,
59 char *fsmName, ostream &out, bool wantComplete );
60 CodeGenData *rubyMakeCodeGen( char *sourceFileName,
61 char *fsmName, ostream &out, bool wantComplete );
62 CodeGenData *csharpMakeCodeGen( char *sourceFileName,
63 char *fsmName, ostream &out, bool wantComplete );
65 void cdLineDirective( ostream &out, const char *fileName, int line );
66 void javaLineDirective( ostream &out, const char *fileName, int line );
67 void rubyLineDirective( ostream &out, const char *fileName, int line );
68 void csharpLineDirective( ostream &out, const char *fileName, int line );
69 void genLineDirective( ostream &out );
71 /*********************************/
73 struct CodeGenData
76 * The interface to the code generator.
78 virtual void finishRagelDef() {}
80 /* These are invoked by the corresponding write statements. */
81 virtual void writeData() {};
82 virtual void writeInit() {};
83 virtual void writeExec() {};
84 virtual void writeExports() {};
86 /* This can also be overwridden to modify the processing of write
87 * statements. */
88 virtual void writeStatement( GenInputLoc &loc, int nargs, char **args );
90 /********************/
92 CodeGenData( ostream &out );
93 virtual ~CodeGenData() {}
95 /*
96 * Collecting the machine.
99 char *sourceFileName;
100 char *fsmName;
101 ostream &out;
102 RedFsmAp *redFsm;
103 Action *allActions;
104 RedAction *allActionTables;
105 Condition *allConditions;
106 CondSpace *allCondSpaces;
107 RedStateAp *allStates;
108 NameInst **nameIndex;
109 int startState;
110 int errState;
111 ActionList actionList;
112 ConditionList conditionList;
113 CondSpaceList condSpaceList;
114 GenInlineList *getKeyExpr;
115 GenInlineList *accessExpr;
116 GenInlineList *prePushExpr;
117 GenInlineList *postPopExpr;
119 /* Overriding variables. */
120 GenInlineList *pExpr;
121 GenInlineList *peExpr;
122 GenInlineList *eofExpr;
123 GenInlineList *csExpr;
124 GenInlineList *topExpr;
125 GenInlineList *stackExpr;
126 GenInlineList *actExpr;
127 GenInlineList *tokstartExpr;
128 GenInlineList *tokendExpr;
129 GenInlineList *dataExpr;
131 KeyOps thisKeyOps;
132 bool wantComplete;
133 EntryIdVect entryPointIds;
134 EntryNameVect entryPointNames;
135 bool hasLongestMatch;
136 ExportList exportList;
138 /* Write options. */
139 bool hasEnd;
140 bool dataPrefix;
141 bool writeFirstFinal;
142 bool writeErr;
143 bool writeCS;
145 void createMachine();
146 void initActionList( unsigned long length );
147 void newAction( int anum, char *name, int line, int col, GenInlineList *inlineList );
148 void initActionTableList( unsigned long length );
149 void initStateList( unsigned long length );
150 void setStartState( unsigned long startState );
151 void setErrorState( unsigned long errState );
152 void addEntryPoint( char *name, unsigned long entryState );
153 void setId( int snum, int id );
154 void setFinal( int snum );
155 void initTransList( int snum, unsigned long length );
156 void newTrans( int snum, int tnum, Key lowKey, Key highKey,
157 long targ, long act );
158 void finishTransList( int snum );
159 void setStateActions( int snum, long toStateAction,
160 long fromStateAction, long eofAction );
161 void setEofTrans( int snum, long targ, long eofAction );
162 void setForcedErrorState()
163 { redFsm->forcedErrorState = true; }
166 void initCondSpaceList( ulong length );
167 void condSpaceItem( int cnum, long condActionId );
168 void newCondSpace( int cnum, int condSpaceId, Key baseKey );
170 void initStateCondList( int snum, ulong length );
171 void addStateCond( int snum, Key lowKey, Key highKey, long condNum );
173 CondSpace *findCondSpace( Key lowKey, Key highKey );
174 Condition *findCondition( Key key );
176 bool setAlphType( char *data );
178 void resolveTargetStates( GenInlineList *inlineList );
179 Key findMaxKey();
181 /* Gather various info on the machine. */
182 void analyzeActionList( RedAction *redAct, GenInlineList *inlineList );
183 void analyzeAction( Action *act, GenInlineList *inlineList );
184 void findFinalActionRefs();
185 void analyzeMachine();
187 void closeMachine();
188 void setValueLimits();
189 void assignActionIds();
191 ostream &source_warning( const GenInputLoc &loc );
192 ostream &source_error( const GenInputLoc &loc );
196 #endif /* _GENDATA_H */