Started working on the direct construction of the backend inline item lists.
[external/ragel.git] / rlgen-ruby / ruby-codegen.h
1 /*
2  *  2007 Victor Hugo Borja <vic@rubyforge.org>
3  *  Copyright 2001-2007 Adrian Thurston <thurston@complang.org>
4  */
5
6 /*  This file is part of Ragel.
7  *
8  *  Ragel is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  * 
13  *  Ragel is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  * 
18  *  You should have received a copy of the GNU General Public License
19  *  along with Ragel; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
21  */
22
23 #ifndef _RUBY_CODEGEN_H
24 #define _RUBY_CODEGEN_H
25
26 #include "common.h"
27 #include "gendata.h"
28
29 /* Integer array line length. */
30 #define IALL 8
31
32 inline string itoa( int i )
33 {
34         char buf[16];
35         sprintf( buf, "%i", i );
36         return buf;
37 }
38
39
40 class RubyCodeGen : public CodeGenData
41 {
42 public:
43    RubyCodeGen( ostream &out ) : CodeGenData(out) { }
44    virtual ~RubyCodeGen() {}
45 protected:
46         ostream &START_ARRAY_LINE();
47         ostream &ARRAY_ITEM( string item, int count, bool last );
48         ostream &END_ARRAY_LINE();
49   
50
51         string FSM_NAME();
52
53         string START_STATE_ID();
54         string ERROR_STATE();
55         string FIRST_FINAL_STATE();
56         void INLINE_LIST(ostream &ret, GenInlineList *inlineList, int targState, bool inFinish);
57         string ACCESS();
58
59         void ACTION( ostream &ret, GenAction *action, int targState, bool inFinish );
60         string GET_KEY();
61         string GET_WIDE_KEY();
62         string GET_WIDE_KEY( RedStateAp *state );
63         string KEY( Key key );
64         string TABS( int level );
65         string INT( int i );
66         void CONDITION( ostream &ret, GenAction *condition );
67         string ALPH_TYPE();
68         string WIDE_ALPH_TYPE();
69         string ARRAY_TYPE( unsigned long maxVal );
70         ostream &ACTIONS_ARRAY();
71         void STATE_IDS();
72
73
74         string DATA_PREFIX();
75         string PM() { return "_" + DATA_PREFIX() + "partition_map"; }
76         string C() { return "_" + DATA_PREFIX() + "cond_spaces"; }
77         string CK() { return "_" + DATA_PREFIX() + "cond_keys"; }
78         string K() { return "_" + DATA_PREFIX() + "trans_keys"; }
79         string I() { return "_" + DATA_PREFIX() + "indicies"; }
80         string CO() { return "_" + DATA_PREFIX() + "cond_offsets"; }
81         string KO() { return "_" + DATA_PREFIX() + "key_offsets"; }
82         string IO() { return "_" + DATA_PREFIX() + "index_offsets"; }
83         string CL() { return "_" + DATA_PREFIX() + "cond_lengths"; }
84         string SL() { return "_" + DATA_PREFIX() + "single_lengths"; }
85         string RL() { return "_" + DATA_PREFIX() + "range_lengths"; }
86         string A() { return "_" + DATA_PREFIX() + "actions"; }
87         string TA() { return "_" + DATA_PREFIX() + "trans_actions"; }
88         string TT() { return "_" + DATA_PREFIX() + "trans_targs"; }
89         string TSA() { return "_" + DATA_PREFIX() + "to_state_actions"; }
90         string FSA() { return "_" + DATA_PREFIX() + "from_state_actions"; }
91         string EA() { return "_" + DATA_PREFIX() + "eof_actions"; }
92         string ET() { return "_" + DATA_PREFIX() + "eof_trans"; }
93         string SP() { return "_" + DATA_PREFIX() + "key_spans"; }
94         string CSP() { return "_" + DATA_PREFIX() + "cond_key_spans"; }
95         string START() { return DATA_PREFIX() + "start"; }
96         string ERROR() { return DATA_PREFIX() + "error"; }
97         string FIRST_FINAL() { return DATA_PREFIX() + "first_final"; }
98         string CTXDATA() { return DATA_PREFIX() + "ctxdata"; }
99
100 public:
101         string NULL_ITEM();
102         ostream &OPEN_ARRAY( string type, string name );
103         ostream &CLOSE_ARRAY();
104         ostream &STATIC_VAR( string type, string name );
105         string ARR_OFF( string ptr, string offset );
106
107         string P();
108         string PE();
109         string EOFV();
110
111         string CS();
112         string TOP();
113         string STACK();
114         string ACT();
115         string TOKSTART();
116         string TOKEND();
117         string DATA();
118
119
120         void finishRagelDef();
121         unsigned int arrayTypeSize( unsigned long maxVal );
122
123 protected:
124         virtual void writeExports();
125         virtual void writeInit();
126
127         /* Determine if we should use indicies. */
128         virtual void calcIndexSize();
129
130         virtual void BREAK( ostream &ret, int targState ) = 0;
131         virtual void GOTO( ostream &ret, int gotoDest, bool inFinish ) = 0;
132         virtual void GOTO_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ) = 0;
133         virtual void CALL( ostream &ret, int callDest, int targState, bool inFinish ) = 0;
134         virtual void CALL_EXPR( ostream &ret, GenInlineItem *ilItem, int targState, bool inFinish ) = 0;
135         virtual void RET( ostream &ret, bool inFinish ) = 0;
136
137
138         virtual void NEXT( ostream &ret, int nextDest, bool inFinish ) = 0;
139         virtual void NEXT_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish ) = 0;
140
141         virtual int TO_STATE_ACTION( RedStateAp *state ) = 0;
142         virtual int FROM_STATE_ACTION( RedStateAp *state ) = 0;
143         virtual int EOF_ACTION( RedStateAp *state ) = 0;
144
145         virtual int TRANS_ACTION( RedTransAp *trans );
146
147         void EXEC( ostream &ret, GenInlineItem *item, int targState, int inFinish );
148         void LM_SWITCH( ostream &ret, GenInlineItem *item, int targState, int inFinish );
149         void SET_ACT( ostream &ret, GenInlineItem *item );
150         void INIT_TOKSTART( ostream &ret, GenInlineItem *item );
151         void INIT_ACT( ostream &ret, GenInlineItem *item );
152         void SET_TOKSTART( ostream &ret, GenInlineItem *item );
153         void SET_TOKEND( ostream &ret, GenInlineItem *item );
154         void GET_TOKEND( ostream &ret, GenInlineItem *item );
155         void SUB_ACTION( ostream &ret, GenInlineItem *item, int targState, bool inFinish );
156
157 protected:
158         ostream &source_warning(const GenInputLoc &loc);
159         ostream &source_error(const GenInputLoc &loc);
160
161
162         /* fields */
163         bool outLabelUsed;
164         bool againLabelUsed;
165         bool useIndicies;
166
167         void genLineDirective( ostream &out );
168 };
169
170 /*
171  * Local Variables:
172  * mode: c++
173  * indent-tabs-mode: 1
174  * c-file-style: "bsd"
175  * End:
176  */
177
178 #endif