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