tuneup jsonic

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

@ -1,4 +1,4 @@
#lang br/quicklang #lang br/quicklang
(module reader br/quicklang (module reader br/quicklang
(require "jsonic/reader.rkt") (require (submod "jsonic/main.rkt" reader))
(provide (all-from-out "jsonic/reader.rkt"))) (provide (all-from-out (submod "jsonic/main.rkt" reader))))

@ -1,4 +1,5 @@
#lang br/quicklang #lang br/quicklang
(require json (for-syntax br/datum racket/string))
(provide (rename-out [jsonic-mb #%module-begin]) (provide (rename-out [jsonic-mb #%module-begin])
jsonic-program jsonic-program
char char
@ -6,13 +7,15 @@
(define-macro (jsonic-mb PARSE-TREE) (define-macro (jsonic-mb PARSE-TREE)
#'(#%module-begin #'(#%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 ...) (define-macro (jsonic-program STR ...)
#'(string-append STR ...)) #'(string-append STR ...))
(define-macro (char TOK) (define-macro (char STR)
#'TOK) #'STR)
(define (stringify result) (define (stringify result)
(cond (cond
@ -20,9 +23,10 @@
[(string? result) (format "~v" result)] [(string? result) (format "~v" result)]
[(list? result) (format "[~a]" (string-join (map stringify result) ", "))] [(list? result) (format "[~a]" (string-join (map stringify result) ", "))]
[(hash? result) (format "{~a}" (string-join (for/list ([(k v) (in-hash result)]) [(hash? result) (format "{~a}" (string-join (for/list ([(k v) (in-hash result)])
(format "~a: ~a" (stringify k) (stringify v))) ", "))] (format "~a: ~a" (stringify k) (stringify v))) ", "))]))
[else (error 'unknown-thing)]))
(define-macro (s-val TOK ...) (define-macro (s-val STR ...)
(with-pattern ([DATUM (read (open-input-string (apply string-append (map syntax->datum (syntax->list #'(TOK ...))))))]) (define s-exp-string
(string-join (map syntax->datum (syntax->list #'(STR ...))) ""))
(with-pattern ([DATUM (format-datum '~a s-exp-string)])
#'(stringify DATUM))) #'(stringify DATUM)))

@ -1,8 +1,8 @@
#lang br/demo/jsonic #lang br/demo/jsonic
{
{"number": @$(* 6 7)$@, "number": @$(* 6 7)$@,
"string": @$(string-append "foo" "bar")$@, "string": @$(string-append "foo" "bar")$@,
"array": @$(range 5)$@, "array": @$(range 5)$@,
"object": @$(hash "k1" "valstring" "k2" 42)$@ "object": @$(hash "k1" "valstring" (format "~a" 42) (hash "k1" (range 10) "k2" 42))$@
// "bar" : // "bar" :
} }

@ -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))

@ -4,4 +4,4 @@ jsonic-program: (char | s-val)*
char: CHAR char: CHAR
s-val: /OPEN CHAR* /CLOSE s-val: /OPEN CHAR* /CLOSE

@ -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))
Loading…
Cancel
Save