plt/location.ss
;;;
;;; Time-stamp: <06/01/27 21:26:17 noel>
;;;
;;; Copyright (C) 2005 by Noel Welsh.
;;;

;;; This library is free software; you can redistribute it
;;; and/or modify it under the terms of the GNU Lesser
;;; General Public License as published by the Free Software
;;; Foundation; either version 2.1 of the License, or (at
;;; your option) any later version.

;;; This library is distributed in the hope that it will be
;;; useful, but WITHOUT ANY WARRANTY; without even the
;;; implied warranty of MERCHANTABILITY or FITNESS FOR A
;;; PARTICULAR PURPOSE.  See the GNU Lesser General Public
;;; License for more details.

;;; You should have received a copy of the GNU Lesser
;;; General Public License along with this library; if not,
;;; write to the Free Software Foundation, Inc., 59 Temple
;;; Place, Suite 330, Boston, MA 02111-1307 USA

;;; Author: Noel Welsh <noelwelsh@yahoo.com>
;;
;;
;; Commentary:

(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)))

  )