Source location info in tag functions #28

Open
opened 5 years ago by otherjoel · 0 comments
otherjoel commented 5 years ago (Migrated from github.com)

In #27 @odanoburu asked about tag functions having info about where they are located in the Pollen source doc:

I wonder if it's possible to include the location of the error in the source file (i.e., the location of the tag in the source file, not of the location of the tag function in pollen.rkt). is this location information available to the tag function? if not, can it be made available?

This info is available at compile time, so if you write your tag function as a macro you absolutely can get to it. I recently cooked up a proof of concept:

#lang racket/base ; ~~~ pollen.rkt ~~~

(require pollen/tag)
(require (for-syntax racket/base syntax/parse))

(provide root note)

(define (root . elems)
  `(body ,@elems))

(define-syntax (note stx)
  (syntax-parse stx
    [(_ args ...)
     (with-syntax ([srcdoc (format "~a" (syntax-source stx))]
                   [srcline (number->string (syntax-line stx))])
      
       #'(note-tag #:src srcdoc #:line srcline args ...))]))

(define note-tag (default-tag-function 'div))

Paste this into example.html.pm and save it into the same folder:

#lang pollen

◊note[#:date "2019"]{This is going to be cool.}

“Running” it in DrRacket produces:

'(body
  (div
   ((date "2019")
    (line "3")
    (src
     "/Users/joel/Documents/code/sandbox/srcloc/example.html.pm"))
   "This is going to be cool."))

So you see that the output div contains the file and line number where ◊note occurs in the Pollen source. In order to make full use of this code you really need to delve into how macros and syntax objects work. I found it kind of brain-melting at first (still do, a bit). But it’s another example of Racket’s power, and it’s really cool that this is available to us in Pollen.

I haven’t used this for error reporting (I do as little error reporting in Pollen as possible since I’m the only user of my tag functions and can spot any issues pretty quickly). But I wanted to make it possible to have output that could link back to its source file and location.

In #27 @odanoburu asked about tag functions having info about where they are located in the Pollen source doc: > I wonder if it's possible to include the location of the error in the source file (i.e., the location of the tag in the source file, not of the location of the tag function in pollen.rkt). is this location information available to the tag function? if not, can it be made available? This info is available at compile time, so if you write your tag function as a [macro](https://beautifulracket.com/explainer/macros.html) you absolutely can get to it. I recently cooked up a proof of concept: ``` #lang racket/base ; ~~~ pollen.rkt ~~~ (require pollen/tag) (require (for-syntax racket/base syntax/parse)) (provide root note) (define (root . elems) `(body ,@elems)) (define-syntax (note stx) (syntax-parse stx [(_ args ...) (with-syntax ([srcdoc (format "~a" (syntax-source stx))] [srcline (number->string (syntax-line stx))]) #'(note-tag #:src srcdoc #:line srcline args ...))])) (define note-tag (default-tag-function 'div)) ``` Paste this into `example.html.pm` and save it into the same folder: ``` #lang pollen ◊note[#:date "2019"]{This is going to be cool.} ``` “Running” it in DrRacket produces: ```racket '(body (div ((date "2019") (line "3") (src "/Users/joel/Documents/code/sandbox/srcloc/example.html.pm")) "This is going to be cool.")) ``` So you see that the output `div` contains the file and line number where `◊note` occurs _in the Pollen source_. In order to make full use of this code you really need to delve into how macros and syntax objects work. I found it kind of brain-melting at first (still do, a bit). But it’s another example of Racket’s power, and it’s really cool that this is available to us in Pollen. I haven’t used this for error reporting (I do as little error reporting in Pollen as possible since I’m the only user of my tag functions and can spot any issues pretty quickly). But I wanted to make it possible to have output that could link back to its source file and location.
This repo is archived. You cannot comment on issues.
No Milestone
No project
No Assignees
1 Participants
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: mbutterick/pollen-users#28
Loading…
There is no content yet.