Skip to content

Commit

Permalink
Merge pull request #29 from carocad/fix/token-length
Browse files Browse the repository at this point in the history
metadata fixes
  • Loading branch information
carocad committed Nov 21, 2019
2 parents de2abd4 + bda1494 commit 8877dc3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject carocad/parcera "0.4.2"
(defproject carocad/parcera "0.4.4"
:description "Grammar-based Clojure parser"
:url "https://github.com/carocad/parcera"
:license {:name "LGPLv3"
Expand Down
28 changes: 15 additions & 13 deletions src/clojure/parcera/antlr/java.clj
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,32 @@
stop (.getStop this)]
(cond
;; happens when the parser rule is a single lexer rule
(= start stop)
{::start {:row (.getLine start)
:column (.getCharPositionInLine start)}
::end {:row (.getLine start)
:column (.getStopIndex start)}}
(identical? start stop)
{:parcera.core/start {:row (.getLine start)
:column (.getCharPositionInLine start)}
:parcera.core/end {:row (.getLine start)
:column (Math/addExact (.getCharPositionInLine start)
(.length (.getText start)))}}

;; no end found - happens on errors
(nil? stop)
{::start {:row (.getLine start)
:column (.getCharPositionInLine start)}}
{:parcera.core/start {:row (.getLine start)
:column (.getCharPositionInLine start)}}

:else
{::start {:row (.getLine start)
:column (.getCharPositionInLine start)}
::end {:row (.getLine stop)
:column (.getCharPositionInLine stop)}}))))
{:parcera.core/start {:row (.getLine start)
:column (.getCharPositionInLine start)}
:parcera.core/end {:row (.getLine stop)
:column (Math/addExact (.getCharPositionInLine stop)
(.length (.getText stop)))}}))))


(extend-type ErrorNodeImpl
antlr/LocationInfo
(span [^ErrorNodeImpl this]
(let [token (.-symbol this)]
{::start {:row (.getLine token)
:column (.getCharPositionInLine token)}})))
{:parcera.core/start {:row (.getLine token)
:column (.getCharPositionInLine token)}})))


(extend-type ClojureParser
Expand Down
20 changes: 19 additions & 1 deletion test/parcera/test/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,25 @@
;(is (clear input))))
(as-> "\\ϕ" input (and (is (valid? input))
(is (roundtrip input))))))
;(is (clear input))))))


(deftest metadata
(testing "simple definitions"
(let [input ":bar"
ast (parcera/ast input)
location (meta (second ast))]
(is (= (:row (::parcera/start location)) 1))
(is (= (:column (::parcera/start location)) 0))
(is (= (:row (::parcera/start location)) 1))
(is (= (:column (::parcera/end location))
(count input)))))

(testing "syntax error"
(let [input "hello/world/"
ast (parcera/ast input)
location (meta (second ast))]
(is (parcera/failure? ast))
(is (some? (:message (::parcera/start location)))))))


(deftest data-structures
Expand Down

0 comments on commit 8877dc3

Please sign in to comment.