okay actually they have to be strings, lets parameterize the acons
This commit is contained in:
26
d-/json.scm
26
d-/json.scm
@@ -27,11 +27,16 @@
|
||||
(define-peg-pattern eobj all (and open-cu close-cu))
|
||||
(define-peg-pattern object all (or eobj
|
||||
(and open-cu
|
||||
(* (and value (ignore ":") ++ value comma))
|
||||
(and value (ignore ":") ++ value)
|
||||
(* (and string (ignore ":") ++ value comma))
|
||||
(and string (ignore ":") ++ value)
|
||||
close-cu)))
|
||||
(define-peg-pattern value body (or number boolean string array object))
|
||||
|
||||
(define *obj-acons*
|
||||
(make-parameter
|
||||
(lambda (key value rest)
|
||||
(acons (string->symbol key) value rest))))
|
||||
|
||||
(define (parse-boolean s)
|
||||
(if (string= s "true") #t #f))
|
||||
|
||||
@@ -49,10 +54,10 @@
|
||||
(define (parse-object ss)
|
||||
(if (or (null? ss) (null? (cdr ss)))
|
||||
'()
|
||||
(let ([key (parse-value (car ss))])
|
||||
(acons (if (string? key) (string->symbol key) key)
|
||||
(parse-value (cadr ss))
|
||||
(parse-object (cddr ss))))))
|
||||
(let ([key (parse-value (car ss))]
|
||||
[value (parse-value (cadr ss))]
|
||||
[rest (parse-object (cddr ss))])
|
||||
((*obj-acons*) key value rest))))
|
||||
|
||||
(define flatwords '(boolean number string array object))
|
||||
|
||||
@@ -69,11 +74,14 @@
|
||||
(make-programming-error)
|
||||
(make-exception-with-irritants s)))))
|
||||
|
||||
(define (json string)
|
||||
(define* (json string #:key obj-acons)
|
||||
"Parses a json string into a sexp"
|
||||
(define match (match-pattern value string))
|
||||
(if match
|
||||
(parse-value (peg:tree match))
|
||||
(if match
|
||||
(if obj-acons
|
||||
(parameterize ([*obj-acons* obj-acons])
|
||||
(parse-value (peg:tree match)))
|
||||
(parse-value (peg:tree match)))
|
||||
(raise-exception (make-exception
|
||||
(make-external-error)
|
||||
(make-exception-with-irritants string)))))
|
||||
|
||||
Reference in New Issue
Block a user