[plt-scheme] parser-tools

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Mon May 14 00:35:30 EDT 2007


> I've recently begun using the parser tools collection and I have a few 
> questions, particularly for the lexer.
>
> If I use start-pos to get a struct position, I cannot figure out how to 
> unpack and cannot find accessors.  Any Idea how to extract that?

Hi Sean,


According to the documentation in the parser-tools:

---------------------------------------------------------
"lex.ss" exports the following structures:

> (struct position (offset line col))
    These structures are bound to start-pos and end-pos.
    Offset is the offset of the character in the input.
    Line in the line number of the character.
    Col is the offset in the current line.

> (struct position-token (token start-pos end-pos))
    src-pos-lexers return these.
---------------------------------------------------------

So you can use position-offset, position-line, and position-col as 
accessors.


If you're familiar with the "plt-match.ss" pattern-matching library, you 
can also use it to destructure and bind the values in there.  For example:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(module test-lexer mzscheme
   (require (lib "lex.ss" "parser-tools")
            (lib "plt-match.ss"))

   ;; print-position: position -> void
   ;; just for debugging: prints out information in a position.
   (define (print-position a-pos)
     (match a-pos
       ((struct position (offset line column))
        (printf "offset: ~a    line: ~a    column: ~a~n"
                offset line column))))

   ;; test: exercise print-position with arbitrary values for the position.
   (print-position (make-position 20 3 7)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


If you have more questions, please feel free to ask!


Posted on the users mailing list.