[plt-scheme] Re: Parsing not ignoring \n

From: Paulo J. Matos (pocmatos at gmail.com)
Date: Fri Jun 19 17:44:53 EDT 2009

On Fri, 2009-06-19 at 11:40 -0700, YC wrote:
> On Fri, Jun 19, 2009 at 11:18 AM, Paulo J. Matos <pocmatos at gmail.com>
> wrote:
>         Still on this, is it not possible to re-insert a char in an
>         input-port?
>         This way, once the lexer recognizes a one-line comment I could
>         re-insert the \n into the port and keep lexing...
> If you are reading from either a file or string port, then you can use
> file-position to rewind the port to the correct position so the next
> lex will start from the correct place. 
> http://docs.plt-scheme.org/reference/port-buffers.html#(def._((quote._~23~25kernel)._file-position))
> Another approach is to use lexer to only handle the start of the
> comment, e.g., // and then write your own custom reader to read until
> it hits newline (but preserving it), and then hand the control back to
> the lexer. 
> (define (custom-comment-reader in)
>   (read-until-new-line ...)) ;; defined elsewhere  
> (define eb-lexer
>   (lexer
>    ("//" (custom-comment-reader input-port)
>          ...)
>    ...))

This is a good idea. But in the meantime I also found another solution.
Which is not to ignore one line comments and define
((:or #\newline one-line-comment) 'NEWLINE)

then, in the parser I will have to know exactly where newlines can
appear... this sounds messy. :) hehe


> Cheers,
> yc

Posted on the users mailing list.