More work on the direct connection between frontend and backend. Now appears to
[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, XmlParser &xmlParser );
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         XmlParser &xmlParser;
92
93         ActionTableMap actionTableMap;
94         int nextActionTableId;
95 };
96
97 class XMLCodeGen : protected GenBase
98 {
99 public:
100         XMLCodeGen( char *fsmName, ParseData *pd, FsmAp *fsm, 
101                         std::ostream &out, XmlParser &xmlParser );
102
103         void writeXML( );
104
105 private:
106         void writeStateActions( StateAp *state );
107         void writeStateList();
108         void writeStateConditions( StateAp *state );
109
110         void writeKey( Key key );
111         void writeText( InlineItem *item );
112         void writeGoto( InlineItem *item );
113         void writeGotoExpr( InlineItem *item );
114         void writeCall( InlineItem *item );
115         void writeCallExpr( InlineItem *item );
116         void writeNext( InlineItem *item );
117         void writeNextExpr( InlineItem *item );
118         void writeEntry( InlineItem *item );
119         void writeLmOnLast( InlineItem *item );
120         void writeLmOnNext( InlineItem *item );
121         void writeLmOnLagBehind( InlineItem *item );
122
123         void writeExports();
124         bool writeNameInst( NameInst *nameInst );
125         void writeEntryPoints();
126         void writeConditions();
127         void writeInlineList( InlineList *inlineList );
128         void writeActionList();
129         void writeActionTableList();
130         void reduceTrans( TransAp *trans );
131         void writeTransList( StateAp *state );
132         void writeEofTrans( StateAp *state );
133         void writeTrans( Key lowKey, Key highKey, TransAp *defTrans );
134         void writeAction( Action *action );
135         void writeLmSwitch( InlineItem *item );
136         void writeMachine();
137         void writeActionExec( InlineItem *item );
138
139         std::ostream &out;
140 };
141
142 class BackendGen : protected GenBase
143 {
144 public:
145         BackendGen( char *fsmName, ParseData *pd, FsmAp *fsm, XmlParser &xmlParser );
146         void makeBackend( );
147
148 private:
149         void makeGenInlineList( GenInlineList *outList, InlineList *inList );
150         void makeKey( GenInlineList *outList, Key key );
151         void makeText( GenInlineList *outList, InlineItem *item );
152         void makeLmOnLast( GenInlineList *outList, InlineItem *item );
153         void makeLmOnNext( GenInlineList *outList, InlineItem *item );
154         void makeLmOnLagBehind( GenInlineList *outList, InlineItem *item );
155         void makeActionExec( GenInlineList *outList, InlineItem *item );
156         void makeLmSwitch( GenInlineList *outList, InlineItem *item );
157         void makeSetTokend( GenInlineList *outList, long offset );
158         void makeSetAct( GenInlineList *outList, long lmId );
159         void makeSubList( GenInlineList *outList, InlineList *inlineList, 
160                         GenInlineItem::Type type );
161         void makeTargetItem( GenInlineList *outList, long targetId, GenInlineItem::Type type );
162         void makeExecGetTokend( GenInlineList *outList );
163         void makeExports();
164         void makeMachine();
165         void makeActionList();
166         void makeAction( Action *action );
167         void makeActionTableList();
168         void makeConditions();
169         void makeEntryPoints();
170         bool makeNameInst( std::string &out, NameInst *nameInst );
171         void makeStateList();
172
173         void makeStateActions( StateAp *state );
174         void makeEofTrans( StateAp *state );
175         void makeStateConditions( StateAp *state );
176         void makeTransList( StateAp *state );
177         void makeTrans( Key lowKey, Key highKey, TransAp *trans );
178 };
179
180
181 #endif /* _XMLDOTGEN_H */