diff --git a/beautiful-racket/br/demo/jsonic/expander.rkt b/beautiful-racket/br/demo/jsonic/expander.rkt index fff5719..3312604 100644 --- a/beautiful-racket/br/demo/jsonic/expander.rkt +++ b/beautiful-racket/br/demo/jsonic/expander.rkt @@ -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))) diff --git a/beautiful-racket/br/demo/jsonic/jsonic-test.rkt b/beautiful-racket/br/demo/jsonic/jsonic-test.rkt index 618d16d..78b5a7b 100644 --- a/beautiful-racket/br/demo/jsonic/jsonic-test.rkt +++ b/beautiful-racket/br/demo/jsonic/jsonic-test.rkt @@ -1,4 +1,8 @@ #lang br/demo/jsonic -{"foo": @(+ 4 (+ 4 2)), - "bar" : ")"} \ No newline at end of file +{"number": @$(* 6 7)$@, + "string": @$(string-append "foo" "bar")$@, + "array": @$(range 5)$@, + "object": @$(hash "k1" "valstring" "k2" 42)$@ + // "bar" : + } \ No newline at end of file diff --git a/beautiful-racket/br/demo/jsonic/parser.rkt b/beautiful-racket/br/demo/jsonic/parser.rkt index c30f2ec..2cbe6c8 100644 --- a/beautiful-racket/br/demo/jsonic/parser.rkt +++ b/beautiful-racket/br/demo/jsonic/parser.rkt @@ -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 - \ No newline at end of file +s-val: /OPEN CHAR* /CLOSE \ No newline at end of file diff --git a/beautiful-racket/br/demo/jsonic/reader.rkt b/beautiful-racket/br/demo/jsonic/reader.rkt index fc7dfbe..da1f11b 100644 --- a/beautiful-racket/br/demo/jsonic/reader.rkt +++ b/beautiful-racket/br/demo/jsonic/reader.rkt @@ -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)