wisp
 
(Arne Babenhauserheide)
2013-04-28: correctly get rid of linebreaks in strings and brackets in a

correctly get rid of linebreaks in strings and brackets in a preprocessing step.

diff --git a/tests/linebreaks.w b/tests/linebreaks.w
new file mode 100644
--- /dev/null
+++ b/tests/linebreaks.w
@@ -0,0 +1,14 @@
+; Test linebreaks in strings and brackets
+
+"flubbub
+
+flabbab"
+
+hrug (nadda
+madda gadda "shoktom
+ mee"  " sep  
+ka"
+  hadda)
+    gom
+
+flu
diff --git a/wisp.w b/wisp.w
--- a/wisp.w
+++ b/wisp.w
@@ -3,45 +3,90 @@
 
 ; we need to be able to replace end-of-line characters in brackets and strings
 
-;; define : nostringandbracketbreaks inport
-;;     ; Replace end of line characters in brackets and strings
-;;     let* 
-;;         : nextchar : read-char inport
-;;           text ""
-;;           incomment #f
-;;           instring #f
-;;           inbrackets 0
-;;           incharform 0
-;;         while : not : eof-object? nextchar
-;;             when : and ( char=? nextchar #\" ) ( not incomment )
-;;                 set! instring : not instring
-;;                 display instring
-;;             when : and ( char=? nextchar #\; ) ( not incomment ) ( not instring )
-;;                 set! incomment #t
-;;             when
-;;                 and incomment 
-;;                     not instring
-;;                     or 
-;;                         char=? nextchar #\newline
-;;                         char=? nextchar #\linefeed
-;;                 set! incomment #f
-;;             ; TODO: This still treats cod as in brackets which is not, possibly because it cannot cope with char literals: #\( and #\)
-;;             when : and ( char=? nextchar #\( ) ( not instring ) ( not incomment ) 
-;;                 set! inbrackets : + inbrackets 1
-;;             when : and ( char=? nextchar #\) ) ( not instring ) ( not incomment ) 
-;;                 set! inbrackets : - inbrackets 1
-;; 
-;;             if : or instring : > inbrackets 0
-;;                 if : char=? nextchar #\linefeed
-;;                     set! text : string-append text "\\LINE_BREAK_N"
-;;                     if : char=? nextchar #\newline
-;;                         set! text : string-append text "\\LINE_BREAK_R"
-;;                         set! text : string-append text : string nextchar
-;;                 ; when not in brackets or string: just append the char
-;;                 set! text : string-append text : string nextchar
-;; 
-;;             set! nextchar : read-char origfile
-;; 
+define : nostringandbracketbreaks inport
+    ; Replace end of line characters in brackets and strings
+    ; FIXME: Breaks if the string is shorter than 2 chars
+    let* 
+        : lastchar : read-char inport
+          nextchar : read-char inport
+          text ""
+          incomment #f
+          instring #f
+          inbrackets 0
+          incharform 0 ; #\<something>
+        while : not : eof-object? nextchar
+            ; already started char forms win over everything, so process them first.
+            ; already started means: after the #\
+            when : >= incharform 2
+                if : char=? nextchar #\space
+                   set! incharform 0
+                   ; else
+                   set! incharform : + incharform 1
+            ; check if we switch to a string: last char is space, linebreak or in a string, not in a charform, not in a comment
+            when 
+                 and 
+                     char=? nextchar #\"
+                     or 
+                        . instring ; when I’m in a string, I can get out
+                        char=? lastchar #\space ; when the last char was a space, I can get into a string
+                        char=? lastchar #\linefeed ; same for newline chars
+                        char=? lastchar #\newline
+                     not incomment
+                     < incharform 1
+                set! instring : not instring
+            ; check if we switch to a comment
+            when 
+                 and 
+                     char=? nextchar #\;
+                     not incomment
+                     not instring
+                     < incharform 2
+                set! incomment #t
+                ; this also closes any potential charform
+                set! incharform 0
+            when
+                and incomment
+                    or 
+                        char=? nextchar #\newline
+                        char=? nextchar #\linefeed
+                set! incomment #f
+            
+            ; check for the beginning of a charform
+            when 
+                and
+                    not instring
+                    not incomment
+                    char=? lastchar #\space
+                    char=? nextchar #\#
+                set! incharform 1
+            ; check whether a charform is continued
+            when
+                and
+                     = incharform 1
+                     char=? lastchar #\#
+                     char=? nextchar #\\
+                set! incharform 2
+                        
+            ; check for brackets
+            when : and ( char=? nextchar #\( ) ( not instring ) ( not incomment ) ( = incharform 0 )
+                set! inbrackets : + inbrackets 1
+            when : and ( char=? nextchar #\) ) ( not instring ) ( not incomment ) ( = incharform 0 )
+                set! inbrackets : - inbrackets 1
+
+            if : or instring ( > inbrackets 0 )
+                if : char=? nextchar #\linefeed
+                    set! text : string-append text "\\LINE_BREAK_N"
+                    if : char=? nextchar #\newline
+                        set! text : string-append text "\\LINE_BREAK_R"
+                        set! text : string-append text : string nextchar
+                ; when not in brackets or string: just append the char
+                set! text : string-append text : string nextchar
+
+            set! lastchar nextchar
+            set! nextchar : read-char inport
+        ; return the text
+        . text
+
 
 ; first step: Be able to mirror a file to stdout
 
@@ -53,7 +98,7 @@ let*
     while : not : eof-object? nextchar
         set! text : string-append text : string nextchar
         set! nextchar : read-char origfile
+    set! text : call-with-input-string text nostringandbracketbreaks
     display text
 
 newline
-; (modify-syntax-entry ?: "w")