simplify jsonic

pull/2/head
Matthew Butterick 8 years ago
parent 2c6d8a781a
commit e792346b96

@ -1,7 +1,28 @@
#lang br/quicklang #lang br/quicklang
(provide (rename-out [jsonic-mb #%module-begin])
jsonic-program
char
s-val)
(define-macro (jsonic-mb PARSE-TREE) (define-macro (jsonic-mb PARSE-TREE)
#'(#%module-begin #'(#%module-begin
'PARSE-TREE)) (display (string-trim PARSE-TREE))))
(provide (rename-out [jsonic-mb #%module-begin]))
(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 #lang br/demo/jsonic
{"foo": @(+ 4 (+ 4 2)), {"number": @$(* 6 7)$@,
"bar" : ")"} "string": @$(string-append "foo" "bar")$@,
"array": @$(range 5)$@,
"object": @$(hash "k1" "valstring" "k2" 42)$@
// "bar" :
}

@ -1,18 +1,7 @@
#lang brag #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 (define our-lexer
(lexer (lexer
[(eof) eof] [(eof) eof]
[(or whitespace [(or (seq "//" (complement (seq any-string "\n" any-string)) "\n")) (next-token)]
(seq "//" (complement (seq any-string "\n" any-string)) "\n")) (next-token)] [(seq "@$") (token 'OPEN lexeme)]
[(char-set ",:[]{}") lexeme] [(seq "$@") (token 'CLOSE lexeme)]
[(seq "@" "(" any-string ")") (token 'S-EXP (string-trim lexeme "@"))] [any-char (token 'CHAR 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 "\""))]))
(our-lexer port)) (our-lexer port))
next-token) next-token)

Loading…
Cancel
Save