HS: sourceInfo API (sourceFor / lineFor / node-get)
This commit is contained in:
@@ -2525,3 +2525,57 @@
|
|||||||
(fn
|
(fn
|
||||||
(fn-name args)
|
(fn-name args)
|
||||||
(let ((fn (host-global fn-name))) (if fn (host-call-fn fn args) nil))))
|
(let ((fn (host-global fn-name))) (if fn (host-call-fn fn args) nil))))
|
||||||
|
|
||||||
|
;; ── SourceInfo API ────────────────────────────────────────────────
|
||||||
|
|
||||||
|
(define
|
||||||
|
hs-source-for
|
||||||
|
(fn
|
||||||
|
(node)
|
||||||
|
(substring (get node :src) (get node :start) (get node :end))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
hs-line-for
|
||||||
|
(fn
|
||||||
|
(node)
|
||||||
|
(let
|
||||||
|
((lines (split (get node :src) "\n"))
|
||||||
|
(line-idx (- (get node :line) 1)))
|
||||||
|
(if (< line-idx (len lines)) (nth lines line-idx) ""))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
hs-node-get
|
||||||
|
(fn
|
||||||
|
(node key)
|
||||||
|
(get (get node :fields) key)))
|
||||||
|
|
||||||
|
(define
|
||||||
|
hs-src
|
||||||
|
(fn (src-str)
|
||||||
|
(hs-source-for (hs-parse-ast src-str))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
hs-src-at
|
||||||
|
(fn
|
||||||
|
(src-str path)
|
||||||
|
(define
|
||||||
|
walk
|
||||||
|
(fn
|
||||||
|
(node keys)
|
||||||
|
(if (or (nil? keys) (= (len keys) 0))
|
||||||
|
node
|
||||||
|
(walk (hs-node-get node (first keys)) (rest keys)))))
|
||||||
|
(hs-source-for (walk (hs-parse-ast src-str) path))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
hs-line-at
|
||||||
|
(fn
|
||||||
|
(src-str path)
|
||||||
|
(define
|
||||||
|
walk
|
||||||
|
(fn
|
||||||
|
(node keys)
|
||||||
|
(if (or (nil? keys) (= (len keys) 0))
|
||||||
|
node
|
||||||
|
(walk (hs-node-get node (first keys)) (rest keys)))))
|
||||||
|
(hs-line-for (walk (hs-parse-ast src-str) path))))
|
||||||
|
|||||||
@@ -2525,3 +2525,57 @@
|
|||||||
(fn
|
(fn
|
||||||
(fn-name args)
|
(fn-name args)
|
||||||
(let ((fn (host-global fn-name))) (if fn (host-call-fn fn args) nil))))
|
(let ((fn (host-global fn-name))) (if fn (host-call-fn fn args) nil))))
|
||||||
|
|
||||||
|
;; ── SourceInfo API ────────────────────────────────────────────────
|
||||||
|
|
||||||
|
(define
|
||||||
|
hs-source-for
|
||||||
|
(fn
|
||||||
|
(node)
|
||||||
|
(substring (get node :src) (get node :start) (get node :end))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
hs-line-for
|
||||||
|
(fn
|
||||||
|
(node)
|
||||||
|
(let
|
||||||
|
((lines (split (get node :src) "\n"))
|
||||||
|
(line-idx (- (get node :line) 1)))
|
||||||
|
(if (< line-idx (len lines)) (nth lines line-idx) ""))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
hs-node-get
|
||||||
|
(fn
|
||||||
|
(node key)
|
||||||
|
(get (get node :fields) key)))
|
||||||
|
|
||||||
|
(define
|
||||||
|
hs-src
|
||||||
|
(fn (src-str)
|
||||||
|
(hs-source-for (hs-parse-ast src-str))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
hs-src-at
|
||||||
|
(fn
|
||||||
|
(src-str path)
|
||||||
|
(define
|
||||||
|
walk
|
||||||
|
(fn
|
||||||
|
(node keys)
|
||||||
|
(if (or (nil? keys) (= (len keys) 0))
|
||||||
|
node
|
||||||
|
(walk (hs-node-get node (first keys)) (rest keys)))))
|
||||||
|
(hs-source-for (walk (hs-parse-ast src-str) path))))
|
||||||
|
|
||||||
|
(define
|
||||||
|
hs-line-at
|
||||||
|
(fn
|
||||||
|
(src-str path)
|
||||||
|
(define
|
||||||
|
walk
|
||||||
|
(fn
|
||||||
|
(node keys)
|
||||||
|
(if (or (nil? keys) (= (len keys) 0))
|
||||||
|
node
|
||||||
|
(walk (hs-node-get node (first keys)) (rest keys)))))
|
||||||
|
(hs-line-for (walk (hs-parse-ast src-str) path))))
|
||||||
|
|||||||
Reference in New Issue
Block a user