diff mde/text/parse.d @ 7:b544c3a7c9ca

Some changes to exceptions and a few more debug commands. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Wed, 16 Jan 2008 12:48:07 +0000
parents dcb24afa0dce
children f63f4f41a2dc
line wrap: on
line diff
--- a/mde/text/parse.d	Thu Jan 10 18:33:24 2008 +0000
+++ b/mde/text/parse.d	Wed Jan 16 12:48:07 2008 +0000
@@ -20,7 +20,7 @@
  *
  * There are also a few utility functions defined; the public ones have their own documentation.
  *
- * On errors, a warning is logged and an TextParseException is thrown. No other exceptions should
+ * On errors, a textParseException is thrown with a suitable message. No other exceptions should
  * be thrown and none thrown from functions used outside this module.
  *************************************************************************************************/
 module mde.text.parse;
@@ -33,11 +33,13 @@
 import cInt = tango.text.convert.Integer;
 import cFloat = tango.text.convert.Float;
 import Util = tango.text.Util;
-import tango.util.log.Log : Log, Logger;
+debug {
+    import tango.util.log.Log : Log, Logger;
 
-private Logger logger;
+    private Logger logger;
+}
 static this () {
-    logger = Log.getLogger ("mde.text.parse");
+    debug logger = Log.getLogger ("mde.text.parse");
 }
 
 //BEGIN parse templates
@@ -46,7 +48,7 @@
 T[S] parse(T : T[S], S) (char[] src) {
     src = Util.trim(src);
     if (src.length < 2 || src[0] != '[' || src[$-1] != ']')
-        throwException (AA_ERR ~ "not [ ... ]");	// bad braces.
+        throw new textParseException (AA_ERR ~ "not [ ... ]");	// bad braces.
     
     T[S] ret;
     foreach (char[] pair; split (src[1..$-1])) {
@@ -58,20 +60,20 @@
                 ++i;
                 while (i < pair.length && pair[i] != c) {
                     if (pair[i] == '\\') {
-                        if (i+2 >= pair.length) throwException (AA_ERR ~ "unfinished escape sequence within string/char");
+                        if (i+2 >= pair.length) throw new textParseException (AA_ERR ~ "unfinished escape sequence within string/char");
                         ++i;	// escape seq.
                     }
                     ++i;
                 }
                 if (i == pair.length) {
                     debug logger.warn ("Pair is: " ~ pair);
-                    throwException (AA_ERR ~ "encountered [ ... KEY] (missing :DATA)");
+                    throw new textParseException (AA_ERR ~ "encountered [ ... KEY] (missing :DATA)");
                 }
             }
             ++i;
         }
         if (i == pair.length) {
-            throwException (AA_ERR ~ "encountered [ ... KEY:] (missing DATA)");
+            throw new textParseException (AA_ERR ~ "encountered [ ... KEY:] (missing DATA)");
         }
         ret[parse!(S) (pair[0..i])] = parse!(T) (pair[i+1..$]);
     }
@@ -94,7 +96,7 @@
 T[] parse(T : T[]) (char[] src) {
     src = Util.trim(src);
     if (src.length >= 2 && src[0] == '[' && src[$-1] == ']') return toArray!(T[]) (src);
-    throwException ("Invalid array: not [x, ..., z]");
+    throw new textParseException ("Invalid array: not [x, ..., z]");
 }
 T parse(T : char[]) (char[] src) {
     src = Util.trim(src);
@@ -114,21 +116,21 @@
             // process a block of escaped characters
             while (t < src.length && src[t] == '\\') {
                 t++;
-                if (t == src.length) throwException ("Invalid string: ends \\\" !");	// next char is "
+                if (t == src.length) throw new textParseException ("Invalid string: ends \\\" !");	// next char is "
                 ret[i++] = replaceEscapedChar (src[t++]);	// throws if it's invalid
             }
         }
         return ret[0..i];
     }
     else if (src.length >= 2 && src[0] == '[' && src[$-1] == ']') return toArray!(T) (src);
-    throwException ("Invalid string: not quoted (\"*\") or char array (['a',...,'c'])");
+    throw new textParseException ("Invalid string: not quoted (\"*\") or char array (['a',...,'c'])");
 }
 T parse(T : ubyte[]) (char[] src) {
     src = Util.trim(src);
     // Standard case:
     if (src.length >= 2 && src[0] == '[' && src[$-1] == ']') return toArray!(T) (src);
     // Special case: sequence of hex digits, each pair of which is a ubyte
-    if (src.length % 2 == 1) throwException ("Invalid binary: odd number of chars");
+    if (src.length % 2 == 1) throw new textParseException ("Invalid binary: odd number of chars");
     T ret;
     ret.length = src.length / 2;	// exact
     for (uint i, pos; pos + 1 < src.length; ++i) {
@@ -152,16 +154,16 @@
 T parse(T : char) (char[] src) {
     src = Util.trim(src);
     if (src.length < 3 || src[0] != '\'' || src[$-1] != '\'')
-        throwException ("Invalid char: not quoted ('c')");
+        throw new textParseException ("Invalid char: not quoted ('c')");
     if (src[1] != '\\' && src.length == 3) return src[1];	// Either non escaped
     if (src.length == 4) return replaceEscapedChar (src[2]);	// Or escaped
     
     // Report various errors; warnings for likely and difficult to tell cases:
     /+ This was caused by a bug. Shouldn't occur now normally.
-    if (src[1] == '\\' && src.length == 3) throwException (`Warning: \' in char! There's currently no support for this during tokenising. Thus your input's probably been garbled!`);	// next char is ' +/
+    if (src[1] == '\\' && src.length == 3) throw new textParseException (`Warning: \' in char! There's currently no support for this during tokenising. Thus your input's probably been garbled!`);	// next char is ' +/
     // Warn in case it's a multibyte UTF-8 character:
-    if (src[1] & 0xC0u) throwException ("Invalid char: too long (non-ASCII UTF-8 characters cannot be read as a single character)");
-    throwException ("Invalid char: too long");
+    if (src[1] & 0xC0u) throw new textParseException ("Invalid char: too long (non-ASCII UTF-8 characters cannot be read as a single character)");
+    throw new textParseException ("Invalid char: too long");
 }
 // unittest covered above
 
@@ -173,7 +175,7 @@
     while (src.length > pos && src[pos] == '0') ++pos;	// strip leading zeros
     if (src.length == pos && pos > 0) return false;
     if (src.length == pos + 1 && src[pos] == '1') return true;
-    throwException ("Invalid bool: not true or false and doesn't evaluate to 0 or 1");
+    throw new textParseException ("Invalid bool: not true or false and doesn't evaluate to 0 or 1");
 }
 unittest {
     assert (parse!(bool[]) (`[true,false,01,00]`) == cast(bool[]) [1,0,1,0]);
@@ -236,20 +238,20 @@
     uint radix, ate, ate2;
     
     ate = cInt.trim (src, sign, radix);
-    if (ate == src.length) throwException ("Invalid integer: no digits");
+    if (ate == src.length) throw new textParseException ("Invalid integer: no digits");
     ulong val = cInt.convert (src[ate..$], radix, &ate2);
     ate += ate2;
     
     while (ate < src.length) {
         if (src[ate] == ' ' || src[ate] == '\t') ++ate;
-        else throwException ("Invalid integer");
+        else throw new textParseException ("Invalid integer");
     }
     
-    if (val > TInt.max) throwException (INT_OUT_OF_RANGE);
+    if (val > TInt.max) throw new textParseException (INT_OUT_OF_RANGE);
     if (sign) {
         long sval = cast(long) -val;
         if (sval > TInt.min) return cast(TInt) sval;
-        else throwException (INT_OUT_OF_RANGE);
+        else throw new textParseException (INT_OUT_OF_RANGE);
     }
     return cast(TInt) val;
 }
@@ -259,7 +261,7 @@
  * when it does. */
 TFloat toTFloat(TFloat) (char[] src) {
     src = postTrim (src);
-    if (src == "") throwException ("Invalid float: no digits");
+    if (src == "") throw new textParseException ("Invalid float: no digits");
     uint ate;
 
     TFloat x = cFloat.parse (src, &ate);
@@ -292,7 +294,7 @@
         else if (c == '[') ++depth;
         else if (c == ']') {
             if (depth) --depth;
-            else throwException ("Invalid array literal: closes before end of data item.");
+            else throw new textParseException ("Invalid array literal: closes before end of data item.");
         }
         else if (c == ',' && depth == 0) {		// only if not an embedded array
             if (ret.length <= k) ret.length = ret.length * 2;
@@ -327,7 +329,7 @@
     char* r = c in escChars;
     if (r != null) return *r;
     
-    throwException ("Invalid escape sequence: \\"~c);	// we didn't return, so something failed
+    throw new textParseException ("Invalid escape sequence: \\"~c);	// we didn't return, so something failed
 }
 
 // Reads one hex char: [0-9A-Fa-f]. Otherwise throws an exception. Doesn't check src.length.
@@ -336,7 +338,7 @@
     if (src[pos] >= '0' && src[pos] <= '9') x = src[pos] - '0';
     else if (src[pos] >= 'A' && src[pos] <= 'F') x = src[pos] - 'A' + 10;
     else if (src[pos] >= 'a' && src[pos] <= 'f') x = src[pos] - 'a' + 10;
-    else throwException ("Invalid hex digit.");
+    else throw new textParseException ("Invalid hex digit.");
     ++pos;
     return x;
 }
@@ -354,11 +356,6 @@
     return ret[0..i];
 }
 
-private void throwException (char[] msg) {
-    logger.warn (msg);			// only small errors are trapped here
-    throw new TextParseException ();
-}
-
 unittest {
     // all utility functions should be well-enough used not to need testing
 }