Some more cleanup. Put a pointer to CodeGenData in ParseData and eliminate codeGenMap...
[external/ragel.git] / ragel / 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 void lineDirective( ostream &out, const char *fileName, int line );
76
77 /*********************************/
78
79 struct CodeGenData
80 {
81         /*
82          * The interface to the code generator.
83          */
84         virtual void finishRagelDef() {}
85
86         /* These are invoked by the corresponding write statements. */
87         virtual void writeData() {};
88         virtual void writeInit() {};
89         virtual void writeExec() {};
90         virtual void writeExports() {};
91         virtual void writeStart() {};
92         virtual void writeFirstFinal() {};
93         virtual void writeError() {};
94
95         /* This can also be overwridden to modify the processing of write
96          * statements. */
97         virtual void writeStatement( InputLoc &loc, int nargs, char **args );
98
99         /********************/
100
101         CodeGenData( ostream &out );
102         virtual ~CodeGenData() {}
103
104         /* 
105          * Collecting the machine.
106          */
107
108         const char *sourceFileName;
109         const char *fsmName;
110         ostream &out;
111         RedFsmAp *redFsm;
112         GenAction *allActions;
113         RedAction *allActionTables;
114         Condition *allConditions;
115         GenCondSpace *allCondSpaces;
116         RedStateAp *allStates;
117         NameInst **nameIndex;
118         int startState;
119         int errState;
120         GenActionList actionList;
121         ConditionList conditionList;
122         CondSpaceList condSpaceList;
123         GenInlineList *getKeyExpr;
124         GenInlineList *accessExpr;
125         GenInlineList *prePushExpr;
126         GenInlineList *postPopExpr;
127
128         /* Overriding variables. */
129         GenInlineList *pExpr;
130         GenInlineList *peExpr;
131         GenInlineList *eofExpr;
132         GenInlineList *csExpr;
133         GenInlineList *topExpr;
134         GenInlineList *stackExpr;
135         GenInlineList *actExpr;
136         GenInlineList *tokstartExpr;
137         GenInlineList *tokendExpr;
138         GenInlineList *dataExpr;
139
140         KeyOps thisKeyOps;
141         bool wantComplete;
142         EntryIdVect entryPointIds;
143         EntryNameVect entryPointNames;
144         bool hasLongestMatch;
145         ExportList exportList;
146
147         /* Write options. */
148         bool noEnd;
149         bool noPrefix;
150         bool noFinal;
151         bool noError;
152         bool noCS;
153
154         void createMachine();
155         void initActionList( unsigned long length );
156         void newAction( int anum, const char *name, int line, int col, GenInlineList *inlineList );
157         void initActionTableList( unsigned long length );
158         void initStateList( unsigned long length );
159         void setStartState( unsigned long startState );
160         void setErrorState( unsigned long errState );
161         void addEntryPoint( char *name, unsigned long entryState );
162         void setId( int snum, int id );
163         void setFinal( int snum );
164         void initTransList( int snum, unsigned long length );
165         void newTrans( int snum, int tnum, Key lowKey, Key highKey, 
166                         long targ, long act );
167         void finishTransList( int snum );
168         void setStateActions( int snum, long toStateAction, 
169                         long fromStateAction, long eofAction );
170         void setEofTrans( int snum, long targ, long eofAction );
171         void setForcedErrorState()
172                 { redFsm->forcedErrorState = true; }
173
174         
175         void initCondSpaceList( ulong length );
176         void condSpaceItem( int cnum, long condActionId );
177         void newCondSpace( int cnum, int condSpaceId, Key baseKey );
178
179         void initStateCondList( int snum, ulong length );
180         void addStateCond( int snum, Key lowKey, Key highKey, long condNum );
181
182         GenCondSpace *findCondSpace( Key lowKey, Key highKey );
183         Condition *findCondition( Key key );
184
185         bool setAlphType( const char *data );
186
187         void resolveTargetStates( GenInlineList *inlineList );
188         Key findMaxKey();
189
190         /* Gather various info on the machine. */
191         void analyzeActionList( RedAction *redAct, GenInlineList *inlineList );
192         void analyzeAction( GenAction *act, GenInlineList *inlineList );
193         void findFinalActionRefs();
194         void analyzeMachine();
195
196         void closeMachine();
197         void setValueLimits();
198         void assignActionIds();
199
200         ostream &source_warning( const InputLoc &loc );
201         ostream &source_error( const InputLoc &loc );
202         void write_option_error( InputLoc &loc, char *arg );
203 };
204
205 #endif