KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > text > RBBISymbolTable


1 /*
2 ***************************************************************************
3 * Copyright (C) 2002-2006 International Business Machines Corporation *
4 * and others. All rights reserved. *
5 ***************************************************************************
6 */

7 package com.ibm.icu.text;
8
9 import java.util.HashMap JavaDoc;
10 import java.util.Collection JavaDoc;
11
12 import java.text.ParsePosition JavaDoc;
13 import com.ibm.icu.lang.UCharacter;
14
15 class RBBISymbolTable implements SymbolTable{
16     
17     String JavaDoc fRules;
18     HashMap JavaDoc fHashTable;
19     RBBIRuleScanner fRuleScanner;
20
21     // These next two fields are part of the mechanism for passing references to
22
// already-constructed UnicodeSets back to the UnicodeSet constructor
23
// when the pattern includes $variable references.
24
String JavaDoc ffffString;
25     UnicodeSet fCachedSetLookup;
26     
27     
28     
29     static class RBBISymbolTableEntry {
30         String JavaDoc key;
31         RBBINode val;
32          };
33
34
35     
36  RBBISymbolTable(RBBIRuleScanner rs, String JavaDoc rules) {
37      fRules = rules;
38      fRuleScanner = rs;
39      fHashTable = new HashMap JavaDoc();
40      ffffString = "\uffff";
41 }
42
43
44
45
46 //
47
// RBBISymbolTable::lookup This function from the abstract symbol table inteface
48
// looks up a variable name and returns a UnicodeString
49
// containing the substitution text.
50
//
51
// The variable name does NOT include the leading $.
52
//
53
public char[] lookup(String JavaDoc s)
54 {
55     RBBISymbolTableEntry el;
56     RBBINode varRefNode;
57     RBBINode exprNode;
58     
59     RBBINode usetNode;
60     String JavaDoc retString;
61  
62     el = (RBBISymbolTableEntry)fHashTable.get(s);
63     if (el == null) {
64         return null;
65     }
66
67     // Walk through any chain of variable assignments that ultimately resolve to a Set Ref.
68
varRefNode = el.val;
69     while (varRefNode.fLeftChild.fType == RBBINode.varRef) {
70         varRefNode = varRefNode.fLeftChild;
71     }
72     
73     exprNode = varRefNode.fLeftChild; // Root node of expression for variable
74
if (exprNode.fType == RBBINode.setRef) {
75         // The $variable refers to a single UnicodeSet
76
// return the ffffString, which will subsequently be interpreted as a
77
// stand-in character for the set by RBBISymbolTable::lookupMatcher()
78
usetNode = exprNode.fLeftChild;
79         fCachedSetLookup = usetNode.fInputSet;
80         retString = ffffString;
81     }
82     else
83     {
84         // The variable refers to something other than just a set.
85
// This is an error in the rules being compiled. $Variables inside of UnicodeSets
86
// must refer only to another set, not to some random non-set expression.
87
// Note: single characters are represented as sets, so they are ok.
88
fRuleScanner.error(RBBIRuleBuilder.U_BRK_MALFORMED_SET);
89         retString = exprNode.fText;
90         fCachedSetLookup = null;
91     }
92     return retString.toCharArray();
93 }
94
95
96
97 //
98
// RBBISymbolTable::lookupMatcher This function from the abstract symbol table
99
// interface maps a single stand-in character to a
100
// pointer to a Unicode Set. The Unicode Set code uses this
101
// mechanism to get all references to the same $variable
102
// name to refer to a single common Unicode Set instance.
103
//
104
// This implementation cheats a little, and does not maintain a map of stand-in chars
105
// to sets. Instead, it takes advantage of the fact that the UnicodeSet
106
// constructor will always call this function right after calling lookup(),
107
// and we just need to remember what set to return between these two calls.
108
public UnicodeMatcher lookupMatcher(int ch)
109 {
110     UnicodeSet retVal = null;
111     if (ch == 0xffff) {
112         retVal = fCachedSetLookup;
113         fCachedSetLookup = null;
114     }
115     return retVal;
116 }
117
118 //
119
// RBBISymbolTable::parseReference This function from the abstract symbol table interface
120
// looks for a $variable name in the source text.
121
// It does not look it up, only scans for it.
122
// It is used by the UnicodeSet parser.
123
//
124
public String JavaDoc parseReference( String JavaDoc text, ParsePosition JavaDoc pos, int limit)
125 {
126     int start = pos.getIndex();
127     int i = start;
128     String JavaDoc result = "";
129     while (i < limit) {
130         int c = UTF16.charAt(text, i);
131         if ((i==start && !UCharacter.isUnicodeIdentifierStart(c)) || !UCharacter.isUnicodeIdentifierPart(c)) {
132             break;
133         }
134         i += UTF16.getCharCount(c);
135     }
136     if (i == start) { // No valid name chars
137
return result; // Indicate failure with empty string
138
}
139     pos.setIndex(i);
140     result = text.substring(start, i);
141     return result;
142 }
143
144
145
146 //
147
// RBBISymbolTable::lookupNode Given a key (a variable name), return the
148
// corresponding RBBI Node. If there is no entry
149
// in the table for this name, return NULL.
150
//
151
RBBINode lookupNode(String JavaDoc key) {
152
153     RBBINode retNode = null;
154     RBBISymbolTableEntry el;
155
156     el = (RBBISymbolTableEntry)fHashTable.get(key);
157     if (el != null) {
158         retNode = el.val;
159     }
160     return retNode;
161 }
162
163
164 //
165
// RBBISymbolTable::addEntry Add a new entry to the symbol table.
166
// Indicate an error if the name already exists -
167
// this will only occur in the case of duplicate
168
// variable assignments.
169
//
170
void addEntry (String JavaDoc key, RBBINode val) {
171     RBBISymbolTableEntry e;
172     e = (RBBISymbolTableEntry )fHashTable.get(key);
173     if (e != null) {
174         fRuleScanner.error(RBBIRuleBuilder.U_BRK_VARIABLE_REDFINITION);
175         return;
176     }
177
178     e = new RBBISymbolTableEntry();
179     e.key = key;
180     e.val = val;
181     fHashTable.put(e.key, e);
182 }
183
184
185
186
187
188 //
189
// RBBISymbolTable::print Debugging function, dump out the symbol table contents.
190
//
191
void rbbiSymtablePrint() {
192     System.out.print("Variable Definitions\n" +
193            "Name Node Val String Val\n" +
194            "----------------------------------------------------------------------\n");
195
196     int pos = -1;
197     RBBISymbolTableEntry [] syms = new RBBISymbolTableEntry[0];
198     Collection JavaDoc t = fHashTable.values();
199     syms = (RBBISymbolTableEntry[]) t.toArray(syms);
200     
201     for (int i=0; i<syms.length; i++) {
202         RBBISymbolTableEntry s = syms[i];
203
204         System.out.print(" " + s.key + " "); // TODO: format output into columns.
205
System.out.print(" " + s.val + " ");
206         System.out.print(s.val.fLeftChild.fText);
207         System.out.print("\n");
208     }
209
210     System.out.println("\nParsed Variable Definitions\n");
211     pos = -1;
212     for (int i=0; i<syms.length; i++) {
213         RBBISymbolTableEntry s = syms[i];
214         System.out.print(s.key);
215         s.val.fLeftChild.printTree(true);
216         System.out.print("\n");
217     }
218 }
219     
220     
221 }
222
Popular Tags