s c h e m a t i c s : c o o k b o o k

/ Cookbook.IdiomLocation

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Capturing Location Information

Problem

You want to capture location information (file and line number, for example) for code you write.

Solution

The PLT Scheme macro system attaches location information to syntax, which you can manipulate using macros. If your macro-fu is not strong, there are some libraries that will help you out. To simply get the source directory and file name you can use procedures in the etc.ss collection in MzLib?:

> (require (lib "etc.ss"))
> (display (this-expression-source-directory))
/home/nhw/tmp/
> (display (this-expression-file-name))
#f

Notice that this-expression-file-name is #f in the example above. This is because I haven't saved the code to file. If I do so the output changes as we'd expect:

(display (this-expression-file-name)) ; display foo.ss, the file I saved the code in

Discussion

For more involved manipulation of location information the following module from SchemeUnit may help.

(module location mzscheme
                                                           
  (require (planet "aif.ss" ("schematics" "macro.plt" 1))
           (lib "list.ss" "srfi" "1"))
                                                           
  (provide location-source
           location-line
           location-column
           location-position
           location-span
           syntax->location
           location->string)
                                                           
  ;; type location = (list string string string string string)
  ;; location : source line column position span
                                                           
  (define location-source first)
  (define location-line second)
  (define location-column third)
  (define location-position fourth)
  (define location-span fifth)
                                                                              
  ;; syntax->location : syntax -> location
  (define (syntax->location stx)
    (define (source->string source)
      (cond
       ((string? source) source)
       ((path? source) (path->string source))
       ((not source) "unknown")
       (else (format "~a" source))))
    (define (maybe-number->string number)
      (if (number? number)
          (number->string number)
          "?"))
    (list (source->string (syntax-source stx))
          (maybe-number->string (syntax-line stx))
          (maybe-number->string (syntax-column stx))
          (maybe-number->string (syntax-position stx))
          (maybe-number->string (syntax-span stx))))
                                                           
  ;; location->string : (list-of string) -> string
  (define (location->string location)
    (string-append (location-source location)
                   ":"
                   (location-line location)
                   ":"
                   (location-column location)))
  )


Comments about this recipe

Contributors

-- NoelWelsh - 18 Apr 2006

CookbookForm
TopicType: Recipe
ParentTopic: IdiomRecipes
TopicOrder: 999

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest