#!/home/arne/wisp/wisp-multiline.sh
; !#
; we need to be able to replace end-of-line characters in brackets and strings
;; nostringandbracketbreaks INPORT
;;
;; Replace linebreaks within brackets and strings in the INPORT by the
;; placeholders \\STRING_BREAK_N and \\STRING_BREAK_R.
;;
;; -Author: Arne Babenhauserheide
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
let*
: filename : list-ref ( command-line ) 1
origfile : open-file filename "r" ; mode
nextchar : read-char origfile
text ""
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