[plt-scheme] regexp-match a full line

From: Eli Barzilay (eli at barzilay.org)
Date: Mon Oct 6 21:25:34 EDT 2008

On Oct  7, Noel Welsh wrote:
> You could make your regexp match to the end of the line (using the $
> symbol IIRC) or you could convert the file to a list of strings and
> match on those.  For the later the port->string-list function in the
> port.plt package on PLaneT will help.

As a side note, that function is easy to define using either
`for/list':

  (define (port->string-list port)
    (for/list ([l (in-lines port)]) l))

or `regexp-split':

  (define (port->string-list port)
    (map bytes->string/utf-8 (regexp-split #rx"\r?\n" port)))


And, as long as I'm looking at that file, the quick-n-dirty hack can
fail in unexpected ways:

  > (port->sexp-list (open-input-string "1 2 3) 4"))
  (1 2 3)
  > (port->sexp-list (open-input-string "#z 1 2 3"))
  UNKNOWN::1: read: bad syntax `#z'

the location is off-by-1 in that last example.

  > (port->sexp-list (open-input-string "1 2 3 #"))
  UNKNOWN::7: read: bad syntax `#)'
  > (port->sexp-list (open-input-string "1 2 3 ["))
  UNKNOWN::8: read: unexpected `)'

these examples expose the fake parens.

And finally, all of these:

  (port->sexp-list (open-input-string "1 2 3 ("))
  (port->sexp-list (open-input-string "1 2 3 #\"))
  (port->sexp-list (open-input-string "1 2 3 \\"))
  (port->sexp-list (open-input-string "1 2 3 \""))

will just get stuck.

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!


Posted on the users mailing list.