Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Side by Side Diff: regexp2000/src/ast.cc

Issue 11203: * Changed string-representation of regexps to handle unprintable chars. (Closed)
Patch Set: Created 12 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 // ---------------------------------------------------------------------------- 183 // ----------------------------------------------------------------------------
184 // Regular expressions 184 // Regular expressions
185 185
186 #define MAKE_ACCEPT(Name) \ 186 #define MAKE_ACCEPT(Name) \
187 void* RegExp##Name::Accept(RegExpVisitor* visitor, void* data) { \ 187 void* RegExp##Name::Accept(RegExpVisitor* visitor, void* data) { \
188 return visitor->Visit##Name(this, data); \ 188 return visitor->Visit##Name(this, data); \
189 } 189 }
190 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ACCEPT) 190 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_ACCEPT)
191 #undef MAKE_ACCEPT 191 #undef MAKE_ACCEPT
192 192
193 #define MAKE_CONVERSION(Name) \ 193 #define MAKE_TYPE_CASE(Name) \
194 RegExp##Name* RegExpTree::As##Name() { \ 194 RegExp##Name* RegExpTree::As##Name() { \
195 return NULL; \ 195 return NULL; \
196 } 196 } \
197 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CONVERSION) 197 bool RegExpTree::Is##Name() { return false; }
198 #undef MAKE_CONVERSION 198 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_TYPE_CASE)
199 #undef MAKE_TYPE_CASE
199 200
200 #define MAKE_CONVERSION(Name) \ 201 #define MAKE_TYPE_CASE(Name) \
201 RegExp##Name* RegExp##Name::As##Name() { \ 202 RegExp##Name* RegExp##Name::As##Name() { \
202 return this; \ 203 return this; \
203 } 204 } \
204 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_CONVERSION) 205 bool RegExp##Name::Is##Name() { return true; }
205 #undef MAKE_CONVERSION 206 FOR_EACH_REG_EXP_TREE_TYPE(MAKE_TYPE_CASE)
207 #undef MAKE_TYPE_CASE
206 208
207 RegExpEmpty RegExpEmpty::kInstance; 209 RegExpEmpty RegExpEmpty::kInstance;
208 210
209 211
210 // Convert regular expression trees to a simple sexp representation. 212 // Convert regular expression trees to a simple sexp representation.
211 // This representation should be different from the input grammar 213 // This representation should be different from the input grammar
212 // in as many cases as possible, to make it more difficult for incorrect 214 // in as many cases as possible, to make it more difficult for incorrect
213 // parses to look as correct ones which is likely if the input and 215 // parses to look as correct ones which is likely if the input and
214 // output formats are alike. 216 // output formats are alike.
215 class RegExpUnparser: public RegExpVisitor { 217 class RegExpUnparser: public RegExpVisitor {
(...skipping 29 matching lines...) Expand all
245 void* RegExpUnparser::VisitAlternative(RegExpAlternative* that, void* data) { 247 void* RegExpUnparser::VisitAlternative(RegExpAlternative* that, void* data) {
246 stream()->Add("(:"); 248 stream()->Add("(:");
247 for (int i = 0; i < that->nodes()->length(); i++) { 249 for (int i = 0; i < that->nodes()->length(); i++) {
248 stream()->Add(" "); 250 stream()->Add(" ");
249 that->nodes()->at(i)->Accept(this, data); 251 that->nodes()->at(i)->Accept(this, data);
250 } 252 }
251 stream()->Add(")"); 253 stream()->Add(")");
252 return NULL; 254 return NULL;
253 } 255 }
254 256
255 257 static void AddChar(StringStream* stream, uc16 character) {
Christian Plesner Hansen 2008/11/17 10:54:35 You could just use StringStream::Add("%k", ...).
Lasse Reichstein 2008/11/17 11:26:32 "%k" uses the format "\\x%X" for anything outside
Christian Plesner Hansen 2008/11/17 11:37:45 Just change %k to do what you want. Nothing depen
256 void RegExpUnparser::VisitCharacterRange(CharacterRange that) { 258 if (character < 32 || (character >= 128 && character < 256)) {
257 if (that.IsSingleton()) { 259 stream->Add("\\x%02x", character);
258 stream()->Add("%c", that.from()); 260 } else if (character >= 256) {
261 stream->Add("\\u%04x", character);
259 } else { 262 } else {
260 stream()->Add("%c-%c", that.from(), that.to()); 263 stream->Add("%c", character);
261 } 264 }
262 } 265 }
263 266
267 void RegExpUnparser::VisitCharacterRange(CharacterRange that) {
268 AddChar(stream(), that.from());
269 if (!that.IsSingleton()) {
270 stream()->Add("-");
271 AddChar(stream(), that.to());
272 }
273 }
274
264 275
265 276
266 void* RegExpUnparser::VisitCharacterClass(RegExpCharacterClass* that, 277 void* RegExpUnparser::VisitCharacterClass(RegExpCharacterClass* that,
267 void* data) { 278 void* data) {
268 if (that->is_negated()) 279 if (that->is_negated())
269 stream()->Add("^"); 280 stream()->Add("^");
270 stream()->Add("["); 281 stream()->Add("[");
271 for (int i = 0; i < that->ranges()->length(); i++) { 282 for (int i = 0; i < that->ranges()->length(); i++) {
272 if (i > 0) stream()->Add(" "); 283 if (i > 0) stream()->Add(" ");
273 VisitCharacterRange(that->ranges()->at(i)); 284 VisitCharacterRange(that->ranges()->at(i));
(...skipping 22 matching lines...) Expand all
296 break; 307 break;
297 case RegExpAssertion::NON_BOUNDARY: 308 case RegExpAssertion::NON_BOUNDARY:
298 stream()->Add("@B"); 309 stream()->Add("@B");
299 break; 310 break;
300 } 311 }
301 return NULL; 312 return NULL;
302 } 313 }
303 314
304 315
305 void* RegExpUnparser::VisitAtom(RegExpAtom* that, void* data) { 316 void* RegExpUnparser::VisitAtom(RegExpAtom* that, void* data) {
306 stream()->Add("'%w'", that->data()); 317 stream()->Add("'");
318 Vector<const uc16> chardata = that->data();
319 for (int i = 0; i < chardata.length(); i++) {
320 AddChar(stream(), chardata[i]);
Christian Plesner Hansen 2008/11/17 10:54:35 Ditto
321 }
322 stream()->Add("'");
307 return NULL; 323 return NULL;
308 } 324 }
309 325
310 326
311 void* RegExpUnparser::VisitText(RegExpText* that, void* data) { 327 void* RegExpUnparser::VisitText(RegExpText* that, void* data) {
312 if (that->elements()->length() == 1) { 328 if (that->elements()->length() == 1) {
313 that->elements()->at(0).data.u_atom->Accept(this, data); 329 that->elements()->at(0).data.u_atom->Accept(this, data);
314 } else { 330 } else {
315 stream()->Add("(!"); 331 stream()->Add("(!");
316 for (int i = 0; i < that->elements()->length(); i++) { 332 for (int i = 0; i < that->elements()->length(); i++) {
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 384
369 385
370 SmartPointer<const char> RegExpTree::ToString() { 386 SmartPointer<const char> RegExpTree::ToString() {
371 RegExpUnparser unparser; 387 RegExpUnparser unparser;
372 Accept(&unparser, NULL); 388 Accept(&unparser, NULL);
373 return unparser.ToString(); 389 return unparser.ToString();
374 } 390 }
375 391
376 392
377 } } // namespace v8::internal 393 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698