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;
47 * The interface to the parser
50 /* These functions must be implemented by the code generation executable.
51 * The openOutput function is invoked when the root element is opened. The
52 * makeCodeGen function is invoked when a ragel_def element is opened. */
53 std::ostream *cdOpenOutput( const char *inputFile );
54 std::ostream *javaOpenOutput( const char *inputFile );
55 std::ostream *rubyOpenOutput( const char *inputFile );
56 std::ostream *csharpOpenOutput( const char *inputFile );
57 std::ostream *dotOpenOutput( const char *inputFile );
59 CodeGenData *cdMakeCodeGen( const char *sourceFileName,
60 const char *fsmName, ostream &out, bool wantComplete );
61 CodeGenData *javaMakeCodeGen( const char *sourceFileName,
62 const char *fsmName, ostream &out, bool wantComplete );
63 CodeGenData *rubyMakeCodeGen( const char *sourceFileName,
64 const char *fsmName, ostream &out, bool wantComplete );
65 CodeGenData *csharpMakeCodeGen( const char *sourceFileName,
66 const char *fsmName, ostream &out, bool wantComplete );
67 CodeGenData *dotMakeCodeGen( const char *sourceFileName,
68 const char *fsmName, ostream &out, bool wantComplete );
70 void cdLineDirective( ostream &out, const char *fileName, int line );
71 void javaLineDirective( ostream &out, const char *fileName, int line );
72 void rubyLineDirective( ostream &out, const char *fileName, int line );
73 void csharpLineDirective( ostream &out, const char *fileName, int line );
74 void genLineDirective( ostream &out );
75 void lineDirective( ostream &out, const char *fileName, int line );
77 /*********************************/
82 * The interface to the code generator.
84 virtual void finishRagelDef() {}
86 /* These are invoked by the corresponding write statements. */
87 virtual void writeData() {};
88 virtual void writeInit() {};
89 virtual void writeExec() {};
90 virtual void writeExports() {};
91 virtual void writeStart() {};
92 virtual void writeFirstFinal() {};
93 virtual void writeError() {};
95 /* This can also be overwridden to modify the processing of write
97 virtual void writeStatement( InputLoc &loc, int nargs, char **args );
99 /********************/
101 CodeGenData( ostream &out );
102 virtual ~CodeGenData() {}
105 * Collecting the machine.
108 const char *sourceFileName;
112 GenAction *allActions;
113 RedAction *allActionTables;
114 Condition *allConditions;
115 GenCondSpace *allCondSpaces;
116 RedStateAp *allStates;
117 NameInst **nameIndex;
120 GenActionList actionList;
121 ConditionList conditionList;
122 CondSpaceList condSpaceList;
123 GenInlineList *getKeyExpr;
124 GenInlineList *accessExpr;
125 GenInlineList *prePushExpr;
126 GenInlineList *postPopExpr;
128 /* Overriding variables. */
129 GenInlineList *pExpr;
130 GenInlineList *peExpr;
131 GenInlineList *eofExpr;
132 GenInlineList *csExpr;
133 GenInlineList *topExpr;
134 GenInlineList *stackExpr;
135 GenInlineList *actExpr;
136 GenInlineList *tokstartExpr;
137 GenInlineList *tokendExpr;
138 GenInlineList *dataExpr;
142 EntryIdVect entryPointIds;
143 EntryNameVect entryPointNames;
144 bool hasLongestMatch;
145 ExportList exportList;
154 void createMachine();
155 void initActionList( unsigned long length );
156 void newAction( int anum, const char *name, int line, int col, GenInlineList *inlineList );
157 void initActionTableList( unsigned long length );
158 void initStateList( unsigned long length );
159 void setStartState( unsigned long startState );
160 void setErrorState( unsigned long errState );
161 void addEntryPoint( char *name, unsigned long entryState );
162 void setId( int snum, int id );
163 void setFinal( int snum );
164 void initTransList( int snum, unsigned long length );
165 void newTrans( int snum, int tnum, Key lowKey, Key highKey,
166 long targ, long act );
167 void finishTransList( int snum );
168 void setStateActions( int snum, long toStateAction,
169 long fromStateAction, long eofAction );
170 void setEofTrans( int snum, long targ, long eofAction );
171 void setForcedErrorState()
172 { redFsm->forcedErrorState = true; }
175 void initCondSpaceList( ulong length );
176 void condSpaceItem( int cnum, long condActionId );
177 void newCondSpace( int cnum, int condSpaceId, Key baseKey );
179 void initStateCondList( int snum, ulong length );
180 void addStateCond( int snum, Key lowKey, Key highKey, long condNum );
182 GenCondSpace *findCondSpace( Key lowKey, Key highKey );
183 Condition *findCondition( Key key );
185 bool setAlphType( const char *data );
187 void resolveTargetStates( GenInlineList *inlineList );
190 /* Gather various info on the machine. */
191 void analyzeActionList( RedAction *redAct, GenInlineList *inlineList );
192 void analyzeAction( GenAction *act, GenInlineList *inlineList );
193 void findFinalActionRefs();
194 void analyzeMachine();
197 void setValueLimits();
198 void assignActionIds();
200 ostream &source_warning( const InputLoc &loc );
201 ostream &source_error( const InputLoc &loc );
202 void write_option_error( InputLoc &loc, char *arg );