|
1 /* |
|
2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "RegularExpression.h" |
|
19 |
|
20 #define KNumOfRecognisedChars 13 |
|
21 |
|
22 // Panic is implemented in NumberGrouping.cpp |
|
23 GLDEF_C void Panic(TNumberGroupingPanic aPanic); |
|
24 |
|
25 CRegularExpression::CRegularExpression() |
|
26 { |
|
27 } |
|
28 |
|
29 CRegularExpression* CRegularExpression::NewL(RPointerArray<TDesC>* aPatterns) |
|
30 { |
|
31 CRegularExpression* s = NewLC(aPatterns); |
|
32 CleanupStack::Pop(); |
|
33 return s; |
|
34 } |
|
35 |
|
36 CRegularExpression* CRegularExpression::NewLC(RPointerArray<TDesC>* aPatterns) |
|
37 { |
|
38 CRegularExpression* s = new(ELeave)CRegularExpression(); |
|
39 CleanupStack::PushL(s); |
|
40 s->ConstructL(aPatterns); |
|
41 return s; |
|
42 } |
|
43 |
|
44 void CRegularExpression::ConstructL(RPointerArray<TDesC>* aPatterns) |
|
45 { |
|
46 GenerateStateTablesL(aPatterns); |
|
47 } |
|
48 |
|
49 CRegularExpression::~CRegularExpression() |
|
50 { |
|
51 for(TInt i = 0; i < iStateMachines.Count(); i++) |
|
52 delete iStateMachines[i]; |
|
53 |
|
54 iStateMachines.Close(); |
|
55 } |
|
56 |
|
57 TInt CRegularExpression::Search(const TDesC& aString) |
|
58 { |
|
59 return SearchFrom(0, aString); |
|
60 } |
|
61 |
|
62 TInt CRegularExpression::SearchFrom(TInt aIndex, const TDesC& aString) |
|
63 { |
|
64 for(TInt i = aIndex; i < iStateMachines.Count(); ++i) |
|
65 { |
|
66 if(iStateMachines[i]->Matches(aString)) |
|
67 return i; |
|
68 } |
|
69 |
|
70 return KErrNotFound; |
|
71 } |
|
72 |
|
73 // Do the parsing |
|
74 void CRegularExpression::GenerateStateTablesL(RPointerArray<TDesC>* aPatterns) |
|
75 { |
|
76 for(TInt nPatternCount = 0; nPatternCount < aPatterns->Count(); ++nPatternCount) |
|
77 { |
|
78 const TDesC& desPattern = *((*aPatterns)[nPatternCount]) ; // I know it looks horrific, but it makes the |
|
79 // rest of the method _so_ much easier to read |
|
80 |
|
81 CStateMachine* StateMachine = CStateMachine::NewLC(KNumOfRecognisedChars, desPattern.Length()); |
|
82 User::LeaveIfError(iStateMachines.Append(StateMachine)); |
|
83 CleanupStack::Pop(); // now the ownership transfers to iStateMachines |
|
84 |
|
85 // parse the desPattern |
|
86 |
|
87 enum TParseState |
|
88 { |
|
89 EAny, |
|
90 EOpenBracketFound, |
|
91 EChoiceOrRange, |
|
92 EChoice, |
|
93 ERange, |
|
94 ELookingForCloseBracket |
|
95 }; |
|
96 |
|
97 TParseState parseState = EAny; |
|
98 |
|
99 TInt nPatternIndex = 0; |
|
100 TInt nState = 0; |
|
101 |
|
102 TBuf<10> bufChoice; |
|
103 bufChoice.Zero(); |
|
104 |
|
105 TBuf<20> bufParsingError = _L("Parsing syntax error"); |
|
106 |
|
107 while(nPatternIndex < desPattern.Length() ) |
|
108 { |
|
109 TChar cChar = desPattern[nPatternIndex]; |
|
110 TBool bEndOfParse = EFalse; |
|
111 |
|
112 if(nPatternIndex == desPattern.Length() - 1) // end of the parse |
|
113 bEndOfParse = ETrue; |
|
114 |
|
115 switch(cChar) |
|
116 { |
|
117 case '1': |
|
118 case '2': |
|
119 case '3': |
|
120 case '4': |
|
121 case '5': |
|
122 case '6': |
|
123 case '7': |
|
124 case '8': |
|
125 case '9': |
|
126 case '0': |
|
127 switch(parseState) |
|
128 { |
|
129 case EAny: |
|
130 iStateMachines[nPatternCount]->AddStateTransistionL(cChar, nState, bEndOfParse? |
|
131 KStateMatched: |
|
132 (nState + 1)); |
|
133 ++nState; |
|
134 break; |
|
135 case EOpenBracketFound: |
|
136 bufChoice.Append(cChar); |
|
137 parseState = EChoiceOrRange; |
|
138 break; |
|
139 case EChoiceOrRange: |
|
140 case EChoice: |
|
141 bufChoice.Append(cChar); |
|
142 parseState = EChoice; |
|
143 break; |
|
144 case ERange: |
|
145 bufChoice.Append(cChar); |
|
146 parseState = ELookingForCloseBracket; |
|
147 break; |
|
148 //case ELookingForCloseBracket: |
|
149 default: |
|
150 User::Panic(bufParsingError, KErrSyntaxError); |
|
151 break; |
|
152 } |
|
153 break; |
|
154 case '[': |
|
155 switch(parseState) |
|
156 { |
|
157 case EAny: |
|
158 bufChoice.Zero(); |
|
159 parseState = EOpenBracketFound; |
|
160 break; |
|
161 //case EOpenBracketFound: |
|
162 //case EChoiceOrRange: |
|
163 //case EChoice: |
|
164 //case ERange: |
|
165 //case ELookingForCloseBracket: |
|
166 default: |
|
167 User::Panic(bufParsingError, KErrSyntaxError); |
|
168 break; |
|
169 |
|
170 } |
|
171 break; |
|
172 case ']': |
|
173 switch(parseState) |
|
174 { |
|
175 case EChoice: |
|
176 { |
|
177 for(TInt i = 0; i < bufChoice.Length(); ++i) |
|
178 { |
|
179 TChar cChar = bufChoice[i]; |
|
180 |
|
181 iStateMachines[nPatternCount]->AddStateTransistionL(cChar.GetNumericValue(), |
|
182 nState, |
|
183 bEndOfParse? |
|
184 KStateMatched: |
|
185 (nState + 1)); |
|
186 } |
|
187 |
|
188 ++nState; |
|
189 parseState = EAny; |
|
190 } |
|
191 break; |
|
192 case ELookingForCloseBracket: |
|
193 { |
|
194 TChar cTopOfRange = bufChoice[1]; |
|
195 TChar cBottomOfRange = bufChoice[0]; |
|
196 |
|
197 TInt nTopOfRange = cTopOfRange.GetNumericValue(); // if they're not there then something |
|
198 TInt nBottomOfRange = cBottomOfRange.GetNumericValue(); // is seriously amiss... |
|
199 |
|
200 if(nTopOfRange < nBottomOfRange) |
|
201 User::Panic(bufParsingError, KErrSyntaxError); |
|
202 |
|
203 for(TInt i = nBottomOfRange; i <= nTopOfRange; ++i) |
|
204 { |
|
205 iStateMachines[nPatternCount]->AddStateTransistionL(i, nState, bEndOfParse? |
|
206 KStateMatched: |
|
207 (nState + 1)); |
|
208 } |
|
209 ++nState; |
|
210 parseState = EAny; |
|
211 } |
|
212 break; |
|
213 //case EAny: |
|
214 //case EOpenBracketFound: |
|
215 //case EChoiceOrRange: |
|
216 //case ERange: |
|
217 default: |
|
218 User::Panic(bufParsingError, KErrSyntaxError); |
|
219 break; |
|
220 } |
|
221 break; |
|
222 case '-': |
|
223 switch(parseState) |
|
224 { |
|
225 case EChoiceOrRange: |
|
226 parseState = ERange; |
|
227 break; |
|
228 //case EAny: |
|
229 //case EOpenBracketFound: |
|
230 //case ELookingForCloseBracket: |
|
231 //case ERange: |
|
232 //case EChoice: |
|
233 default: |
|
234 User::Panic(bufParsingError, KErrSyntaxError); |
|
235 break; |
|
236 } |
|
237 break; |
|
238 case '.': |
|
239 case '+': |
|
240 switch(parseState) |
|
241 { |
|
242 case EAny: |
|
243 iStateMachines[nPatternCount]->AddStateTransistionL(cChar, nState, bEndOfParse? |
|
244 KStateMatched: |
|
245 (nState + 1)); |
|
246 ++nState; |
|
247 break; |
|
248 //case EOpenBracketFound: |
|
249 //case EChoiceOrRange: |
|
250 //case ELookingForCloseBracket: |
|
251 //case ERange: |
|
252 //case EChoice: |
|
253 default: |
|
254 User::Panic(bufParsingError, KErrSyntaxError); |
|
255 break; |
|
256 } |
|
257 break; |
|
258 default: |
|
259 User::Panic(bufParsingError, KErrSyntaxError); |
|
260 break; |
|
261 } |
|
262 |
|
263 ++nPatternIndex; |
|
264 } |
|
265 |
|
266 if(parseState != EAny) |
|
267 User::Panic(bufParsingError, KErrSyntaxError); |
|
268 } |
|
269 } |
|
270 |
|
271 void CRegularExpression::GetWildcardVersionOfPattern( |
|
272 TInt aPatternIndex, TText aWildcard, TDes& aWildcardedPattern) const |
|
273 { |
|
274 __ASSERT_ALWAYS( (0 <= aPatternIndex) && (aPatternIndex < iStateMachines.Count()), |
|
275 Panic(ENumberGroupingNoSuchStateMachine) ); |
|
276 iStateMachines[aPatternIndex]->GetWildcardVersionOfPattern( aWildcard, aWildcardedPattern ); |
|
277 } |
|
278 |
|
279 // End of File |