diff mde/mergetag/read.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/mergetag/read.d	Thu Jan 10 18:33:24 2008 +0000
+++ b/mde/mergetag/read.d	Wed Jan 16 12:48:07 2008 +0000
@@ -9,6 +9,7 @@
 // package imports
 public import mde.mergetag.dataset;
 public import mde.mergetag.exception;
+import mde.text.exception : textParseException;
 
 // tango imports
 import tango.io.UnicodeFile;
@@ -79,6 +80,7 @@
     static Logger logger;
     
     // Non-static symbols:
+    final char[] ErrFile;		// added after ErrInFile to do the same without the "in " bit.
     final char[] ErrInFile;		// something like "in \"path/file.mtt\""
     
     final char[] fbuf;			// file is read into this
@@ -149,8 +151,9 @@
             throwMTErr ("Error reading file: " ~ e.msg, new MTFileIOException);
         }
         // Remember the file name so that we can report errors (somewhat) informatively:
-        ErrInFile = " in \"" ~ path.path ~ path.file ~ '"';
-        
+        ErrFile = path.path ~ path.file;
+        ErrInFile = " in \"" ~ ErrFile ~ '"';
+                
         // Version checking & matching header section tag:
         if (fbuf.length < 6 || fbuf[0] != '{' || fbuf[1] != 'M' || fbuf[2] != 'T' || fbuf[5] != '}')
             throwMTErr("Not a valid MergeTag text file" ~ ErrInFile, new MTFileFormatException);
@@ -220,6 +223,15 @@
         read (hs);
     }
     public void read (View!(ID) secSet = new ArrayBag!(ID)) {	/** ditto */
+        /* Look for a section; return it if it exists otherwise create a new section:
+        *     use dataSecCreator if it exists or just create a DefaultData if not.
+        */
+        DataSection getOrCreateSec (ID id) {
+            DataSection* i = id in dataset.sec;
+            if (i) return *i;
+            return (dataset.sec[id] = (dataSecCreator !is null) ? dataSecCreator(id) : new DefaultData);
+        }
+        
         if (allRead || fatal) return;			// never do anything in either case
         if (secSet.size) {
             if (secTable.length) {
@@ -228,6 +240,7 @@
                     if (psmd && !psmd.read) {			// may not exist
                         DataSection ds = getOrCreateSec (id);
                         parseSection (psmd.pos, &ds);
+                        debug ds.debugFunc ();
                         psmd.read = true;
                     }
                 }
@@ -239,6 +252,7 @@
                         if (secSet.contains(id)) {
                             DataSection ds = getOrCreateSec (id);
                             pos = parseSection (pos, &ds);
+                            debug ds.debugFunc ();
                             secTable[id].read = true;
                         }
                     } catch (MTStringIDException) {	// don't do any of the stuff above
@@ -252,6 +266,7 @@
                     if (!smd.read) {
                         DataSection ds = getOrCreateSec (id);
                         parseSection (smd.pos, &ds);
+                        debug ds.debugFunc ();
                         smd.read = true;
                     }
                 }
@@ -261,6 +276,7 @@
                         ID id = fbufReadSecMarker (pos);
                         DataSection ds = getOrCreateSec (id);
                         pos = parseSection (pos, &ds);
+                        debug ds.debugFunc ();
                     } catch (MTStringIDException) {
                         pos = parseSection (pos, null);	// just skip the section
                     }
@@ -281,6 +297,28 @@
     slightly faster, but a tiny difference isn't worth the extra effort/risk of using char*'s.
     */
     private uint parseSection (uint pos, DataSection* dsec) {
+        /* Searches fbuf starting from start to find one of <=>| and stops at its index.
+    
+        If quotable then be quote-aware for single and double quotes.
+        Note: there's no length restriction for the content of the quote since it could be a single
+        non-ascii UTF-8 char which would look like several chars.
+        */
+        void fbufLocateDataTagChar (inout uint pos, bool quotable) {
+            for (; pos < fbuf.length; ++pos) {
+                if ((fbuf[pos] >= '<' && fbuf[pos] <= '>') || fbuf[pos] == '|') return;
+                else if (quotable) {
+                    char c = fbuf[pos];
+                    if (c == '\'' || c == '"') {
+                        ++pos;
+                        while (fbuf[pos] != c) {
+                            if (fbuf[pos] == '\\') ++pos;	// escape seq.
+                            fbufIncrement(pos);
+                        } 
+                    }
+                }
+            }
+        }
+        
         bool comment = false;				// preceding char was !
         for (; pos < fbuf.length; ++pos) {
             if (Util.isSpace(fbuf[pos])) continue;	// whitespace
@@ -315,8 +353,9 @@
                     try {
                         dsec.addTag (type, tagID, data);
                     }
-                    catch (TextParseException) {
-                        logger.warn("Above error occured" ~ ErrInFile);	// following a parse error
+                    catch (textParseException e) {
+                        logger.warn ("While reading " ~ ErrFile ~ ":");	// following a parse error
+                        logger.warn (e.msg);
                     }
                     catch (MTUnknownTypeException e) {
                         logger.warn ("Unsupported type \"" ~ type ~ "\" " ~ ErrInFile /*~ ":"*/);
@@ -354,15 +393,6 @@
         return pos;
     }
     
-    /* Look for a section; return it if it exists otherwise create a new section:
-     *     use dataSecCreator if it exists or just create a DefaultData if not.
-     */
-    DataSection getOrCreateSec (ID id) {
-        DataSection* i = id in dataset.sec;
-        if (i) return *i;
-        return (dataset.sec[id] = (dataSecCreator != null) ? dataSecCreator(id) : new DefaultData);
-    }
-    
     /* Parses fbuf for a section marker. Already knows fbuf[pos] == '{'.
     */
     private ID fbufReadSecMarker (inout uint pos) {
@@ -399,27 +429,6 @@
         }
     }
     
-    /* Searches fbuf starting from start to find one of <=>| and stops at its index.
-    
-    If quotable then be quote-aware for single and double quotes.
-    Note: there's no length restriction for the content of the quote since it could be a single
-    non-ascii UTF-8 char which would look like several chars.
-    */
-    private void fbufLocateDataTagChar (inout uint pos, bool quotable) {
-        for (; pos < fbuf.length; ++pos) {
-            if ((fbuf[pos] >= '<' && fbuf[pos] <= '>') || fbuf[pos] == '|') return;
-            else if (quotable) {
-                char c = fbuf[pos];
-                if (c == '\'' || c == '"') {
-                    ++pos;
-                    while (fbuf[pos] != c) {
-                        if (fbuf[pos] == '\\') ++pos;	// escape seq.
-                        fbufIncrement(pos);
-                    } 
-                }
-            }
-        }
-    }
     /* Increments pos and checks it hasn't hit fbuf.length . */
     private void fbufIncrement(inout uint pos) {
         ++pos;