changeset 1603:06958d5a7088

Don't treat comment-only lines as blank, in determining split point of .urp files
author Adam Chlipala <adam@chlipala.net>
date Sun, 20 Nov 2011 18:33:21 -0500
parents e44be6ece475
children b1af16cdc659
files src/compiler.sml tests/comment.ur tests/comment.urp
diffstat 2 files changed, 41 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/src/compiler.sml	Sat Nov 19 10:43:57 2011 -0500
+++ b/src/compiler.sml	Sun Nov 20 18:33:21 2011 -0500
@@ -1,4 +1,4 @@
-(* Copyright (c) 2008-2010, Adam Chlipala
+(* Copyright (c) 2008-2011, Adam Chlipala
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -347,20 +347,34 @@
      Settings.setMinHeap (#minHeap job);
      Settings.setSigFile (#sigFile job))
 
+datatype commentableLine =
+         EndOfFile
+       | OnlyComment
+       | Content of string
+
 fun inputCommentableLine inf =
-    Option.map (fn s =>
-                   let
-                       val s = #1 (Substring.splitl (fn ch => ch <> #"#") (Substring.full s))
-                       val s = #1 (Substring.splitr (not o Char.isSpace) s)
-                   in
-                       Substring.string (if Substring.size s > 0 andalso Char.isSpace (Substring.sub (s, Substring.size s - 1)) then
-                                             if Substring.size s > 1 andalso Char.isSpace (Substring.sub (s, Substring.size s - 2)) then
-                                                 Substring.trimr 2 s
-                                             else
-                                                 Substring.trimr 1 s
-                                         else
-                                             s)
-                   end) (TextIO.inputLine inf)
+    case TextIO.inputLine inf of
+        NONE => EndOfFile
+      | SOME s =>
+        let
+            val (befor, after) = Substring.splitl (fn ch => ch <> #"#") (Substring.full s)
+        in
+            if not (Substring.isEmpty after)
+               andalso Substring.foldl (fn (ch, b) => b andalso Char.isSpace ch) true befor then
+                OnlyComment
+            else
+                let
+                    val s = #1 (Substring.splitr (not o Char.isSpace) befor)
+                in
+                    Content (Substring.string (if Substring.size s > 0 andalso Char.isSpace (Substring.sub (s, Substring.size s - 1)) then
+                                                   if Substring.size s > 1 andalso Char.isSpace (Substring.sub (s, Substring.size s - 2)) then
+                                                       Substring.trimr 2 s
+                                                   else
+                                                       Substring.trimr 1 s
+                                               else
+                                                   s))
+                end
+        end
 
 fun parseUrp' accLibs fname =
     if not (Posix.FileSys.access (fname ^ ".urp", []) orelse Posix.FileSys.access (fname ^ "/lib.urp", []))
@@ -417,9 +431,9 @@
 
                     fun hasSpaceLine () =
                         case inputCommentableLine inf of
-                            NONE => false
-                          | SOME s => s = "debug" orelse s = "profile"
-                                      orelse CharVector.exists (fn ch => ch = #" " orelse ch = #"\t") s orelse hasSpaceLine ()
+                            Content s => s = "debug" orelse s = "profile"
+                                         orelse CharVector.exists (fn ch => ch = #" " orelse ch = #"\t") s orelse hasSpaceLine ()
+                          | _ => false
 
                     val hasBlankLine = hasSpaceLine ()
 
@@ -467,8 +481,7 @@
 
                     fun readSources acc =
                         case inputCommentableLine inf of
-                            NONE => rev acc
-                          | SOME line =>
+                            Content line =>
                             let
                                 val acc = if CharVector.all Char.isSpace line then
                                               acc
@@ -483,6 +496,7 @@
                             in
                                 readSources acc
                             end
+                          | _ => rev acc
 
                     val prefix = ref (case Settings.getUrlPrefix () of "/" => NONE | s => SOME s)
                     val database = ref (Settings.getDbstring ())
@@ -639,9 +653,10 @@
 
                     fun read () =
                         case inputCommentableLine inf of
-                            NONE => finish []
-                          | SOME "" => finish (readSources [])
-                          | SOME line =>
+                            EndOfFile => finish []
+                          | OnlyComment => read ()
+                          | Content "" => finish (readSources [])
+                          | Content line =>
                             let
                                 val (cmd, arg) = Substring.splitl (fn x => not (Char.isSpace x)) (Substring.full line)
                                 val cmd = Substring.string (trim cmd)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/comment.urp	Sun Nov 20 18:33:21 2011 -0500
@@ -0,0 +1,4 @@
+#library common
+database dbname=comment
+
+comment