KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.daffodilwoods.daffodildb.utils.parser;
2
3 import java.lang.reflect.Field JavaDoc;
4
5 /*
6  * A CharacterStringLiteralProductionRules extends ProductionRules. It
7  * implements productionRules parsePart Method. At any point of time when
8  * it parsed anything between this two quotes <quote>.....<quote>.
9  */

10
11 public class CharacterStringLiteralProductionRules
12     extends ProductionRules {
13
14   char c;
15
16   CharacterStringLiteralProductionRules(char c0, ClassLoader JavaDoc classLoader0) {
17     super(classLoader0);
18     c=c0;
19     ruleKey = nameOfRule;
20     className = c=='\'' ? "charactestringliteral" : "delimitedidentifier";
21   }
22
23   /**
24    * This function receives ParseElements as an argument.
25    * Loop starts from current position . If it is <quote> then it traverse
26    * through whole string till next <quote>. and accumulate everything in an
27    * StringBuffer && returns that result.
28    * If it is not an <quote> then ParseException is returned.
29    */

30   Object JavaDoc parsePart(ParseElements pe) {
31     int position = pe.position;
32     char[] val = pe.queryArray;
33     int length = val.length;
34
35     while (position < pe.queryArray.length && Character.isWhitespace(val[position]))
36       position++;
37
38     if (position == pe.queryArray.length || val[position] != c){
39       return pe.parseException;
40     }
41
42     position++;
43     StringBuffer JavaDoc stbfr = new StringBuffer JavaDoc(); // StringBuffer is used to buffer
44
while (position < length) {
45       if (val[position] == c) {
46         position++;
47         if (position < length && val[position] == c) {
48           stbfr.append(val[position]);
49           position++;
50           continue;
51         }
52         else {
53           pe.position = position;
54           /* Check placed to support empty '' but not to support empty ""
55              Done by Kaushik on 19/08/2004 beacuse of a problem(check constraint) at client side
56            */

57           if(c!='\'' && stbfr.toString().length()==0)
58             return pe.parseException;
59           return stbfr.toString();
60         }
61       }
62       stbfr.append(val[position]);
63       position++;
64     }
65     return pe.parseException;
66   }
67 }
68
Popular Tags