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
|
#lang br/quicklang
|
||||||
|
|
||||||
(require json)
|
(require json)
|
||||||
(define-macro (js-module-begin PARSE-TREE)
|
(define-macro (js-module-begin PARSE-TREE)
|
||||||
#'(#%module-begin
|
#'(#%module-begin
|
||||||
(define result-string PARSE-TREE)
|
(define result-string PARSE-TREE)
|
||||||
(when (string->jsexpr result-string)
|
(define validated-jsexpr (string->jsexpr result-string))
|
||||||
(display result-string))))
|
(display (jsexpr->string validated-jsexpr))))
|
||||||
(provide (rename-out [js-module-begin #%module-begin]))
|
(provide (rename-out [js-module-begin #%module-begin]))
|
||||||
|
|
||||||
(define-macro (jsonic-program S-EXP-OR-JSON-CHAR ...)
|
(define-macro (jsonic-program S-EXP-OR-JSON-CHAR ...)
|
||||||
#'(string-trim (string-append S-EXP-OR-JSON-CHAR ...)))
|
#'(string-trim (string-append S-EXP-OR-JSON-CHAR ...)))
|
||||||
(provide jsonic-program)
|
(provide jsonic-program)
|
||||||
|
|
||||||
(define-macro (json-char TOKEN)
|
(define-macro (json-char CHAR-STR) #'CHAR-STR)
|
||||||
#'TOKEN)
|
|
||||||
(provide json-char)
|
(provide json-char)
|
||||||
|
|
||||||
(define (list->json x)
|
(define-macro (s-exp SEXP-STR)
|
||||||
(format "[~a]" (string-join (map ->string x) ", ")))
|
(with-pattern ([SEXP-DATUM (format-datum '~a (syntax->datum #'SEXP-STR))])
|
||||||
|
#'(jsexpr->string SEXP-DATUM)))
|
||||||
(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)))
|
|
||||||
(provide s-exp)
|
(provide s-exp)
|
@ -1,10 +1,12 @@
|
|||||||
#lang br/demo/jsonic
|
#lang br/demo/jsonic-b
|
||||||
// a line comment
|
// a line comment
|
||||||
[
|
[
|
||||||
|
@$ 'null $@,
|
||||||
|
@$ #f $@,
|
||||||
@$ (* 6 7) $@,
|
@$ (* 6 7) $@,
|
||||||
@$ "string" $@,
|
@$ "string" $@,
|
||||||
@$ (list "array" "of" "strings") $@,
|
@$ (list "array" "of" "strings") $@,
|
||||||
@$ (hash "key-1" 42
|
@$ (hash 'key-1 42
|
||||||
"key-2" "value"
|
'key-2 "value"
|
||||||
"key-3" (hash "subkey" 21)) $@
|
'key-3 (hash 'subkey 21)) $@
|
||||||
]
|
]
|
@ -1,4 +1,4 @@
|
|||||||
#lang brag
|
#lang brag
|
||||||
jsonic-program: (s-exp | json-char)*
|
jsonic-program: (s-exp | json-char)*
|
||||||
s-exp: SEXP
|
s-exp: SEXP-TOK
|
||||||
json-char: CHAR
|
json-char: CHAR-TOK
|
||||||
|
Loading…
Reference in New Issue