Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tree: ceb4fe2ccd
Fetching contributors…

Cannot retrieve contributors at this time

44 lines (35 sloc) 1.13 kB
(ns parsatron.languages.bencode
(:refer-clojure :exclude [char])
(:use [the.parsatron]))
(declare ben-value)
(defparser positive-int []
(let->> [digits (many1 (digit))]
(always (read-string (apply str digits)))))
(defparser negative-int []
(let->> [digits (>> (char \-) (many1 (digit)))]
(always (read-string (apply str digits)))))
(defparser ben-integer []
(between (char \i) (char \e)
(either
(positive-int)
(negative-int))))
(defparser ben-bytestring []
(let->> [length (positive-int)
_ (char \:)
chars (times length (any-char))]
(always (apply str chars))))
(defparser ben-list []
(between (char \l) (char \e)
(many (ben-value))))
(defparser ben-dictionary []
(let [entry (let->> [key (ben-value)
val (ben-value)]
(always [key val]))]
(between (char \d) (char \e)
(let->> [entries (many entry)]
(always (into {} entries))))))
(defparser ben-value []
(choice (ben-integer)
(ben-bytestring)
(ben-list)
(ben-dictionary)))
Jump to Line
Something went wrong with that request. Please try again.