2 * Copyright 2001-2008 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
36 #include "parsetree.h"
46 struct FactorWithLabel;
59 typedef DList<LongestMatch> LmList;
62 /* Graph dictionary. */
65 public AvlTreeEl<GraphDictEl>,
66 public DListEl<GraphDictEl>
68 GraphDictEl( const char *k )
69 : key(k), value(0), isInstance(false) { }
70 GraphDictEl( const char *k, VarDef *value )
71 : key(k), value(value), isInstance(false) { }
73 const char *getKey() { return key; }
79 /* Location info of graph definition. Points to variable name of assignment. */
83 typedef AvlTree<GraphDictEl, const char*, CmpStr> GraphDict;
84 typedef DList<GraphDictEl> GraphList;
86 /* Priority name dictionary. */
87 typedef AvlMapEl<char*, int> PriorDictEl;
88 typedef AvlMap<char*, int, CmpStr> PriorDict;
90 /* Local error name dictionary. */
91 typedef AvlMapEl<const char*, int> LocalErrDictEl;
92 typedef AvlMap<const char*, int, CmpStr> LocalErrDict;
94 /* Tree of instantiated names. */
95 typedef BstMapEl<const char*, NameInst*> NameMapEl;
96 typedef BstMap<const char*, NameInst*, CmpStr> NameMap;
97 typedef Vector<NameInst*> NameVect;
98 typedef BstSet<NameInst*> NameSet;
100 /* Node in the tree of instantiated names. */
103 NameInst( const InputLoc &loc, NameInst *parent, const char *name, int id, bool isLabel ) :
104 loc(loc), parent(parent), name(name), id(id), isLabel(isLabel),
105 isLongestMatch(false), numRefs(0), numUses(0), start(0), final(0) {}
109 /* Keep parent pointers in the name tree to retrieve
110 * fully qulified names. */
121 /* Names underneath us, excludes anonymous names. */
124 /* All names underneath us in order of appearance. */
127 /* Join scopes need an implicit "final" target. */
128 NameInst *start, *final;
130 /* During a fsm generation walk, lists the names that are referenced by
131 * epsilon operations in the current scope. After the link is made by the
132 * epsilon reference and the join operation is complete, the label can
133 * have its refcount decremented. Once there are no more references the
134 * entry point can be removed from the fsm returned. */
135 NameVect referencedNames;
137 /* Pointers for the name search queue. */
138 NameInst *prev, *next;
140 /* Check if this name inst or any name inst below is referenced. */
144 typedef DList<NameInst> NameInstList;
146 /* Stack frame used in walking the name tree. */
149 NameInst *prevNameInst;
151 NameInst *prevLocalScope;
154 /* Class to collect information about the machine during the
158 /* Create a new parse data object. This is done at the beginning of every
159 * fsm specification. */
160 ParseData( const char *fileName, char *sectionName, const InputLoc §ionLoc );
164 * Setting up the graph dict.
167 /* Initialize a graph dict with the basic fsms. */
168 void initGraphDict();
169 void createBuiltin( const char *name, BuiltinMachine builtin );
171 /* Make a name id in the current name instantiation scope if it is not
173 NameInst *addNameInst( const InputLoc &loc, const char *data, bool isLabel );
174 void makeRootNames();
175 void makeNameTree( GraphDictEl *gdNode );
176 void makeExportsNameTree();
177 void fillNameIndex( NameInst *from );
178 void printNameTree();
180 /* Increments the usage count on entry names. Names that are no longer
181 * needed will have their entry points unset. */
182 void unsetObsoleteEntries( FsmAp *graph );
184 /* Resove name references in action code and epsilon transitions. */
185 NameSet resolvePart( NameInst *refFrom, const char *data, bool recLabelsOnly );
186 void resolveFrom( NameSet &result, NameInst *refFrom,
187 const NameRef &nameRef, int namePos );
188 NameInst *resolveStateRef( const NameRef &nameRef, InputLoc &loc, Action *action );
189 void resolveNameRefs( InlineList *inlineList, Action *action );
190 void resolveActionNameRefs();
192 /* Set the alphabet type. If type types are not valid returns false. */
193 bool setAlphType( const InputLoc &loc, char *s1, char *s2 );
194 bool setAlphType( const InputLoc &loc, char *s1 );
196 /* Override one of the variables ragel uses. */
197 bool setVariable( char *var, InlineList *inlineList );
199 /* Unique actions. */
200 void removeDups( ActionTable &actionTable );
201 void removeActionDups( FsmAp *graph );
203 /* Dumping the name instantiation tree. */
204 void printNameInst( NameInst *nameInst, int level );
206 /* Make the graph from a graph dict node. Does minimization. */
207 FsmAp *makeInstance( GraphDictEl *gdNode );
208 FsmAp *makeSpecific( GraphDictEl *gdNode );
211 /* Checking the contents of actions. */
212 void checkAction( Action *action );
213 void checkInlineList( Action *act, InlineList *inlineList );
215 void analyzeAction( Action *action, InlineList *inlineList );
216 void analyzeGraph( FsmAp *graph );
219 void prepareMachineGen( GraphDictEl *graphDictEl );
220 void prepareMachineGenTBWrapped( GraphDictEl *graphDictEl );
221 void generateXML( ostream &out );
222 void generateReduced( InputData &inputData );
224 bool generatingSectionSubset;
229 * Data collected during the parse.
232 /* Dictionary of graphs. Both instances and non-instances go here. */
235 /* The list of instances. */
236 GraphList instanceList;
238 /* Dictionary of actions. Lets actions be defined and then referenced. */
239 ActionDict actionDict;
241 /* Dictionary of named priorities. */
244 /* Dictionary of named local errors. */
245 LocalErrDict localErrDict;
247 /* List of actions. Will be pasted into a switch statement. */
248 ActionList actionList;
250 /* The id of the next priority name and label. */
251 int nextPriorKey, nextLocalErrKey, nextNameId, nextCondId;
253 /* The default priority number key for a machine. This is active during
254 * the parse of the rhs of a machine assignment. */
257 int curDefLocalErrKey;
260 HostType *userAlphType;
262 InputLoc alphTypeLoc;
264 /* Element type and get key expression. */
265 InlineList *getKeyExpr;
266 InlineList *accessExpr;
268 /* Stack management */
269 InlineList *prePushExpr;
270 InlineList *postPopExpr;
272 /* Overriding variables. */
278 InlineList *stackExpr;
280 InlineList *tokstartExpr;
281 InlineList *tokendExpr;
282 InlineList *dataExpr;
284 /* The alphabet range. */
285 char *lowerNum, *upperNum;
287 InputLoc rangeLowLoc, rangeHighLoc;
289 /* The name of the file the fsm is from, and the spec name. */
290 const char *fileName;
294 /* Counting the action and priority ordering. */
298 /* Root of the name tree. One root is for the instantiated machines. The
299 * other root is for exported definitions. */
301 NameInst *exportsRootName;
303 /* Name tree walking. */
304 NameInst *curNameInst;
307 /* The place where resolved epsilon transitions go. These cannot go into
308 * the parse tree because a single epsilon op can resolve more than once
309 * to different nameInsts if the machine it's in is used more than once. */
310 NameVect epsilonResolvedLinks;
311 int nextEpsilonResolvedLink;
313 /* Root of the name tree used for doing local name searches. */
314 NameInst *localNameScope;
316 void setLmInRetLoc( InlineList *inlineList );
317 void initLongestMatchData();
318 void setLongestMatchData( FsmAp *graph );
320 void initExportsNameWalk();
321 NameInst *nextNameScope() { return curNameInst->childVect[curNameChild]; }
322 NameFrame enterNameScope( bool isLocal, int numScopes );
323 void popNameScope( const NameFrame &frame );
324 void resetNameScope( const NameFrame &frame );
326 /* Make name ids to name inst pointers. */
327 NameInst **nameIndex;
329 /* Counter for assigning ids to longest match items. */
330 int nextLongestMatchId;
331 bool lmRequiresErrorState;
333 /* List of all longest match parse tree items. */
336 Action *newAction( const char *name, InlineList *inlineList );
338 Action *initTokStart;
350 void beginProcessing()
352 ::condData = &thisCondData;
353 ::keyOps = &thisKeyOps;
356 CondData thisCondData;
359 ExportList exportList;
364 void afterOpMinimize( FsmAp *fsm, bool lastInSeq = true );
365 Key makeFsmKeyHex( char *str, const InputLoc &loc, ParseData *pd );
366 Key makeFsmKeyDec( char *str, const InputLoc &loc, ParseData *pd );
367 Key makeFsmKeyNum( char *str, const InputLoc &loc, ParseData *pd );
368 Key makeFsmKeyChar( char c, ParseData *pd );
369 void makeFsmKeyArray( Key *result, char *data, int len, ParseData *pd );
370 void makeFsmUniqueKeyArray( KeySet &result, char *data, int len,
371 bool caseInsensitive, ParseData *pd );
372 FsmAp *makeBuiltin( BuiltinMachine builtin, ParseData *pd );
373 FsmAp *dotFsm( ParseData *pd );
374 FsmAp *dotStarFsm( ParseData *pd );
376 void errorStateLabels( const NameSet &locations );
386 std::ostringstream data;
389 Vector<char *> writeArgs;
393 InputItem *prev, *next;
402 typedef AvlMap<char*, Parser *, CmpStr> ParserDict;
403 typedef AvlMapEl<char*, Parser *> ParserDictEl;
405 typedef DList<InputItem> InputItemList;
407 extern ParserDict parserDict;
408 extern InputItemList inputItems;