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
22 #include "rlgen-dot.h"
28 /* Override this so that write statement processing is ignored */
29 void GraphvizDotGen::writeStatement( GenInputLoc &, int, char ** )
33 std::ostream &GraphvizDotGen::KEY( Key key )
35 if ( displayPrintables && key.isPrintable() ) {
36 // Output values as characters, ensuring we escape the quote (") character
37 char cVal = (char) key.getVal();
40 out << "'\\" << cVal << "'";
67 out << "'" << cVal << "'";
72 if ( keyOps->isSigned )
75 out << (unsigned long) key.getVal();
81 std::ostream &GraphvizDotGen::TRANS_ACTION( RedStateAp *fromState, RedTransAp *trans )
84 RedAction *actions[3];
86 if ( fromState->fromStateAction != 0 )
87 actions[n++] = fromState->fromStateAction;
88 if ( trans->action != 0 )
89 actions[n++] = trans->action;
90 if ( trans->targ != 0 && trans->targ->toStateAction != 0 )
91 actions[n++] = trans->targ->toStateAction;
96 /* Loop the existing actions and write out what's there. */
97 for ( int a = 0; a < n; a++ ) {
98 for ( GenActionTable::Iter actIt = actions[a]->key.first(); actIt.lte(); actIt++ ) {
99 GenAction *action = actIt->value;
100 out << action->nameOrLoc();
101 if ( a < n-1 || !actIt.last() )
108 std::ostream &GraphvizDotGen::ACTION( RedAction *action )
112 for ( GenActionTable::Iter actIt = action->key.first(); actIt.lte(); actIt++ ) {
113 GenAction *action = actIt->value;
114 if ( action->name != 0 )
117 out << action->loc.line << ":" << action->loc.col;
124 std::ostream &GraphvizDotGen::ONCHAR( Key lowKey, Key highKey )
126 if ( lowKey > keyOps->maxKey ) {
127 GenCondSpace *condSpace = findCondSpace( lowKey, highKey );
128 Key values = ( lowKey - condSpace->baseKey ) / keyOps->alphSize();
130 lowKey = keyOps->minKey +
131 (lowKey - condSpace->baseKey - keyOps->alphSize() * values.getVal());
132 highKey = keyOps->minKey +
133 (highKey - condSpace->baseKey - keyOps->alphSize() * values.getVal());
135 if ( lowKey != highKey ) {
141 for ( GenCondSet::Iter csi = condSpace->condSet; csi.lte(); csi++ ) {
142 bool set = values & (1 << csi.pos());
145 out << (*csi)->nameOrLoc();
152 /* Output the key. Possibly a range. */
154 if ( highKey != lowKey ) {
162 void GraphvizDotGen::writeTransList( RedStateAp *state )
164 /* Build the set of unique transitions out of this state. */
165 RedTransSet stTransSet;
166 for ( RedTransList::Iter tel = state->outRange; tel.lte(); tel++ ) {
167 /* If we haven't seen the transitions before, the move forward
168 * emitting all the transitions on the same character. */
169 if ( stTransSet.insert( tel->value ) ) {
170 /* Write out the from and to states. */
171 out << "\t" << state->id << " -> ";
173 if ( tel->value->targ == 0 )
174 out << "err_" << state->id;
176 out << tel->value->targ->id;
178 /* Begin the label. */
179 out << " [ label = \"";
180 ONCHAR( tel->lowKey, tel->highKey );
182 /* Walk the transition list, finding the same. */
183 for ( RedTransList::Iter mtel = tel.next(); mtel.lte(); mtel++ ) {
184 if ( mtel->value == tel->value ) {
186 ONCHAR( mtel->lowKey, mtel->highKey );
190 /* Write the action and close the transition. */
191 TRANS_ACTION( state, tel->value );
196 /* Write the default transition. */
197 if ( state->defTrans != 0 ) {
198 /* Write out the from and to states. */
199 out << "\t" << state->id << " -> ";
201 if ( state->defTrans->targ == 0 )
202 out << "err_" << state->id;
204 out << state->defTrans->targ->id;
206 /* Begin the label. */
207 out << " [ label = \"DEF";
209 /* Write the action and close the transition. */
210 TRANS_ACTION( state, state->defTrans );
215 void GraphvizDotGen::writeDotFile( )
218 "digraph " << fsmName << " {\n"
221 /* Define the psuedo states. Transitions will be done after the states
222 * have been defined as either final or not final. */
223 out << " node [ shape = point ];\n";
225 if ( redFsm->startState != 0 )
228 /* Psuedo states for entry points in the entry map. */
229 for ( EntryIdVect::Iter en = entryPointIds; en.lte(); en++ ) {
230 RedStateAp *state = allStates + *en;
231 out << " en_" << state->id << ";\n";
234 /* Psuedo states for final states with eof actions. */
235 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
236 if ( st->eofTrans != 0 && st->eofTrans->action != 0 )
237 out << " eof_" << st->id << ";\n";
238 if ( st->eofAction != 0 )
239 out << " eof_" << st->id << ";\n";
242 out << " node [ shape = circle, height = 0.2 ];\n";
244 /* Psuedo states for states whose default actions go to error. */
245 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
246 bool needsErr = false;
247 if ( st->defTrans != 0 && st->defTrans->targ == 0 )
250 for ( RedTransList::Iter tel = st->outRange; tel.lte(); tel++ ) {
251 if ( tel->value->targ == 0 ) {
259 out << " err_" << st->id << " [ label=\"\"];\n";
262 /* Attributes common to all nodes, plus double circle for final states. */
263 out << " node [ fixedsize = true, height = 0.65, shape = doublecircle ];\n";
265 /* List Final states. */
266 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
268 out << " " << st->id << ";\n";
271 /* List transitions. */
272 out << " node [ shape = circle ];\n";
274 /* Walk the states. */
275 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ )
276 writeTransList( st );
278 /* Transitions into the start state. */
279 if ( redFsm->startState != 0 )
280 out << " ENTRY -> " << redFsm->startState->id << " [ label = \"IN\" ];\n";
282 /* Transitions into the entry points. */
283 for ( EntryIdVect::Iter en = entryPointIds; en.lte(); en++ ) {
284 RedStateAp *state = allStates + *en;
285 char *name = entryPointNames[en.pos()];
286 out << " en_" << state->id << " -> " << state->id <<
287 " [ label = \"" << name << "\" ];\n";
290 /* Out action transitions. */
291 for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
292 if ( st->eofTrans != 0 && st->eofTrans->action != 0 ) {
293 out << " " << st->id << " -> eof_" <<
294 st->id << " [ label = \"EOF";
295 ACTION( st->eofTrans->action ) << "\" ];\n";
297 if ( st->eofAction != 0 ) {
298 out << " " << st->id << " -> eof_" <<
299 st->id << " [ label = \"EOF";
300 ACTION( st->eofAction ) << "\" ];\n";
308 void GraphvizDotGen::finishRagelDef()
310 if ( !graphvizDone ) {
313 /* For dot file generation we want to pick default transitions. */
314 redFsm->chooseDefaultSpan();
316 /* Write out with it. */