Danny Milosavljevic <dan...@scratchpost.org> writes:

> (define (skip-comments text)
>   (replace (string-append "//[^\n]*?|"
>                           "/[*].*?[*]/|"
>                           "'([.]|[^'])*?'|"
>                           "\"([.]|[^\"])*?\"")

This last part will remove anything between double quotes, such as every
string.  Another problem seems to be the handling of /* comments */.
It’s a greedy regex that will swallow any character until it finally
hits */ — even if the characters in between may have been */.

I would not use regular expressions here but a read loop such as this:

--8<---------------cut here---------------start------------->8---
(define (strip-comments port)
  (let loop ((char (get-char port))
             (balance 0)
             (chars '()))
    (cond
     ;; done!
     ((eof-object? char)
      (list->string (reverse chars)))
     ;; line comment
     ((and (equal? char #\/)
           (equal? #\/ (peek-char port)))
      (begin (read-line port)
             (loop (get-char port) balance chars)))
     ;; begin of block comment
     ((and (equal? char #\/)
           (equal? #\* (peek-char port)))
      (begin (read-char port)
             (loop (get-char port) (1+ balance) chars)))
     ;; end of block comment
     ((and (positive? balance)
           (equal? char #\*)
           (equal? #\/ (peek-char port)))
      (begin (read-char port)
             (loop (get-char port) (1- balance) chars)))
     ;; inside block comment
     ((positive? balance)
      (loop (get-char port) balance chars))
     ;; just a plain ol’ character
     (else (loop (get-char port) balance (cons char chars))))))

;; Strip all comments from the file “fname” and show the result.
(display (call-with-input-file fname strip-comments))
--8<---------------cut here---------------end--------------->8---

--
Ricardo




Reply via email to