comparison mde/text/parse.d @ 6:dcb24afa0dce

Some fixes from mde/text/format.d unittests plus a few more fixes. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Thu, 10 Jan 2008 18:33:24 +0000
parents 9a990644948c
children b544c3a7c9ca
comparison
equal deleted inserted replaced
5:76d0adc92f2e 6:dcb24afa0dce
40 logger = Log.getLogger ("mde.text.parse"); 40 logger = Log.getLogger ("mde.text.parse");
41 } 41 }
42 42
43 //BEGIN parse templates 43 //BEGIN parse templates
44 // Associative arrays 44 // Associative arrays
45 const char[] AA_ERR = "Invalid associative array: ";
45 T[S] parse(T : T[S], S) (char[] src) { 46 T[S] parse(T : T[S], S) (char[] src) {
46 src = Util.trim(src); 47 src = Util.trim(src);
47 if (src.length < 2 || src[0] != '[' || src[$-1] != ']') 48 if (src.length < 2 || src[0] != '[' || src[$-1] != ']')
48 throwException ("Invalid associative array: not [a:x, ..., c:z]"); 49 throwException (AA_ERR ~ "not [ ... ]"); // bad braces.
49 50
50 T[S] ret; 51 T[S] ret;
51 foreach (char[] pair; split (src[1..$-1])) { 52 foreach (char[] pair; split (src[1..$-1])) {
52 uint i = 0; 53 uint i = 0;
53 while (i < pair.length) { // advance to the ':' 54 while (i < pair.length) { // advance to the ':'
54 char c = pair[i]; 55 char c = pair[i];
55 if (c == ':') break; 56 if (c == ':') break;
56 if (c == '\'' || c == '"') { // string or character 57 if (c == '\'' || c == '"') { // string or character
57 ++i; 58 ++i;
58 while (i < pair.length && pair[i] != c) { 59 while (i < pair.length && pair[i] != c) {
59 if (pair[i] == '\\') ++i; // escape seq. 60 if (pair[i] == '\\') {
61 if (i+2 >= pair.length) throwException (AA_ERR ~ "unfinished escape sequence within string/char");
62 ++i; // escape seq.
63 }
60 ++i; 64 ++i;
61 } // Doesn't throw if no terminal quote at end of pair (in this case an error is thrown anyway) 65 }
66 if (i == pair.length) {
67 debug logger.warn ("Pair is: " ~ pair);
68 throwException (AA_ERR ~ "encountered [ ... KEY] (missing :DATA)");
69 }
62 } 70 }
63 ++i; 71 ++i;
64 } 72 }
65 if (i == pair.length) { 73 if (i == pair.length) {
66 debug logger.trace ("In pair: " ~ pair); 74 throwException (AA_ERR ~ "encountered [ ... KEY:] (missing DATA)");
67 throwException ("Invalid key:value pair in associative array literal"); 75 }
68 }
69 debug logger.trace ("pair is: " ~ pair[0..i] ~ " : " ~ pair[i+1..$]);
70 ret[parse!(S) (pair[0..i])] = parse!(T) (pair[i+1..$]); 76 ret[parse!(S) (pair[0..i])] = parse!(T) (pair[i+1..$]);
71 } 77 }
72 return ret; 78 return ret;
73 } 79 }
74 unittest { 80 unittest {
106 i = j; 112 i = j;
107 113
108 // process a block of escaped characters 114 // process a block of escaped characters
109 while (t < src.length && src[t] == '\\') { 115 while (t < src.length && src[t] == '\\') {
110 t++; 116 t++;
111 if (t == src.length) throwException (`Warning: string ends \" !`); // next char is " 117 if (t == src.length) throwException ("Invalid string: ends \\\" !"); // next char is "
112 ret[i++] = replaceEscapedChar (src[t++]); // throws if it's invalid 118 ret[i++] = replaceEscapedChar (src[t++]); // throws if it's invalid
113 } 119 }
114 } 120 }
115 return ret[0..i]; 121 return ret[0..i];
116 } 122 }