improve tokenizer macros
parent
ece5a5c290
commit
da2bdce15a
@ -0,0 +1,4 @@
|
||||
#lang br/quicklang
|
||||
(module reader br/quicklang
|
||||
(require (submod "jsonic-b/main.rkt" reader))
|
||||
(provide (all-from-out (submod "jsonic-b/main.rkt" reader))))
|
@ -1,37 +1,20 @@
|
||||
#lang br/quicklang
|
||||
|
||||
(require json)
|
||||
(define-macro (js-module-begin PARSE-TREE)
|
||||
#'(#%module-begin
|
||||
(define result-string PARSE-TREE)
|
||||
(when (string->jsexpr result-string)
|
||||
(display result-string))))
|
||||
(define validated-jsexpr (string->jsexpr result-string))
|
||||
(display (jsexpr->string validated-jsexpr))))
|
||||
(provide (rename-out [js-module-begin #%module-begin]))
|
||||
|
||||
(define-macro (jsonic-program S-EXP-OR-JSON-CHAR ...)
|
||||
#'(string-trim (string-append S-EXP-OR-JSON-CHAR ...)))
|
||||
(provide jsonic-program)
|
||||
|
||||
(define-macro (json-char TOKEN)
|
||||
#'TOKEN)
|
||||
(define-macro (json-char CHAR-STR) #'CHAR-STR)
|
||||
(provide json-char)
|
||||
|
||||
(define (list->json x)
|
||||
(format "[~a]" (string-join (map ->string x) ", ")))
|
||||
|
||||
(define (hash->json x)
|
||||
(format "{~a}" (string-join (for/list ([(k v) (in-hash x)])
|
||||
(format "~a: ~a" (->string k) (->string v))) ", ")))
|
||||
|
||||
(define (->string x)
|
||||
(cond
|
||||
[(number? x) (number->string x)]
|
||||
[(string? x) (format "~v" x)]
|
||||
[(list? x) (list->json x)]
|
||||
[(hash? x) (hash->json x)]
|
||||
[else ""]))
|
||||
|
||||
(define-macro (s-exp EXP-STRING)
|
||||
(with-pattern ([EXP-DATUM (format-datum '~a (syntax->datum #'EXP-STRING))])
|
||||
#'(->string EXP-DATUM)))
|
||||
(define-macro (s-exp SEXP-STR)
|
||||
(with-pattern ([SEXP-DATUM (format-datum '~a (syntax->datum #'SEXP-STR))])
|
||||
#'(jsexpr->string SEXP-DATUM)))
|
||||
(provide s-exp)
|
@ -1,10 +1,12 @@
|
||||
#lang br/demo/jsonic
|
||||
#lang br/demo/jsonic-b
|
||||
// a line comment
|
||||
[
|
||||
@$ 'null $@,
|
||||
@$ #f $@,
|
||||
@$ (* 6 7) $@,
|
||||
@$ "string" $@,
|
||||
@$ (list "array" "of" "strings") $@,
|
||||
@$ (hash "key-1" 42
|
||||
"key-2" "value"
|
||||
"key-3" (hash "subkey" 21)) $@
|
||||
@$ (hash 'key-1 42
|
||||
'key-2 "value"
|
||||
'key-3 (hash 'subkey 21)) $@
|
||||
]
|
@ -1,4 +1,4 @@
|
||||
#lang brag
|
||||
jsonic-program: (s-exp | json-char)*
|
||||
s-exp: SEXP
|
||||
json-char: CHAR
|
||||
s-exp: SEXP-TOK
|
||||
json-char: CHAR-TOK
|
||||
|
Loading…
Reference in New Issue