From a3f434c5511925cc347d119386b7a03593eaea1e Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 23 Sep 2016 08:46:06 -0700 Subject: [PATCH] resume in lexing comments --- beautiful-racket/br/demo/jsonic.rkt | 4 +++ beautiful-racket/br/demo/jsonic/expander.rkt | 7 ++++ .../br/demo/jsonic/jsonic-test.rkt | 3 ++ beautiful-racket/br/demo/jsonic/parser.rkt | 19 ++++++++++ beautiful-racket/br/demo/jsonic/reader.rkt | 35 +++++++++++++++++++ 5 files changed, 68 insertions(+) create mode 100644 beautiful-racket/br/demo/jsonic.rkt create mode 100644 beautiful-racket/br/demo/jsonic/expander.rkt create mode 100644 beautiful-racket/br/demo/jsonic/jsonic-test.rkt create mode 100644 beautiful-racket/br/demo/jsonic/parser.rkt create mode 100644 beautiful-racket/br/demo/jsonic/reader.rkt diff --git a/beautiful-racket/br/demo/jsonic.rkt b/beautiful-racket/br/demo/jsonic.rkt new file mode 100644 index 0000000..fd86369 --- /dev/null +++ b/beautiful-racket/br/demo/jsonic.rkt @@ -0,0 +1,4 @@ +#lang br/quicklang +(module reader br/quicklang + (require "jsonic/reader.rkt") + (provide (all-from-out "jsonic/reader.rkt"))) diff --git a/beautiful-racket/br/demo/jsonic/expander.rkt b/beautiful-racket/br/demo/jsonic/expander.rkt new file mode 100644 index 0000000..fff5719 --- /dev/null +++ b/beautiful-racket/br/demo/jsonic/expander.rkt @@ -0,0 +1,7 @@ +#lang br/quicklang + +(define-macro (jsonic-mb PARSE-TREE) + #'(#%module-begin + 'PARSE-TREE)) +(provide (rename-out [jsonic-mb #%module-begin])) + diff --git a/beautiful-racket/br/demo/jsonic/jsonic-test.rkt b/beautiful-racket/br/demo/jsonic/jsonic-test.rkt new file mode 100644 index 0000000..9343f6e --- /dev/null +++ b/beautiful-racket/br/demo/jsonic/jsonic-test.rkt @@ -0,0 +1,3 @@ +#lang br/demo/jsonic + +{"id": "file"} \ No newline at end of file diff --git a/beautiful-racket/br/demo/jsonic/parser.rkt b/beautiful-racket/br/demo/jsonic/parser.rkt new file mode 100644 index 0000000..30e2da9 --- /dev/null +++ b/beautiful-racket/br/demo/jsonic/parser.rkt @@ -0,0 +1,19 @@ +#lang brag + +jsonic-program: value* + +value: array | object | string | number + +object: "{" [kvpair ("," kvpair)*] "}" + +array: "[" [value ("," value)*] "]" + +string: STRING + +number: NUMBER + +kvpair: STRING ":" value + +s-val: "@" ("[" STRING "]" | "{" STRING "}" | "#" STRING "#" ) + + \ No newline at end of file diff --git a/beautiful-racket/br/demo/jsonic/reader.rkt b/beautiful-racket/br/demo/jsonic/reader.rkt new file mode 100644 index 0000000..f513cd9 --- /dev/null +++ b/beautiful-racket/br/demo/jsonic/reader.rkt @@ -0,0 +1,35 @@ +#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] + [(seq (* "\n") (* whitespace) "//" any-string (* "\n")) (next-token)] + [whitespace (next-token)] + [(char-set ",:[]{}") lexeme] + [(seq (* "-") (+ (or 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)) + 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)) + +(module+ main + (test-tokenize @string-append{ + {"id": "file" + // yeah baby + }})) \ No newline at end of file