changeset 745:7299159c3a19

Improved DDocParser.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Sat, 09 Feb 2008 23:23:01 +0100
parents 7173ece1b696
children 32a8ddd330f8
files trunk/src/dil/doc/Doc.d trunk/src/dil/doc/Parser.d
diffstat 2 files changed, 36 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/trunk/src/dil/doc/Doc.d	Sat Feb 09 22:54:31 2008 +0100
+++ b/trunk/src/dil/doc/Doc.d	Sat Feb 09 23:23:01 2008 +0100
@@ -104,16 +104,16 @@
     textEnd = p + text.length;
 
     char* summaryBegin;
-    char* idBegin, idEnd;
-    char* nextIdBegin, nextIdEnd;
+    string ident, nextIdent;
+    char* bodyBegin, nextBodyBegin;
 
     skipWhitespace(p);
     summaryBegin = p;
 
-    if (findNextIdColon(idBegin, idEnd))
+    if (findNextIdColon(ident, bodyBegin))
     { // Check that this is not an explicit section.
-      if (summaryBegin != idBegin)
-        scanSummaryAndDescription(summaryBegin, idBegin);
+      if (summaryBegin != ident.ptr)
+        scanSummaryAndDescription(summaryBegin, ident.ptr);
     }
     else // There are no explicit sections.
     {
@@ -121,19 +121,35 @@
       return sections;
     }
 
-    assert(idBegin && idEnd);
+    assert(ident.length);
     // Continue parsing.
-    while (findNextIdColon(nextIdBegin, nextIdEnd))
+    while (findNextIdColon(nextIdent, nextBodyBegin))
     {
-      sections ~= new Section(makeString(idBegin, idEnd), makeString(idEnd+1, nextIdBegin));
-      idBegin = nextIdBegin;
-      idEnd = nextIdEnd;
+      sections ~= new Section(ident, textBody(bodyBegin, nextIdent.ptr));
+      ident = nextIdent;
+      bodyBegin = nextBodyBegin;
     }
     // Add last section.
-    sections ~= new Section(makeString(idBegin, idEnd), makeString(idEnd+1, textEnd));
+    sections ~= new Section(ident, textBody(bodyBegin, textEnd));
     return sections;
   }
 
+  /// Removes trailing whitespace characters from the text body.
+  char[] textBody(char* begin, char* end)
+  {
+    // The body of A is empty, e.g.:
+    // A:
+    // B: some text
+    // ^- begin and end point to B (or to this.textEnd in the 2nd case.)
+    if (begin is end)
+      return "";
+    // Remove trailing whitespace.
+    while (isspace(*--end) || *end == '\n')
+    {}
+    end++;
+    return makeString(begin, end);
+  }
+
   void scanSummaryAndDescription(char* p, char* end)
   {
     assert(p < end);
@@ -170,10 +186,10 @@
 
   /// Find next "Identifier:".
   /// Params:
-  ///   idBegin = set to the first character of the Identifier
-  ///   idEnd   = set to the colon following the Identifier
+  ///   ident = set to the Identifier
+  ///   bodyBegin = set to the beginning of the text body (whitespace skipped.)
   /// Returns: true if found
-  bool findNextIdColon(ref char* ref_idBegin, ref char* ref_idEnd)
+  bool findNextIdColon(ref char[] ident, ref char* bodyBegin)
   {
     while (p < textEnd)
     {
@@ -187,10 +203,13 @@
         do // IdChar*
           p++;
         while (p < textEnd && (isident(*p) || isUnicodeAlpha(p, textEnd)))
+        auto idEnd = p;
         if (p < textEnd && *p == ':') // :
         {
-          ref_idBegin = idBegin;
-          ref_idEnd = p;
+          p++;
+          skipWhitespace(p);
+          bodyBegin = p;
+          ident = makeString(idBegin, idEnd);
           return true;
         }
       }
--- a/trunk/src/dil/doc/Parser.d	Sat Feb 09 22:54:31 2008 +0100
+++ b/trunk/src/dil/doc/Parser.d	Sat Feb 09 23:23:01 2008 +0100
@@ -63,7 +63,7 @@
     // B = some text
     // ^- begin and end point to B (or to this.textEnd in the 2nd case.)
     if (begin is end)
-      return null;
+      return "";
     // Remove trailing whitespace.
     while (isspace(*--end) || *end == '\n')
     {}