Some more movement of code and data within the layer between frontend and
[external/ragel.git] / ragel / xmlcodegen.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 _XMLDOTGEN_H
23 #define _XMLDOTGEN_H
24
25 #include <iostream>
26 #include "avltree.h"
27 #include "fsmgraph.h"
28 #include "parsedata.h"
29 #include "redfsm.h"
30
31 /* Forwards. */
32 struct TransAp;
33 struct FsmAp;
34 struct ParseData;
35 struct GenInlineList;
36
37 struct RedActionTable
38 :
39         public AvlTreeEl<RedActionTable>
40 {
41         RedActionTable( const ActionTable &key )
42         :       
43                 key(key), 
44                 id(0)
45         { }
46
47         const ActionTable &getKey() 
48                 { return key; }
49
50         ActionTable key;
51         int id;
52 };
53
54 typedef AvlTree<RedActionTable, ActionTable, CmpActionTable> ActionTableMap;
55
56 struct NextRedTrans
57 {
58         Key lowKey, highKey;
59         TransAp *trans;
60         TransAp *next;
61
62         void load() {
63                 if ( trans != 0 ) {
64                         next = trans->next;
65                         lowKey = trans->lowKey;
66                         highKey = trans->highKey;
67                 }
68         }
69
70         NextRedTrans( TransAp *t ) {
71                 trans = t;
72                 load();
73         }
74
75         void increment() {
76                 trans = next;
77                 load();
78         }
79 };
80
81 struct GenBase
82 {
83         GenBase( char *fsmName, ParseData *pd, FsmAp *fsm );
84
85         void appendTrans( TransListVect &outList, Key lowKey, Key highKey, TransAp *trans );
86         void reduceActionTables();
87
88         char *fsmName;
89         ParseData *pd;
90         FsmAp *fsm;
91
92         ActionTableMap actionTableMap;
93         int nextActionTableId;
94 };
95
96 class XMLCodeGen : protected GenBase
97 {
98 public:
99         XMLCodeGen( char *fsmName, ParseData *pd, FsmAp *fsm, std::ostream &out );
100
101         void writeXML( );
102
103 private:
104         void writeStateActions( StateAp *state );
105         void writeStateList();
106         void writeStateConditions( StateAp *state );
107
108         void writeKey( Key key );
109         void writeText( InlineItem *item );
110         void writeGoto( InlineItem *item );
111         void writeGotoExpr( InlineItem *item );
112         void writeCall( InlineItem *item );
113         void writeCallExpr( InlineItem *item );
114         void writeNext( InlineItem *item );
115         void writeNextExpr( InlineItem *item );
116         void writeEntry( InlineItem *item );
117         void writeLmOnLast( InlineItem *item );
118         void writeLmOnNext( InlineItem *item );
119         void writeLmOnLagBehind( InlineItem *item );
120
121         void writeExports();
122         bool writeNameInst( NameInst *nameInst );
123         void writeEntryPoints();
124         void writeConditions();
125         void writeInlineList( InlineList *inlineList );
126         void writeActionList();
127         void writeActionTableList();
128         void reduceTrans( TransAp *trans );
129         void writeTransList( StateAp *state );
130         void writeEofTrans( StateAp *state );
131         void writeTrans( Key lowKey, Key highKey, TransAp *defTrans );
132         void writeAction( Action *action );
133         void writeLmSwitch( InlineItem *item );
134         void writeMachine();
135         void writeActionExec( InlineItem *item );
136
137         std::ostream &out;
138 };
139
140 class BackendGen : protected GenBase
141 {
142 public:
143         BackendGen( char *fsmName, ParseData *pd, FsmAp *fsm, InputData &inputData );
144         void makeBackend( );
145
146 private:
147         void makeGenInlineList( GenInlineList *outList, InlineList *inList );
148         void makeKey( GenInlineList *outList, Key key );
149         void makeText( GenInlineList *outList, InlineItem *item );
150         void makeLmOnLast( GenInlineList *outList, InlineItem *item );
151         void makeLmOnNext( GenInlineList *outList, InlineItem *item );
152         void makeLmOnLagBehind( GenInlineList *outList, InlineItem *item );
153         void makeActionExec( GenInlineList *outList, InlineItem *item );
154         void makeLmSwitch( GenInlineList *outList, InlineItem *item );
155         void makeSetTokend( GenInlineList *outList, long offset );
156         void makeSetAct( GenInlineList *outList, long lmId );
157         void makeSubList( GenInlineList *outList, InlineList *inlineList, 
158                         GenInlineItem::Type type );
159         void makeTargetItem( GenInlineList *outList, long targetId, GenInlineItem::Type type );
160         void makeExecGetTokend( GenInlineList *outList );
161         void makeExports();
162         void makeMachine();
163         void makeActionList();
164         void makeAction( Action *action );
165         void makeActionTableList();
166         void makeConditions();
167         void makeEntryPoints();
168         bool makeNameInst( std::string &out, NameInst *nameInst );
169         void makeStateList();
170
171         void makeStateActions( StateAp *state );
172         void makeEofTrans( StateAp *state );
173         void makeStateConditions( StateAp *state );
174         void makeTransList( StateAp *state );
175         void makeTrans( Key lowKey, Key highKey, TransAp *trans );
176
177         void open_ragel_def( char *fsmName );
178         void close_ragel_def();
179
180         InputData &inputData;
181
182         /* Collected during parsing. */
183         int curAction;
184         int curActionTable;
185         int curTrans;
186         int curState;
187         int curCondSpace;
188         int curStateCond;
189
190 };
191
192
193 #endif /* _XMLDOTGEN_H */