2 * Copyright 2005-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
32 extern bool generateDot;
35 typedef DList<GenAction> GenActionList;
37 typedef unsigned long ulong;
39 extern int gblErrorCount;
43 typedef AvlMap<char *, CodeGenData*, CmpStr> CodeGenMap;
44 typedef AvlMapEl<char *, CodeGenData*> CodeGenMapEl;
46 void cdLineDirective( ostream &out, const char *fileName, int line );
47 void javaLineDirective( ostream &out, const char *fileName, int line );
48 void rubyLineDirective( ostream &out, const char *fileName, int line );
49 void csharpLineDirective( ostream &out, const char *fileName, int line );
50 void genLineDirective( ostream &out );
51 void lineDirective( ostream &out, const char *fileName, int line );
55 /*********************************/
60 * The interface to the code generator.
62 virtual void finishRagelDef() {}
64 /* These are invoked by the corresponding write statements. */
65 virtual void writeData() {};
66 virtual void writeInit() {};
67 virtual void writeExec() {};
68 virtual void writeExports() {};
69 virtual void writeStart() {};
70 virtual void writeFirstFinal() {};
71 virtual void writeError() {};
73 /* This can also be overwridden to modify the processing of write
75 virtual void writeStatement( InputLoc &loc, int nargs, char **args );
77 /********************/
79 CodeGenData( ostream &out );
80 virtual ~CodeGenData() {}
83 * Collecting the machine.
86 const char *sourceFileName;
90 GenAction *allActions;
91 RedAction *allActionTables;
92 Condition *allConditions;
93 GenCondSpace *allCondSpaces;
94 RedStateAp *allStates;
98 GenActionList actionList;
99 ConditionList conditionList;
100 CondSpaceList condSpaceList;
101 GenInlineList *getKeyExpr;
102 GenInlineList *accessExpr;
103 GenInlineList *prePushExpr;
104 GenInlineList *postPopExpr;
106 /* Overriding variables. */
107 GenInlineList *pExpr;
108 GenInlineList *peExpr;
109 GenInlineList *eofExpr;
110 GenInlineList *csExpr;
111 GenInlineList *topExpr;
112 GenInlineList *stackExpr;
113 GenInlineList *actExpr;
114 GenInlineList *tokstartExpr;
115 GenInlineList *tokendExpr;
116 GenInlineList *dataExpr;
120 EntryIdVect entryPointIds;
121 EntryNameVect entryPointNames;
122 bool hasLongestMatch;
123 ExportList exportList;
132 void createMachine();
133 void initActionList( unsigned long length );
134 void newAction( int anum, const char *name, int line, int col, GenInlineList *inlineList );
135 void initActionTableList( unsigned long length );
136 void initStateList( unsigned long length );
137 void setStartState( unsigned long startState );
138 void setErrorState( unsigned long errState );
139 void addEntryPoint( char *name, unsigned long entryState );
140 void setId( int snum, int id );
141 void setFinal( int snum );
142 void initTransList( int snum, unsigned long length );
143 void newTrans( int snum, int tnum, Key lowKey, Key highKey,
144 long targ, long act );
145 void finishTransList( int snum );
146 void setStateActions( int snum, long toStateAction,
147 long fromStateAction, long eofAction );
148 void setEofTrans( int snum, long targ, long eofAction );
149 void setForcedErrorState()
150 { redFsm->forcedErrorState = true; }
153 void initCondSpaceList( ulong length );
154 void condSpaceItem( int cnum, long condActionId );
155 void newCondSpace( int cnum, int condSpaceId, Key baseKey );
157 void initStateCondList( int snum, ulong length );
158 void addStateCond( int snum, Key lowKey, Key highKey, long condNum );
160 GenCondSpace *findCondSpace( Key lowKey, Key highKey );
161 Condition *findCondition( Key key );
163 bool setAlphType( const char *data );
165 void resolveTargetStates( GenInlineList *inlineList );
168 /* Gather various info on the machine. */
169 void analyzeActionList( RedAction *redAct, GenInlineList *inlineList );
170 void analyzeAction( GenAction *act, GenInlineList *inlineList );
171 void findFinalActionRefs();
172 void analyzeMachine();
175 void setValueLimits();
176 void assignActionIds();
178 ostream &source_warning( const InputLoc &loc );
179 ostream &source_error( const InputLoc &loc );
180 void write_option_error( InputLoc &loc, char *arg );
183 CodeGenData *makeCodeGen( const char *sourceFileName,
184 const char *fsmName, ostream &out );