From 640f3d53e3b5a2eb0a13f25e34eedad697ae98b3 Mon Sep 17 00:00:00 2001 From: Camilo Roca Date: Wed, 20 Nov 2019 23:04:32 +0100 Subject: [PATCH 1/3] fix: wrong token column end computed --- project.clj | 2 +- src/clojure/parcera/antlr/java.clj | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/project.clj b/project.clj index 567d2df..b3176de 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject carocad/parcera "0.4.2" +(defproject carocad/parcera "0.4.3" :description "Grammar-based Clojure parser" :url "https://github.com/carocad/parcera" :license {:name "LGPLv3" diff --git a/src/clojure/parcera/antlr/java.clj b/src/clojure/parcera/antlr/java.clj index 525038d..b288fe8 100644 --- a/src/clojure/parcera/antlr/java.clj +++ b/src/clojure/parcera/antlr/java.clj @@ -52,11 +52,12 @@ stop (.getStop this)] (cond ;; happens when the parser rule is a single lexer rule - (= start stop) + (identical? start stop) {::start {:row (.getLine start) :column (.getCharPositionInLine start)} ::end {:row (.getLine start) - :column (.getStopIndex start)}} + :column (Math/addExact (.getCharPositionInLine start) + (.length (.getText start)))}} ;; no end found - happens on errors (nil? stop) @@ -67,7 +68,8 @@ {::start {:row (.getLine start) :column (.getCharPositionInLine start)} ::end {:row (.getLine stop) - :column (.getCharPositionInLine stop)}})))) + :column (Math/addExact (.getCharPositionInLine stop) + (.length (.getText stop)))}})))) (extend-type ErrorNodeImpl From 3a63a2ff151e79c4d447feeff885c96b4c67494a Mon Sep 17 00:00:00 2001 From: Camilo Roca Date: Wed, 20 Nov 2019 23:31:30 +0100 Subject: [PATCH 2/3] fix: invalid keywords used --- src/clojure/parcera/antlr/java.clj | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/clojure/parcera/antlr/java.clj b/src/clojure/parcera/antlr/java.clj index b288fe8..85c225a 100644 --- a/src/clojure/parcera/antlr/java.clj +++ b/src/clojure/parcera/antlr/java.clj @@ -53,31 +53,31 @@ (cond ;; happens when the parser rule is a single lexer rule (identical? start stop) - {::start {:row (.getLine start) - :column (.getCharPositionInLine start)} - ::end {:row (.getLine start) - :column (Math/addExact (.getCharPositionInLine start) - (.length (.getText start)))}} + {: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 (Math/addExact (.getCharPositionInLine stop) - (.length (.getText 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 From bda14949c714d6f18270f542357d816a355b492e Mon Sep 17 00:00:00 2001 From: Camilo Roca Date: Wed, 20 Nov 2019 23:31:46 +0100 Subject: [PATCH 3/3] more tests added version bumped --- project.clj | 2 +- test/parcera/test/core.cljc | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/project.clj b/project.clj index b3176de..9a0e304 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject carocad/parcera "0.4.3" +(defproject carocad/parcera "0.4.4" :description "Grammar-based Clojure parser" :url "https://github.com/carocad/parcera" :license {:name "LGPLv3" diff --git a/test/parcera/test/core.cljc b/test/parcera/test/core.cljc index 079828c..06c025a 100644 --- a/test/parcera/test/core.cljc +++ b/test/parcera/test/core.cljc @@ -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