Uploaded image for project: 'tools.reader'
  1. TRDR-42

Allow wrapping constants so their location can be accessed

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Declined
    • Labels:
      None

      Description

      Currently the reader will lose all source information (line, column, end-line, end-column, file) on values that don't support metadata.

      This change allows (optionally) wrapping everything that doesn't support metadata in the Constant record.

      This would allow the CLJS analyzer to show correct locations that it currently cannot do in some circumstances.

      (+ 1 "2")
      

      The location of "2" is unknown and thus the location of the error is that of the +.

      The default behaviour is unchanged.

      (require '[clojure.tools.reader :as rdr])
      (require '[clojure.tools.reader.reader-types :as rdrt])
      
      (let [rdr (-> (rdrt/string-reader "[1 \"2\" nil true false 1.01])")
                    (rdrt/indexing-push-back-reader))
            x (rdr/read {} rdr)]
        (binding [*print-meta* true]
          (prn x)))
      

      Result:

      ^{:line 1, :column 1, :end-line 1, :end-column 28} [1 "2" nil true false 1.01]
      

      With wrapping enabled you'd get:

      (binding [rdr/*wrap-constants* true]
        (let [rdr (-> (rdrt/string-reader "[1 \"2\" nil true false 1.01])")
                      (rdrt/indexing-push-back-reader))
              x (rdr/read {} rdr)]
          (binding [*print-meta* true]
            (prn x))))
      

      Result:

      ^{:line 1, :column 1, :end-line 1, :end-column 28} [#clojure.tools.reader.Constant{:loc-info {:line 1, :column 2, :end-line 1, :end-column 3}, :value 1} #clojure.tools.reader.Constant{:loc-info {:line 1, :column 4, :end-line 1, :end-column 7}, :value "2"} #clojure.tools.reader.Constant{:loc-info {:line 1, :column 8, :end-line 1, :end-column 11}, :value nil} #clojure.tools.reader.Constant{:loc-info {:line 1, :column 12, :end-line 1, :end-column 16}, :value true} #clojure.tools.reader.Constant{:loc-info {:line 1, :column 17, :end-line 1, :end-column 22}, :value false} #clojure.tools.reader.Constant{:loc-info {:line 1, :column 23, :end-line 1, :end-column 27}, :value 1.01}]
      

      The loc-info property of the Constant record could be moved to the metadata of the record.

      The CLJS analyzer could easily unwrap this and extract the correct location info to merge that into the AST.

        Attachments

          Activity

            People

            • Assignee:
              bronsa Nicola Mometto
              Reporter:
              thheller Thomas Heller
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: