simplify jsonic
This commit is contained in:
parent
2c6d8a781a
commit
e792346b96
|
@ -1,7 +1,28 @@
|
|||
#lang br/quicklang
|
||||
(provide (rename-out [jsonic-mb #%module-begin])
|
||||
jsonic-program
|
||||
char
|
||||
s-val)
|
||||
|
||||
(define-macro (jsonic-mb PARSE-TREE)
|
||||
#'(#%module-begin
|
||||
'PARSE-TREE))
|
||||
(provide (rename-out [jsonic-mb #%module-begin]))
|
||||
(display (string-trim PARSE-TREE))))
|
||||
|
||||
(define-macro (jsonic-program STR ...)
|
||||
#'(string-append STR ...))
|
||||
|
||||
(define-macro (char TOK)
|
||||
#'TOK)
|
||||
|
||||
(define (stringify result)
|
||||
(cond
|
||||
[(number? result) (number->string result)]
|
||||
[(string? result) (format "~v" result)]
|
||||
[(list? result) (format "[~a]" (string-join (map stringify result) ", "))]
|
||||
[(hash? result) (format "{~a}" (string-join (for/list ([(k v) (in-hash result)])
|
||||
(format "~a: ~a" (stringify k) (stringify v))) ", "))]
|
||||
[else (error 'unknown-thing)]))
|
||||
|
||||
(define-macro (s-val TOK ...)
|
||||
(with-pattern ([DATUM (read (open-input-string (apply string-append (map syntax->datum (syntax->list #'(TOK ...))))))])
|
||||
#'(stringify DATUM)))
|
||||
|
|
|
@ -1,4 +1,8 @@
|
|||
#lang br/demo/jsonic
|
||||
|
||||
{"foo": @(+ 4 (+ 4 2)),
|
||||
"bar" : ")"}
|
||||
{"number": @$(* 6 7)$@,
|
||||
"string": @$(string-append "foo" "bar")$@,
|
||||
"array": @$(range 5)$@,
|
||||
"object": @$(hash "k1" "valstring" "k2" 42)$@
|
||||
// "bar" :
|
||||
}
|
|
@ -1,18 +1,7 @@
|
|||
#lang brag
|
||||
|
||||
jsonic-program: value*
|
||||
jsonic-program: (char | s-val)*
|
||||
|
||||
value: array | object | string | number | s-val
|
||||
char: CHAR
|
||||
|
||||
object: /"{" [kvpair (/"," kvpair)*] /"}"
|
||||
|
||||
array: /"[" [value (/"," value)*] /"]"
|
||||
|
||||
string: STRING
|
||||
|
||||
number: NUMBER
|
||||
|
||||
kvpair: STRING /":" value
|
||||
|
||||
s-val: S-EXP
|
||||
|
||||
s-val: /OPEN CHAR* /CLOSE
|
|
@ -14,13 +14,10 @@
|
|||
(define our-lexer
|
||||
(lexer
|
||||
[(eof) eof]
|
||||
[(or whitespace
|
||||
(seq "//" (complement (seq any-string "\n" any-string)) "\n")) (next-token)]
|
||||
[(char-set ",:[]{}") lexeme]
|
||||
[(seq "@" "(" any-string ")") (token 'S-EXP (string-trim lexeme "@"))]
|
||||
[(seq (repetition 0 1 "-") (+ numeric) (repetition 0 1 (seq "." (* numeric))))
|
||||
(token 'NUMBER lexeme)] ;; Q: what is grammar for a JS number?
|
||||
[(seq "\"" (complement (seq any-string "\"" any-string)) "\"") (token 'STRING (string-trim lexeme "\""))]))
|
||||
[(or (seq "//" (complement (seq any-string "\n" any-string)) "\n")) (next-token)]
|
||||
[(seq "@$") (token 'OPEN lexeme)]
|
||||
[(seq "$@") (token 'CLOSE lexeme)]
|
||||
[any-char (token 'CHAR lexeme)]))
|
||||
(our-lexer port))
|
||||
next-token)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user