2 * Copyright 2001-2007 Adrian Thurston <thurston@complang.org>
5 /* This file is part of Ragel.
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.
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.
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
23 #include "dotcodegen.h"
35 /* Invoked by the parser when a ragel definition is opened. */
36 CodeGenData *dotMakeCodeGen( const char *sourceFileName, const char *fsmName,
37 ostream &out, bool wantComplete )
39 CodeGenData *codeGen = new GraphvizDotGen(out);
41 codeGen->sourceFileName = sourceFileName;
42 codeGen->fsmName = fsmName;
43 codeGen->wantComplete = wantComplete;
48 /* Override this so that write statement processing is ignored */
49 void GraphvizDotGen::writeStatement( InputLoc &, int, char ** )
53 std::ostream &GraphvizDotGen::KEY( Key key )
55 if ( displayPrintables && key.isPrintable() ) {
56 // Output values as characters, ensuring we escape the quote (") character
57 char cVal = (char) key.getVal();
60 out << "'\\" << cVal << "'";
87 out << "'" << cVal << "'";
92 if ( keyOps->isSigned )
95 out << (unsigned long) key.getVal();
101 std::ostream &GraphvizDotGen::TRANS_ACTION( RedStateAp *fromState, RedTransAp *trans )
104 RedAction *actions[3];
106 if ( fromState->fromStateAction != 0 )
107 actions[n++] = fromState->fromStateAction;
108 if ( trans->action != 0 )
109 actions[n++] = trans->action;
110 if ( trans->targ != 0 && trans->targ->toStateAction != 0 )
111 actions[n++] = trans->targ->toStateAction;
116 /* Loop the existing actions and write out what's there. */
117 for ( int a = 0; a < n; a++ ) {
118 for ( GenActionTable::Iter actIt = actions[a]->key.first(); actIt.lte(); actIt++ ) {
119 GenAction *action = actIt->value;
120 out << action->nameOrLoc();
121 if ( a < n-1 || !actIt.last() )
128 std::ostream &GraphvizDotGen::ACTION( RedAction *action )
132 for ( GenActionTable::Iter actIt = action->key.first(); actIt.lte(); actIt++ ) {
133 GenAction *action = actIt->value;
134 if ( action->name != 0 )
137 out << action->loc.line << ":" << action->loc.col;
144 std::ostream &GraphvizDotGen::ONCHAR( Key lowKey, Key highKey )
146 if ( lowKey > keyOps->maxKey ) {
147 GenCondSpace *condSpace = findCondSpace( lowKey, highKey );
148 Key values = ( lowKey - condSpace->baseKey ) / keyOps->alphSize();
150 lowKey = keyOps->minKey +
151 (lowKey - condSpace->baseKey - keyOps->alphSize() * values.getVal());
152 highKey = keyOps->minKey +
153 (highKey - condSpace->baseKey - keyOps->alphSize() * values.getVal());
155 if ( lowKey != highKey ) {
161 for ( GenCondSet::Iter csi = condSpace->condSet; csi.lte(); csi++ ) {
162 bool set = values & (1 << csi.pos());
165 out << (*csi)->nameOrLoc();
172 /* Output the key. Possibly a range. */
174 if ( highKey != lowKey ) {
182 void GraphvizDotGen::writeTransList( RedStateAp *state )
184 /* Build the set of unique transitions out of this state. */
185 RedTransSet stTransSet;
186 for ( RedTransList::Iter tel = state->outRange; tel.lte(); tel++ ) {
187 /* If we haven't seen the transitions before, the move forward
188 * emitting all the transitions on the same character. */
189 if ( stTransSet.insert( tel->value ) ) {
190 /* Write out the from and to states. */
191 out << "\t" << state->id << " -> ";
193 if ( tel->value->targ == 0 )
194 out << "err_" << state->id;
196 out << tel->value->targ->id;
198 /* Begin the label. */
199 out << " [ label = \"";
200 ONCHAR( tel->lowKey, tel->highKey );
202 /* Walk the transition list, finding the same. */
203 for ( RedTransList::Iter mtel = tel.next(); mtel.lte(); mtel++ ) {
204 if ( mtel->value == tel->value ) {
206 ONCHAR( mtel->lowKey, mtel->highKey );
210 /* Write the action and close the transition. */
211 TRANS_ACTION( state, tel->value );
216 /* Write the default transition. */
217 if ( state->defTrans != 0 ) {
218 /* Write out the from and to states. */
219 out << "\t" << state->id << " -> ";
221 if ( state->defTrans->targ == 0 )
222 out << "err_" << state->id;
224 out << state->defTrans->targ->id;
226 /* Begin the label. */
227 out << " [ label = \"DEF";
229 /* Write the action and close the transition. */
230 TRANS_ACTION( state, state->defTrans );
235 void GraphvizDotGen::writeDotFile( )
238 "digraph " << fsmName << " {\n"
241 /* Define the psuedo states. Transitions will be done after the states
242 * have been defined as either final or not final. */
243 out << " node [ shape = point ];\n";
245 if ( redFsm->startState != 0 )
248 /* Psuedo states for entry points in the entry map. */
249 for ( EntryIdVect::Iter en = entryPointIds; en.lte(); en++ ) {
250 RedStateAp *state = allStates + *en;
251 out << " en_" << state->id << ";\n";
254 /* Psuedo states for final states with eof actions. */
255 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
256 if ( st->eofTrans != 0 && st->eofTrans->action != 0 )
257 out << " eof_" << st->id << ";\n";
258 if ( st->eofAction != 0 )
259 out << " eof_" << st->id << ";\n";
262 out << " node [ shape = circle, height = 0.2 ];\n";
264 /* Psuedo states for states whose default actions go to error. */
265 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
266 bool needsErr = false;
267 if ( st->defTrans != 0 && st->defTrans->targ == 0 )
270 for ( RedTransList::Iter tel = st->outRange; tel.lte(); tel++ ) {
271 if ( tel->value->targ == 0 ) {
279 out << " err_" << st->id << " [ label=\"\"];\n";
282 /* Attributes common to all nodes, plus double circle for final states. */
283 out << " node [ fixedsize = true, height = 0.65, shape = doublecircle ];\n";
285 /* List Final states. */
286 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
288 out << " " << st->id << ";\n";
291 /* List transitions. */
292 out << " node [ shape = circle ];\n";
294 /* Walk the states. */
295 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ )
296 writeTransList( st );
298 /* Transitions into the start state. */
299 if ( redFsm->startState != 0 )
300 out << " ENTRY -> " << redFsm->startState->id << " [ label = \"IN\" ];\n";
302 /* Transitions into the entry points. */
303 for ( EntryIdVect::Iter en = entryPointIds; en.lte(); en++ ) {
304 RedStateAp *state = allStates + *en;
305 char *name = entryPointNames[en.pos()];
306 out << " en_" << state->id << " -> " << state->id <<
307 " [ label = \"" << name << "\" ];\n";
310 /* Out action transitions. */
311 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
312 if ( st->eofTrans != 0 && st->eofTrans->action != 0 ) {
313 out << " " << st->id << " -> eof_" <<
314 st->id << " [ label = \"EOF";
315 ACTION( st->eofTrans->action ) << "\" ];\n";
317 if ( st->eofAction != 0 ) {
318 out << " " << st->id << " -> eof_" <<
319 st->id << " [ label = \"EOF";
320 ACTION( st->eofAction ) << "\" ];\n";
328 void GraphvizDotGen::finishRagelDef()
330 if ( !graphvizDone ) {
333 /* For dot file generation we want to pick default transitions. */
334 redFsm->chooseDefaultSpan();
336 /* Write out with it. */