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