Fork me on GitHub
#rewrite-clj
<
2019-11-12
>
sogaiu00:11:16

am trying to get at the name for an ns form, and atm, i'm doing something like this:

(defn ns?
  [zloc]
  (when (rz/list? zloc)
    (when-let [head (rz/down zloc)]
      (let [tag (rz/tag head)
            find-ns-name (fn [start-zloc]
                           (let [rzloc (rz/right start-zloc)
                                 r-tag (rz/tag rzloc)]
                             (cond (= r-tag :token)
                                   rzloc
                                   ;;
                                   (= r-tag :meta)
                                   (-> rzloc
                                     rz/down
                                     rz/right)
                                   ;;
                                   :else
                                   nil)))]
        (cond (= tag :token)
              (when (= (rz/string head) "ns")
                (find-ns-name head))
              ;;
              (= tag :meta)
              (let [target (-> head
                             rz/down
                             rz/right)]
                (when (= (rz/string target) "ns")
                  (find-ns-name head)))
              ;;
              :else
              nil)))))

sogaiu00:11:36

the metadata being the container for things seems to make taversal awkward

๐Ÿ‘ 4
lread03:11:06

thanks, I will capture this is my notes!

sogaiu02:11:07

borkdude was talking with dominicm briefly about partial parsing for edamame -- does that sound like something that's remotely doable in rewrite-clj* at some point?

lread03:11:39

I donโ€™t know @sogaiu, I have not thought about that yet

sogaiu03:11:14

thanks -- just putting something out for consideration ๐Ÿ™‚ i was hoping that may be there's a chance someone else has figured out how to deal with not well-formed stuff...

sogaiu04:11:24

@lee saw the nice logo for the first time :thumbsup:

sogaiu22:11:50

does it seem possible / practical for there to be a mode of operation where, when rewrite-clj* encounters a situation where it is expecting a closing delimiter but doesn't find one to fake one being there? possibly then subsequently ignoring anything else it was asked to read, then try to close as many delimiters as necessary to complete parsing? in a slightly more general formulation: upon encountering some classes of errors (e.g. missing delimiter), is it possible / practical to just try to make sense of whatever has been encountered so far while ignoring everything else that would follow?

borkdude23:11:42

@sogaiu I think that is technically possible. Clj-kondo has a fork of tools.reader which warns about which opening paren was expected to close, e.g. [), so it could have just pretended to read [] instead

borkdude23:11:12

but instead of that it throws an error with a better error message than tools.reader currently throws

borkdude23:11:39

$ clj-kondo --lint - <<< '[)'
<stdin>:1:1: error: Mismatched bracket: found an opening [ and a closing ) on line 1
<stdin>:1:2: error: Mismatched bracket: found an opening [ on line 1 and a closing )
linting took 23ms, errors: 2, warnings: 0

borkdude23:11:29

as you can see, it also spits out two errors, so you get squiggles in two places

borkdude23:11:09

What is your use case for this?

sogaiu23:11:33

@borkdude i am writing an external tool that among other things tries to "detect the expression before cursor / point". suppose i initially write this code in an editor buffer:

(comment

  (-> (rz/of-string "(+ 1 1)")
    rz/down
    rz/right
    rz/string)

...
)
at some point i want to take a look at something a bit different:
(comment

  (-> (rz/of-string "(+ 1 1)")
    rz/down
    rz/tag) ; request detection of expression here so it can be sent to repl

    rz/right
    rz/string)

...
)
i'd like to be able to send the text from the beginning of the file up through "rz/tag)" to detect:
(-> (rz/of-string "(+ 1 1)")
  rz/down
  rz/tag)
right now all of the text of the buffer is sent and it is not successfully read/parsed.

borkdude23:11:35

good luck. sleep time

sogaiu23:11:44

thanks ๐Ÿ™‚