Tizen 2.1 base
[external/ragel.git] / rlcodegen / gendata.h
1 /*
2  *  Copyright 2005-2006 Adrian Thurston <thurston@cs.queensu.ca>
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 "redfsm.h"
27 #include "fsmcodegen.h"
28 #include "common.h"
29
30 struct NameInst;
31 typedef DList<Action> ActionList;
32
33 typedef unsigned long ulong;
34
35 typedef AvlMap<char *, CodeGenData*, CmpStr> CodeGenMap;
36 typedef AvlMapEl<char *, CodeGenData*> CodeGenMapEl;
37
38 #define WO_NOEND    0x01
39 #define WO_NOERROR  0x02
40 #define WO_NOPREFIX 0x04
41 #define WO_NOFF     0x08
42
43 struct CodeGenData
44 {
45         CodeGenData( char *fileName, char *fsmName, bool wantComplete )
46         :
47                 fileName(fileName),
48                 fsmName(fsmName), 
49                 redFsm(0), 
50                 allActions(0),
51                 allActionTables(0),
52                 allConditions(0),
53                 allCondSpaces(0),
54                 allStates(0),
55                 nameIndex(0),
56                 startState(0),
57                 getKeyExpr(0),
58                 accessExpr(0),
59                 curStateExpr(0),
60                 codeGen(0),
61                 wantComplete(wantComplete),
62                 writeOps(0),
63                 writeData(false),
64                 writeInit(false),
65                 writeExec(false),
66                 writeEOF(false),
67                 hasLongestMatch(false),
68                 hasEnd(true),
69                 dataPrefix(true),
70                 writeFirstFinal(true),
71                 writeErr(true),
72                 hasBeenPrepared(false)
73         { }
74
75         /* 
76          * Collecting the machine.
77          */
78
79         char *fileName;
80         char *fsmName;
81         RedFsmAp *redFsm;
82         Action *allActions;
83         RedAction *allActionTables;
84         Condition *allConditions;
85         CondSpace *allCondSpaces;
86         RedStateAp *allStates;
87         NameInst **nameIndex;
88         int startState;
89         ActionList actionList;
90         ConditionList conditionList;
91         CondSpaceList condSpaceList;
92         InlineList *getKeyExpr;
93         InlineList *accessExpr;
94         InlineList *curStateExpr;
95         FsmCodeGen *codeGen;
96         KeyOps thisKeyOps;
97         bool wantComplete;
98         int writeOps;
99         bool writeData;
100         bool writeInit;
101         bool writeExec;
102         bool writeEOF;
103         EntryIdVect entryPointIds;
104         EntryNameVect entryPointNames;
105         bool hasLongestMatch;
106
107         /* Write options. */
108         bool hasEnd;
109         bool dataPrefix;
110         bool writeFirstFinal;
111         bool writeErr;
112
113         void createMachine();
114         void initActionList( unsigned long length );
115         void newAction( int anum, char *name, int line, int col, InlineList *inlineList );
116         void initActionTableList( unsigned long length );
117         void initStateList( unsigned long length );
118         void setStartState( unsigned long startState );
119         void addEntryPoint( char *name, unsigned long entryState );
120         void setFinal( int snum );
121         void initTransList( int snum, unsigned long length );
122         void newTrans( int snum, int tnum, Key lowKey, Key highKey, 
123                         long targ, long act );
124         void finishTransList( int snum );
125         void setStateActions( int snum, long toStateAction, 
126                         long fromStateAction, long eofAction );
127         void finishMachine();
128         void setForcedErrorState()
129                 { redFsm->forcedErrorState = true; }
130         
131         void initCondSpaceList( ulong length );
132         void condSpaceItem( int cnum, long condActionId );
133         void newCondSpace( int cnum, int condSpaceId, Key baseKey );
134
135         void initStateCondList( int snum, ulong length );
136         void addStateCond( int snum, Key lowKey, Key highKey, long condNum );
137
138         CondSpace *findCondSpace( Key lowKey, Key highKey );
139         Condition *findCondition( Key key );
140
141         bool setAlphType( char *data );
142
143         void makeCodeGen();
144         void generateGraphviz();
145         void resolveTargetStates( InlineList *inlineList );
146         Key findMaxKey();
147
148         void generate();
149         void generateCode();
150         void prepareMachine();
151         bool hasBeenPrepared;
152 };
153
154 extern CodeGenData *cgd;
155
156 void lineDirective( ostream &out, char *fileName, int line );
157 void genLineDirective( ostream &out );
158
159 #endif /* _GENDATA_H */