update(add) packaging directory and spec file from OBSTF:Private, OBS
[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 _XMLCODEGEN_H
23 #define _XMLCODEGEN_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 struct CodeGenData;
37
38 struct RedActionTable
39 :
40         public AvlTreeEl<RedActionTable>
41 {
42         RedActionTable( const ActionTable &key )
43         :       
44                 key(key), 
45                 id(0)
46         { }
47
48         const ActionTable &getKey() 
49                 { return key; }
50
51         ActionTable key;
52         int id;
53 };
54
55 typedef AvlTree<RedActionTable, ActionTable, CmpActionTable> ActionTableMap;
56
57 struct NextRedTrans
58 {
59         Key lowKey, highKey;
60         TransAp *trans;
61         TransAp *next;
62
63         void load() {
64                 if ( trans != 0 ) {
65                         next = trans->next;
66                         lowKey = trans->lowKey;
67                         highKey = trans->highKey;
68                 }
69         }
70
71         NextRedTrans( TransAp *t ) {
72                 trans = t;
73                 load();
74         }
75
76         void increment() {
77                 trans = next;
78                 load();
79         }
80 };
81
82 struct GenBase
83 {
84         GenBase( char *fsmName, ParseData *pd, FsmAp *fsm );
85
86         void appendTrans( TransListVect &outList, Key lowKey, Key highKey, TransAp *trans );
87         void reduceActionTables();
88
89         char *fsmName;
90         ParseData *pd;
91         FsmAp *fsm;
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, std::ostream &out );
101
102         void writeXML( );
103
104 private:
105         void writeStateActions( StateAp *state );
106         void writeStateList();
107         void writeStateConditions( StateAp *state );
108
109         void writeKey( Key key );
110         void writeText( InlineItem *item );
111         void writeGoto( InlineItem *item );
112         void writeGotoExpr( InlineItem *item );
113         void writeCall( InlineItem *item );
114         void writeCallExpr( InlineItem *item );
115         void writeNext( InlineItem *item );
116         void writeNextExpr( InlineItem *item );
117         void writeEntry( InlineItem *item );
118         void writeLmOnLast( InlineItem *item );
119         void writeLmOnNext( InlineItem *item );
120         void writeLmOnLagBehind( InlineItem *item );
121
122         void writeExports();
123         bool writeNameInst( NameInst *nameInst );
124         void writeEntryPoints();
125         void writeConditions();
126         void writeInlineList( InlineList *inlineList );
127         void writeActionList();
128         void writeActionTableList();
129         void reduceTrans( TransAp *trans );
130         void writeTransList( StateAp *state );
131         void writeEofTrans( StateAp *state );
132         void writeTrans( Key lowKey, Key highKey, TransAp *defTrans );
133         void writeAction( Action *action );
134         void writeLmSwitch( InlineItem *item );
135         void writeMachine();
136         void writeActionExec( InlineItem *item );
137
138         std::ostream &out;
139 };
140
141 class BackendGen : protected GenBase
142 {
143 public:
144         BackendGen( char *fsmName, ParseData *pd, FsmAp *fsm, CodeGenData *cgd );
145         void makeBackend( );
146
147 private:
148         void makeGenInlineList( GenInlineList *outList, InlineList *inList );
149         void makeKey( GenInlineList *outList, Key key );
150         void makeText( GenInlineList *outList, InlineItem *item );
151         void makeLmOnLast( GenInlineList *outList, InlineItem *item );
152         void makeLmOnNext( GenInlineList *outList, InlineItem *item );
153         void makeLmOnLagBehind( GenInlineList *outList, InlineItem *item );
154         void makeActionExec( GenInlineList *outList, InlineItem *item );
155         void makeLmSwitch( GenInlineList *outList, InlineItem *item );
156         void makeSetTokend( GenInlineList *outList, long offset );
157         void makeSetAct( GenInlineList *outList, long lmId );
158         void makeSubList( GenInlineList *outList, InlineList *inlineList, 
159                         GenInlineItem::Type type );
160         void makeTargetItem( GenInlineList *outList, NameInst *nameTarg, GenInlineItem::Type type );
161         void makeExecGetTokend( GenInlineList *outList );
162         void makeExports();
163         void makeMachine();
164         void makeActionList();
165         void makeAction( Action *action );
166         void makeActionTableList();
167         void makeConditions();
168         void makeEntryPoints();
169         bool makeNameInst( std::string &out, NameInst *nameInst );
170         void makeStateList();
171
172         void makeStateActions( StateAp *state );
173         void makeEofTrans( StateAp *state );
174         void makeStateConditions( StateAp *state );
175         void makeTransList( StateAp *state );
176         void makeTrans( Key lowKey, Key highKey, TransAp *trans );
177
178         void close_ragel_def();
179
180         CodeGenData *cgd;
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 #endif