Re: [racket-users] using `define-runtime-path` inside macros

2018-11-21 Thread Alex Harsanyi


On Friday, August 31, 2018 at 11:11:29 PM UTC+8, Matthew Butterick wrote:
>
>
> On Aug 31, 2018, at 3:25 AM, Alex Harsanyi  > wrote:
>
> Is there a way to write this macro such that the runtime path is defined
> relative to the location of the file that uses `define-sql-statement`?
>
>
> One option, using `syntax-source` to get the starting directory:
>
>
Unfortunately, this solution does not work.  The problem is that 
syntax-source returns an absolute path, so `define-runtime-path` will 
receive an absolute path as an argument.   This means that, even if a 
distribution is created, the path will still refer to the original file 
name inside the source tree, and if the distribution is moved to another 
computer, it will fail to find the file.  It is interesting however, that a 
copy of the file is present in the distribution directory, but this is not 
actually used when the program runs.

BTW, it took a long time to discover this problem, because I happen to run 
my "distributed" program on the same machine where I built it, and until 
last week, the exe in the distribution had access to the files in the 
source repository...

Alex. 


> #lang racket
> (require racket/runtime-path db (for-syntax racket/base))
>
> (define-syntax (define-sql-statement stx)
>   (syntax-case stx ()
> [(_ name path)
>  (let-values ([(dir fn _) (split-path (syntax-source stx))])
>(with-syntax ([dir (or dir 'same)])
>  #'(begin
>  (define-runtime-path rpath (build-path dir path))
>  (define name
>(let ([vq #f])
>  (lambda ()
>(unless vq
>  (let ([s (call-with-input-file rpath port->string)])
>(set! vq (virtual-statement (lambda (_) s)
>vq))]))
>
> (provide define-sql-statement)
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] using `define-runtime-path` inside macros

2018-08-31 Thread Matthew Butterick

> On Aug 31, 2018, at 4:14 PM, Alex Harsanyi  wrote:
> 
> Thanks for this solution, it works.  While I understand what is being done, I 
> do not understand why it is necessary

>From the docs:

"The path is normally computed by taking a relative path result from expr and 
adding it to a path for the enclosing file ... If the expression has no source 
module, the syntax-source location associated with the form is used" [1]

IOW when you wrap `define-runtime-path` in a macro, the "syntax-source location 
associated with the form" becomes that of the macro-definition site. In this 
case, we are overriding this behavior by building the path manually with the 
`syntax-source` we prefer (namely, that of the calling site)


[1] 
http://docs.racket-lang.org/reference/Filesystem.html#(form._((lib._racket%2Fruntime-path..rkt)._define-runtime-path))
 




-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] using `define-runtime-path` inside macros

2018-08-31 Thread Alex Harsanyi


On Friday, August 31, 2018 at 11:11:29 PM UTC+8, Matthew Butterick wrote:
>
>
> On Aug 31, 2018, at 3:25 AM, Alex Harsanyi  > wrote:
>
> Is there a way to write this macro such that the runtime path is defined
> relative to the location of the file that uses `define-sql-statement`?
>
>
> One option, using `syntax-source` to get the starting directory:
>

Hi Matthew, 

Thanks for this solution, it works.  While I understand what is being done, 
I do not understand why it is necessary

Another interesting thing is that, with my original macro, when running the 
code using racket, the file path had to be relative to the file containing 
the definition, but when creating an executable using raco exe, the file 
path had to be relative to the file using the macro.  Your solution works 
in both cases.

Best Regards,
Alex.



>
> #lang racket
> (require racket/runtime-path db (for-syntax racket/base))
>
> (define-syntax (define-sql-statement stx)
>   (syntax-case stx ()
> [(_ name path)
>  (let-values ([(dir fn _) (split-path (syntax-source stx))])
>(with-syntax ([dir (or dir 'same)])
>  #'(begin
>  (define-runtime-path rpath (build-path dir path))
>  (define name
>(let ([vq #f])
>  (lambda ()
>(unless vq
>  (let ([s (call-with-input-file rpath port->string)])
>(set! vq (virtual-statement (lambda (_) s)
>vq))]))
>
> (provide define-sql-statement)
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] using `define-runtime-path` inside macros

2018-08-31 Thread Matthias Felleisen


#lang racket
(require racket/runtime-path db (for-syntax racket/base syntax/parse))

(define-syntax (define-sql-statement stx)
  (syntax-parse stx
[(_ name #;path)
 (define location (syntax-source #'name))
 (define-values (path file _) (split-path location))
 (with-syntax ([path path])
   #'(begin
   (define-runtime-path rpath path)
   (define name
 (let ([vq #f])
   (lambda ()
 (unless vq
   (let ([s (call-with-input-file rpath port->string)])
 (set! vq (virtual-statement (lambda (_) s)
 vq)]))

(define-sql-statement a #;"b")

(provide define-sql-statement)

(But this creates a system-specific path.) 


> On Aug 31, 2018, at 6:25 AM, Alex Harsanyi  wrote:
> 
> 
> I have defined a macro to help with using SQL queries from external files: it
> uses `define-runtime-path` to store the path to the file and defines a
> function that reads the data when called the first time and constructs a
> `virtual-statement`:
> 
> #lang racket
> (require racket/runtime-path db (for-syntax racket/base))
> 
> (define-syntax (define-sql-statement stx)
>   (syntax-case stx ()
> [(_ name path)
>  #'(begin
>  (define-runtime-path rpath path)
>  (define name
>(let ([vq #f])
>  (lambda ()
>(unless vq
>  (let ([s (call-with-input-file rpath port->string)])
>(set! vq (virtual-statement (lambda (_) s)
>vq]))
> 
> (provide define-sql-statement)
> 
> Unfortunately, the generated runtime path is relative to the location of the
> file defining the macro, not the file that uses it.  That is, in the example
> below, "query.sql" needs to be in "subdir" together with
> "define-sql-statement.rkt":
> 
> #lang racket
> (require "subdir/define-sql-statement.rkt")
> (define-sql-statement x "query.sql")
> 
> Is there a way to write this macro such that the runtime path is defined
> relative to the location of the file that uses `define-sql-statement`?
> 
> If you want to test this, you can put any text in query.sql (e.g "select *
> from test")
> 
> Thanks,
> Alex.
> 
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] using `define-runtime-path` inside macros

2018-08-31 Thread Matthew Butterick

> On Aug 31, 2018, at 3:25 AM, Alex Harsanyi  wrote:
> 
> Is there a way to write this macro such that the runtime path is defined
> relative to the location of the file that uses `define-sql-statement`?


One option, using `syntax-source` to get the starting directory:


#lang racket
(require racket/runtime-path db (for-syntax racket/base))

(define-syntax (define-sql-statement stx)
  (syntax-case stx ()
[(_ name path)
 (let-values ([(dir fn _) (split-path (syntax-source stx))])
   (with-syntax ([dir (or dir 'same)])
 #'(begin
 (define-runtime-path rpath (build-path dir path))
 (define name
   (let ([vq #f])
 (lambda ()
   (unless vq
 (let ([s (call-with-input-file rpath port->string)])
   (set! vq (virtual-statement (lambda (_) s)
   vq))]))

(provide define-sql-statement)

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.