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