diff --git a/beautiful-racket/br/demo/jsonic.rkt b/beautiful-racket/br/demo/jsonic.rkt index fd86369..1ec7d36 100644 --- a/beautiful-racket/br/demo/jsonic.rkt +++ b/beautiful-racket/br/demo/jsonic.rkt @@ -1,4 +1,4 @@ #lang br/quicklang (module reader br/quicklang - (require "jsonic/reader.rkt") - (provide (all-from-out "jsonic/reader.rkt"))) + (require (submod "jsonic/main.rkt" reader)) + (provide (all-from-out (submod "jsonic/main.rkt" reader)))) diff --git a/beautiful-racket/br/demo/jsonic/expander.rkt b/beautiful-racket/br/demo/jsonic/expander.rkt index 3312604..83cccf7 100644 --- a/beautiful-racket/br/demo/jsonic/expander.rkt +++ b/beautiful-racket/br/demo/jsonic/expander.rkt @@ -1,4 +1,5 @@ #lang br/quicklang +(require json (for-syntax br/datum racket/string)) (provide (rename-out [jsonic-mb #%module-begin]) jsonic-program char @@ -6,13 +7,15 @@ (define-macro (jsonic-mb PARSE-TREE) #'(#%module-begin - (display (string-trim PARSE-TREE)))) + (define json-string (string-trim PARSE-TREE)) + (when (string->jsexpr json-string) + (display json-string)))) (define-macro (jsonic-program STR ...) #'(string-append STR ...)) -(define-macro (char TOK) - #'TOK) +(define-macro (char STR) + #'STR) (define (stringify result) (cond @@ -20,9 +23,10 @@ [(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)])) + (format "~a: ~a" (stringify k) (stringify v))) ", "))])) -(define-macro (s-val TOK ...) - (with-pattern ([DATUM (read (open-input-string (apply string-append (map syntax->datum (syntax->list #'(TOK ...))))))]) +(define-macro (s-val STR ...) + (define s-exp-string + (string-join (map syntax->datum (syntax->list #'(STR ...))) "")) + (with-pattern ([DATUM (format-datum '~a s-exp-string)]) #'(stringify DATUM))) diff --git a/beautiful-racket/br/demo/jsonic/jsonic-test.rkt b/beautiful-racket/br/demo/jsonic/jsonic-test.rkt index 78b5a7b..e67f6c5 100644 --- a/beautiful-racket/br/demo/jsonic/jsonic-test.rkt +++ b/beautiful-racket/br/demo/jsonic/jsonic-test.rkt @@ -1,8 +1,8 @@ #lang br/demo/jsonic - -{"number": @$(* 6 7)$@, - "string": @$(string-append "foo" "bar")$@, - "array": @$(range 5)$@, - "object": @$(hash "k1" "valstring" "k2" 42)$@ - // "bar" : - } \ No newline at end of file +{ + "number": @$(* 6 7)$@, + "string": @$(string-append "foo" "bar")$@, + "array": @$(range 5)$@, + "object": @$(hash "k1" "valstring" (format "~a" 42) (hash "k1" (range 10) "k2" 42))$@ +// "bar" : +} \ No newline at end of file diff --git a/beautiful-racket/br/demo/jsonic/main.rkt b/beautiful-racket/br/demo/jsonic/main.rkt new file mode 100644 index 0000000..e33b0f8 --- /dev/null +++ b/beautiful-racket/br/demo/jsonic/main.rkt @@ -0,0 +1,31 @@ +#lang at-exp br/quicklang +(require "parser.rkt") + +(module+ reader + (define (read-syntax path port) + (define parse-tree (parse path (tokenize port))) + (define module-datum `(module bf-mod br/demo/jsonic/expander + ,parse-tree)) + (datum->syntax #f module-datum)) + (provide read-syntax)) + +(require parser-tools/lex parser-tools/lex-sre brag/support) +(define (tokenize port) + (define (next-token) + (define our-lexer + (lexer + [(eof) eof] + ;; (char-complement "\n") means any char but "\n" + ;; (complement "\n") means any whole string except "\n" + [(seq "//" (* (char-complement "\n"))) (next-token)] + ["@$" (token 'OPEN lexeme)] + ["$@" (token 'CLOSE lexeme)] + [any-char (token 'CHAR lexeme)])) + (our-lexer port)) + next-token) + +(define (test-tokenize str) + (define ip (open-input-string str)) + (define token-producer (tokenize ip)) + (for/list ([token (in-producer token-producer eof)]) + token)) diff --git a/beautiful-racket/br/demo/jsonic/parser.rkt b/beautiful-racket/br/demo/jsonic/parser.rkt index 2cbe6c8..cd618d3 100644 --- a/beautiful-racket/br/demo/jsonic/parser.rkt +++ b/beautiful-racket/br/demo/jsonic/parser.rkt @@ -4,4 +4,4 @@ jsonic-program: (char | s-val)* char: CHAR -s-val: /OPEN CHAR* /CLOSE \ 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 deleted file mode 100644 index da1f11b..0000000 --- a/beautiful-racket/br/demo/jsonic/reader.rkt +++ /dev/null @@ -1,28 +0,0 @@ -#lang at-exp br/quicklang -(require "parser.rkt") - -(define (read-syntax path port) - (define parse-tree (parse path (tokenize port))) - (define module-datum `(module bf-mod br/demo/jsonic/expander - ,parse-tree)) - (datum->syntax #f module-datum)) -(provide read-syntax) - -(require parser-tools/lex parser-tools/lex-sre brag/support) -(define (tokenize port) - (define (next-token) - (define our-lexer - (lexer - [(eof) eof] - [(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) - -(define (test-tokenize str) - (define ip (open-input-string str)) - (define token-producer (tokenize ip)) - (for/list ([token (in-producer token-producer eof)]) - token))