[plt-scheme] Reporting syntax-error to a DrScheme tool

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Wed Mar 1 07:09:49 EST 2006

Robby Findler wrote:
> At Wed, 01 Mar 2006 02:12:08 +0100, Jens Axel Søgaard wrote:

>>Okay. I think what misled me was this note from the parser-tools/doc.txt
>>
>> > (file-path string) - sets the parameter file-path, which the lexer
>>    will use as the source location if it raises a read error.  This
>>    allows DrScheme to open the file containing the error.
>>
>>and the example parser has the following
>>
>>  (error (lambda (a name val start end)
>>               (raise-read-error
>>                "read-error"
>>                source-name
>>                (position-line start)
>>                (position-col start)
>>                (position-offset start)
>>                (- (position-offset end)
>>                   (position-offset start)))))
>>
>>So I was thinking: "To signal an error just raise an read-error
>>exception".
>>
>>I hadn't look at errortrace before (since the parse error is raised
>>before any "code" is generated).
> 
> Oh! That's different. I misunderstood.
> 
> If you embed a source location into the exception record, that should
> be showing up, even without errortrace.

It eventually did - I think the problem was that I had set the correct
filename.

I solved the puzzle by looking at Shriram's Algol60 tool. For the
mail archive I have written down what I did below. Shriram's parser
used the very clever approach of using syntax-objects as tokens,
which makes it easy to pass the source location information around.

To make things easy I copied the approach, which used a cfg-parser
in stead of the normal parser-tool parser. While doing this I think
I have found a bug - I am just not sure who to blame.

I used an

    (require (lib "cfg-parser" "algol60"))

to reuse Shriram's cfg-parser. After installing my tool the
I get the following error when starting DrScheme:

   Error invoking tool: #<path:c:\...\algol60>

   standard-module-name-resolver: module previously loaded with suffix
   #"", cannot load with suffix #".ss":
   #<path:c:\programmer\plt\collects\algol60\cfg-parser.ss>

Sure enough in the language selection dialog the Algol60! language
is now missing, while my Stylesheet language works fine.



And now to what I did.

My parse function now starts

   (define parse
     (cfg-parser
      (error    (lambda (a b stx)
                  (raise-read-error
                    (format "parse error near ~a"
                            (syntax-e stx))
			   (syntax-source stx)
			   (syntax-line stx)
			   (syntax-column stx)
			   (syntax-position stx)
			   (syntax-span stx))))

and is called like this from front-end/complete-program

(define/public (front-end/complete-program port settings eachpack-cache)
   (front-end port settings))

(define/private (front-end port settings)
   (let ([name (object-name port)])
     (lambda ()
       (if (eof-object? (peek-char port))
            eof
	   (parse-css-port port name)))))

my on-execute is still

(define/public (on-execute settings run-in-user-thread)
             ; (read-case-sensitive #f)
             (run-in-user-thread
              (lambda ()
                (error-display-handler
                 (drscheme:debug:make-debug-error-display-handler
                    (error-display-handler)))
                (error-print-source-location #t)
                (void))))

-- 
Jens Axel Søgaard




Posted on the users mailing list.