Implemented makeLmSwitch and started making calls to makeGenInlineList.
[external/ragel.git] / redfsm / gendata.h
1 /*
2  *  Copyright 2005-2007 Adrian Thurston <thurston@complang.org>
3  */
4
5 /*  This file is part of Ragel.
6  *
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.
11  * 
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.
16  * 
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 
20  */
21
22 #ifndef _GENDATA_H
23 #define _GENDATA_H
24
25 #include <iostream>
26 #include "config.h"
27 #include "redfsm.h"
28 #include "common.h"
29
30 using std::ostream;
31
32 extern bool generateDot;
33
34 struct NameInst;
35 typedef DList<GenAction> GenActionList;
36
37 typedef unsigned long ulong;
38
39 extern int gblErrorCount;
40
41 struct CodeGenData;
42
43 typedef AvlMap<char *, CodeGenData*, CmpStr> CodeGenMap;
44 typedef AvlMapEl<char *, CodeGenData*> CodeGenMapEl;
45
46 /*
47  * The interface to the parser
48  */
49
50 /* These functions must be implemented by the code generation executable.
51  * The openOutput function is invoked when the root element is opened.  The
52  * makeCodeGen function is invoked when a ragel_def element is opened. */
53 std::ostream *cdOpenOutput( const char *inputFile );
54 std::ostream *javaOpenOutput( const char *inputFile );
55 std::ostream *rubyOpenOutput( const char *inputFile );
56 std::ostream *csharpOpenOutput( const char *inputFile );
57 std::ostream *dotOpenOutput( const char *inputFile );
58
59 CodeGenData *cdMakeCodeGen( const char *sourceFileName, 
60                 const char *fsmName, ostream &out, bool wantComplete );
61 CodeGenData *javaMakeCodeGen( const char *sourceFileName, 
62                 const char *fsmName, ostream &out, bool wantComplete );
63 CodeGenData *rubyMakeCodeGen( const char *sourceFileName, 
64                 const char *fsmName, ostream &out, bool wantComplete );
65 CodeGenData *csharpMakeCodeGen( const char *sourceFileName, 
66                 const char *fsmName, ostream &out, bool wantComplete );
67 CodeGenData *dotMakeCodeGen( const char *sourceFileName, 
68                 const char *fsmName, ostream &out, bool wantComplete );
69
70 void cdLineDirective( ostream &out, const char *fileName, int line );
71 void javaLineDirective( ostream &out, const char *fileName, int line );
72 void rubyLineDirective( ostream &out, const char *fileName, int line );
73 void csharpLineDirective( ostream &out, const char *fileName, int line );
74 void genLineDirective( ostream &out );
75
76 /*********************************/
77
78 struct CodeGenData
79 {
80         /*
81          * The interface to the code generator.
82          */
83         virtual void finishRagelDef() {}
84
85         /* These are invoked by the corresponding write statements. */
86         virtual void writeData() {};
87         virtual void writeInit() {};
88         virtual void writeExec() {};
89         virtual void writeExports() {};
90
91         /* This can also be overwridden to modify the processing of write
92          * statements. */
93         virtual void writeStatement( GenInputLoc &loc, int nargs, char **args );
94
95         /********************/
96
97         CodeGenData( ostream &out );
98         virtual ~CodeGenData() {}
99
100         /* 
101          * Collecting the machine.
102          */
103
104         const char *sourceFileName;
105         const char *fsmName;
106         ostream &out;
107         RedFsmAp *redFsm;
108         GenAction *allActions;
109         RedAction *allActionTables;
110         Condition *allConditions;
111         GenCondSpace *allCondSpaces;
112         RedStateAp *allStates;
113         NameInst **nameIndex;
114         int startState;
115         int errState;
116         GenActionList actionList;
117         ConditionList conditionList;
118         CondSpaceList condSpaceList;
119         GenInlineList *getKeyExpr;
120         GenInlineList *accessExpr;
121         GenInlineList *prePushExpr;
122         GenInlineList *postPopExpr;
123
124         /* Overriding variables. */
125         GenInlineList *pExpr;
126         GenInlineList *peExpr;
127         GenInlineList *eofExpr;
128         GenInlineList *csExpr;
129         GenInlineList *topExpr;
130         GenInlineList *stackExpr;
131         GenInlineList *actExpr;
132         GenInlineList *tokstartExpr;
133         GenInlineList *tokendExpr;
134         GenInlineList *dataExpr;
135
136         KeyOps thisKeyOps;
137         bool wantComplete;
138         EntryIdVect entryPointIds;
139         EntryNameVect entryPointNames;
140         bool hasLongestMatch;
141         ExportList exportList;
142
143         /* Write options. */
144         bool hasEnd;
145         bool dataPrefix;
146         bool writeFirstFinal;
147         bool writeErr;
148         bool writeCS;
149
150         void createMachine();
151         void initActionList( unsigned long length );
152         void newAction( int anum, char *name, int line, int col, GenInlineList *inlineList );
153         void initActionTableList( unsigned long length );
154         void initStateList( unsigned long length );
155         void setStartState( unsigned long startState );
156         void setErrorState( unsigned long errState );
157         void addEntryPoint( char *name, unsigned long entryState );
158         void setId( int snum, int id );
159         void setFinal( int snum );
160         void initTransList( int snum, unsigned long length );
161         void newTrans( int snum, int tnum, Key lowKey, Key highKey, 
162                         long targ, long act );
163         void finishTransList( int snum );
164         void setStateActions( int snum, long toStateAction, 
165                         long fromStateAction, long eofAction );
166         void setEofTrans( int snum, long targ, long eofAction );
167         void setForcedErrorState()
168                 { redFsm->forcedErrorState = true; }
169
170         
171         void initCondSpaceList( ulong length );
172         void condSpaceItem( int cnum, long condActionId );
173         void newCondSpace( int cnum, int condSpaceId, Key baseKey );
174
175         void initStateCondList( int snum, ulong length );
176         void addStateCond( int snum, Key lowKey, Key highKey, long condNum );
177
178         GenCondSpace *findCondSpace( Key lowKey, Key highKey );
179         Condition *findCondition( Key key );
180
181         bool setAlphType( const char *data );
182
183         void resolveTargetStates( GenInlineList *inlineList );
184         Key findMaxKey();
185
186         /* Gather various info on the machine. */
187         void analyzeActionList( RedAction *redAct, GenInlineList *inlineList );
188         void analyzeAction( GenAction *act, GenInlineList *inlineList );
189         void findFinalActionRefs();
190         void analyzeMachine();
191
192         void closeMachine();
193         void setValueLimits();
194         void assignActionIds();
195
196         ostream &source_warning( const GenInputLoc &loc );
197         ostream &source_error( const GenInputLoc &loc );
198 };
199
200
201 #endif /* _GENDATA_H */