spacepaste

  1.  
  2. #!/home/arne/wisp/wisp-multiline.sh
  3. ; !#
  4. ;; This file might need to be licensed permissively for inclusion in
  5. ;; an SRFI. Only change it, if you agree to this possible relicensing
  6. ;; of your contribution to this file. I will not accept changes here
  7. ;; which do not allow that.
  8. ; we need to be able to replace end-of-line characters in brackets and strings
  9. ;; TODO: Check whether I can offload the string processing to the
  10. ;; read-function. That’s a source of endless complications. Required:
  11. ;; A kind of unrolling step which appends the string-representation of
  12. ;; the read strings back into the code. I would have to process a list
  13. ;; of strings instead of one big string. Or rather, each line would be
  14. ;; a list of strings.
  15. ;; bootstrap via python3 wisp.py wisp-guile.w > 1 && guile 1 wisp-guile.w > 2 && guile 2 wisp-guile.w > 3 && diff 2 3
  16. ;;
  17. ;; -Author: Arne Babenhauserheide
  18. ;; Copyright (C) Arne Babenhauserheide (2013--2015). All Rights Reserved.
  19. ;; Permission is hereby granted, free of charge, to any person
  20. ;; obtaining a copy of this software and associated documentation
  21. ;; files (the "Software"), to deal in the Software without
  22. ;; restriction, including without limitation the rights to use, copy,
  23. ;; modify, merge, publish, distribute, sublicense, and/or sell copies
  24. ;; of the Software, and to permit persons to whom the Software is
  25. ;; furnished to do so, subject to the following conditions:
  26. ;;
  27. ;; The above copyright notice and this permission notice shall be
  28. ;; included in all copies or substantial portions of the Software.
  29. ;;
  30. ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  31. ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  32. ;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  33. ;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  34. ;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  35. ;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  36. ;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  37. ;; SOFTWARE.
  38. (define-module (wisp)
  39. #:export (wisp2lisp wisp-chunkreader))
  40. (use-modules
  41. ((srfi srfi-1))
  42. ((ice-9 regex)))
  43. (define (endsinunevenbackslashes text ); comment
  44. (if (= 0 (string-length text))
  45. #f
  46. (let counter
  47. ((last (string-take-right text 1))
  48. (rest (string-append " " (string-drop-right text 1)))
  49. (count 0))
  50. (cond
  51. ((= 0 (string-length rest )); end clause: read all
  52. (odd? count))
  53. ; end clause: no \
  54. ((not (equal? last (string #\\)))
  55. (odd? count))
  56. (else
  57. (counter (string-take-right rest 1) (string-drop-right rest 1) (+ 1 count)))))))
  58. (define (nostringandbracketbreaks inport)
  59. "Replace all linebreaks inside strings and brackets with placeholders."
  60. (let ((expressions (list (nostringandbracketbreaksreader inport))))
  61. (while (not (eof-object? (peek-char inport)))
  62. (set! expressions (append expressions (list (nostringandbracketbreaksreader inport)))))
  63. (string-join expressions "\n")))
  64. (define (nostringandbracketbreaksreader inport)
  65. "Read one wisp-expression from the inport.
  66. Ends with three consecutive linebreaks or eof."
  67. ; Replace end of line characters in brackets and strings
  68. ; FIXME: Breaks if the string is shorter than 2 chars
  69. ; FIXME: Breaks if the text begins with a comment.
  70. (let*
  71. ((lastchar (read-char inport))
  72. (nextchar (read-char inport))
  73. (text (if (eof-object? lastchar) "" (string lastchar)))
  74. (incomment #f)
  75. (incommentfirstchar #f ); first char of a comment
  76. (instring #f)
  77. (inbrackets 0)
  78. (incharform 0 )); #\<something>
  79. (while
  80. (not
  81. (or (eof-object? nextchar)
  82. (and
  83. (or (char=? nextchar #\newline ) (char=? nextchar #\return ) )
  84. (or (char=? lastchar #\newline ) (char=? lastchar #\return ) )
  85. (string-suffix? "\n\n" text )))); text includes lastchar
  86. ; incommentfirstchar is only valid for exactly one char
  87. (when incommentfirstchar (set! incommentfirstchar #f ))
  88. ; but add incommentfirstchar if we just started the text
  89. (when (equal? text ";" ); initial comment
  90. (set! incommentfirstchar #f)
  91. (set! incomment #t)
  92. (set! text (string-append text "\\REALCOMMENTHERE")))
  93. ; already started char forms win over everything, so process them first.
  94. ; already started means: after the #\
  95. ; FIXME: Fails to capture #t and #f which can kill line splitting if it happens inside brackets
  96. (when (= incharform 1)
  97. (when (not (and (char=? lastchar #\# ) (or (char=? #\f nextchar) (char=? #\t nextchar))))
  98. ; format #t "1: set incharform 0: lastchar ~a nextchar ~a instring ~a incomment ~a incharform ~a" lastchar nextchar instring incomment incharform
  99. ; newline
  100. (set! incharform 0)))
  101. (when (>= incharform 2)
  102. (if (or (char=? nextchar #\space) (char=?
  103. nextchar #\newline ) (char=? nextchar #\return ) )
  104. (begin
  105. ; format #t "2: set incharform 0: lastchar ~a nextchar ~a instring ~a incomment ~a incharform ~a" lastchar nextchar instring incomment incharform
  106. ; newline
  107. (set! incharform 0))
  108. ; else
  109. (set! incharform (+ incharform 1))))
  110. ; check if we switch to a string: last char is space, linebreak or in a string, not in a charform, not in a comment
  111. (when
  112. (and
  113. (char=? nextchar #\")
  114. (not incomment)
  115. (< incharform 1)
  116. (or
  117. (and
  118. instring ; when I’m in a string, I can get out
  119. (or
  120. (not (char=? lastchar #\\ )); if the last char is not a backslash (escaped quote)
  121. ; or the last char is a backslash preceded by an uneven number of backslashes (so the backslash is actually an escaped backslash)
  122. (and (char=? lastchar #\\)
  123. ; if all backslashes before the lastchar are paired, the final quote is escaped.
  124. (not (endsinunevenbackslashes text)))))
  125. (char=? lastchar #\space ); when the last char was a space, I can get into a string
  126. (char=? lastchar #\newline ); same for newline chars
  127. (char=? lastchar #\return )
  128. (and (not instring ); outside of strings, brackets are pseudo-whitespace, too
  129. (or
  130. (char=? lastchar #\( )
  131. (char=? lastchar #\))
  132. (char=? lastchar #\[ )
  133. (char=? lastchar #\])
  134. ; TODO: Only match for braces {} if curly infix is enabled
  135. (char=? lastchar #\{ )
  136. (char=? lastchar #\})))))
  137. (set! instring (not instring)))
  138. ; check if we switch to a comment
  139. (when
  140. (and
  141. ; FIXME: this should be
  142. ; char=? nextchar #\;
  143. (equal? ";" (string nextchar))
  144. (not incomment)
  145. (not instring)
  146. (< incharform 2))
  147. (set! incomment #t)
  148. (set! incommentfirstchar #t)
  149. ; this also closes any potential charform
  150. (set! incharform 0))
  151. (when
  152. (and incomment
  153. (or
  154. (char=? nextchar #\return)
  155. (char=? nextchar #\newline)))
  156. (set! incomment #f))
  157. ; check for the beginning of a charform
  158. (when
  159. (and
  160. (not instring)
  161. (not incomment)
  162. (char=? lastchar #\space)
  163. (char=? nextchar #\#))
  164. (set! incharform 1))
  165. ; check whether a charform is continued
  166. (when
  167. (and
  168. (= incharform 1)
  169. (char=? lastchar #\#)
  170. (char=? nextchar #\\))
  171. (set! incharform 2))
  172. ; check for brackets
  173. ; FIXME: This only fixes a single linebreak inside parens, but if a second occurs on the same line it breaks. I do not know why. Maybe something with having lastchar as linebreak.
  174. (when (not (or instring incomment))
  175. (when
  176. (and
  177. (not (string-suffix? text "#"))
  178. (not (char=? #\\ lastchar))
  179. (not (endsinunevenbackslashes (string-drop-right text (min 1 (string-length text))))))
  180. ; TODO: Only match for braces {} if curly infix is enabled
  181. ; FIXME: Catch wrong ordering of parens/brackets/braces like ({)}
  182. (when (or (equal? "[" (string nextchar)) (equal? "(" (string nextchar)) (equal? "{" (string nextchar)))
  183. (set! inbrackets (+ inbrackets 1)))
  184. (when (or (equal? "}" (string nextchar)) (equal? ")" (string nextchar)) (equal? "]" (string nextchar)))
  185. (set! inbrackets (- inbrackets 1)))))
  186. (if (or instring (> inbrackets 0))
  187. (if (char=? nextchar #\newline)
  188. ; we have to actually construct the escape
  189. ; sequence here to be able to parse ourselves.
  190. (set! text (string-append text (string-append "\\LINE_" "BREAK_N")))
  191. (if (char=? nextchar #\return)
  192. (set! text (string-append text (string-append "\\LINE_" "BREAK_R")))
  193. ; else
  194. (set! text (string-append text (string nextchar)))))
  195. ; mark the start of a comment, so we do not have to
  196. ; repeat the string matching in later code. We include
  197. ; the comment character!
  198. ; not (instring or inbrackets) = neither instring nor inbrackets
  199. (if incommentfirstchar
  200. (set! text (string-append text ( string nextchar ) "\\REALCOMMENTHERE"))
  201. ; when not in brackets or string or starting a
  202. ; comment: just append the char
  203. (set! text (string-append text (string nextchar)))))
  204. (set! lastchar nextchar)
  205. (set! nextchar (read-char inport)))
  206. ; return the text
  207. text))
  208. ; As next part we have split a text into a list of lines which we can process one by one.
  209. (define (splitlines inport )
  210. (let
  211. ((lines '())
  212. (nextchar (read-char inport))
  213. (nextline ""))
  214. (while (not (eof-object? nextchar))
  215. (if (not (or (char=? nextchar #\return ) (char=? nextchar #\newline )))
  216. (set! nextline (string-append nextline (string nextchar)))
  217. (begin
  218. (set! lines (append lines (list nextline)))
  219. (set! nextline "")))
  220. (set! nextchar (read-char inport)))
  221. (append lines (list nextline))))
  222. (define (line-indent line)
  223. (list-ref line 0))
  224. (define (line-content line)
  225. (list-ref line 1))
  226. (define (line-comment line)
  227. (list-ref line 2))
  228. (define (line-continues? line)
  229. "Check whether the line is a continuation of a previous line (should not start with a bracket)."
  230. (if (equal? #f (line-content line))
  231. #f ; this is the EOF line. It does not continue (to ensure that the last brackets get closed)
  232. (string-prefix? ". " (line-content line))))
  233. (define (line-empty-code? line)
  234. "Check whether the code-part of the line is empty: contains only whitespace and/or comment."
  235. (equal? "" (line-content line)))
  236. (define (line-only-colon? line)
  237. "Check whether the line content consists only of a colon and whitespace."
  238. (equal? ":" (string-trim-right (line-content line))))
  239. (define (line-only-prefix? line prefix)
  240. "Check whether the line content consists only of a given prefix and whitespace."
  241. (equal? prefix (string-trim-right (line-content line))))
  242. (define (line-merge-comment line)
  243. "Merge comment and content into the content. Return the new line."
  244. (let
  245. ((indent (line-indent line))
  246. (content (line-content line))
  247. (comment (line-comment line)))
  248. (if (equal? "" comment)
  249. line ; no change needed
  250. (list indent (string-append content ";" comment)
  251. ""))))
  252. ; skip the leading indentation
  253. (define (skipindent inport)
  254. (let skipper
  255. ((inunderbars #t)
  256. (indent 0)
  257. (nextchar (read-char inport)))
  258. ; when the file ends, do not do anything else
  259. (if (not (eof-object? nextchar ))
  260. ; skip underbars
  261. (if inunderbars
  262. (if (char=? nextchar #\_ ); still in underbars?
  263. (skipper
  264. #t ; still in underbars?
  265. (+ indent 1)
  266. (read-char inport))
  267. ; else, reevaluate without inunderbars
  268. (skipper #f indent nextchar))
  269. ; else: skip remaining spaces
  270. (if (char=? nextchar #\space)
  271. (skipper
  272. #f
  273. (+ indent 1)
  274. (read-char inport))
  275. (begin
  276. (unread-char nextchar inport)
  277. indent)))
  278. indent)))
  279. ; Now we have to split a single line into indentation, content and comment.
  280. (define (splitindent inport)
  281. (let
  282. ((indent (skipindent inport)))
  283. (let
  284. ((nextchar (read-char inport))
  285. (inindent #t ); it always begins in indent
  286. (incomment #f ); but not in a comment
  287. (commentstart #f)
  288. (commentstartidentifier "\\REALCOMMENTHERE")
  289. (commentstartidentifierlength 16)
  290. (commentidentifierindex 0)
  291. (content "")
  292. (comment ""))
  293. (while (not (eof-object? nextchar))
  294. (write (eof-object? nextchar))
  295. ; check whether we leave the content
  296. ; FIXME: (wisp.py) the reader cuts the ; here, when I write it as this:
  297. ; when : and ( not incomment ) : char=? nextchar #\;
  298. ; FIXME: THIS mistreats #\; as comment! (shown 4 lines after this comment…)
  299. (when
  300. (and
  301. (not incomment)
  302. ; FIXME: this should be but would break
  303. ; char=? nextchar #\;
  304. (equal? ";" (string nextchar))
  305. (not (string-suffix? ( string #\# #\\ ) content)))
  306. (set! commentstart #t)
  307. (set! comment (string-append comment (string nextchar)))
  308. (set! nextchar (read-char inport))
  309. (continue))
  310. ; check whether we stay in the commentcheck
  311. (when (and commentstart (char=? nextchar (string-ref commentstartidentifier commentidentifierindex)))
  312. (set! commentidentifierindex (+ commentidentifierindex 1))
  313. (set! comment (string-append comment (string nextchar)))
  314. (when (= commentidentifierindex commentstartidentifierlength)
  315. (set! commentstart #f)
  316. (set! incomment #t)
  317. ; reset used variables
  318. (set! commentidentifierindex 0)
  319. (set! comment ""))
  320. (set! nextchar (read-char inport))
  321. (continue))
  322. ; if we cannot complete the commentcheck, we did not start a real comment. Append it to the content
  323. (when (and commentstart (not (char=? nextchar (string-ref commentstartidentifier commentidentifierindex))))
  324. (set! commentstart #f)
  325. (set! content (string-append content comment (string nextchar)))
  326. (set! comment "")
  327. (set! commentidentifierindex 0)
  328. (set! nextchar (read-char inport))
  329. (continue))
  330. ; if we are in the comment, just append to the comment
  331. (when incomment
  332. (set! comment (string-append comment (string nextchar)))
  333. (set! nextchar (read-char inport))
  334. (continue))
  335. ; if nothing else is true, we are in the content
  336. (set! content (string-append content (string nextchar)))
  337. (set! nextchar (read-char inport)))
  338. (when commentstart
  339. (set! content (string-append content comment))
  340. (set! comment ""))
  341. ; return the indentation, the content and the comment
  342. (list indent content comment))))
  343. ; Now use the function to split a list of lines
  344. (define (linestoindented lines)
  345. (let splitter
  346. ((unprocessed lines)
  347. (processed '()))
  348. (if (equal? unprocessed '())
  349. processed
  350. ; else: let-recursion
  351. (splitter
  352. (list-tail unprocessed 1)
  353. (append processed
  354. (list
  355. (call-with-input-string
  356. (list-ref unprocessed 0)
  357. splitindent)))))))
  358. (define (read-whole-file filename)
  359. (let ((origfile (open-file filename "r")))
  360. (let reader
  361. ((text "")
  362. (nextchar (read-char origfile)))
  363. (if (eof-object? nextchar)
  364. text
  365. (reader
  366. (string-append text (string nextchar))
  367. (read-char origfile))))))
  368. (define (wisp2lisp-add-inline-colon-brackets line)
  369. "Add inline colon brackets to a wisp-line (indent,content,comment).
  370. A line with only a colon and whitespace gets no additional parens!
  371. Also unescape \\: to :.
  372. "
  373. ; if the line only consists of a colon and whitespace, do not change it.
  374. (if (line-only-colon? line)
  375. line
  376. (let ((content (line-content line)))
  377. ; replace final " :" by a function call. There we are by definition of the line-splitting not in a string.
  378. (when (string-suffix? " :" content)
  379. (set! content (string-append (string-drop-right content 1) "()")))
  380. ; process the content in reverse direction, so we can detect ' : and turn it into '(
  381. ; let linebracketizer ( ( instring #f ) ( inbrackets 0 ) ( bracketstoadd 0 ) ( unprocessed content ) ( processed "" ) )
  382. (let linebracketizer (( instring #f ) ( inbrackets 0 ) ( bracketstoadd 0 ) ( unprocessed content ) ( processed "" ) )
  383. (if (< (string-length unprocessed) 2)
  384. ; if unprocessed is < 2 chars, it cannot contain ": ". We are done.
  385. (list
  386. (line-indent line)
  387. (string-append unprocessed processed (xsubstring ")" 0 bracketstoadd))
  388. (line-comment line))
  389. ; else
  390. (let
  391. ((lastletter (string-take-right unprocessed 1))
  392. (lastupto3 (string-take-right unprocessed (min 3 (string-length unprocessed))))
  393. (lastupto4 (string-take-right unprocessed (min 4 (string-length unprocessed))))
  394. (lastupto6 (string-take-right unprocessed (min 6 (string-length unprocessed)))))
  395. ; check if we’re in a string
  396. (when
  397. (or
  398. (and
  399. (not instring)
  400. (equal? "\"" lastletter)
  401. (not (equal? "#\\\"" lastupto3)))
  402. (and
  403. instring
  404. (equal? "\"" lastletter)
  405. (not (endsinunevenbackslashes (string-drop-right unprocessed 1)))))
  406. (set! instring (not instring)))
  407. (when (not instring)
  408. (when
  409. (or
  410. ; TODO: Only match for braces {} if curly infix is enabled
  411. ; FIXME: Catch wrong ordering of parens/brackets/braces like ({)}
  412. (and (equal? "{" lastletter) (not (equal? "#\\{" lastupto3)))
  413. (and (equal? "[" lastletter) (not (equal? "#\\[" lastupto3)))
  414. (and (equal? "(" lastletter) (not (equal? "#\\(" lastupto3))))
  415. (set! inbrackets (- inbrackets 1)))
  416. (when
  417. (or
  418. (and (equal? ")" lastletter) (not (equal? "#\\)" lastupto3)))
  419. (and (equal? "]" lastletter) (not (equal? "#\\]" lastupto3)))
  420. (and (equal? "}" lastletter) (not (equal? "#\\}" lastupto3))))
  421. (set! inbrackets (+ 1 inbrackets )))); remember that we're going backwards!
  422. ; error handling: inbrackets must never be smaller than 0 - due to the line splitting.
  423. (when (< inbrackets 0)
  424. (throw 'more-inline-brackets-closed-than-opened inbrackets line))
  425. ; when we’re in a string or in brackets , just skip to the next char
  426. (cond
  427. ((or instring (> inbrackets 0))
  428. (linebracketizer instring inbrackets bracketstoadd
  429. (string-drop-right unprocessed 1)
  430. (string-append lastletter processed)))
  431. ; else check for " : ": That adds a new inline bracket
  432. ; support : at the beginning of a line, too.
  433. ((or (equal? " : " lastupto3) (equal? ": " lastupto3))
  434. ; replace the last 2 chars with "(" and note
  435. ; that we need an additional closing bracket
  436. ; at the end.
  437. (linebracketizer instring inbrackets (+ 1 bracketstoadd )
  438. (string-append (string-drop-right unprocessed 2) )
  439. (string-append "(" processed)))
  440. ; turn " ' (" into " '(", do not modify unprocessed, except to shorten it!
  441. ; same for ` , #' #` #, #,@,
  442. ((and (string-prefix? "(" processed) (equal? " ' " lastupto3))
  443. ; leave out the second space
  444. (linebracketizer instring inbrackets bracketstoadd
  445. (string-append (string-drop-right unprocessed 2) "'")
  446. processed))
  447. ((and (string-prefix? "(" processed) (equal? " , " lastupto3))
  448. ; leave out the second space
  449. (linebracketizer instring inbrackets bracketstoadd
  450. (string-append (string-drop-right unprocessed 2) ",")
  451. processed))
  452. ((and (string-prefix? "(" processed) (equal? " ` " lastupto3))
  453. ; leave out the second space
  454. (linebracketizer instring inbrackets bracketstoadd
  455. (string-append (string-drop-right unprocessed 2) "`")
  456. processed))
  457. ((and (string-prefix? "(" processed) (equal? " #` " lastupto4))
  458. ; leave out the second space
  459. (linebracketizer instring inbrackets bracketstoadd
  460. (string-append (string-drop-right unprocessed 3) "#`")
  461. processed))
  462. ((and (string-prefix? "(" processed) (equal? " #' " lastupto4))
  463. ; leave out the second space
  464. (linebracketizer instring inbrackets bracketstoadd
  465. (string-append (string-drop-right unprocessed 3) "#'")
  466. processed))
  467. ((and (string-prefix? "(" processed) (equal? " #, " lastupto4))
  468. ; leave out the second space
  469. (linebracketizer instring inbrackets bracketstoadd
  470. (string-append (string-drop-right unprocessed 3) "#,")
  471. processed))
  472. ((and (string-prefix? "(" processed) (equal? " #,@, " lastupto6))
  473. ; leave out the second space
  474. (linebracketizer instring inbrackets bracketstoadd
  475. (string-append (string-drop-right unprocessed 5) "#,@,")
  476. processed))
  477. (else ; just go on
  478. (linebracketizer instring inbrackets bracketstoadd
  479. (string-drop-right unprocessed 1)
  480. (string-append lastletter processed))))))))))
  481. (define (last-indent levels)
  482. "Retrieve the indentation of the last line: Simply the highest level."
  483. (list-ref levels 0))
  484. (define (line-add-starting-bracket line)
  485. "Add a starting bracket to the line, if it is no continuation line (it is more indented than the previous).
  486. If line starts with one of ' , ` #` #' #, #,@, then turn it into '(... instead of ('...
  487. If line is indented and only contains : and optional whitespace, remove the :.
  488. The line *must* have a whitespace after the prefix, except if the prefix is the only non-whitespace on the line."
  489. ; if the line only contains a colon, we just replace its content with an opening paren.
  490. (if (line-only-colon? line ); FIXME: Check for this somewhere else.
  491. (list
  492. (line-indent line)
  493. (string-append "(" (string-drop (line-content line) 1 )); keep whitespace
  494. (line-comment line))
  495. (let loop ((paren-prefixes (list "' " ", " "` " "#` " "#' " "#, " "#,@, ")))
  496. ; first check whether we are done checking
  497. (if (null-list? paren-prefixes)
  498. ; construct the line structure: '(indentation-depth content comment)
  499. (list
  500. (line-indent line)
  501. (string-append
  502. "("
  503. (line-content line))
  504. (line-comment line))
  505. ; otherwise check all possible prefixes
  506. (let*
  507. ((prefix (car paren-prefixes))
  508. (prefix-no-space (string-drop-right prefix 1)))
  509. (cond
  510. ((string-prefix? prefix (line-content line))
  511. (list
  512. (line-indent line)
  513. (string-append
  514. prefix-no-space "("
  515. (string-drop (line-content line) (string-length prefix)))
  516. (line-comment line)))
  517. ((line-only-prefix? line prefix-no-space)
  518. (list
  519. (line-indent line)
  520. (string-append
  521. (string-drop-right prefix 1) "("
  522. (string-drop (line-content line) (string-length prefix-no-space)))
  523. (line-comment line)))
  524. (else
  525. (loop (cdr paren-prefixes)))))))))
  526. (define (line-add-closing-brackets line number)
  527. "Add a closing bracket to the line."
  528. (list
  529. (line-indent line)
  530. (string-append
  531. (line-content line)
  532. (xsubstring ")" 0 number))
  533. (line-comment line)))
  534. (define (line-indent-brackets-to-close line-indent levels line-continues prev-continues)
  535. "Find the number of brackets to close to reduce the levels to the line-indent."
  536. ; adjust the levels until the highest indentation level is equal
  537. ; to the indentation of the next line. Then check for
  538. ; continuation.
  539. (let closer ((bracketstoclose 0) (rest levels))
  540. (let ((highest-level (list-ref rest 0)))
  541. ; finish-condition
  542. (if (= line-indent highest-level)
  543. (if prev-continues
  544. bracketstoclose
  545. (+ 1 bracketstoclose))
  546. (if (> line-indent highest-level)
  547. (closer (- bracketstoclose 1) (append (list line-indent) rest ))
  548. (closer (+ bracketstoclose 1) (list-tail rest 1)))))))
  549. (define (line-indent-brackets-to-open line-indent levels line-continues prev-continues)
  550. "Find the number of brackets to open to fit the line-indent and continuation marker."
  551. (if line-continues
  552. 0
  553. 1))
  554. (define (line-indent-levels-adjust levels next-indent)
  555. "Add or remove levels so the highest remaining level matches next-indent."
  556. (let adjuster ((lev levels))
  557. (let ((highest-level (list-ref lev 0)))
  558. (if (= next-indent highest-level)
  559. lev
  560. (if (> next-indent highest-level)
  561. (append (list next-indent) lev)
  562. (adjuster (list-tail lev 1)))))))
  563. (define (line-drop-continuation-dot line)
  564. (let ((content (line-content line)))
  565. (list
  566. (line-indent line)
  567. (if (line-continues? line)
  568. (string-drop content 2)
  569. content)
  570. (line-comment line ))))
  571. (define (wisp2lisp-parse lisp prev lines)
  572. "Parse the body of the wisp-code."
  573. (set! prev (wisp2lisp-add-inline-colon-brackets prev )); prev already is a code-line.
  574. (if (not (or (line-continues? prev) (line-empty-code? prev)))
  575. (set! prev (line-add-starting-bracket prev)))
  576. (set! lines (map-in-order wisp2lisp-add-inline-colon-brackets lines))
  577. (let bracketizer ((levels '(0)) (pre prev) (unprocessed lines) (processed lisp) (whitespace '()))
  578. ; levels is the list of levels, with the lowest to the right. i.e: '(12 8 4 0)
  579. ; once we processed everything, we pass the bracketizer pre as f one last time
  580. (if (equal? #f (line-content pre))
  581. processed
  582. (let ((next (if (equal? unprocessed '()) (list 0 #f #f) (list-ref unprocessed 0 )))); this is the break condition for the next loop!
  583. (if (line-empty-code? next ); empty lines get silently added, but otherwise ignored
  584. (bracketizer levels pre
  585. (list-tail unprocessed 1)
  586. processed
  587. (append whitespace (list next)))
  588. ; firstoff add the next indent to the levels, so we only work on the levels, prev-continues, next-continues and next-indent
  589. ; if pre was a continuation, the real levels are 1 lower than the counted levels
  590. (let*
  591. ((next-indent (line-indent next))
  592. (pre-indent (line-indent pre))
  593. (pre-continues (line-continues? pre))
  594. (next-continues (line-continues? next))
  595. (final-line (equal? #f (line-content next)))
  596. (bracketstocloseprev (if (line-empty-code? pre) 0 (line-indent-brackets-to-close next-indent levels next-continues pre-continues)))
  597. (bracketstoopennext (line-indent-brackets-to-open next-indent levels next-continues pre-continues))
  598. (newnext (if final-line next (if (> bracketstoopennext 0) (line-add-starting-bracket next) next)))
  599. (newpre (line-drop-continuation-dot (line-add-closing-brackets pre bracketstocloseprev)))
  600. (newlevels (line-indent-levels-adjust levels next-indent)))
  601. (bracketizer newlevels newnext
  602. (if final-line unprocessed (list-tail unprocessed 1))
  603. (append processed (list newpre) whitespace)
  604. (list))))))))
  605. (define (wisp2lisp-initial-comments lisp prev lines)
  606. "Keep all starting comments: do not start them with a bracket."
  607. (let skip-initial-comments ((lisp lisp) (prev prev) (lines lines))
  608. (if (= 0 (length lines )); file only contained comments, maybe including the hashbang
  609. (list lisp prev lines)
  610. (if (line-empty-code? prev)
  611. (skip-initial-comments (append lisp (list prev))
  612. (list-ref lines 0) (list-tail lines 1))
  613. (list lisp prev lines)))))
  614. (define (wisp2lisp-hashbang lisp prev unprocessed)
  615. "Parse a potential initial hashbang line."
  616. (if
  617. (and
  618. (equal? lisp '() ); really the first line
  619. (equal? 0 (line-indent prev))
  620. (string-prefix? "#!" (line-content prev)))
  621. (wisp2lisp-hashbang (append lisp (list (line-merge-comment prev)))
  622. (list-ref unprocessed 0) (list-tail unprocessed 1))
  623. (list lisp prev unprocessed)))
  624. (define (wisp2lisp-lines lines)
  625. "Parse indentation in the lines to add the correct brackets."
  626. (if (equal? lines '())
  627. '()
  628. (let
  629. ((lisp '() ); the processed lines
  630. (prev (list-ref lines 0 )); the last line
  631. (unprocessed (list-tail lines 1 ))); obvious :)
  632. (let*
  633. ((hashbanged (wisp2lisp-hashbang lisp prev unprocessed))
  634. (deinitialized (apply wisp2lisp-initial-comments hashbanged))
  635. (parsed (apply wisp2lisp-parse deinitialized)))
  636. parsed))))
  637. (define (line-unescape-underscore-and-colon line)
  638. "Unescape underscores at the beginning of the line and colon."
  639. (let loop
  640. ((processed "")
  641. (unprocessed (line-content line)))
  642. (if (equal? "" unprocessed)
  643. (list
  644. (line-indent line)
  645. processed
  646. (line-comment line))
  647. (let
  648. ((next (string (string-ref unprocessed 0))))
  649. (if (equal? "" processed )
  650. (cond
  651. ; get rid of \_
  652. ((string-prefix? "(\\_" unprocessed)
  653. (loop processed (string-append "(" (string-drop unprocessed 2))))
  654. ; get rid of \:
  655. ((string-prefix? "(\\:" unprocessed)
  656. (loop processed (string-append "(" (string-drop unprocessed 2))))
  657. ; get rid of . \:
  658. ((string-prefix? "\\:" unprocessed)
  659. (loop processed (string-drop unprocessed 1)))
  660. (else
  661. (loop
  662. (string-append processed next)
  663. (string-drop unprocessed 1))))
  664. (cond
  665. ((string-prefix? " \\:" unprocessed)
  666. (loop
  667. (string-append processed " :" )
  668. (string-drop unprocessed 3)))
  669. ((string-prefix? "(\\:" unprocessed)
  670. (loop
  671. (string-append processed "(:" )
  672. (string-drop unprocessed 3)))
  673. (else
  674. (loop
  675. (string-append processed next)
  676. (string-drop unprocessed 1)))))))))
  677. (define (unescape-underscore-and-colon lines)
  678. "Unescape underscores at the beginning of each line and colon."
  679. (let loop
  680. ((processed '())
  681. (unprocessed lines))
  682. (if (equal? unprocessed '())
  683. processed
  684. (let ((current (car unprocessed)))
  685. (loop
  686. (append processed (list (line-unescape-underscore-and-colon current)))
  687. (cdr unprocessed))))))
  688. (define* (string-replace-substring s substr replacement #:optional (start 0) (end (string-length s)))
  689. "Replace every instance of substring in s by replacement."
  690. (let ((substr-length (string-length substr)))
  691. (if (zero? substr-length)
  692. (error "string-replace-substring: empty substr")
  693. (let loop
  694. ((start start)
  695. (pieces (list (substring s 0 start))))
  696. (let ((idx (string-contains s substr start end)))
  697. (if idx
  698. (loop (+ idx substr-length)
  699. (cons* replacement
  700. (substring s start idx)
  701. pieces))
  702. (string-concatenate-reverse
  703. (cons (substring s start)
  704. pieces))))))))
  705. (define (unescape-linebreaks text)
  706. "unescape linebreaks"
  707. (string-replace-substring
  708. ; we have to construct the placeholders here to avoid unescaping them when we parse ourselves…
  709. (string-replace-substring text (string-append "\\LINE_" "BREAK_N") (string #\newline))
  710. (string-append "\\LINE_" "BREAK_R")
  711. (string #\return )))
  712. (define (unescape-comments text)
  713. "unescape comments"
  714. (string-replace-substring text
  715. ; we have to construct the placeholders here to avoid unescaping them when we parse ourselves…
  716. (string-append ";" "\\REALCOMMENTHERE")
  717. ";"))
  718. (define (wisp-chunkreader inport)
  719. "Read one wisp-expression from inport, without escaping of fake newlines but with correct detection of real new lines.
  720. Realized by reading with newline and comment escaping and unescaping both again after reading."
  721. (unescape-comments
  722. (unescape-linebreaks
  723. (nostringandbracketbreaksreader inport))))
  724. (define (join-lisp-lines lisp-lines)
  725. (let join ((joined "") (unprocessed lisp-lines))
  726. (if (not (equal? unprocessed '()))
  727. (let*
  728. ((next (list-ref unprocessed 0))
  729. (nextstring
  730. (string-append
  731. (xsubstring " " 0 (line-indent next))
  732. ; here we re-add all necessary linebreakswe get rid
  733. (unescape-linebreaks (line-content next))
  734. (if (equal? "" (line-comment next ))
  735. ""
  736. (string-append ";" (line-comment next)))
  737. "\n")))
  738. (join (string-append joined nextstring) (list-tail unprocessed 1)))
  739. joined)))
  740. (define (wisp2lisp text )
  741. (let*
  742. ((nobreaks (call-with-input-string text nostringandbracketbreaks))
  743. (textlines (call-with-input-string nobreaks splitlines))
  744. (lines (linestoindented textlines))
  745. (lisp-lines (wisp2lisp-lines lines))
  746. (clean-lines (unescape-underscore-and-colon lisp-lines)))
  747. (join-lisp-lines clean-lines)))
  748. ; first step: Be able to mirror a file to stdout
  749. (if (< 1 (length (command-line)))
  750. (let*
  751. ((filename (list-ref ( command-line ) 1))
  752. (text (read-whole-file filename))
  753. ; Lines consist of lines with indent, content and comment. See
  754. ; line-indent, line-content, line-comment and the other
  755. ; line-functions for details.
  756. ; textlines : split-wisp-lines text
  757. ; lines : linestoindented textlines
  758. (lisp (wisp2lisp text)))
  759. (display lisp)
  760. (newline))
  761. #f)
  762.