From 6a25de64621897fbe75f7c224e623d0c586bb8db Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Wed, 26 Feb 2014 08:27:54 -0800 Subject: [PATCH] simplify reader modules; put reader-modes in world --- file-tools.rkt | 4 ++-- lang/reader-base.rkt | 9 ++++++++- lang/reader.rkt | 7 +------ main.rkt | 10 +++++----- markup/lang/reader.rkt | 7 +------ pre/lang/reader.rkt | 7 +------ ptree.rkt | 2 +- ptree/lang/reader.rkt | 7 +------ world.rkt | 12 +++++++++--- 9 files changed, 29 insertions(+), 36 deletions(-) diff --git a/file-tools.rkt b/file-tools.rkt index a1c8d87..43edfed 100644 --- a/file-tools.rkt +++ b/file-tools.rkt @@ -152,7 +152,7 @@ (define+provide/contract (decoder-source? x) (any/c . -> . coerce/boolean?) - (and (pathish? x) ((->path x) . has-ext? . DECODER_SOURCE_EXT))) + (and (pathish? x) ((->path x) . has-ext? . MARKUP_SOURCE_EXT))) (define+provide/contract (null-source? x) @@ -196,7 +196,7 @@ (coerce/path? . -> . coerce/path?) (if (decoder-source? x) x - (add-ext x DECODER_SOURCE_EXT))) + (add-ext x MARKUP_SOURCE_EXT))) (define+provide/contract (project-files-with-ext ext) diff --git a/lang/reader-base.rkt b/lang/reader-base.rkt index 382eee5..92c4feb 100644 --- a/lang/reader-base.rkt +++ b/lang/reader-base.rkt @@ -2,7 +2,7 @@ (require (only-in scribble/reader make-at-reader) pollen/world) -(provide (all-defined-out)) +(provide (all-defined-out) (all-from-out pollen/world)) (define read-inner (make-at-reader #:command-char EXPRESSION_DELIMITER #:syntax? #t #:inside? #t)) @@ -20,3 +20,10 @@ (define reader-mode ',reader-mode) ,@file-contents) file-contents))) + +(define-syntax-rule (make-reader-with-mode mode) + (begin + (define reader-mode mode) + (define pollen-read-syntax (make-pollen-read-syntax reader-mode)) + (define pollen-read (make-pollen-read pollen-read-syntax)) + (provide (rename-out [pollen-read read] [pollen-read-syntax read-syntax]) read-inner))) diff --git a/lang/reader.rkt b/lang/reader.rkt index cc3bd3f..7db9edb 100644 --- a/lang/reader.rkt +++ b/lang/reader.rkt @@ -1,9 +1,4 @@ #lang racket/base - (require pollen/lang/reader-base) -(provide (rename-out [pollen-read read] [pollen-read-syntax read-syntax]) read-inner) - -(define reader-mode 'auto) -(define pollen-read-syntax (make-pollen-read-syntax reader-mode)) -(define pollen-read (make-pollen-read pollen-read-syntax)) \ No newline at end of file +(make-reader-with-mode reader-mode-auto) \ No newline at end of file diff --git a/main.rkt b/main.rkt index 5600a25..435c582 100644 --- a/main.rkt +++ b/main.rkt @@ -60,17 +60,17 @@ ;; set the parser mode based on reader mode (define parser-mode - (if (reader-mode . equal? . 'auto) + (if (reader-mode . equal? . reader-mode-auto) (let* ([file-ext-pattern (pregexp "\\w+$")] [here-ext (car (regexp-match file-ext-pattern inner-here-path))]) (cond - [(equal? here-ext (symbol->string PTREE_SOURCE_EXT)) 'ptree] - [(equal? here-ext (symbol->string DECODER_SOURCE_EXT)) 'markup] + [(equal? (string->symbol here-ext) PTREE_SOURCE_EXT) reader-mode-ptree] + [(equal? (string->symbol here-ext) MARKUP_SOURCE_EXT) reader-mode-markup] [else 'pre])) reader-mode)) (define main (apply (cond - [(equal? parser-mode 'ptree) + [(equal? parser-mode reader-mode-ptree) (λ xs (decode (cons PTREE_ROOT_NODE xs) #:xexpr-elements-proc (λ(xs) (filter (compose1 not (def/c whitespace?)) xs))))] ;; 'root is the hook for the decoder function. @@ -87,6 +87,6 @@ ;; for output in DrRacket (module+ main - (if (equal? parser-mode 'pre) + (if (equal? parser-mode reader-mode-preproc) (display main) (print main))))) diff --git a/markup/lang/reader.rkt b/markup/lang/reader.rkt index c69617b..d8bbb1d 100644 --- a/markup/lang/reader.rkt +++ b/markup/lang/reader.rkt @@ -1,9 +1,4 @@ #lang racket/base - (require pollen/lang/reader-base) -(provide (rename-out [pollen-read read] [pollen-read-syntax read-syntax]) read-inner) - -(define reader-mode 'markup) -(define pollen-read-syntax (make-pollen-read-syntax reader-mode)) -(define pollen-read (make-pollen-read pollen-read-syntax)) \ No newline at end of file +(make-reader-with-mode reader-mode-markup) \ No newline at end of file diff --git a/pre/lang/reader.rkt b/pre/lang/reader.rkt index 6359f6c..0c29073 100644 --- a/pre/lang/reader.rkt +++ b/pre/lang/reader.rkt @@ -1,9 +1,4 @@ #lang racket/base - (require pollen/lang/reader-base) -(provide (rename-out [pollen-read read] [pollen-read-syntax read-syntax]) read-inner) - -(define reader-mode 'pre) -(define pollen-read-syntax (make-pollen-read-syntax reader-mode)) -(define pollen-read (make-pollen-read pollen-read-syntax)) \ No newline at end of file +(make-reader-with-mode reader-mode-preproc) \ No newline at end of file diff --git a/ptree.rkt b/ptree.rkt index c648a5c..fb7568f 100644 --- a/ptree.rkt +++ b/ptree.rkt @@ -51,7 +51,7 @@ (define+provide/contract (directory->ptree dir) (directory-pathish? . -> . ptree?) - (let ([files (map remove-ext (filter (λ(x) (has-ext? x DECODER_SOURCE_EXT)) (directory-list dir)))]) + (let ([files (map remove-ext (filter (λ(x) (has-ext? x MARKUP_SOURCE_EXT)) (directory-list dir)))]) (ptree-root->ptree (cons PTREE_ROOT_NODE files)))) ;; Try loading from ptree file, or failing that, synthesize ptree. diff --git a/ptree/lang/reader.rkt b/ptree/lang/reader.rkt index 5f2dcc6..fa801ea 100644 --- a/ptree/lang/reader.rkt +++ b/ptree/lang/reader.rkt @@ -1,9 +1,4 @@ #lang racket/base - (require pollen/lang/reader-base) -(provide (rename-out [pollen-read read] [pollen-read-syntax read-syntax]) read-inner) - -(define reader-mode 'ptree) -(define pollen-read-syntax (make-pollen-read-syntax reader-mode)) -(define pollen-read (make-pollen-read pollen-read-syntax)) \ No newline at end of file +(make-reader-with-mode reader-mode-ptree) \ No newline at end of file diff --git a/world.rkt b/world.rkt index 66d117d..ea7dcd1 100644 --- a/world.rkt +++ b/world.rkt @@ -7,10 +7,10 @@ (define POLLEN_VERSION "0.001") (define PREPROC_SOURCE_EXT 'p) -(define DECODER_SOURCE_EXT 'pm) +(define MARKUP_SOURCE_EXT 'pm) (define NULL_SOURCE_EXT 'px) (define PTREE_SOURCE_EXT 'ptree) -(define DECODABLE_EXTENSIONS (list DECODER_SOURCE_EXT PTREE_SOURCE_EXT)) +(define DECODABLE_EXTENSIONS (list MARKUP_SOURCE_EXT PTREE_SOURCE_EXT)) (define DEFAULT_PTREE "main.ptree") (define PTREE_ROOT_NODE 'ptree-root) @@ -55,4 +55,10 @@ (define DASHBOARD_CSS "poldash.css") (define current-module-root (make-parameter #f)) -(define current-server-extras-path (make-parameter #f)) \ No newline at end of file +(define current-server-extras-path (make-parameter #f)) + + +(define reader-mode-auto 'auto) +(define reader-mode-preproc 'pre) +(define reader-mode-markup 'markup) +(define reader-mode-ptree 'ptree)