Skip to content

Instantly share code, notes, and snippets.

@shaunlebron
Created December 15, 2018 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaunlebron/80eff44e2140708d9d33fd95a0547662 to your computer and use it in GitHub Desktop.
Save shaunlebron/80eff44e2140708d9d33fd95a0547662 to your computer and use it in GitHub Desktop.
parindent on instaparse
diff --git a/runner/cljs/runner/runner.cljs b/runner/cljs/runner/runner.cljs
index e1b2f97..74bc9f6 100644
--- a/runner/cljs/runner/runner.cljs
+++ b/runner/cljs/runner/runner.cljs
@@ -1,13 +1,13 @@
(ns instaparse.runner.runner
(:require [cljs.nodejs :as nodejs]
- [instaparse.abnf-test]
- [instaparse.auto-flatten-seq-test]
- [instaparse.core-test]
- [instaparse.defparser-test]
- [instaparse.grammars]
- [instaparse.repeat-test]
- [instaparse.specs]
- [cljs.test :as test :refer-macros [run-tests]]))
+ [instaparse.abnf-test]
+ [instaparse.auto-flatten-seq-test]
+ [instaparse.core-test]
+ [instaparse.defparser-test]
+ [instaparse.grammars]
+ [instaparse.repeat-test]
+ [instaparse.specs]
+ [cljs.test :as test :refer-macros [run-tests]]))
(nodejs/enable-util-print!)
diff --git a/src/instaparse/abnf.cljc b/src/instaparse/abnf.cljc
index a740413..3838f35 100644
--- a/src/instaparse/abnf.cljc
+++ b/src/instaparse/abnf.cljc
@@ -2,15 +2,15 @@
"This is the context free grammar that recognizes ABNF notation."
(:refer-clojure :exclude [cat])
(:require [instaparse.transform :as t]
- [instaparse.cfg :as cfg]
- [instaparse.gll :as gll]
- [instaparse.reduction :as red]
- [instaparse.util :refer [throw-runtime-exception]]
- [instaparse.combinators-source :refer
- [Epsilon opt plus star rep alt ord cat string-ci string
- string-ci regexp nt look neg hide hide-tag unicode-char]]
- #?(:cljs [goog.string.format])
- [clojure.walk :as walk])
+ [instaparse.cfg :as cfg]
+ [instaparse.gll :as gll]
+ [instaparse.reduction :as red]
+ [instaparse.util :refer [throw-runtime-exception]]
+ [instaparse.combinators-source :refer
+ [Epsilon opt plus star rep alt ord cat string-ci string
+ string-ci regexp nt look neg hide hide-tag unicode-char]]
+ #?(:cljs [goog.string.format])
+ [clojure.walk :as walk])
#?(:cljs (:require-macros [instaparse.abnf :refer [precompile-cljs-grammar]])))
(def ^:dynamic *case-insensitive*
@@ -204,17 +204,17 @@ regexp = #\"#'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'\"
:repetition (fn
([repeat element]
- (cond
- (empty? repeat) (star element)
- (= (count repeat) 2) (rep (:low repeat) (:high repeat) element)
- (= (:low repeat) 1) (plus element)
- (= (:high repeat) 1) (opt element)
- :else (rep (or (:low repeat) 0)
- (or (:high repeat) #?(:clj Double/POSITIVE_INFINITY
- :cljs js/Infinity))
- element)))
+ (cond
+ (empty? repeat) (star element)
+ (= (count repeat) 2) (rep (:low repeat) (:high repeat) element)
+ (= (:low repeat) 1) (plus element)
+ (= (:high repeat) 1) (opt element)
+ :else (rep (or (:low repeat) 0)
+ (or (:high repeat) #?(:clj Double/POSITIVE_INFINITY
+ :cljs js/Infinity))
+ element)))
([element]
- element))
+ element))
:option opt
:hide hide
:look look
diff --git a/src/instaparse/auto_flatten_seq.cljc b/src/instaparse/auto_flatten_seq.cljc
index 7de638c..14b0bd9 100644
--- a/src/instaparse/auto_flatten_seq.cljc
+++ b/src/instaparse/auto_flatten_seq.cljc
@@ -78,164 +78,164 @@
(flat-seq v (delve v [0]))
nil))
([v index]
- (lazy-seq
- (cons (get-in v index)
- (when-let [next-index (advance v index)]
- (flat-seq v next-index))))))
+ (lazy-seq
+ (cons (get-in v index)
+ (when-let [next-index (advance v index)]
+ (flat-seq v next-index))))))
#?(:clj
-(deftype AutoFlattenSeq [^PersistentVector v ^int premix-hashcode ^int hashcode
- ^int cnt ^boolean dirty
- ^:unsynchronized-mutable ^clojure.lang.ISeq cached-seq]
- Object
- (toString [self] (.toString (seq self)))
- (hashCode [self] hashcode)
- (equals [self other]
- (and (instance? AutoFlattenSeq other)
- (== hashcode (.hashcode ^AutoFlattenSeq other))
- (== cnt (.cnt ^AutoFlattenSeq other))
- (= dirty (.dirty ^AutoFlattenSeq other))
- (= v (.v ^AutoFlattenSeq other))))
- clojure.lang.IHashEq
- (hasheq [self] hashcode)
- java.util.Collection
- (iterator [self]
- (if-let [^java.util.Collection s (seq self)]
- (.iterator s)
- (let [^java.util.Collection e ()]
- (.iterator e))))
- (size [self]
- cnt)
- (toArray [self]
- (let [^java.util.Collection s (seq self)]
- (.toArray s)))
- clojure.lang.Sequential
- clojure.lang.ISeq
- (equiv [self other]
- (and (== hashcode (hash other))
- (== cnt (count other))
- (or (== cnt 0)
- (= (seq self) other))))
- (empty [self] (with-meta EMPTY (meta self)))
- (first [self] (first (seq self)))
- (next [self] (next (seq self)))
- (more [self] (rest (seq self)))
- (cons [self obj]
- (cons obj self))
- ConjFlat
- (conj-flat [self obj]
- (cond
- (nil? obj) self
- (afs? obj)
- (cond
- (zero? cnt) obj
- (<= (count obj) threshold)
- (let [phc (hash-cat self obj)
- new-cnt (+ cnt (count obj))]
- (AutoFlattenSeq. (into v obj) phc (mix-collection-hash-bc phc new-cnt) new-cnt
- (or dirty (.dirty ^AutoFlattenSeq obj)) nil))
- :else
- (let [phc (hash-cat self obj)
- new-cnt (+ cnt (count obj))]
- (AutoFlattenSeq. (conj v obj) phc (mix-collection-hash-bc phc new-cnt) new-cnt
- true nil)))
- :else
- (let [phc (hash-conj premix-hashcode obj)
- new-cnt (inc cnt)]
- (AutoFlattenSeq. (conj v obj) phc (mix-collection-hash-bc phc new-cnt) new-cnt dirty nil))))
- (cached? [self] cached-seq)
- clojure.lang.Counted
- (count [self] cnt)
- clojure.lang.ILookup
- (valAt [self key]
- (.valAt v key))
- (valAt [self key not-found]
- (.valAt v key not-found))
- clojure.lang.IObj
- (withMeta [self metamap]
- (AutoFlattenSeq. (with-meta v metamap) premix-hashcode hashcode cnt dirty nil))
- clojure.lang.IMeta
- (meta [self]
- (meta v))
- clojure.lang.Seqable
- (seq [self]
- (if cached-seq cached-seq
- (do
- (set! cached-seq (if dirty (flat-seq v) (seq v)))
- cached-seq))))
-:cljs
-(deftype AutoFlattenSeq [^PersistentVector v ^number premix-hashcode ^number hashcode ^number cnt ^boolean dirty
- ^:unsynchronized-mutable ^ISeq cached-seq]
- Object
- (toString [self] (pr-str* (seq self)))
- IHash
- (-hash [self] hashcode)
- ISequential
- ISeq
- (-first [self] (first (seq self)))
- (-rest [self] (rest (seq self)))
- IEquiv
- (-equiv [self other]
- (and ;(instance? AutoFlattenSeq other)
+ (deftype AutoFlattenSeq [^PersistentVector v ^int premix-hashcode ^int hashcode
+ ^int cnt ^boolean dirty
+ ^:unsynchronized-mutable ^clojure.lang.ISeq cached-seq]
+ Object
+ (toString [self] (.toString (seq self)))
+ (hashCode [self] hashcode)
+ (equals [self other]
+ (and (instance? AutoFlattenSeq other)
+ (== hashcode (.hashcode ^AutoFlattenSeq other))
+ (== cnt (.cnt ^AutoFlattenSeq other))
+ (= dirty (.dirty ^AutoFlattenSeq other))
+ (= v (.v ^AutoFlattenSeq other))))
+ clojure.lang.IHashEq
+ (hasheq [self] hashcode)
+ java.util.Collection
+ (iterator [self]
+ (if-let [^java.util.Collection s (seq self)]
+ (.iterator s)
+ (let [^java.util.Collection e ()]
+ (.iterator e))))
+ (size [self]
+ cnt)
+ (toArray [self]
+ (let [^java.util.Collection s (seq self)]
+ (.toArray s)))
+ clojure.lang.Sequential
+ clojure.lang.ISeq
+ (equiv [self other]
+ (and (== hashcode (hash other))
+ (== cnt (count other))
+ (or (== cnt 0)
+ (= (seq self) other))))
+ (empty [self] (with-meta EMPTY (meta self)))
+ (first [self] (first (seq self)))
+ (next [self] (next (seq self)))
+ (more [self] (rest (seq self)))
+ (cons [self obj]
+ (cons obj self))
+ ConjFlat
+ (conj-flat [self obj]
+ (cond
+ (nil? obj) self
+ (afs? obj)
+ (cond
+ (zero? cnt) obj
+ (<= (count obj) threshold)
+ (let [phc (hash-cat self obj)
+ new-cnt (+ cnt (count obj))]
+ (AutoFlattenSeq. (into v obj) phc (mix-collection-hash-bc phc new-cnt) new-cnt
+ (or dirty (.dirty ^AutoFlattenSeq obj)) nil))
+ :else
+ (let [phc (hash-cat self obj)
+ new-cnt (+ cnt (count obj))]
+ (AutoFlattenSeq. (conj v obj) phc (mix-collection-hash-bc phc new-cnt) new-cnt
+ true nil)))
+ :else
+ (let [phc (hash-conj premix-hashcode obj)
+ new-cnt (inc cnt)]
+ (AutoFlattenSeq. (conj v obj) phc (mix-collection-hash-bc phc new-cnt) new-cnt dirty nil))))
+ (cached? [self] cached-seq)
+ clojure.lang.Counted
+ (count [self] cnt)
+ clojure.lang.ILookup
+ (valAt [self key]
+ (.valAt v key))
+ (valAt [self key not-found]
+ (.valAt v key not-found))
+ clojure.lang.IObj
+ (withMeta [self metamap]
+ (AutoFlattenSeq. (with-meta v metamap) premix-hashcode hashcode cnt dirty nil))
+ clojure.lang.IMeta
+ (meta [self]
+ (meta v))
+ clojure.lang.Seqable
+ (seq [self]
+ (if cached-seq cached-seq
+ (do
+ (set! cached-seq (if dirty (flat-seq v) (seq v)))
+ cached-seq))))
+ :cljs
+ (deftype AutoFlattenSeq [^PersistentVector v ^number premix-hashcode ^number hashcode ^number cnt ^boolean dirty
+ ^:unsynchronized-mutable ^ISeq cached-seq]
+ Object
+ (toString [self] (pr-str* (seq self)))
+ IHash
+ (-hash [self] hashcode)
+ ISequential
+ ISeq
+ (-first [self] (first (seq self)))
+ (-rest [self] (rest (seq self)))
+ IEquiv
+ (-equiv [self other]
+ (and ;(instance? AutoFlattenSeq other)
(= hashcode (hash other))
(= cnt (count other))
(or (= cnt 0)
(= (seq self) other))))
- ICollection
- (-conj [self o] (cons o self))
- IEmptyableCollection
- (-empty [self] (with-meta EMPTY (meta self)))
- INext
- (-next [self] (next (seq self)))
- ConjFlat
- (conj-flat [self obj]
- (cond
- (nil? obj) self
- (afs? obj)
- (cond
- (zero? cnt) obj
- (<= (count obj) threshold)
- (let [phc (hash-cat self obj)
- new-cnt (+ cnt (count obj))]
- (AutoFlattenSeq. (into v obj) phc (mix-collection-hash phc new-cnt) new-cnt
- (or dirty (.-dirty ^AutoFlattenSeq obj)) nil))
- :else
- (let [phc (hash-cat self obj)
- new-cnt (+ cnt (count obj))]
- (AutoFlattenSeq. (conj v obj) phc (mix-collection-hash phc new-cnt) new-cnt
- true nil)))
- :else
- (let [phc (hash-conj premix-hashcode obj)
- new-cnt (inc cnt)]
- (AutoFlattenSeq. (conj v obj) phc (mix-collection-hash phc new-cnt) new-cnt dirty nil))))
- (cached? [self] cached-seq)
- ICounted
- (-count [self] cnt)
- ILookup
- (-lookup [self key]
- (-lookup v key))
- (-lookup [self key not-found]
- (-lookup v key not-found))
- IWithMeta
- (-with-meta [self metamap]
- (AutoFlattenSeq. (with-meta v metamap) premix-hashcode hashcode cnt dirty nil))
- IMeta
- (-meta [self]
- (meta v))
- ISeqable
- (-seq [self]
- (if cached-seq cached-seq
- (do
- (set! cached-seq (if dirty (flat-seq v) (seq v)))
- cached-seq)))))
+ ICollection
+ (-conj [self o] (cons o self))
+ IEmptyableCollection
+ (-empty [self] (with-meta EMPTY (meta self)))
+ INext
+ (-next [self] (next (seq self)))
+ ConjFlat
+ (conj-flat [self obj]
+ (cond
+ (nil? obj) self
+ (afs? obj)
+ (cond
+ (zero? cnt) obj
+ (<= (count obj) threshold)
+ (let [phc (hash-cat self obj)
+ new-cnt (+ cnt (count obj))]
+ (AutoFlattenSeq. (into v obj) phc (mix-collection-hash phc new-cnt) new-cnt
+ (or dirty (.-dirty ^AutoFlattenSeq obj)) nil))
+ :else
+ (let [phc (hash-cat self obj)
+ new-cnt (+ cnt (count obj))]
+ (AutoFlattenSeq. (conj v obj) phc (mix-collection-hash phc new-cnt) new-cnt
+ true nil)))
+ :else
+ (let [phc (hash-conj premix-hashcode obj)
+ new-cnt (inc cnt)]
+ (AutoFlattenSeq. (conj v obj) phc (mix-collection-hash phc new-cnt) new-cnt dirty nil))))
+ (cached? [self] cached-seq)
+ ICounted
+ (-count [self] cnt)
+ ILookup
+ (-lookup [self key]
+ (-lookup v key))
+ (-lookup [self key not-found]
+ (-lookup v key not-found))
+ IWithMeta
+ (-with-meta [self metamap]
+ (AutoFlattenSeq. (with-meta v metamap) premix-hashcode hashcode cnt dirty nil))
+ IMeta
+ (-meta [self]
+ (meta v))
+ ISeqable
+ (-seq [self]
+ (if cached-seq cached-seq
+ (do
+ (set! cached-seq (if dirty (flat-seq v) (seq v)))
+ cached-seq)))))
#?(:clj
(defn- hash-cat ^long [^AutoFlattenSeq v1 ^AutoFlattenSeq v2]
(let [c (count v2)
e (int (expt 31 c))]
(unchecked-add-int
- (unchecked-multiply-int e (.premix-hashcode v1))
- (unchecked-subtract-int (.premix-hashcode v2) e))))
+ (unchecked-multiply-int e (.premix-hashcode v1))
+ (unchecked-subtract-int (.premix-hashcode v2) e))))
:cljs
(defn- hash-cat ^number [^AutoFlattenSeq v1 ^AutoFlattenSeq v2]
(let [c (count v2)
@@ -251,8 +251,8 @@
(loop [acc (int 1) i (int 0)]
(if (< i cnt)
(recur (unchecked-add-int
- (unchecked-multiply-int thirty-one acc)
- (hash (v i)))
+ (unchecked-multiply-int thirty-one acc)
+ (hash (v i)))
(inc i))
acc)))
(hash v)))
@@ -311,195 +311,195 @@
(^PersistentVector get-vec [self]))
#?(:clj
-(deftype FlattenOnDemandVector [v ; ref containing PersistentVector or nil
- ^int hashcode
- ^int cnt
- flat] ; ref containing PersistentVector or nil
- GetVec
- (get-vec [self]
- (when (not @flat)
- (dosync
- (when (not @flat)
- (ref-set flat (with-meta (flat-vec @v) (meta @v)))
- (ref-set v nil)))) ; clear out v so it can be garbage collected
- @flat)
+ (deftype FlattenOnDemandVector [v ; ref containing PersistentVector or nil
+ ^int hashcode
+ ^int cnt
+ flat] ; ref containing PersistentVector or nil
+ GetVec
+ (get-vec [self]
+ (when (not @flat)
+ (dosync
+ (when (not @flat)
+ (ref-set flat (with-meta (flat-vec @v) (meta @v)))
+ (ref-set v nil)))) ; clear out v so it can be garbage collected
+ @flat)
- Object
- (toString [self] (.toString (get-vec self)))
- (hashCode [self] hashcode)
- (equals [self other]
- (and (instance? FlattenOnDemandVector other)
- (== hashcode (.hashcode ^FlattenOnDemandVector other))
- (== cnt (.cnt ^FlattenOnDemandVector other))
- (= v (.v ^FlattenOnDemandVector other))
- (= flat (.flat ^FlattenOnDemandVector other))))
- clojure.lang.IHashEq
- (hasheq [self] hashcode)
- java.util.Collection
- (iterator [self]
- (.iterator (get-vec self)))
- (size [self]
- cnt)
- (toArray [self]
- (.toArray (get-vec self)))
- clojure.lang.IPersistentCollection
- (equiv [self other]
- (or
- (and (== hashcode (hash other))
- (== cnt (count other))
- (= (get-vec self) other))))
- (empty [self] (with-meta [] (meta self)))
- clojure.lang.Counted
- (count [self] cnt)
- clojure.lang.IPersistentVector
- (assoc [self i val]
- (assoc (get-vec self) i val))
- (assocN [self i val]
- (.assocN (get-vec self) i val))
- (length [self]
- cnt)
- (cons [self obj]
- (conj (get-vec self) obj))
- clojure.lang.IObj
- (withMeta [self metamap]
- (if @flat
- (FlattenOnDemandVector. (ref @v) hashcode cnt (ref (with-meta @flat metamap)))
- (FlattenOnDemandVector. (ref (with-meta @v metamap)) hashcode cnt (ref @flat))))
- clojure.lang.IMeta
- (meta [self]
- (if @flat (meta @flat) (meta @v)))
- clojure.lang.Seqable
- (seq [self]
- (seq (get-vec self)))
- clojure.lang.ILookup
- (valAt [self key]
- (.valAt (get-vec self) key))
- (valAt [self key not-found]
- (.valAt (get-vec self) key not-found))
- clojure.lang.Indexed
- (nth [self i]
- (.nth (get-vec self) i))
- (nth [self i not-found]
- (.nth (get-vec self) i not-found))
- clojure.lang.IFn
- (invoke [self arg]
- (.invoke (get-vec self) arg))
- (applyTo [self arglist]
- (.applyTo (get-vec self) arglist))
- clojure.lang.Reversible
- (rseq [self]
- (if (pos? cnt)
- (rseq (get-vec self))
- nil))
- clojure.lang.IPersistentStack
- (peek [self]
- (peek (get-vec self)))
- (pop [self]
- (pop (get-vec self)))
- clojure.lang.Associative
- (containsKey [self k]
- (.containsKey (get-vec self) k))
- (entryAt [self k]
- (.entryAt (get-vec self) k))
- IKVReduce
- (kv-reduce [self f init]
- (.kvreduce (get-vec self) f init))
- java.lang.Comparable
- (compareTo [self that]
- (.compareTo (get-vec self) that))
- java.util.List
- (get [self i] (nth (get-vec self) i))
- (indexOf [self o] (.indexOf (get-vec self) o))
- (lastIndexOf [self o] (.lastIndexOf (get-vec self) o))
- (listIterator [self]
- (.listIterator (get-vec self) 0))
- (listIterator [self i]
- (.listIterator (get-vec self) i))
- (subList [self a z]
- (.subList (get-vec self) a z))
- )
-:cljs
-(deftype FlattenOnDemandVector [v ; atom containing PersistentVector or nil
- ^number hashcode
- ^number cnt
- flat] ; atom containing PersistentVector or nil
- GetVec
- (get-vec [self]
- (when (not @flat)
- (swap! flat (fn [_] (with-meta (flat-vec @v) (meta @v))))
- (swap! v (fn [_] nil))) ; clear out v so it can be garbage collected
- @flat)
+ Object
+ (toString [self] (.toString (get-vec self)))
+ (hashCode [self] hashcode)
+ (equals [self other]
+ (and (instance? FlattenOnDemandVector other)
+ (== hashcode (.hashcode ^FlattenOnDemandVector other))
+ (== cnt (.cnt ^FlattenOnDemandVector other))
+ (= v (.v ^FlattenOnDemandVector other))
+ (= flat (.flat ^FlattenOnDemandVector other))))
+ clojure.lang.IHashEq
+ (hasheq [self] hashcode)
+ java.util.Collection
+ (iterator [self]
+ (.iterator (get-vec self)))
+ (size [self]
+ cnt)
+ (toArray [self]
+ (.toArray (get-vec self)))
+ clojure.lang.IPersistentCollection
+ (equiv [self other]
+ (or
+ (and (== hashcode (hash other))
+ (== cnt (count other))
+ (= (get-vec self) other))))
+ (empty [self] (with-meta [] (meta self)))
+ clojure.lang.Counted
+ (count [self] cnt)
+ clojure.lang.IPersistentVector
+ (assoc [self i val]
+ (assoc (get-vec self) i val))
+ (assocN [self i val]
+ (.assocN (get-vec self) i val))
+ (length [self]
+ cnt)
+ (cons [self obj]
+ (conj (get-vec self) obj))
+ clojure.lang.IObj
+ (withMeta [self metamap]
+ (if @flat
+ (FlattenOnDemandVector. (ref @v) hashcode cnt (ref (with-meta @flat metamap)))
+ (FlattenOnDemandVector. (ref (with-meta @v metamap)) hashcode cnt (ref @flat))))
+ clojure.lang.IMeta
+ (meta [self]
+ (if @flat (meta @flat) (meta @v)))
+ clojure.lang.Seqable
+ (seq [self]
+ (seq (get-vec self)))
+ clojure.lang.ILookup
+ (valAt [self key]
+ (.valAt (get-vec self) key))
+ (valAt [self key not-found]
+ (.valAt (get-vec self) key not-found))
+ clojure.lang.Indexed
+ (nth [self i]
+ (.nth (get-vec self) i))
+ (nth [self i not-found]
+ (.nth (get-vec self) i not-found))
+ clojure.lang.IFn
+ (invoke [self arg]
+ (.invoke (get-vec self) arg))
+ (applyTo [self arglist]
+ (.applyTo (get-vec self) arglist))
+ clojure.lang.Reversible
+ (rseq [self]
+ (if (pos? cnt)
+ (rseq (get-vec self))
+ nil))
+ clojure.lang.IPersistentStack
+ (peek [self]
+ (peek (get-vec self)))
+ (pop [self]
+ (pop (get-vec self)))
+ clojure.lang.Associative
+ (containsKey [self k]
+ (.containsKey (get-vec self) k))
+ (entryAt [self k]
+ (.entryAt (get-vec self) k))
+ IKVReduce
+ (kv-reduce [self f init]
+ (.kvreduce (get-vec self) f init))
+ java.lang.Comparable
+ (compareTo [self that]
+ (.compareTo (get-vec self) that))
+ java.util.List
+ (get [self i] (nth (get-vec self) i))
+ (indexOf [self o] (.indexOf (get-vec self) o))
+ (lastIndexOf [self o] (.lastIndexOf (get-vec self) o))
+ (listIterator [self]
+ (.listIterator (get-vec self) 0))
+ (listIterator [self i]
+ (.listIterator (get-vec self) i))
+ (subList [self a z]
+ (.subList (get-vec self) a z))
+ )
+ :cljs
+ (deftype FlattenOnDemandVector [v ; atom containing PersistentVector or nil
+ ^number hashcode
+ ^number cnt
+ flat] ; atom containing PersistentVector or nil
+ GetVec
+ (get-vec [self]
+ (when (not @flat)
+ (swap! flat (fn [_] (with-meta (flat-vec @v) (meta @v))))
+ (swap! v (fn [_] nil))) ; clear out v so it can be garbage collected
+ @flat)
- Object
- (toString [self]
- (pr-str* (get-vec self)))
- IHash
- (-hash [self] hashcode)
- IEquiv
- (-equiv [self other]
- (or
- (and (= hashcode (hash other))
- (= cnt (count other))
- (= (get-vec self) other))))
- IEmptyableCollection
- (-empty [self] (with-meta [] (meta self)))
- ICounted
- (-count [self] cnt)
- IVector
- (-assoc-n [self i val]
- (-assoc-n (get-vec self) i val))
- ICollection
- (-conj [self obj]
- (conj (get-vec self) obj))
- IWithMeta
- (-with-meta [self metamap]
- (if @flat
- (FlattenOnDemandVector. (atom @v) hashcode cnt (atom (with-meta @flat metamap)))
- (FlattenOnDemandVector. (atom (with-meta @v metamap)) hashcode cnt (atom @flat))))
- IMeta
- (-meta [self]
- (if @flat (meta @flat) (meta @v)))
- ISequential
- ISeqable
- (-seq [self]
- (seq (get-vec self)))
- ILookup
- (-lookup [self key]
- (-lookup (get-vec self) key))
- (-lookup [self key not-found]
- (-lookup (get-vec self) key not-found))
- IIndexed
- (-nth [self i]
- (-nth (get-vec self) i))
- (-nth [self i not-found]
- (-nth (get-vec self) i not-found))
- IFn
- (-invoke [self arg]
- (-invoke (get-vec self) arg))
- (-invoke [self arg not-found]
- (-invoke (get-vec self) arg not-found))
- IReversible
- (-rseq [self]
- (if (pos? cnt)
- (rseq (get-vec self))
- nil))
- IStack
- (-peek [self]
- (-peek (get-vec self)))
- (-pop [self]
- (-pop (get-vec self)))
- IAssociative
- (-assoc [self i val]
- (assoc (get-vec self) i val))
- (-contains-key? [self k]
- (-contains-key? (get-vec self) k))
- IKVReduce
- (-kv-reduce [self f init]
- (-kv-reduce (get-vec self) f init))
- IComparable
- (-compare [self that]
- (-compare (get-vec self) that))
- ))
+ Object
+ (toString [self]
+ (pr-str* (get-vec self)))
+ IHash
+ (-hash [self] hashcode)
+ IEquiv
+ (-equiv [self other]
+ (or
+ (and (= hashcode (hash other))
+ (= cnt (count other))
+ (= (get-vec self) other))))
+ IEmptyableCollection
+ (-empty [self] (with-meta [] (meta self)))
+ ICounted
+ (-count [self] cnt)
+ IVector
+ (-assoc-n [self i val]
+ (-assoc-n (get-vec self) i val))
+ ICollection
+ (-conj [self obj]
+ (conj (get-vec self) obj))
+ IWithMeta
+ (-with-meta [self metamap]
+ (if @flat
+ (FlattenOnDemandVector. (atom @v) hashcode cnt (atom (with-meta @flat metamap)))
+ (FlattenOnDemandVector. (atom (with-meta @v metamap)) hashcode cnt (atom @flat))))
+ IMeta
+ (-meta [self]
+ (if @flat (meta @flat) (meta @v)))
+ ISequential
+ ISeqable
+ (-seq [self]
+ (seq (get-vec self)))
+ ILookup
+ (-lookup [self key]
+ (-lookup (get-vec self) key))
+ (-lookup [self key not-found]
+ (-lookup (get-vec self) key not-found))
+ IIndexed
+ (-nth [self i]
+ (-nth (get-vec self) i))
+ (-nth [self i not-found]
+ (-nth (get-vec self) i not-found))
+ IFn
+ (-invoke [self arg]
+ (-invoke (get-vec self) arg))
+ (-invoke [self arg not-found]
+ (-invoke (get-vec self) arg not-found))
+ IReversible
+ (-rseq [self]
+ (if (pos? cnt)
+ (rseq (get-vec self))
+ nil))
+ IStack
+ (-peek [self]
+ (-peek (get-vec self)))
+ (-pop [self]
+ (-pop (get-vec self)))
+ IAssociative
+ (-assoc [self i val]
+ (assoc (get-vec self) i val))
+ (-contains-key? [self k]
+ (-contains-key? (get-vec self) k))
+ IKVReduce
+ (-kv-reduce [self f init]
+ (-kv-reduce (get-vec self) f init))
+ IComparable
+ (-compare [self that]
+ (-compare (get-vec self) that))
+ ))
#?(:cljs
(extend-protocol IPrintWithWriter
@@ -514,15 +514,15 @@
(vec (seq afs))
#?(:clj
(FlattenOnDemandVector.
- (ref (.-v afs))
- (.-hashcode afs)
- (.-cnt afs)
- (ref nil))
+ (ref (.-v afs))
+ (.-hashcode afs)
+ (.-cnt afs)
+ (ref nil))
:cljs
(FlattenOnDemandVector.
- (atom (.-v afs))
- (.-hashcode afs)
- (.-cnt afs)
- (atom nil))))
+ (atom (.-v afs))
+ (.-hashcode afs)
+ (.-cnt afs)
+ (atom nil))))
:else
(.-v afs)))
diff --git a/src/instaparse/cfg.cljc b/src/instaparse/cfg.cljc
index 36aabd8..7bb684b 100644
--- a/src/instaparse/cfg.cljc
+++ b/src/instaparse/cfg.cljc
@@ -4,13 +4,13 @@
(:require [instaparse.combinators-source :refer
[Epsilon opt plus star rep alt ord cat string-ci string
string-ci regexp nt look neg hide hide-tag]]
- [instaparse.reduction :refer [apply-standard-reductions]]
- [instaparse.gll :refer [parse]]
- [instaparse.util :refer [throw-illegal-argument-exception
- throw-runtime-exception]]
- [clojure.string :as str]
- #?(:cljs [cljs.tools.reader :as reader])
- #?(:cljs [cljs.tools.reader.reader-types :as readers])))
+ [instaparse.reduction :refer [apply-standard-reductions]]
+ [instaparse.gll :refer [parse]]
+ [instaparse.util :refer [throw-illegal-argument-exception
+ throw-runtime-exception]]
+ [clojure.string :as str]
+ #?(:cljs [cljs.tools.reader :as reader])
+ #?(:cljs [cljs.tools.reader.reader-types :as readers])))
(def ^:dynamic *case-insensitive-literals*
"Sets whether all string literal terminals in a built grammar
@@ -78,87 +78,87 @@
(neg (nt :epsilon))
(regexp
(regex-doc "[^, \\r\\t\\n<>(){}\\[\\]+*?:=|'\"#&!;./]+" "Non-terminal")))
- :hide-nt (cat (hide (string "<"))
- opt-whitespace
- (nt :nt)
- opt-whitespace
- (hide (string ">")))
- :alt-or-ord (hide-tag (alt (nt :alt) (nt :ord)))
- :alt (cat (nt :cat)
- (star
- (cat
- opt-whitespace
- (hide (string "|"))
- opt-whitespace
- (nt :cat))))
- :ord (cat (nt :cat)
- (plus
- (cat
- opt-whitespace
- (hide (string "/"))
- opt-whitespace
- (nt :cat))))
- :paren (cat (hide (string "("))
- opt-whitespace
- (nt :alt-or-ord)
- opt-whitespace
- (hide (string ")")))
- :hide (cat (hide (string "<"))
- opt-whitespace
- (nt :alt-or-ord)
- opt-whitespace
- (hide (string ">")))
- :cat (plus (cat
- opt-whitespace
- (alt (nt :factor) (nt :look) (nt :neg))
- opt-whitespace))
- :string (alt
- (regexp single-quoted-string)
- (regexp double-quoted-string))
- :regexp (alt
- (regexp single-quoted-regexp)
- (regexp double-quoted-regexp))
- :opt (alt
- (cat (hide (string "["))
- opt-whitespace
- (nt :alt-or-ord)
- opt-whitespace
- (hide (string "]")))
- (cat (nt :factor)
- opt-whitespace
- (hide (string "?"))))
- :star (alt
- (cat (hide (string "{"))
- opt-whitespace
- (nt :alt-or-ord)
- opt-whitespace
- (hide (string "}")))
- (cat (nt :factor)
- opt-whitespace
- (hide (string "*"))))
- :plus (cat (nt :factor)
- opt-whitespace
- (hide (string "+")))
- :look (cat (hide (string "&"))
- opt-whitespace
- (nt :factor))
- :neg (cat (hide (string "!"))
- opt-whitespace
- (nt :factor))
- :epsilon (alt (string "Epsilon")
- (string "epsilon")
- (string "EPSILON")
- (string "eps")
- (string "\u03b5"))
- :factor (hide-tag (alt (nt :nt)
- (nt :string)
- (nt :regexp)
- (nt :opt)
- (nt :star)
- (nt :plus)
- (nt :paren)
- (nt :hide)
- (nt :epsilon)))
+ :hide-nt (cat (hide (string "<"))
+ opt-whitespace
+ (nt :nt)
+ opt-whitespace
+ (hide (string ">")))
+ :alt-or-ord (hide-tag (alt (nt :alt) (nt :ord)))
+ :alt (cat (nt :cat)
+ (star
+ (cat
+ opt-whitespace
+ (hide (string "|"))
+ opt-whitespace
+ (nt :cat))))
+ :ord (cat (nt :cat)
+ (plus
+ (cat
+ opt-whitespace
+ (hide (string "/"))
+ opt-whitespace
+ (nt :cat))))
+ :paren (cat (hide (string "("))
+ opt-whitespace
+ (nt :alt-or-ord)
+ opt-whitespace
+ (hide (string ")")))
+ :hide (cat (hide (string "<"))
+ opt-whitespace
+ (nt :alt-or-ord)
+ opt-whitespace
+ (hide (string ">")))
+ :cat (plus (cat
+ opt-whitespace
+ (alt (nt :factor) (nt :look) (nt :neg))
+ opt-whitespace))
+ :string (alt
+ (regexp single-quoted-string)
+ (regexp double-quoted-string))
+ :regexp (alt
+ (regexp single-quoted-regexp)
+ (regexp double-quoted-regexp))
+ :opt (alt
+ (cat (hide (string "["))
+ opt-whitespace
+ (nt :alt-or-ord)
+ opt-whitespace
+ (hide (string "]")))
+ (cat (nt :factor)
+ opt-whitespace
+ (hide (string "?"))))
+ :star (alt
+ (cat (hide (string "{"))
+ opt-whitespace
+ (nt :alt-or-ord)
+ opt-whitespace
+ (hide (string "}")))
+ (cat (nt :factor)
+ opt-whitespace
+ (hide (string "*"))))
+ :plus (cat (nt :factor)
+ opt-whitespace
+ (hide (string "+")))
+ :look (cat (hide (string "&"))
+ opt-whitespace
+ (nt :factor))
+ :neg (cat (hide (string "!"))
+ opt-whitespace
+ (nt :factor))
+ :epsilon (alt (string "Epsilon")
+ (string "epsilon")
+ (string "EPSILON")
+ (string "eps")
+ (string "\u03b5"))
+ :factor (hide-tag (alt (nt :nt)
+ (nt :string)
+ (nt :regexp)
+ (nt :opt)
+ (nt :star)
+ (nt :plus)
+ (nt :paren)
+ (nt :hide)
+ (nt :epsilon)))
;; extra entrypoint to be used by the ebnf combinator
:rules-or-parser (hide-tag (alt (nt :rules) (nt :alt-or-ord)))}))
@@ -238,7 +238,7 @@
(escape stripped)
final-string
(re-pattern remove-escaped-single-quotes)]
-; (safe-read-regexp (str remove-escaped-single-quotes \"))]
+ ; (safe-read-regexp (str remove-escaped-single-quotes \"))]
final-string))
diff --git a/src/instaparse/combinators.cljc b/src/instaparse/combinators.cljc
index 964b6fd..52275fa 100644
--- a/src/instaparse/combinators.cljc
+++ b/src/instaparse/combinators.cljc
@@ -5,8 +5,8 @@
:cljs (:require-macros
[instaparse.macros :refer [defclone]]))
(:require [instaparse.combinators-source :as c]
- [instaparse.cfg :as cfg]
- [instaparse.abnf :as abnf]))
+ [instaparse.cfg :as cfg]
+ [instaparse.abnf :as abnf]))
;; The actual source is in combinators-source.
;; This was necessary to avoid a cyclical dependency in the namespaces.
diff --git a/src/instaparse/combinators_source.cljc b/src/instaparse/combinators_source.cljc
index 974f50a..3c08adc 100644
--- a/src/instaparse/combinators_source.cljc
+++ b/src/instaparse/combinators_source.cljc
@@ -4,7 +4,7 @@
(:require [instaparse.reduction :refer [singleton? red
raw-non-terminal-reduction
reduction-types]]
- [instaparse.util :refer [throw-illegal-argument-exception #?(:cljs regexp-flags)]]))
+ [instaparse.util :refer [throw-illegal-argument-exception #?(:cljs regexp-flags)]]))
;; Ways to build parsers
@@ -44,12 +44,12 @@
(defn ord "Ordered choice, i.e., parser1 / parser2"
([] Epsilon)
([parser1 & parsers]
- (let [parsers (if (= parser1 Epsilon)
- (remove #{Epsilon} parsers)
- parsers)]
- (if (seq parsers)
- (ord2 parser1 (apply ord parsers))
- parser1))))
+ (let [parsers (if (= parser1 Epsilon)
+ (remove #{Epsilon} parsers)
+ parsers)]
+ (if (seq parsers)
+ (ord2 parser1 (apply ord parsers))
+ parser1))))
(defn cat "Concatenation, i.e., parser1 parser2 ..."
[& parsers]
diff --git a/src/instaparse/core.cljc b/src/instaparse/core.cljc
index b88f951..657b574 100644
--- a/src/instaparse/core.cljc
+++ b/src/instaparse/core.cljc
@@ -1,20 +1,20 @@
(ns instaparse.core
(#?(:clj :require :cljs :require-macros)
- [instaparse.macros :refer [defclone
- set-global-var!]])
+ [instaparse.macros :refer [defclone
+ set-global-var!]])
(:require [clojure.walk :as walk]
- [instaparse.gll :as gll]
- [instaparse.cfg :as cfg]
- [instaparse.failure :as fail]
- [instaparse.print :as print]
- [instaparse.reduction :as red]
- [instaparse.transform :as t]
- [instaparse.abnf :as abnf]
- [instaparse.repeat :as repeat]
- [instaparse.combinators-source :as c]
- [instaparse.line-col :as lc]
- [instaparse.viz :as viz]
- [instaparse.util :refer [throw-illegal-argument-exception]]))
+ [instaparse.gll :as gll]
+ [instaparse.cfg :as cfg]
+ [instaparse.failure :as fail]
+ [instaparse.print :as print]
+ [instaparse.reduction :as red]
+ [instaparse.transform :as t]
+ [instaparse.abnf :as abnf]
+ [instaparse.repeat :as repeat]
+ [instaparse.combinators-source :as c]
+ [instaparse.line-col :as lc]
+ [instaparse.viz :as viz]
+ [instaparse.util :refer [throw-illegal-argument-exception]]))
(def ^:dynamic *default-output-format* :hiccup)
(defn set-default-output-format!
@@ -136,27 +136,27 @@
#?(:clj (gll/bind-trace trace?)))))
(defrecord Parser [grammar start-production output-format]
-#?@(:clj
- [clojure.lang.IFn
- (invoke [parser text] (parse parser text))
- (invoke [parser text key1 val1] (parse parser text key1 val1))
- (invoke [parser text key1 val1 key2 val2] (parse parser text key1 val1 key2 val2))
- (invoke [parser text key1 val1 key2 val2 key3 val3] (parse parser text key1 val1 key2 val2 key3 val3))
- (applyTo [parser args] (apply parse parser args))]
-
- :cljs
- [IFn
- (-invoke [parser text] (parse parser text))
- (-invoke [parser text key1 val1] (parse parser text key1 val1))
- (-invoke [parser text key1 val1 key2 val2] (parse parser text key1 val1 key2 val2))
- (-invoke [parser text key1 val1 key2 val2 key3 val3] (parse parser text key1 val1 key2 val2 key3 val3))
- (-invoke [parser text a b c d e f g h] (parse parser text a b c d e f g h))
- (-invoke [parser text a b c d e f g h i j] (parse parser text a b c d e f g h i j))
- (-invoke [parser text a b c d e f g h i j k l] (parse parser text a b c d e f g h i j k l))
- (-invoke [parser text a b c d e f g h i j k l m n] (parse parser text a b c d e f g h i j k l m n))
- (-invoke [parser text a b c d e f g h i j k l m n o p] (parse parser text a b c d e f g h i j k l m n o p))
- (-invoke [parser text a b c d e f g h i j k l m n o p q r] (parse parser text a b c d e f g h i j k l m n o p))
- (-invoke [parser text a b c d e f g h i j k l m n o p q r s more] (apply parse parser text a b c d e f g h i j k l m n o p q r s more))]))
+ #?@(:clj
+ [clojure.lang.IFn
+ (invoke [parser text] (parse parser text))
+ (invoke [parser text key1 val1] (parse parser text key1 val1))
+ (invoke [parser text key1 val1 key2 val2] (parse parser text key1 val1 key2 val2))
+ (invoke [parser text key1 val1 key2 val2 key3 val3] (parse parser text key1 val1 key2 val2 key3 val3))
+ (applyTo [parser args] (apply parse parser args))]
+
+ :cljs
+ [IFn
+ (-invoke [parser text] (parse parser text))
+ (-invoke [parser text key1 val1] (parse parser text key1 val1))
+ (-invoke [parser text key1 val1 key2 val2] (parse parser text key1 val1 key2 val2))
+ (-invoke [parser text key1 val1 key2 val2 key3 val3] (parse parser text key1 val1 key2 val2 key3 val3))
+ (-invoke [parser text a b c d e f g h] (parse parser text a b c d e f g h))
+ (-invoke [parser text a b c d e f g h i j] (parse parser text a b c d e f g h i j))
+ (-invoke [parser text a b c d e f g h i j k l] (parse parser text a b c d e f g h i j k l))
+ (-invoke [parser text a b c d e f g h i j k l m n] (parse parser text a b c d e f g h i j k l m n))
+ (-invoke [parser text a b c d e f g h i j k l m n o p] (parse parser text a b c d e f g h i j k l m n o p))
+ (-invoke [parser text a b c d e f g h i j k l m n o p q r] (parse parser text a b c d e f g h i j k l m n o p))
+ (-invoke [parser text a b c d e f g h i j k l m n o p q r s more] (apply parse parser text a b c d e f g h i j k l m n o p q r s more))]))
#?(:clj
(defmethod clojure.core/print-method Parser [x writer]
@@ -256,8 +256,8 @@
(map->Parser parser))
:cljs
(throw-illegal-argument-exception
- "Expected string, map, or vector as grammar specification, got "
- (pr-str grammar-specification))))]
+ "Expected string, map, or vector as grammar specification, got "
+ (pr-str grammar-specification))))]
(let [auto-whitespace (get options :auto-whitespace)
; auto-whitespace is keyword, parser, or nil
@@ -293,36 +293,36 @@
(if (string? grammar)
`(def ~name
(map->Parser
- ~(binding [abnf/*case-insensitive* (:instaparse.abnf/case-insensitive opts false)]
- (let [macro-time-opts (select-keys opts [:input-format
- :output-format
- :string-ci
- :no-slurp])
- runtime-opts (dissoc opts :start)
- macro-time-parser (apply parser grammar (apply concat macro-time-opts))
- pre-processed-grammar (:grammar macro-time-parser)
-
- grammar-producing-code
- (->> pre-processed-grammar
- (walk/postwalk
- (fn [form]
- (cond
- ;; Lists cannot be evaluated verbatim
- (seq? form)
- (list* 'list form)
-
- ;; Regexp terminals are handled differently in cljs
- (= :regexp (:tag form))
- `(merge (c/regexp ~(str (:regexp form)))
- ~(dissoc form :tag :regexp))
-
- :else form))))
-
- start-production
- (or (:start opts) (:start-production macro-time-parser))]
- `(parser ~grammar-producing-code
- :start ~start-production
- ~@(apply concat runtime-opts))))))
+ ~(binding [abnf/*case-insensitive* (:instaparse.abnf/case-insensitive opts false)]
+ (let [macro-time-opts (select-keys opts [:input-format
+ :output-format
+ :string-ci
+ :no-slurp])
+ runtime-opts (dissoc opts :start)
+ macro-time-parser (apply parser grammar (apply concat macro-time-opts))
+ pre-processed-grammar (:grammar macro-time-parser)
+
+ grammar-producing-code
+ (->> pre-processed-grammar
+ (walk/postwalk
+ (fn [form]
+ (cond
+ ;; Lists cannot be evaluated verbatim
+ (seq? form)
+ (list* 'list form)
+
+ ;; Regexp terminals are handled differently in cljs
+ (= :regexp (:tag form))
+ `(merge (c/regexp ~(str (:regexp form)))
+ ~(dissoc form :tag :regexp))
+
+ :else form))))
+
+ start-production
+ (or (:start opts) (:start-production macro-time-parser))]
+ `(parser ~grammar-producing-code
+ :start ~start-production
+ ~@(apply concat runtime-opts))))))
`(def ~name (parser ~grammar ~@(apply concat opts))))))
(defn failure?
diff --git a/src/instaparse/gll.cljc b/src/instaparse/gll.cljc
index 0290128..de9675c 100644
--- a/src/instaparse/gll.cljc
+++ b/src/instaparse/gll.cljc
@@ -5,37 +5,37 @@
listeners and parse commands that are on the stack."
(:require
- ;; Incremental vector provides a more performant hashing strategy
- ;; for this use-case for vectors
- ;; We use the auto flatten version
- [instaparse.auto-flatten-seq :as afs]
+ ;; Incremental vector provides a more performant hashing strategy
+ ;; for this use-case for vectors
+ ;; We use the auto flatten version
+ [instaparse.auto-flatten-seq :as afs]
- ;; failure contains the augment-failure function, which is called to
- ;; add enough information to the failure object for pretty printing
- [instaparse.failure :as fail]
+ ;; failure contains the augment-failure function, which is called to
+ ;; add enough information to the failure object for pretty printing
+ [instaparse.failure :as fail]
- ;; reduction contains code relating to reductions and flattening.
- [instaparse.reduction :as red]
+ ;; reduction contains code relating to reductions and flattening.
+ [instaparse.reduction :as red]
- ;; Two of the public combinators are needed.
- [instaparse.combinators-source :refer [Epsilon nt]]
+ ;; Two of the public combinators are needed.
+ [instaparse.combinators-source :refer [Epsilon nt]]
- ;; Need a way to convert parsers into strings for printing and error messages.
- [instaparse.print :as print]
+ ;; Need a way to convert parsers into strings for printing and error messages.
+ [instaparse.print :as print]
- ;; Utility to preserve RegExp flags
- #?(:cljs
- [instaparse.util :refer [regexp-flags]])
+ ;; Utility to preserve RegExp flags
+ #?(:cljs
+ [instaparse.util :refer [regexp-flags]])
- ;; Unicode utilities for char-range
- #?(:cljs
- [goog.i18n.uChar :as u]))
+ ;; Unicode utilities for char-range
+ #?(:cljs
+ [goog.i18n.uChar :as u]))
#?(:cljs
(:use-macros
- [instaparse.gll :only
- [log profile dprintln dpprint success
- attach-diagnostic-meta trace-or-false]])))
+ [instaparse.gll :only
+ [log profile dprintln dpprint success
+ attach-diagnostic-meta trace-or-false]])))
;; As of Java 7, strings no longer have fast substring operation,
;; so we use Segments instead, which implement the CharSequence
@@ -62,48 +62,48 @@
;;;;; SETUP DIAGNOSTIC MACROS AND VARS
#?(:clj (do
-(defonce PRINT false)
-(defmacro dprintln [& body]
- (when PRINT `(println ~@body)))
-(defmacro dpprint [& body]
- (when PRINT `(clojure.pprint/pprint ~@body)))
-
-(defonce PROFILE false)
-(defmacro profile [& body]
- (when PROFILE
- `(do ~@body)))
-
-;; By default TRACE is set to false, and all these macros are used
-;; throughout the code to ensure there is absolutely no performance
-;; penalty from the tracing code. Everything related to tracing
-;; is compiled away.
-;;
-;; We recompile this file with TRACE set to true to activate the
-;; tracing code.
-;;
-;; bind-trace is the one exception where we can't completely compile
-;; the new code away, because it is used in instaparse.core, which won't be
-;; recompiled. Still, binding is a relatively slow operation, so by testing
-;; whether TRACE is true inside the expansion, we can at least avoid
-;; the performance hit of binding every time.
-
-(defonce TRACE false)
-(def ^:dynamic *trace* false)
-(defmacro log [tramp & body]
- (when TRACE
- `(when (:trace? ~tramp) (println ~@body))))
-(defmacro attach-diagnostic-meta [f metadata]
- (if TRACE
- `(with-meta ~f ~metadata)
- f))
-(defmacro bind-trace [trace? body]
- `(if TRACE
- (binding [*trace* ~trace?] ~body)
- ~body))
-(defmacro trace-or-false []
- (if TRACE '*trace* false))
-
-))
+ (defonce PRINT false)
+ (defmacro dprintln [& body]
+ (when PRINT `(println ~@body)))
+ (defmacro dpprint [& body]
+ (when PRINT `(clojure.pprint/pprint ~@body)))
+
+ (defonce PROFILE false)
+ (defmacro profile [& body]
+ (when PROFILE
+ `(do ~@body)))
+
+ ;; By default TRACE is set to false, and all these macros are used
+ ;; throughout the code to ensure there is absolutely no performance
+ ;; penalty from the tracing code. Everything related to tracing
+ ;; is compiled away.
+ ;;
+ ;; We recompile this file with TRACE set to true to activate the
+ ;; tracing code.
+ ;;
+ ;; bind-trace is the one exception where we can't completely compile
+ ;; the new code away, because it is used in instaparse.core, which won't be
+ ;; recompiled. Still, binding is a relatively slow operation, so by testing
+ ;; whether TRACE is true inside the expansion, we can at least avoid
+ ;; the performance hit of binding every time.
+
+ (defonce TRACE false)
+ (def ^:dynamic *trace* false)
+ (defmacro log [tramp & body]
+ (when TRACE
+ `(when (:trace? ~tramp) (println ~@body))))
+ (defmacro attach-diagnostic-meta [f metadata]
+ (if TRACE
+ `(with-meta ~f ~metadata)
+ f))
+ (defmacro bind-trace [trace? body]
+ `(if TRACE
+ (binding [*trace* ~trace?] ~body)
+ ~body))
+ (defmacro trace-or-false []
+ (if TRACE '*trace* false))
+
+ ))
; In diagnostic messages, how many characters ahead do we want to show.
(def ^:dynamic *diagnostic-char-lookahead* 10)
@@ -235,10 +235,10 @@
([grammar text segment] (make-tramp grammar text segment -1 nil))
([grammar text fail-index node-builder] (make-tramp grammar text (text->segment text) fail-index node-builder))
([grammar text segment fail-index node-builder]
- (Tramp. grammar text segment
- fail-index node-builder
- (atom []) (atom []) (atom 0) (atom (sorted-map-by >))
- (atom {}) (atom {}) (atom nil) (atom (Failure. 0 [])) (trace-or-false))))
+ (Tramp. grammar text segment
+ fail-index node-builder
+ (atom []) (atom []) (atom 0) (atom (sorted-map-by >))
+ (atom {}) (atom {}) (atom nil) (atom (Failure. 0 [])) (trace-or-false))))
; A Success record contains the result and the index to continue from
(defn make-success [result index] {:result result :index index})
@@ -276,7 +276,7 @@
f #(listener result)]
(profile (add! :push-message))
#_(dprintln "push-message" i c @(:generation tramp) (count @(:stack tramp))
- (count @(:next-stack tramp)))
+ (count @(:next-stack tramp)))
#_(dprintln "push-message: listener result" listener result)
(if (> c @(:generation tramp))
(swap! (:next-stack tramp) conj f)
@@ -452,46 +452,46 @@
"Executes the stack until exhausted"
([tramp] (run tramp nil))
([tramp found-result?]
- (let [stack (:stack tramp)]
- ;_ (dprintln "run" found-result? (count @(:stack tramp)) (count @(:next-stack tramp)))]
- (cond
- @(:success tramp)
- (do (log tramp "Successful parse.\nProfile: " @stats)
- (cons (:result @(:success tramp))
- (lazy-seq
- (do (reset! (:success tramp) nil)
- (run tramp true)))))
+ (let [stack (:stack tramp)]
+ ;_ (dprintln "run" found-result? (count @(:stack tramp)) (count @(:next-stack tramp)))]
+ (cond
+ @(:success tramp)
+ (do (log tramp "Successful parse.\nProfile: " @stats)
+ (cons (:result @(:success tramp))
+ (lazy-seq
+ (do (reset! (:success tramp) nil)
+ (run tramp true)))))
- (pos? (count @stack))
- (do ;(dprintln "stacks" (count @stack) (count @(:next-stack tramp)))
- (step stack) (recur tramp found-result?))
-
- (pos? (count @(:negative-listeners tramp)))
- (let [[index listeners] (first @(:negative-listeners tramp))
- listener (peek listeners)]
- (log tramp (format "Exhausted results for %s at index %d (%s)"
- (print/combinators->str (((meta listener) :creator) 1))
- (((meta listener) :creator) 0)
- (string-context (:text tramp)
- (((meta listener) :creator) 0))))
- (listener)
- (if (= (count listeners) 1)
- (swap! (:negative-listeners tramp) dissoc index)
- (swap! (:negative-listeners tramp) update-in [index] pop))
- (recur tramp found-result?))
+ (pos? (count @stack))
+ (do ;(dprintln "stacks" (count @stack) (count @(:next-stack tramp)))
+ (step stack) (recur tramp found-result?))
+
+ (pos? (count @(:negative-listeners tramp)))
+ (let [[index listeners] (first @(:negative-listeners tramp))
+ listener (peek listeners)]
+ (log tramp (format "Exhausted results for %s at index %d (%s)"
+ (print/combinators->str (((meta listener) :creator) 1))
+ (((meta listener) :creator) 0)
+ (string-context (:text tramp)
+ (((meta listener) :creator) 0))))
+ (listener)
+ (if (= (count listeners) 1)
+ (swap! (:negative-listeners tramp) dissoc index)
+ (swap! (:negative-listeners tramp) update-in [index] pop))
+ (recur tramp found-result?))
- found-result?
- (let [next-stack (:next-stack tramp)]
- #_(dprintln "Swapping stacks" (count @(:stack tramp))
- (count @(:next-stack tramp)))
- (reset! stack @next-stack)
- (reset! next-stack [])
- (swap! (:generation tramp) inc)
- #_(dprintln "Swapped stacks" (count @(:stack tramp))
- (count @(:next-stack tramp)))
- (recur tramp nil))
+ found-result?
+ (let [next-stack (:next-stack tramp)]
+ #_(dprintln "Swapping stacks" (count @(:stack tramp))
+ (count @(:next-stack tramp)))
+ (reset! stack @next-stack)
+ (reset! next-stack [])
+ (swap! (:generation tramp) inc)
+ #_(dprintln "Swapped stacks" (count @(:stack tramp))
+ (count @(:next-stack tramp)))
+ (recur tramp nil))
- :else nil))))
+ :else nil))))
;; Listeners
@@ -517,9 +517,9 @@
(defn CatListener [results-so-far parser-sequence node-key tramp]
(dpprint {:tag :CatListener
- :results-so-far results-so-far
- :parser-sequence (map :tag parser-sequence)
- :node-key [(node-key 0) (:tag (node-key 1))]})
+ :results-so-far results-so-far
+ :parser-sequence (map :tag parser-sequence)
+ :node-key [(node-key 0) (:tag (node-key 1))]})
(fn [result]
(let [{parsed-result :result continue-index :index} result
new-results-so-far (afs/conj-flat results-so-far parsed-result)]
@@ -529,10 +529,10 @@
(success tramp node-key new-results-so-far continue-index)))))
(defn CatFullListener [results-so-far parser-sequence node-key tramp]
-; (dpprint {:tag :CatFullListener
-; :results-so-far results-so-far
-; :parser-sequence (map :tag parser-sequence)
-; :node-key [(node-key 0) (:tag (node-key 1))]})
+ ; (dpprint {:tag :CatFullListener
+ ; :results-so-far results-so-far
+ ; :parser-sequence (map :tag parser-sequence)
+ ; :node-key [(node-key 0) (:tag (node-key 1))]})
(fn [result]
(let [{parsed-result :result continue-index :index} result
new-results-so-far (afs/conj-flat results-so-far parsed-result)]
diff --git a/src/instaparse/line_col.cljc b/src/instaparse/line_col.cljc
index 4bb29ab..b38cea7 100644
--- a/src/instaparse/line_col.cljc
+++ b/src/instaparse/line_col.cljc
@@ -1,6 +1,6 @@
(ns instaparse.line-col
(:require [instaparse.transform]
- [instaparse.util :refer [throw-illegal-argument-exception]]))
+ [instaparse.util :refer [throw-illegal-argument-exception]]))
; Function to annotate parse-tree with line and column metadata.
diff --git a/src/instaparse/print.cljc b/src/instaparse/print.cljc
index 4694026..baef8f2 100644
--- a/src/instaparse/print.cljc
+++ b/src/instaparse/print.cljc
@@ -54,30 +54,30 @@
"Stringifies a parser built from combinators"
([p] (combinators->str p false))
([{:keys [parser parser1 parser2 parsers tag] :as p} hidden?]
- (if (and (not hidden?) (:hide p))
- (str \< (combinators->str p true) \>)
- (case tag
- :epsilon "\u03b5"
- :opt (str (paren-for-compound hidden? parser) "?")
- :plus (str (paren-for-compound hidden? parser) "+")
- :star (str (paren-for-compound hidden? parser) "*")
- :rep (if (not= (:min p) (:max p))
- (str (paren-for-compound hidden? parser) \{
- (:min p) \, (:max p) \})
- (str (paren-for-compound hidden? parser) \{
- (:min p)\}))
- :alt (str/join " | " (map (partial paren-for-tags #{:ord} hidden?) parsers))
- :ord (str (paren-for-tags #{:alt} hidden? parser1)
- " / "
- (paren-for-tags #{:alt} hidden? parser2))
- :cat (str/join " " (map (partial paren-for-tags #{:alt :ord} hidden?) parsers))
- :string (with-out-str (pr (:string p)))
- :string-ci (with-out-str (pr (:string p)))
- :char (char-range->str p)
- :regexp (regexp->str (:regexp p))
- :nt (subs (str (:keyword p)) 1)
- :look (str "&" (paren-for-compound hidden? parser))
- :neg (str "!" (paren-for-compound hidden? parser))))))
+ (if (and (not hidden?) (:hide p))
+ (str \< (combinators->str p true) \>)
+ (case tag
+ :epsilon "\u03b5"
+ :opt (str (paren-for-compound hidden? parser) "?")
+ :plus (str (paren-for-compound hidden? parser) "+")
+ :star (str (paren-for-compound hidden? parser) "*")
+ :rep (if (not= (:min p) (:max p))
+ (str (paren-for-compound hidden? parser) \{
+ (:min p) \, (:max p) \})
+ (str (paren-for-compound hidden? parser) \{
+ (:min p)\}))
+ :alt (str/join " | " (map (partial paren-for-tags #{:ord} hidden?) parsers))
+ :ord (str (paren-for-tags #{:alt} hidden? parser1)
+ " / "
+ (paren-for-tags #{:alt} hidden? parser2))
+ :cat (str/join " " (map (partial paren-for-tags #{:alt :ord} hidden?) parsers))
+ :string (with-out-str (pr (:string p)))
+ :string-ci (with-out-str (pr (:string p)))
+ :char (char-range->str p)
+ :regexp (regexp->str (:regexp p))
+ :nt (subs (str (:keyword p)) 1)
+ :look (str "&" (paren-for-compound hidden? parser))
+ :neg (str "!" (paren-for-compound hidden? parser))))))
(defn rule->str
"Takes a non-terminal symbol and a parser built from combinators,
diff --git a/src/instaparse/reduction.cljc b/src/instaparse/reduction.cljc
index 04637a5..248279e 100644
--- a/src/instaparse/reduction.cljc
+++ b/src/instaparse/reduction.cljc
@@ -1,6 +1,6 @@
(ns instaparse.reduction
(:require [instaparse.auto-flatten-seq :as afs]
- [instaparse.util :refer [throw-illegal-argument-exception]]))
+ [instaparse.util :refer [throw-illegal-argument-exception]]))
;; utilities
@@ -48,9 +48,9 @@
(defn apply-standard-reductions
([grammar] (apply-standard-reductions standard-non-terminal-reduction grammar))
([reduction-type grammar]
- (if-let [reduction (reduction-types reduction-type)]
- (into {} (for [[k v] grammar]
- (if (:red v) [k v]
- [k (assoc v :red (reduction k))])))
- (throw-illegal-argument-exception
- "Invalid output format " reduction-type ". Use :enlive or :hiccup."))))
+ (if-let [reduction (reduction-types reduction-type)]
+ (into {} (for [[k v] grammar]
+ (if (:red v) [k v]
+ [k (assoc v :red (reduction k))])))
+ (throw-illegal-argument-exception
+ "Invalid output format " reduction-type ". Use :enlive or :hiccup."))))
diff --git a/src/instaparse/repeat.cljc b/src/instaparse/repeat.cljc
index b035201..8d98fc0 100644
--- a/src/instaparse/repeat.cljc
+++ b/src/instaparse/repeat.cljc
@@ -1,11 +1,11 @@
(ns instaparse.repeat
(:require [instaparse.gll :as gll
#?@(:clj [:refer [profile]])]
- [instaparse.combinators-source :as c]
- [instaparse.auto-flatten-seq :as afs]
- [instaparse.viz :as viz]
- [instaparse.reduction :as red]
- [instaparse.failure :as fail])
+ [instaparse.combinators-source :as c]
+ [instaparse.auto-flatten-seq :as afs]
+ [instaparse.viz :as viz]
+ [instaparse.reduction :as red]
+ [instaparse.failure :as fail])
#?(:cljs
(:require-macros [instaparse.gll :refer [profile]])))
@@ -19,12 +19,12 @@
(defn get-end
(#?(:clj ^long [parse]
:cljs ^number [parse])
- (let [[start end] (viz/span parse)]
- (if end (long end) (count parse))))
+ (let [[start end] (viz/span parse)]
+ (if end (long end) (count parse))))
(#?(:clj ^long [parse ^long index]
:cljs ^number [parse ^number index])
- (let [[start end] (viz/span parse)]
- (if end (long end) (+ index (count parse))))))
+ (let [[start end] (viz/span parse)]
+ (if end (long end) (+ index (count parse))))))
(defn parse-from-index [grammar initial-parser text segment index]
(let [tramp (gll/make-tramp grammar text segment)]
@@ -53,120 +53,120 @@
(defn repeat-parse-hiccup
([grammar initial-parser root-tag text segment]
- (repeat-parse-hiccup grammar initial-parser root-tag text segment 0))
+ (repeat-parse-hiccup grammar initial-parser root-tag text segment 0))
([grammar initial-parser root-tag text segment index]
- (let [length (count text)
- first-result (parse-from-index grammar initial-parser text segment index)]
- (loop [index (long index)
- parses (afs/auto-flatten-seq [root-tag])
+ (let [length (count text)
+ first-result (parse-from-index grammar initial-parser text segment index)]
+ (loop [index (long index)
+ parses (afs/auto-flatten-seq [root-tag])
- [parse end follow-ups :as selection]
- (select-parse grammar initial-parser text segment index first-result)]
- (cond
- (nil? selection) failure-signal
- (= index end) failure-signal
- (nil? follow-ups) (gll/safe-with-meta
- (afs/convert-afs-to-vec
- (afs/conj-flat parses parse))
- {:optimize :memory
- :instaparse.gll/start-index 0
- :instaparse.gll/end-index length})
- :else (recur (long end)
- (afs/conj-flat parses parse)
- (select-parse grammar initial-parser text segment end follow-ups)))))))
+ [parse end follow-ups :as selection]
+ (select-parse grammar initial-parser text segment index first-result)]
+ (cond
+ (nil? selection) failure-signal
+ (= index end) failure-signal
+ (nil? follow-ups) (gll/safe-with-meta
+ (afs/convert-afs-to-vec
+ (afs/conj-flat parses parse))
+ {:optimize :memory
+ :instaparse.gll/start-index 0
+ :instaparse.gll/end-index length})
+ :else (recur (long end)
+ (afs/conj-flat parses parse)
+ (select-parse grammar initial-parser text segment end follow-ups)))))))
(defn repeat-parse-enlive
([grammar initial-parser root-tag text segment]
- (repeat-parse-enlive grammar initial-parser root-tag text segment 0))
+ (repeat-parse-enlive grammar initial-parser root-tag text segment 0))
([grammar initial-parser root-tag text segment index]
- (let [length (count text)
- first-result (parse-from-index grammar initial-parser text segment index)]
- (loop [index (long index)
- parses afs/EMPTY
+ (let [length (count text)
+ first-result (parse-from-index grammar initial-parser text segment index)]
+ (loop [index (long index)
+ parses afs/EMPTY
- [parse end follow-ups :as selection]
- (select-parse grammar initial-parser text segment index first-result)]
- (cond
- (nil? selection) failure-signal
- (= index end) failure-signal
- (nil? follow-ups) (gll/safe-with-meta
- {:tag root-tag
- :content (seq (afs/conj-flat parses parse))}
- {:optimize :memory
- :instaparse.gll/start-index 0
- :instaparse.gll/end-index length})
- :else (recur (long end)
- (afs/conj-flat parses parse)
- (select-parse grammar initial-parser text segment end follow-ups)))))))
+ [parse end follow-ups :as selection]
+ (select-parse grammar initial-parser text segment index first-result)]
+ (cond
+ (nil? selection) failure-signal
+ (= index end) failure-signal
+ (nil? follow-ups) (gll/safe-with-meta
+ {:tag root-tag
+ :content (seq (afs/conj-flat parses parse))}
+ {:optimize :memory
+ :instaparse.gll/start-index 0
+ :instaparse.gll/end-index length})
+ :else (recur (long end)
+ (afs/conj-flat parses parse)
+ (select-parse grammar initial-parser text segment end follow-ups)))))))
(defn repeat-parse-no-tag
([grammar initial-parser text segment]
- (repeat-parse-no-tag grammar initial-parser text segment 0))
+ (repeat-parse-no-tag grammar initial-parser text segment 0))
([grammar initial-parser text segment index]
- (let [length (count text)
- first-result (parse-from-index grammar initial-parser text segment index)]
- (loop [index (long index)
- parses afs/EMPTY
+ (let [length (count text)
+ first-result (parse-from-index grammar initial-parser text segment index)]
+ (loop [index (long index)
+ parses afs/EMPTY
- [parse end follow-ups :as selection]
- (select-parse grammar initial-parser text segment index first-result)]
- (cond
- (nil? selection) failure-signal
- (= index end) failure-signal
- (nil? follow-ups) (gll/safe-with-meta
- (afs/conj-flat parses parse)
- {:optimize :memory
- :instaparse.gll/start-index 0
- :instaparse.gll/end-index length})
- :else (recur (long end)
- (afs/conj-flat parses parse)
- (select-parse grammar initial-parser text segment end follow-ups)))))))
+ [parse end follow-ups :as selection]
+ (select-parse grammar initial-parser text segment index first-result)]
+ (cond
+ (nil? selection) failure-signal
+ (= index end) failure-signal
+ (nil? follow-ups) (gll/safe-with-meta
+ (afs/conj-flat parses parse)
+ {:optimize :memory
+ :instaparse.gll/start-index 0
+ :instaparse.gll/end-index length})
+ :else (recur (long end)
+ (afs/conj-flat parses parse)
+ (select-parse grammar initial-parser text segment end follow-ups)))))))
(defn repeat-parse
([grammar initial-parser output-format text] (repeat-parse-no-tag grammar initial-parser text (gll/text->segment text)))
([grammar initial-parser output-format root-tag text]
- {:pre [(#{:hiccup :enlive} output-format)]}
- (cond
- (= output-format :hiccup)
- (repeat-parse-hiccup grammar initial-parser root-tag text (gll/text->segment text))
- (= output-format :enlive)
- (repeat-parse-enlive grammar initial-parser root-tag text (gll/text->segment text)))))
+ {:pre [(#{:hiccup :enlive} output-format)]}
+ (cond
+ (= output-format :hiccup)
+ (repeat-parse-hiccup grammar initial-parser root-tag text (gll/text->segment text))
+ (= output-format :enlive)
+ (repeat-parse-enlive grammar initial-parser root-tag text (gll/text->segment text)))))
(defn repeat-parse-with-header
([grammar header-parser repeating-parser output-format root-tag text]
- (let [segment (gll/text->segment text)
- length (count text)
- header-results (parse-from-index grammar header-parser text segment 0)]
- (if (or (empty? header-results)
- (:hide header-parser))
- failure-signal
- (let [header-result (apply max-key get-end header-results)
- end (get-end header-result)
- repeat-result (repeat-parse-no-tag grammar (:parser repeating-parser) text segment end)
- span-meta {:optimize :memory
- :instaparse.gll/start-index 0
- :instaparse.gll/end-index length}]
- (if (or (instance? instaparse.gll.Failure repeat-result)
- (and (= (:tag repeating-parser) :star)
- (empty-result? repeat-result)))
- failure-signal
- (case output-format
- :enlive
- (gll/safe-with-meta
- {:tag root-tag
- :content
- (afs/conj-flat (afs/conj-flat afs/EMPTY header-result) repeat-result)}
- span-meta)
- :hiccup
- (gll/safe-with-meta
- (afs/convert-afs-to-vec
- (afs/conj-flat (afs/conj-flat (afs/auto-flatten-seq [root-tag])
- header-result)
- repeat-result))
- span-meta)
- (gll/safe-with-meta
- (afs/conj-flat (afs/conj-flat afs/EMPTY header-result) repeat-result)
- span-meta))))))))
+ (let [segment (gll/text->segment text)
+ length (count text)
+ header-results (parse-from-index grammar header-parser text segment 0)]
+ (if (or (empty? header-results)
+ (:hide header-parser))
+ failure-signal
+ (let [header-result (apply max-key get-end header-results)
+ end (get-end header-result)
+ repeat-result (repeat-parse-no-tag grammar (:parser repeating-parser) text segment end)
+ span-meta {:optimize :memory
+ :instaparse.gll/start-index 0
+ :instaparse.gll/end-index length}]
+ (if (or (instance? instaparse.gll.Failure repeat-result)
+ (and (= (:tag repeating-parser) :star)
+ (empty-result? repeat-result)))
+ failure-signal
+ (case output-format
+ :enlive
+ (gll/safe-with-meta
+ {:tag root-tag
+ :content
+ (afs/conj-flat (afs/conj-flat afs/EMPTY header-result) repeat-result)}
+ span-meta)
+ :hiccup
+ (gll/safe-with-meta
+ (afs/convert-afs-to-vec
+ (afs/conj-flat (afs/conj-flat (afs/auto-flatten-seq [root-tag])
+ header-result)
+ repeat-result))
+ span-meta)
+ (gll/safe-with-meta
+ (afs/conj-flat (afs/conj-flat afs/EMPTY header-result) repeat-result)
+ span-meta))))))))
(defn try-repeating-parse-strategy-with-header
[grammar text start-production start-rule output-format]
@@ -213,7 +213,7 @@
result))
:else (try-repeating-parse-strategy-with-header
- grammar text start-production start-rule output-format))))
+ grammar text start-production start-rule output-format))))
(defn used-memory-optimization? [tree]
(= :memory (-> tree meta :optimize)))
\ No newline at end of file
diff --git a/src/instaparse/transform.cljc b/src/instaparse/transform.cljc
index 2e08979..7b124a9 100644
--- a/src/instaparse/transform.cljc
+++ b/src/instaparse/transform.cljc
@@ -1,7 +1,7 @@
(ns instaparse.transform
"Functions to transform parse trees"
(:require [instaparse.gll]
- [instaparse.util :refer [throw-illegal-argument-exception]]))
+ [instaparse.util :refer [throw-illegal-argument-exception]]))
(defn map-preserving-meta [f l]
(with-meta (map f l) (meta l)))
diff --git a/src/instaparse/util.cljc b/src/instaparse/util.cljc
index b0d1be8..af4a9e6 100644
--- a/src/instaparse/util.cljc
+++ b/src/instaparse/util.cljc
@@ -13,8 +13,8 @@
throw))
#?(:cljs
- (defn regexp-flags [re]
- (cond-> ""
- (.-ignoreCase re) (str "i")
- (.-multiline re) (str "m")
- (.-unicode re) (str "u"))))
+ (defn regexp-flags [re]
+ (cond-> ""
+ (.-ignoreCase re) (str "i")
+ (.-multiline re) (str "m")
+ (.-unicode re) (str "u"))))
diff --git a/test/instaparse/abnf_test.cljc b/test/instaparse/abnf_test.cljc
index 87ee901..fe13024 100644
--- a/test/instaparse/abnf_test.cljc
+++ b/test/instaparse/abnf_test.cljc
@@ -1,14 +1,14 @@
(ns instaparse.abnf-test
(:require
- #?(:clj [instaparse.core :refer [parser parses defparser]]
- :cljs [instaparse.core :refer [parser parses] :refer-macros [defparser]])
- [instaparse.core-test :refer [parsers-similar?]]
- [instaparse.combinators :refer [ebnf abnf]]
- #?(:clj [clojure.test :refer [deftest are is]]
- :cljs [cljs.test])
- #?(:clj [clojure.java.io :as io]))
+ #?(:clj [instaparse.core :refer [parser parses defparser]]
+ :cljs [instaparse.core :refer [parser parses] :refer-macros [defparser]])
+ [instaparse.core-test :refer [parsers-similar?]]
+ [instaparse.combinators :refer [ebnf abnf]]
+ #?(:clj [clojure.test :refer [deftest are is]]
+ :cljs [cljs.test])
+ #?(:clj [clojure.java.io :as io]))
#?(:cljs (:require-macros
- [cljs.test :refer [is are deftest]])))
+ [cljs.test :refer [is are deftest]])))
(defparser uri-parser
"test/data/abnf_uri.txt"
@@ -26,19 +26,19 @@
uri-parser
(binding [instaparse.abnf/*case-insensitive* true]
(parser
- "test/data/abnf_uri.txt"
- :input-format :abnf
- :instaparse.abnf/case-insensitive true))
+ "test/data/abnf_uri.txt"
+ :input-format :abnf
+ :instaparse.abnf/case-insensitive true))
(binding [instaparse.abnf/*case-insensitive* true]
(parser
- (io/resource "data/abnf_uri.txt")
- :input-format :abnf
- :instaparse.abnf/case-insensitive true))
+ (io/resource "data/abnf_uri.txt")
+ :input-format :abnf
+ :instaparse.abnf/case-insensitive true))
(binding [instaparse.abnf/*case-insensitive* true]
(parser
- (slurp "test/data/abnf_uri.txt")
- :input-format :abnf
- :instaparse.abnf/case-insensitive true)))
+ (slurp "test/data/abnf_uri.txt")
+ :input-format :abnf
+ :instaparse.abnf/case-insensitive true)))
"Verify that defparser, auto-slurp from string filename,
auto-slurp from resource (URL), and manual slurp all return
equivalent parsers.")))
@@ -210,9 +210,9 @@ to test the lookahead"
(deftest abnf-combinator-test
(let [p (parser (merge
- {:S (abnf "A / B")}
- (abnf "<A> = 1*'a'")
- {:B (abnf "'='")})
+ {:S (abnf "A / B")}
+ (abnf "<A> = 1*'a'")
+ {:B (abnf "'='")})
:start :S)]
(are [x y] (= y x)
(p "aAaa")
diff --git a/test/instaparse/auto_flatten_seq_test.cljc b/test/instaparse/auto_flatten_seq_test.cljc
index bbc74ba..ee8533b 100644
--- a/test/instaparse/auto_flatten_seq_test.cljc
+++ b/test/instaparse/auto_flatten_seq_test.cljc
@@ -1,8 +1,8 @@
(ns instaparse.auto-flatten-seq-test
(:require
- [instaparse.auto-flatten-seq :refer [auto-flatten-seq conj-flat convert-afs-to-vec]]
- #?(:clj [clojure.test :refer [deftest are is]]
- :cljs [cljs.test]))
+ [instaparse.auto-flatten-seq :refer [auto-flatten-seq conj-flat convert-afs-to-vec]]
+ #?(:clj [clojure.test :refer [deftest are is]]
+ :cljs [cljs.test]))
#?(:cljs (:require-macros [cljs.test :refer [deftest are is]])))
(defn rand-mutation [v iv]
diff --git a/test/instaparse/core_test.cljc b/test/instaparse/core_test.cljc
index a8ad3c5..414e428 100644
--- a/test/instaparse/core_test.cljc
+++ b/test/instaparse/core_test.cljc
@@ -1,21 +1,21 @@
(ns instaparse.core-test
#?(:clj (:refer-clojure :exclude [cat read-string]))
(:require
- #?(:clj [clojure.test :refer [deftest are is]]
- :cljs [cljs.test :as t])
- #?(:clj [clojure.edn :refer [read-string]]
- :cljs [cljs.reader :refer [read-string]])
- #?(:clj [instaparse.core :as insta :refer [defparser]]
- :cljs [instaparse.core :as insta :refer-macros [defparser]])
- [instaparse.cfg :refer [ebnf]]
- [instaparse.line-col :as lc]
- [instaparse.combinators-source :refer [Epsilon opt plus star rep
- alt ord cat string-ci string
- string-ci regexp nt look neg
- hide hide-tag]]
- [clojure.walk :as walk])
+ #?(:clj [clojure.test :refer [deftest are is]]
+ :cljs [cljs.test :as t])
+ #?(:clj [clojure.edn :refer [read-string]]
+ :cljs [cljs.reader :refer [read-string]])
+ #?(:clj [instaparse.core :as insta :refer [defparser]]
+ :cljs [instaparse.core :as insta :refer-macros [defparser]])
+ [instaparse.cfg :refer [ebnf]]
+ [instaparse.line-col :as lc]
+ [instaparse.combinators-source :refer [Epsilon opt plus star rep
+ alt ord cat string-ci string
+ string-ci regexp nt look neg
+ hide hide-tag]]
+ [clojure.walk :as walk])
#?(:cljs (:require-macros
- [cljs.test :refer [is are deftest run-tests]])))
+ [cljs.test :refer [is are deftest run-tests]])))
(defn parsers-similar?
"Tests if parsers are equal."
@@ -30,8 +30,8 @@
(instance? instaparse.core.Parser form)
(into {} form)
(instance? #?(:clj java.util.regex.Pattern
- :cljs js/RegExp)
- form)
+ :cljs js/RegExp)
+ form)
[::regex (str form)]
:else
form))))
@@ -366,7 +366,7 @@
Double = #'[0-9]+\\.[0-9]*|\\.[0-9]+';
<ConstExpr> = Int | Double;
Input = ConstExpr <ws> ConstExpr;"
- :start :Input))
+ :start :Input))
(def case-insensitive-regexp
(insta/parser
@@ -379,25 +379,25 @@
[:AB [:A "a" "a" "a" "a" "a"] [:B "b" "b" "b"]]
[:AB [:A "a" "a" "a" "a"] [:B "b" "b"]]]
-#?@(:clj [(as-and-bs (StringBuilder. "aaaaabbbaaaabb"))
- [:S
- [:AB [:A "a" "a" "a" "a" "a"] [:B "b" "b" "b"]]
- [:AB [:A "a" "a" "a" "a"] [:B "b" "b"]]]])
+ #?@(:clj [(as-and-bs (StringBuilder. "aaaaabbbaaaabb"))
+ [:S
+ [:AB [:A "a" "a" "a" "a" "a"] [:B "b" "b" "b"]]
+ [:AB [:A "a" "a" "a" "a"] [:B "b" "b"]]]])
(as-and-bs "aaaaabbbaaaabb")
(as-and-bs "aaaaabbbaaaabb" :optimize :memory)
(as-and-bs-enlive "aaaaabbbaaaabb")
'{:tag :S,
- :content
- ({:tag :AB,
- :content
- ({:tag :A, :content ("a" "a" "a" "a" "a")}
+ :content
+ ({:tag :AB,
+ :content
+ ({:tag :A, :content ("a" "a" "a" "a" "a")}
{:tag :B, :content ("b" "b" "b")})}
{:tag :AB,
- :content
- ({:tag :A, :content ("a" "a" "a" "a")}
- {:tag :B, :content ("b" "b")})})}
+ :content
+ ({:tag :A, :content ("a" "a" "a" "a")}
+ {:tag :B, :content ("b" "b")})})}
(as-and-bs-enlive "aaaaabbbaaaabb")
(as-and-bs-enlive "aaaaabbbaaaabb" :optimize :memory)
@@ -454,7 +454,7 @@
(insta/transform
{:num read-string
- :plus +}
+ :plus +}
(addition-e "1+2+3+4+5"))
15
@@ -499,7 +499,7 @@
(insta/parses
(insta/parser
- "Regex = (CharNonRange | Range) +
+ "Regex = (CharNonRange | Range) +
Range = Char <'-'> Char
CharNonRange = Char ! ('-' Char)
Char = #'[-x]' | 'c' (! 'd') 'x'")
@@ -518,9 +518,9 @@
(insta/parses ambiguous-tokenizer "defn my cond")
'([:sentence
- [:identifier "defn"]
- [:identifier "my"]
- [:identifier "cond"]]
+ [:identifier "defn"]
+ [:identifier "my"]
+ [:identifier "cond"]]
[:sentence [:keyword "defn"] [:identifier "my"] [:identifier "cond"]]
[:sentence [:identifier "defn"] [:identifier "my"] [:keyword "cond"]]
[:sentence [:keyword "defn"] [:identifier "my"] [:keyword "cond"]])
@@ -547,9 +547,9 @@
(words-and-numbers-one-character-at-a-time "abc 123 def" :optimize :memory)
(insta/transform
- {:word str,
- :number (comp read-string str)}
- (words-and-numbers-one-character-at-a-time "abc 123 def"))
+ {:word str,
+ :number (comp read-string str)}
+ (words-and-numbers-one-character-at-a-time "abc 123 def"))
[:sentence "abc" 123 "def"]
(->> (words-and-numbers-enlive "abc 123 def")
@@ -576,9 +576,9 @@
(arithmetic "1-2/(3-4)+5*6" :optimize :memory)
(->> (arithmetic "1-2/(3-4)+5*6")
- (insta/transform
- {:add +, :sub -, :mul *, :div /,
- :number read-string :expr identity}))
+ (insta/transform
+ {:add +, :sub -, :mul *, :div /,
+ :number read-string :expr identity}))
33
(paren-ab-hide-both-tags "(aba)")
@@ -612,7 +612,7 @@
((insta/parser "S = ('a'?)+") "" :optimize :memory)
((insta/parser
- "a = b c .
+ "a = b c .
b = 'b' .
c = 'c' .") "bc")
[:a [:b "b"] [:c "c"]]
diff --git a/test/instaparse/defparser_test.cljc b/test/instaparse/defparser_test.cljc
index 6246f0b..1c561b8 100644
--- a/test/instaparse/defparser_test.cljc
+++ b/test/instaparse/defparser_test.cljc
@@ -1,11 +1,11 @@
(ns instaparse.defparser-test
(:require
- #?(:clj [clojure.test :as t :refer [deftest are is]]
- :cljs [cljs.test :as t :refer-macros [deftest are is]])
- #?(:clj [instaparse.core :as insta :refer [defparser]]
- :cljs [instaparse.core :as insta :refer-macros [defparser]])
- [instaparse.combinators :as c]
- [instaparse.core-test :refer [parsers-similar?]]))
+ #?(:clj [clojure.test :as t :refer [deftest are is]]
+ :cljs [cljs.test :as t :refer-macros [deftest are is]])
+ #?(:clj [instaparse.core :as insta :refer [defparser]]
+ :cljs [instaparse.core :as insta :refer-macros [defparser]])
+ [instaparse.combinators :as c]
+ [instaparse.core-test :refer [parsers-similar?]]))
(defparser p1 "S = #'a' | 'b'")
diff --git a/test/instaparse/grammars.cljc b/test/instaparse/grammars.cljc
index 82dc9b1..78ba482 100644
--- a/test/instaparse/grammars.cljc
+++ b/test/instaparse/grammars.cljc
@@ -2,15 +2,15 @@
#?(:clj (:refer-clojure :exclude [cat]))
(:require #?(:clj [clojure.test :refer [deftest are]]
:cljs [cljs.test :as t])
- [instaparse.reduction :refer [apply-standard-reductions]]
- [instaparse.combinators :refer [Epsilon opt plus
- star rep alt ord
- cat string string-ci
- regexp nt look neg
- hide hide-tag
- ebnf abnf]]
- [instaparse.gll :as gll]
- [instaparse.core :as insta])
+ [instaparse.reduction :refer [apply-standard-reductions]]
+ [instaparse.combinators :refer [Epsilon opt plus
+ star rep alt ord
+ cat string string-ci
+ regexp nt look neg
+ hide hide-tag
+ ebnf abnf]]
+ [instaparse.gll :as gll]
+ [instaparse.core :as insta])
#?(:cljs (:require-macros [cljs.test :refer [is are deftest run-tests testing]])))
(defn- parse [grammar start text]
@@ -40,8 +40,8 @@
(cat (string "b") (nt :s))
Epsilon)})
(def grammar10 {:s (alt (cat (nt :s) (string "a") )
- (cat (nt :s) (string "b") )
- Epsilon)})
+ (cat (nt :s) (string "b") )
+ Epsilon)})
(def grammar11 {:s (alt (cat (nt :s) (string "a")) (string "a"))})
(def grammar12 {:s (alt (nt :a) (nt :a) (nt :a))
:a (alt (cat (nt :s) (string "a")) (string "a"))})
@@ -54,10 +54,10 @@
(cat (string "(") (nt :s) (string ")"))
(cat (nt :s) (nt :s)))})
(def non-ll-grammar {:s (alt (nt :a) (nt :b))
- :a (alt (cat (string "a") (nt :a) (string "b"))
- Epsilon)
- :b (alt (cat (string "a") (nt :b) (string "bb"))
- Epsilon)})
+ :a (alt (cat (string "a") (nt :a) (string "b"))
+ Epsilon)
+ :b (alt (cat (string "a") (nt :b) (string "bb"))
+ Epsilon)})
(def grammar14 {:s (cat (opt (string "a")) (string "b"))})
(def grammar15 {:s (cat (opt (string "a")) (opt (string "b")))})
(def grammar16 {:s (plus (string "a"))})
diff --git a/test/instaparse/repeat_test.cljc b/test/instaparse/repeat_test.cljc
index 4a79538..2eff067 100644
--- a/test/instaparse/repeat_test.cljc
+++ b/test/instaparse/repeat_test.cljc
@@ -1,12 +1,12 @@
(ns instaparse.repeat-test
(:require #?(:clj [clojure.test :refer [deftest are]]
:cljs [cljs.test :as t])
- [instaparse.core :as insta]
- [instaparse.repeat :as repeat])
+ [instaparse.core :as insta]
+ [instaparse.repeat :as repeat])
#?(:cljs (:require-macros [cljs.test :refer [are deftest]])))
(def user-parser
-"content = user-block*
+ "content = user-block*
user-block = (user before-section after-section < blank-line* >)
user = prefix separator number separator name newline
before-section = < before > lines error-line*
@@ -37,8 +37,8 @@ number = #'[0-9]+'
tree3 (parser-enlive text)
tree4 (parser-enlive text :optimize :memory)]
(and (= tree1 tree2) (= tree3 tree4)
- (= optimize? (repeat/used-memory-optimization? tree2))
- (= optimize? (repeat/used-memory-optimization? tree4))))
+ (= optimize? (repeat/used-memory-optimization? tree2))
+ (= optimize? (repeat/used-memory-optimization? tree4))))
;user-parser text true
"S = 'ab'*" "ababab" true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment