KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > utils > parser > ProductionRules


1 package com.daffodilwoods.daffodildb.utils.parser;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.HashMap JavaDoc;
5
6 import java.lang.reflect.*;
7 import com.daffodilwoods.daffodildb.server.sql99.token._TOKEN;
8 import com.daffodilwoods.database.resource.*;
9 import com.daffodilwoods.database.utility.P;
10 import com.daffodilwoods.daffodildb.utils.DBStack;
11 import java.util.Iterator JavaDoc;
12 import java.util.Map JavaDoc;
13
14 /**
15  * A ProductionRules is an abstract class which is extended by all rules.
16  * Two Methods of this class is declared as abstract namely parsePart(ParseElements)
17  * and displayRules().
18  * This class is our main class which contains all the necessary variables required.
19  * Function of this class is to provide parse(ParseElements) method which is used
20  * in parsing the query and parse(query,position,queryArray) method is used to tokenize
21  * the given query.
22  * This Class makes use of reflection package to intantiate the class and assign it's
23  * declared the value forfields which it is parsed.
24  */

25
26 public abstract class ProductionRules implements java.io.Serializable JavaDoc,ProductionRuleParserConstants, Comparable JavaDoc{
27
28    /**
29     * Object array holding all the rules contained in this rule.
30     * Decalred Global as it is used by all the rules.
31     */

32    public Object JavaDoc []rules;
33    Object JavaDoc []updatedRules;
34
35    /**
36     * String variable denoting the name of rule.
37     * Decalred Global as it is used by all the rules.
38     */

39    public String JavaDoc nameOfRule;
40
41    /**
42     * RuleKey for actually containing info about rule
43     */

44    String JavaDoc ruleKey;
45
46    /**toke
47     * Object Variable to hold reference of class instantiated.
48     * Decalred global as StringProductionRules are instantiated at the time making rules.
49     * Commented by Kaushik on 09/09/2004 as part of Parser Optimization
50    Object classObject; */

51
52    /**
53     * Class Variable to hold reference of class.
54     * Decalred global as every class is loaded at the time of generating the rules.
55     */

56    Class JavaDoc classMaker;
57
58    /**
59     *
60     */

61    String JavaDoc className;
62
63 /**
64  * Character Array declared as it is used as an delimeter to determine whether the keyword
65  * is found or not.
66  */

67    static char[] delimitedTokens = new char[]{'(',',',' ',')','[',']',';','\n','\t','.','\'','"',13};
68
69
70 /**
71  * Object Variable to Hold the value contained in the rule used in making rules comparable.
72  */

73    Object JavaDoc result;
74
75    /**
76     * Boolean RecusrsiveFlag is taken to denote whether rule is recursive or not.
77     * If true means rule is recursive otherwise not.
78     */

79    boolean recursiveflag = false;
80
81    /**
82     * Boolean variable held to denote whether tree is to painted for rules as it is traversed.
83     */

84
85
86    /**
87     * TokenRule is a ProductionRule of <token> rule held used to tokenize query.
88     * Decalred Global as it is initialized at the time of generating rules.
89     */

90    ProductionRules tokenRule;
91
92    /**
93     * Array Of FieldNames which contains fieldnames of class of rule.
94     * Declared Global as it is initialized at the time of generating the rules.
95     */

96    String JavaDoc []fieldNames;
97    Object JavaDoc []fields;
98
99    /**
100     * HashMap on Temporary Basis
101     */

102    HashMap JavaDoc hashMap;
103
104    boolean angularType = false;
105    boolean repUnion = false;
106    boolean toMakeClass = true;
107    ClassLoader JavaDoc classLoader;
108
109    TokenGenerator TG=ProductionRuleParser.tk;
110
111    void setRepUnionType(boolean b){
112       repUnion = true;
113    }
114
115    ProductionRules(ClassLoader JavaDoc classLoader0){
116       classLoader = classLoader0;
117    }
118
119    /**
120     * This function recieves argument as an arrayList which contains rules contained in this rule.
121     * Assigns Object array of rules contained in this class.
122     * Calculates the fieldNames of all rules and stores in an String array.
123     * This Function gets a call at the time of generating the rules.
124     */

125
126    public void setProductionRules(ArrayList JavaDoc productionRules){
127       rules = productionRules.toArray();
128       updatedRules = rules;
129       if (( this instanceof StringProductionRules ) && productionRules.size() == 1 ){
130          fieldNames = new String JavaDoc[1];
131          fieldNames[0] = new StringBuffer JavaDoc("_").append((String JavaDoc)productionRules.get(0)).append("0").toString();
132          return;
133       }
134       if( nameOfRule != null ){
135          int size = rules == null ? 0 : rules.length;
136          fieldNames = new String JavaDoc[size];
137          for(int i = size - 1 ; i >= 0 ; i--){
138             String JavaDoc name = null;
139             if ( size == 1 && toMakeClass && !repUnion){
140                name=new StringBuffer JavaDoc("_").append(nameOfRule).append("0").toString();
141             }
142             else{
143                ProductionRules rule = (ProductionRules)rules[i];
144                if(rule instanceof StringProductionRules){
145                   name = new StringBuffer JavaDoc("_").append(((StringProductionRules)rule).keyWord).append(i).toString();
146                }
147                else{
148                   name = new StringBuffer JavaDoc("_").append(repUnion ? rule.ruleKey : rule.getProductionRuleName()).append(i).toString();
149                }
150             }
151             fieldNames[i] = updateNameOfRule(name);
152          }
153       }
154    }
155
156    /**
157     * This function sets the name of Rule. This name of rule is passed at the time of generating the rules.
158     */

159
160    public void setProductionRuleName(String JavaDoc nameOfRule1){
161       this.nameOfRule = nameOfRule1;
162       int size = rules == null ? 0 : rules.length;
163       fieldNames = new String JavaDoc[size];
164       for(int i = size - 1 ; i >= 0 ; i--){
165          String JavaDoc name = null;
166          if (size == 1 && toMakeClass && !repUnion){
167             StringBuffer JavaDoc str=new StringBuffer JavaDoc("_");
168             name = str.append(nameOfRule).append("0").toString();
169          }
170          else{
171             ProductionRules rule = (ProductionRules)rules[i];
172             if ( rule instanceof StringProductionRules ){
173                StringBuffer JavaDoc str=new StringBuffer JavaDoc("_");
174                name = str.append(( (StringProductionRules) rule).keyWord).append(i).toString();
175             }
176             else{
177                StringBuffer JavaDoc str=new StringBuffer JavaDoc("_");
178                name = str.append(repUnion ? rule.ruleKey : rule.getProductionRuleName()).append(i).toString();
179             }
180          }
181          fieldNames[i] = updateNameOfRule(name);
182       }
183       className = initailizeNameOfClass(nameOfRule);
184    }
185
186    /**
187     * This Function Initialize the name Of rule.
188       for example : rulename abc-ss is changed as sql.abc_ss
189       Initialised name of rule is used to load class into memory using reflection.
190       */

191
192    String JavaDoc initailizeNameOfClass(String JavaDoc nameRule){
193       return updateNameOfRule(nameRule);
194    }
195
196    /**
197     * This function returns nameOfRule.
198     */

199
200    String JavaDoc getProductionRuleName(){
201       return nameOfRule;
202    }
203
204    /**
205     * This function returns rules stored in this rule.
206     */

207
208
209    /**
210     * This function is used to tokenize the query.
211     * Parameters passed to this function are :
212     * a) query := String variable denoting query to be tokenize.
213     * b) position := Represents parse position of query.From this position
214     * query is to be tokenize.
215     * c) queryArray := Represents query in the form of character array.
216     * This Function returns an object array of 2 length in which at first position
217     * Token made is stored and at the second position parse position is stored.
218     */

219
220
221    /*private Object parse(String query,int position,char []queryArray) throws DException{
222       ParseElements pe = new ParseElements();
223        pe.successfulRule = new HashMap();
224        pe.position = position;
225        pe.query = query;
226        pe.hashMap = hashMap;
227        pe.tokenFlag = true;
228        pe.bestOptionFlag = false;
229        pe.queryArray = queryArray;
230        if (debugTreeFlag) {
231          pe.dTree = new DebugTree();
232          pe.dTree.setTitle(nameOfRule + " --- " + query);
233          pe.dTree.show();
234          pe.parentNode = pe.dTree.createChildNode( (DefaultMutableTreeNode) pe.
235                                                   dTree.tree.getModel().getRoot(),
236                                                   nameOfRule);
237        }
238        Object object = parse(pe);
239        return new Object[] {
240            object, new Integer(pe.position)};
241      }
242
243 */

244 /* private Object parse(String query,int position,char []queryArray) throws DException{
245       if (parseElement != null) {
246         return parse(query, position, queryArray, parseElement);
247       }
248       else {
249         parseElement = new ParseElements();
250         parseElement.successfulRule = new HashMap();
251         parseElement.position = position;
252         parseElement.query = query;
253         parseElement.hashMap = hashMap;
254         parseElement.tokenFlag = true;
255         parseElement.bestOptionFlag = false;
256         parseElement.queryArray = queryArray;
257         if (debugTreeFlag) {
258           parseElement.dTree = new DebugTree();
259           parseElement.dTree.setTitle(nameOfRule + " --- " + query);
260           parseElement.dTree.show();
261           parseElement.parentNode = parseElement.dTree.createChildNode( (DefaultMutableTreeNode) parseElement.
262                                                    dTree.tree.getModel().getRoot(),
263                                                    nameOfRule);
264         }
265         Object object = parse(parseElement);
266         return new Object[] {
267             object, new Integer(parseElement.position)};
268       }
269     }
270 */

271    private Object JavaDoc parse(String JavaDoc query,int position,char []queryArray,ParseElements pe) throws DException{
272       pe.successfulRule.clear();
273       pe.position = position;
274       pe.hashMap = hashMap;
275
276       Object JavaDoc object = parse(pe);
277       return new Object JavaDoc[]{object,new Integer JavaDoc(pe.position)};
278    }
279
280    public static void traverseSuccessFulMap(ParseElements pe){
281      Iterator JavaDoc it=pe.successfulRule.entrySet().iterator();
282      Map.Entry JavaDoc me;
283      while(it.hasNext()){
284        me =(Map.Entry JavaDoc)it.next();
285      }
286    }
287
288
289
290    /**
291     * This Function is called to make tokens of query and to get tokens actually parsed.
292     * At the time of making tokens Best Option is not to be used and binary rule is to be applied therefore
293     * EnvironmentFlags.tokenFlag is set to false and EnvironmentFlags.bestOptionFlag is set to true.
294     * After Tokenizing the query Best Option is to be used and binary is not to be used therefore reverse
295     * operation is performed.
296     * Best Option is option with the help of which rule whose parseposition is best among all the rules
297     * parse positions is considered.
298     * In this function getTokensOfQuery() method is used to tokenize the query.
299     * Parameters passed is a query which is to be parsed.
300     * Return type of this function is an object which we get after parsing a query.
301     * ParseException is thrown when query is not parsed.
302     */

303    public Object JavaDoc parseUsingTokens(String JavaDoc query) throws DException {
304       if(query == null)
305          throw new DException("DSE8007",null);//NEW Query passed is null.
306
query = query.trim();
307       ParseElements pe = new ParseElements();
308       pe.successfulRule = new HashMap JavaDoc();
309       pe.tokenFlag = false;
310       pe.bestOptionFlag = true;
311       pe.hashMap = hashMap;
312       pe.position = 0;
313       pe.query = query;
314       pe.parseException.setQuery(query);
315       pe.queryArray = query.toCharArray();
316
317       Object JavaDoc o[] =getTokensOfQuery(query,pe.queryArray,new ParseElements());
318
319
320
321       pe.tokens = (Object JavaDoc[])o[0];
322       pe.parseException.position = -1;
323       pe.index = (Object JavaDoc[])o[1];
324
325
326
327       pe.queryTokenCharacterArray = makeCharacterArray(pe.tokens);
328
329       Object JavaDoc object = parse(pe);
330
331       if ( pe.position < pe.tokens.length ){
332          com.daffodilwoods.daffodildb.server.serversystem.PrintHandler.print("NOT PARSED [" + query+"]");
333          pe.parseException.setQuery(query);
334          int position = pe.parseException.causeOfFailurePosition;
335
336          pe.parseException.causeOfFailure = "";
337          if(pe.tokens[position] instanceof com.daffodilwoods.daffodildb.server.sql99.token.SRESERVEDWORD1206543922)
338             pe.parseException.causeOfFailure = "the keyword ";
339
340          pe.parseException.causeOfFailure += pe.tokens[position];
341          throw pe.parseException;
342       }
343       return object;
344    }
345
346    public Object JavaDoc parseBatchUsingTokens(String JavaDoc query) throws DException {
347       if(query == null)
348          throw new DException("DSE8007",null);
349       query = query.trim();
350       ArrayList JavaDoc list = new ArrayList JavaDoc();
351       ParseElements pe = new ParseElements();
352       pe.successfulRule = new HashMap JavaDoc();
353       pe.tokenFlag = false;
354       pe.bestOptionFlag = true;
355       pe.hashMap = hashMap;
356       pe.position = 0;
357       pe.query = query;
358       pe.queryArray = query.toCharArray();
359
360       Object JavaDoc o[] = getTokensOfQuery(query,pe.queryArray,new ParseElements());
361       pe.parseException.position = -1;
362       pe.tokens = (Object JavaDoc[])o[0];
363       pe.index = (Object JavaDoc[])o[1];
364
365
366
367       pe.queryTokenCharacterArray = makeCharacterArray(pe.tokens);
368       Object JavaDoc object = parse(pe);
369       checkingForPosition(object,pe,list);
370       return list.toArray();
371    }
372
373    private void checkingForPosition(Object JavaDoc object,ParseElements pe,ArrayList JavaDoc list)throws DException{
374       if ( pe.position < pe.tokens.length ){
375          if (object instanceof ParseException){
376            int position = pe.parseException.causeOfFailurePosition;
377             pe.parseException.causeOfFailure = "";
378             if(pe.tokens[position] instanceof com.daffodilwoods.daffodildb.server.sql99.token.SRESERVEDWORD1206543922)
379                pe.parseException.causeOfFailure = "the keyword ";
380             pe.parseException.causeOfFailure += pe.tokens[position];
381             throw pe.parseException;
382          }
383          else{
384             list.add(object);
385             checkingForPosition(parse(pe),pe,list);
386          }
387          }else list.add(object);
388    }
389
390
391    private char[][] makeCharacterArray(Object JavaDoc []arrayOfTokens) throws DException {
392       char [][]array = new char[arrayOfTokens.length][];
393       for(int i = 0; i < array.length; ++i){
394          if ( ((_TOKEN)arrayOfTokens[i]).getType() == 1 || ((_TOKEN)arrayOfTokens[i]).getType() == 2 ){
395             String JavaDoc s = arrayOfTokens[i].toString().trim().toUpperCase();
396             StringBuffer JavaDoc s1 =new StringBuffer JavaDoc("S");
397             s1.append(s).append(s.hashCode());
398             array[i] = updateNameOfRule(s1.toString()).toCharArray();
399          }else{
400             String JavaDoc name= arrayOfTokens[i].getClass().getName();
401             name = ((DaffodilClassLoader)classLoader).getQualifiedName(name);
402             array[i] = name.substring(name.lastIndexOf(".")+1).toCharArray();
403          }
404       }
405       return array;
406    }
407
408
409 /*private void makeCharacterArray(ParseElements pe) throws DException {
410      pe.queryTokenCharacterArray = new char[pe.tokens.length][];
411       for(int i = 0; i < pe.queryTokenCharacterArray.length; ++i){
412          if ( ((_TOKEN)pe.tokens[i]).getType() == 1 || ((_TOKEN)pe.tokens[i]).getType() == 2 ){
413             String s = pe.tokens[i].toString().trim().toUpperCase();
414             StringBuffer s1 =new StringBuffer("S");
415             s1.append(s).append(s.hashCode());
416             pe.queryTokenCharacterArray[i] = updateNameOfRule(s1.toString()).toCharArray();
417          }else{
418             String name= pe.tokens[i].getClass().getName();
419             name = ((DaffodilClassLoader)classLoader).getQualifiedName(name);
420             pe.queryTokenCharacterArray[i] = name.substring(name.lastIndexOf(".")+1).toCharArray();
421          }
422       }
423   }
424 */

425    /**
426     * Methods are declared as abstract which is to be implemented by the classes which extends this
427     * class.
428     * ParsePart method is used to actually parse the query this parsepart method returns the object
429     * of rule satisfied by the query.
430     * Display Rules method is used to display the rules stored in each rule.
431     */

432
433    abstract Object JavaDoc parsePart(ParseElements pe) throws DException;
434
435    /*
436    * This Function is the main method called to actually parsed the query.
437    * Parameter passed is parseElements which contains all the necessary variables initialized
438    * required at the time of parsing.
439    * Restores the position in case of parseException.
440    * If Object is succesfully parsed then its decalred fields are initialised using reflection package.
441    * Return Type of method is object parsed, In case of exception ParseException is returned.
442     */

443
444    Object JavaDoc parse(ParseElements pe) throws DException{
445       int position = pe.position;
446
447       Object JavaDoc object = parsePart(pe);
448       if(object instanceof ParseException){
449          pe.position = position;
450          return object;
451       }
452       return object;
453    }
454
455    /**
456     * This method is used to tokenize the query.
457     * Parse(query,queryArray) method is called from this method to tokenize the query.
458     * Parameters passed is a query which is to be tokenize and character array of query.
459     * Return type is an object array of 2 length, At first position Tokens array is stored, and at the
460     * second position positions of tokens ( position of token in query ) is stored.
461     * Tokens Array is used to parse the query.
462     * Positions array is used to throw the parseException at particular position.
463     */

464    private Object JavaDoc[] getTokensOfQuery(String JavaDoc query,char []queryArray,ParseElements parseElements) throws DException{
465
466       ArrayList JavaDoc listOfTokens = new ArrayList JavaDoc();
467       ArrayList JavaDoc index = new ArrayList JavaDoc();
468
469       parseElements.successfulRule=new HashMap JavaDoc();
470       parseElements.query = query;
471       parseElements.tokenFlag = true;
472       parseElements.bestOptionFlag = false;
473       parseElements.queryArray = queryArray;
474
475       int position = 0 ;
476       while(position < query.length() ){
477          Object JavaDoc object = null;
478          int prevPosition = position;
479          Object JavaDoc result[] = (Object JavaDoc [])tokenRule.parse(query,position,queryArray,parseElements);
480          object = result[0];
481          position = result[1].hashCode();
482          if (object instanceof ParseException){
483             ParseException pe = (ParseException)object;
484             pe.setQuery(query);
485             if(pe.causeOfFailure == null){
486                pe.causeOfFailure = ""+queryArray[pe.position>=parseElements.queryArray.length? parseElements.queryArray.length -1 : pe.position];
487             }
488           if(listOfTokens.size() == 0)
489             throw new DException("DSE2057",null);
490           else if(pe.position < query.length() ){
491             throw pe;
492           }else{
493             position = pe.position;
494             object = null;
495           }
496
497          }
498          if ( object != null ){
499             listOfTokens.add(object);
500             index.add(new Integer JavaDoc(prevPosition));
501          }
502       }
503
504       Object JavaDoc []o = new Object JavaDoc[2];
505       o[0] = listOfTokens.toArray();
506       o[1] = index.toArray();
507       return o;
508    }
509
510    /**
511     * This method is used to return comparable object for a rule.
512     * If rule is comparable then rule returns otheriwse returns null.
513     *
514     */

515    public Object JavaDoc getComparableObject(){
516       return null;
517    }
518
519    public Object JavaDoc getComparableObjectArray(){
520       return null;
521    }
522
523    /**
524     * This method is used to get recursiveObjects.
525     * Parameters passed are :
526     * a) nameOfRule : signifies rule name for which getRecursiveObject is called.
527     * b) occuredRules : Stack occuredRules is used to avoid recursion.
528     * If all of the rules stored in rule is recursive the rule returns itself otherwise
529     * array of rules as recusrive are returned.
530     */

531
532    public Object JavaDoc getRecursiveObject(String JavaDoc nameOfRule,DBStack occuredRules){
533       return null;
534    }
535
536    /**
537     * This method is used to get NonRecursiveObjects.
538     * Parameters passed are :
539     * a) nameOfRule : signifies rule name for which getRecursiveObject is called.
540     * b) occuredRules : Stack occuredRules is used to avoid recursion.
541     * If all of the rules stored in rule is nonrecursive the rule returns itself otherwise
542     * array of rules as nonrecursive are returned.
543     */

544    public Object JavaDoc getNonRecursiveObject(String JavaDoc nameOfRule,DBStack occuredRules){
545       return null;
546    }
547
548    /**
549     * This method is used to create node for a rule which is running.
550     * Recieves ParseElements as an argument.
551     */

552  /* private void displayTreeRunning(ParseElements pe){
553       pe.parentNode = pe.dTree.createChildNode(pe.parentNode,nameOfRule + " - "+pe.recursionState+ " Running " + pe.position);
554    }
555 */

556    /**
557     * This method is used to set the user object node for a rule which is failed.
558     * Recieves ParseElements as an argument and a Boolean.
559     * If var boolean is true then rule is failed by parse.
560     * If var boolean is false then rule is failed from HashMap.
561     */

562    private void displayTreeFailure(ParseElements pe,boolean found){
563       String JavaDoc str = found ? (nameOfRule+" - "+pe.recursionState+" Failed ["+pe.position + "]" + "] RETURN TYPE : " +pe.parseException.returnType) : (nameOfRule+" - "+pe.recursionState+" Failed From HashMap "+pe.position);
564       pe.parentNode.setUserObject(str);
565    }
566
567    /**
568     * This method is used to set the user object node for a rule which it is successfull.
569     * Recieves ParseElements as an argument and a Boolean.
570     * If var boolean is true then rule is successfull by parse.
571     * If var boolean is false then rule is successfull from HashMap.
572     */

573    private void displayTreeSuccess(ParseElements pe,int position,String JavaDoc query,boolean found){
574       String JavaDoc str= ""+pe.position;
575       if ( pe.tokens != null ){
576          for(int i = position; i < pe.position ; ++i){
577             try {
578                str += pe.tokens[i].getClass().getName();
579             }
580             catch (RuntimeException JavaDoc ex) {
581                str = ""+ex;
582             }
583          }
584       }
585       else
586          str += query.substring(position,pe.position)+"]";
587       str = found ? (nameOfRule+" - "+pe.recursionState+" Successful ["+ str +"]" ) : (nameOfRule+" - "+pe.recursionState+" Successful From HashMap ["+ str+"]");
588       pe.parentNode.setUserObject(str);
589    }
590
591    /**
592     * Loading Fields of the class
593     */

594    void loadFields(int i,Class JavaDoc cls) throws DException{
595       try{
596          Object JavaDoc arrayOfFields[] = new Object JavaDoc[i+1];
597          for(;i >= 0 ; i--)
598             arrayOfFields[i] = cls.getDeclaredField((String JavaDoc)fieldNames[i]);
599          fields = arrayOfFields;
600       }catch(Exception JavaDoc e){
601          throw new DException("DSE0",new Object JavaDoc[]{e.getMessage()} );
602       }
603    }
604
605    /**
606     * This method updates the nameOfRule to a desired format.
607     * For example : rulename abc-ss is changed as abc_ss
608     */

609    String JavaDoc updateNameOfRule(String JavaDoc nameRule){
610      /* both lines */
611       char[] value = nameRule.toCharArray();
612       StringBuffer JavaDoc str = new StringBuffer JavaDoc();
613       for(int i = 0 ; i < value.length ; i++)
614          if(value[i] == '-' )
615             str.append('_');
616           else if(!(value[i] == ' ' || value[i] == ':' || value[i] == '(' || value[i] == ')'|| value[i] == '\\'|| value[i] == '\''))
617             str.append(value[i]);
618         return str.toString();
619    }
620
621    /**
622     * This method is used to create a tree showing hierarchy of rules for which it is called.
623     */

624 /*
625    public void createTree(){
626       ArrayList arr = new ArrayList();
627       DebugTree dTree = new DebugTree();
628       dTree.setTitle(nameOfRule);
629       dTree.show();
630       DefaultMutableTreeNode parentNode = dTree.createChildNode((DefaultMutableTreeNode)dTree.tree.getModel().getRoot(),nameOfRule);
631       createTree(arr,this,parentNode,dTree);
632    }
633    int count = 0;
634
635    private void createTree(ArrayList arr,ProductionRules rule,DefaultMutableTreeNode parentNode,DebugTree dTree){
636       int size = rule.rules == null ? -1 : rule.rules.length-1;
637       count++;
638       for(int i = size ; i >= 0 ; i--){
639          String asdName = "";
640          Object aqw = rule.rules[i];
641          ProductionRules object = null;
642          if(aqw instanceof String){
643             asdName += aqw;
644          }
645          else{
646             object = (ProductionRules)aqw;
647             if(object instanceof OrProductionRules)
648                asdName = "Or_";
649             else if(object instanceof UnionProductionRules)
650                asdName = "Union_";
651             else if(object instanceof SimpleProductionRules)
652                asdName = "Simple_";
653             else if(object instanceof OrProductionRulesWithHashMap)
654                asdName = "OrH_";
655             else if(object instanceof UnionProductionRulesWithHashMap)
656                asdName = "UnionH_";
657             else if(object instanceof SimpleProductionRulesWithHashMap)
658                asdName = "SimpleH_";
659             asdName += object.getProductionRuleName();
660          }
661          if(count > 10 &&/**/
/* arr.contains(asdName)){
662             dTree.createChildNode(parentNode,asdName+" : Already Exists");
663             continue;
664          }
665          DefaultMutableTreeNode childNode = dTree.createChildNode(parentNode,asdName);
666          arr.add(asdName);
667          if(object != null)
668             createTree(arr,object,childNode,dTree);
669       }
670       count--;
671    }*/

672
673    /**
674     * CheckLookAhead Method is used to check lookhead stack for and rule.
675       If it is contained in stack or not.
676       */

677    public boolean checkLookAheadStack(ParseElements pe){
678           /*Stack lookAhead = (Stack)pe.lookAhead.clone();
679           int position = pe.position;
680           while(!lookAhead.empty()){
681                  ProductionRules pq = (ProductionRules)lookAhead.pop();
682                  if(pq instanceof OptionalProductionRules){
683                         pe.position = position;
684                         return true;
685                  }
686                  Object asd = pq.parse(pe);
687                  if(asd instanceof ParseException){
688                         pe.position = position;
689                         return false;
690                  }
691
692           }
693           pe.position = position;*/

694       return true;
695    }
696
697    /**
698     * Overrided the method toString of Object. Returning nameOfRule.
699     */

700
701    public String JavaDoc toString(){
702       return nameOfRule;
703    }
704
705    /**
706     * Overrided the method equals of Object. Returning true or false.
707     */

708
709    public boolean equals(Object JavaDoc object){
710      if(this == object)
711        return true;
712       String JavaDoc name = ((ProductionRules)object).ruleKey;
713       return ruleKey.equalsIgnoreCase(name);
714    }
715
716    public int hashCode(){
717       int hashCode = 1;
718       hashCode = 31*hashCode + (nameOfRule==null ? 0 : nameOfRule.hashCode());
719       return hashCode;
720    }
721
722    /**
723     * Comparator for sorting Rules which are comparable in case of trokenFlag true.
724     */

725
726    public int compareTo(Object JavaDoc o) {
727       ProductionRules object2 = (ProductionRules)o;
728       return ((Comparable JavaDoc)result).compareTo(object2.result);
729    }
730
731    public Object JavaDoc parseComments(ParseElements pe){
732     int position = pe.position ;
733     char[] val = pe.queryArray ;
734     while(position < pe.queryArray.length && Character.isWhitespace(val[position]))
735         position++;
736
737      try {
738         while (val[position] == '-' && val[position + 1] == '-') {
739            while (val[position] != ';') {
740               position++;
741            }
742            position++;
743            while(position < pe.queryArray.length && Character.isWhitespace(val[position]))
744              position++;
745          }
746
747         while (val[position] == '/' && val[position + 1] == '*') {
748            while (val[position] != '*' || val[position + 1] != '/') {
749               position++;
750            }
751            position += 2;
752            while(position < pe.queryArray.length && Character.isWhitespace(val[position]))
753              position++;
754          }
755      } catch (ArrayIndexOutOfBoundsException JavaDoc ex) {
756        pe.position = position ;
757         pe.parseException.setPosition(pe.getIndexPosition(pe.position));
758         return pe.parseException;
759      }
760
761      return new Integer JavaDoc(position);
762    }
763
764 }
765
Popular Tags