PasteRack.org
Paste # 1266
2019-07-06 16:57:39

Fork as a new paste.

Paste viewed 437 times.


Embed:

#lang racket

(require megaparsack
         megaparsack/text)
(require data/monad
         data/applicative)

(define (token p)
  (do
    (many/p space/p)
    (v <- p)
    (many/p space/p)
    (pure v)))

(define (symbol s)
  (token (string/p s)))

(define json-string
  (do
    (char/p #\")
    (x <- (many/p (satisfy/p (lambda (c) (not (equal? c #\"))))))
    (char/p #\")
    (pure (list->string x))))

(define json-bool
  (or/p
   (do
     (symbol "true")
     (pure #t))
   (do
     (symbol "false")
     (pure #f))))

(define json-null
  (do
    (symbol "null")
    (pure '())))

(define ufloat
  (do
    (whole <- integer/p)
    (char/p #\.)
    (decimal <- integer/p)
    (pure (string->number (string-append (number->string whole) "." (number->string decimal))))))

(define float
  (or/p
   (do
     (char/p #\-)
     (n <- ufloat)
     (pure (- n)))
   ufloat))

(define efloat
  (do
    (frac <- float)
    (or/p (char/p #\e) (char/p #\E))
    (exp <- integer/p)
    (pure (* frac (expt 10 exp)))))

(define int
  (or/p
   (do
    (char/p #\-)
    (n <- integer/p)
    (pure (- n)))
   integer/p))

(define integer (token int))

(define json-float
  (or/p
    (try/p
     (do
      (n <- (or/p (try/p efloat) float))
      (pure n)))
    (do
      (n <- integer)
      (pure n))))

=>

standard-module-name-resolver: collection not found
  for module path: megaparsack
  collection: "megaparsack"
  in collection directories:
   /home/pasterack/.racket/7.3/collects
   /home/pasterack/racket73/collects
   ... [166 additional linked and package directories]
  context...:
   show-collection-err
   standard-module-name-resolver
   syntax-local-module-exports
   /home/pasterack/racket73/collects/racket/require-transform.rkt:266:2: expand-import
   /home/pasterack/racket73/collects/racket/private/reqprov.rkt:571:24
   /home/pasterack/racket73/collects/racket/private/reqprov.rkt:559:5
   /home/pasterack/racket73/collects/racket/require-transform.rkt:266:2: expand-import
   /home/pasterack/racket73/collects/racket/private/reqprov.rkt:266:21: try-next
   /home/pasterack/racket73/collects/racket/private/reqprov.rkt:348:21: try-next
   /home/pasterack/racket73/collects/racket/private/reqprov.rkt:243:2: require
   apply-transformer-in-context
   apply-transformer52
   dispatch-transformer41
   do-local-expand50
   /home/pasterack/racket73/collects/syntax/wrap-modbeg.rkt:46:4: do-wrapping-module-begin
   apply-transformer-in-context
   ...