From 8dea96894b6965189bfb873fbf24751c2e440935 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 6 May 2016 10:34:43 -0700 Subject: [PATCH 01/13] add dependency --- brag/info.rkt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brag/info.rkt b/brag/info.rkt index 45a38e2..810a1d4 100755 --- a/brag/info.rkt +++ b/brag/info.rkt @@ -1,6 +1,6 @@ #lang setup/infotab -(define deps '("base" "parser-tools-lib" "rackunit-lib" "python-tokenizer")) +(define deps '("base" "beautiful-racket-lib" "parser-tools-lib" "rackunit-lib" "python-tokenizer")) (define build-deps '("at-exp-lib" "parser-tools-doc" "racket-doc" "scribble-lib")) (define collection 'multi) From f072c9f808e1cd39e6895c6355bf93d6d3250b28 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 6 May 2016 10:55:04 -0700 Subject: [PATCH 02/13] angle-hiding in parser --- beautiful-racket/br/demo/txtadv/expander.rkt | 20 ++++++++------------ beautiful-racket/br/demo/txtadv/parser.rkt | 18 +++++++++--------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/beautiful-racket/br/demo/txtadv/expander.rkt b/beautiful-racket/br/demo/txtadv/expander.rkt index af11bf1..2c64400 100644 --- a/beautiful-racket/br/demo/txtadv/expander.rkt +++ b/beautiful-racket/br/demo/txtadv/expander.rkt @@ -35,7 +35,7 @@ #'(module-begin _section ...)) (provide verb-section) -(define-inverting #'(verb-section _heading _verb-item ...) +(define-inverting #'(verb-section _verb-item ...) (inject-syntax ([#'in-verbs (shared-syntax 'in-verbs)]) #'(define-verbs in-verbs _verb-item ...))) @@ -47,14 +47,11 @@ (provide verb-name) (define-cases #'verb-name - ;; cases with literals go first, so they're not caught by wildcards - [#'(_ "," _id) #'(_id #f)] - [#'(_ "," _id _underscore) #'(_id #t)] [#'(_ _id) #'(_id #f)] [#'(_ _id _underscore) #'(_id #t)]) (provide everywhere-section) -(define-inverting #'(everywhere-section _heading [_name _desc] ...) +(define-inverting #'(everywhere-section [_name _desc] ...) #'(define-everywhere everywhere-actions ([_name _desc] ...))) @@ -63,7 +60,7 @@ #'(_id _desc)) (provide things-section) -(define-inverting #'(things-section _heading _thing ...) +(define-inverting #'(things-section _thing ...) #'(begin _thing ...)) (provide thing-item) @@ -71,7 +68,7 @@ #'(define-thing _thingname [_actionname _actiondesc] ...)) (provide places-section) -(define-inverting #'(places-section _heading _placeitem ...) +(define-inverting #'(places-section _placeitem ...) #'(begin _placeitem ...)) (provide place-item) @@ -82,18 +79,17 @@ (define #'(place-descrip _desc) #'_desc) (provide place-items) -(define-inverting #'(place-items "[" _id ... "]") #'(_id ...)) +(define-inverting #'(place-items _id ...) #'(_id ...)) (provide place-name) (define-cases #'place-name - [#'(_ "," _id) #'_id] [#'(_ _id) #'_id]) (provide s-exp) (define-cases-inverting #'s-exp - [#'(_ "(" _sx ... ")") #'(_sx ...)] - [#'(_ _sx) #'_sx]) + [#'(_ _sx) #'_sx] + [#'(_ _sx ... ) #'(_sx ...)]) ;; todo: consolidate the game-starters. @@ -107,7 +103,7 @@ everywhere-actions))) (provide start-section) -(define #'(start-section _heading _where) +(define #'(start-section _where) (inject-syntax ([#'in-verbs (shared-syntax 'in-verbs)]) #'(init-game _where in-verbs diff --git a/beautiful-racket/br/demo/txtadv/parser.rkt b/beautiful-racket/br/demo/txtadv/parser.rkt index afa8e0f..9a0798c 100644 --- a/beautiful-racket/br/demo/txtadv/parser.rkt +++ b/beautiful-racket/br/demo/txtadv/parser.rkt @@ -2,30 +2,30 @@ txtadv-program : verb-section everywhere-section things-section places-section start-section -verb-section : "===VERBS===" verb-item+ +verb-section : <"===VERBS==="> verb-item+ verb-item : verb-name+ s-exp -verb-name : [","] ID ["_"] +verb-name : [<",">] ID ["_"] -everywhere-section : "===EVERYWHERE===" id-desc+ +everywhere-section : <"===EVERYWHERE==="> id-desc+ -things-section : "===THINGS===" thing-item+ +things-section : <"===THINGS==="> thing-item+ thing-item : DASHED-NAME id-desc+ -places-section : "===PLACES===" place-item+ +places-section : <"===PLACES==="> place-item+ place-item : DASHED-NAME place-descrip place-items id-desc+ place-descrip : STRING ; `place-desc` is already used in expander -place-items : "[" place-name* "]" ; `place-things` is already used +place-items : <"["> place-name* <"]"> ; `place-things` is already used -place-name : [","] ID +place-name : [<",">] ID -start-section : "===START===" place-name +start-section : <"===START==="> place-name id-desc : ID s-exp -s-exp : ID | STRING | "(" s-exp* ")" \ No newline at end of file +s-exp : ID | STRING | <"("> s-exp* <")"> \ No newline at end of file From 2026c603de63cb04bcc8449d484b6457956b9910 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 6 May 2016 12:36:25 -0700 Subject: [PATCH 03/13] make ids hideable --- brag/brag/codegen/codegen.rkt | 52 +++++++++++------------ brag/brag/codegen/runtime.rkt | 14 +++--- brag/brag/elider/json-elider-toy.rkt | 4 +- brag/brag/elider/json-elider.rkt | 2 +- brag/brag/elider/test-json-elider-toy.rkt | 9 ++++ brag/brag/elider/test-json-elider.rkt | 4 +- 6 files changed, 45 insertions(+), 40 deletions(-) create mode 100644 brag/brag/elider/test-json-elider-toy.rkt diff --git a/brag/brag/codegen/codegen.rkt b/brag/brag/codegen/codegen.rkt index 2b91fd3..c459fe1 100755 --- a/brag/brag/codegen/codegen.rkt +++ b/brag/brag/codegen/codegen.rkt @@ -43,8 +43,8 @@ MB: `rules` still carries 'hide syntax property |# #;(report flattened-rules) - - + + #| MB: `flattened-rules` still carries 'hide syntax property |# @@ -52,7 +52,7 @@ #;(report flattened-rules) (define generated-rule-codes (map flat-rule->yacc-rule flattened-rules)) - + #| MB: `generated-rule-codes` loses the 'hide syntax property |# @@ -216,29 +216,29 @@ (for/list ([translated-pattern (in-list translated-patterns)] [primitive-pattern (syntax->list a-clause)] [pos (in-naturals 1)]) - (with-syntax ([$X - (format-id translated-pattern "$~a" pos)] - [$X-start-pos - (format-id translated-pattern "$~a-start-pos" pos)] - [$X-end-pos - (format-id translated-pattern "$~a-end-pos" pos)]) - (syntax-case primitive-pattern (id lit token inferred-id) - - ;; When a rule usage is inferred, the value of $X is a syntax object - ;; whose head is the name of the inferred rule . We strip that out, - ;; leaving the residue to be absorbed. - [(inferred-id val reason) - #'(syntax-case $X () - [(inferred-rule-name . rest) - (syntax->list #'rest)])] - [(id val) - #'(list $X)] - ;; move the 'hide syntax property into the translated-action - ;; because syntax gets datum-ized - [(lit val) - #`(list (atomic-datum->syntax $X $X-start-pos $X-end-pos #,(syntax-property primitive-pattern 'hide)))] - [(token val) - #`(list (atomic-datum->syntax $X $X-start-pos $X-end-pos #,(syntax-property primitive-pattern 'hide)))])))) + (if (syntax-property primitive-pattern 'hide) + #'null + (with-syntax ([$X + (format-id translated-pattern "$~a" pos)] + [$X-start-pos + (format-id translated-pattern "$~a-start-pos" pos)] + [$X-end-pos + (format-id translated-pattern "$~a-end-pos" pos)]) + (syntax-case primitive-pattern (id lit token inferred-id) + + ;; When a rule usage is inferred, the value of $X is a syntax object + ;; whose head is the name of the inferred rule . We strip that out, + ;; leaving the residue to be absorbed. + [(inferred-id val reason) + #'(syntax-case $X () + [(inferred-rule-name . rest) + (syntax->list #'rest)])] + [(id val) + #'(list $X)] + [(lit val) + #'(list (atomic-datum->syntax $X $X-start-pos $X-end-pos))] + [(token val) + #'(list (atomic-datum->syntax $X $X-start-pos $X-end-pos))]))))) (define whole-rule-loc (if (empty? translated-patterns) diff --git a/brag/brag/codegen/runtime.rkt b/brag/brag/codegen/runtime.rkt index eb220da..03eed87 100755 --- a/brag/brag/codegen/runtime.rkt +++ b/brag/brag/codegen/runtime.rkt @@ -150,15 +150,12 @@ This would be the place to check a syntax property for hiding. (define stx-with-original?-property (read-syntax #f (open-input-string "meaningless-string"))) -(define elided (gensym)) ;; atomic-datum->syntax: datum position position ;; Helper that does the ugly work in wrapping a datum into a syntax ;; with source location. -(define (atomic-datum->syntax d start-pos end-pos [hide? #f]) - (if hide? - elided - (datum->syntax #f d (positions->srcloc start-pos end-pos) stx-with-original?-property))) +(define (atomic-datum->syntax d start-pos end-pos) + (datum->syntax #f d (positions->srcloc start-pos end-pos) stx-with-original?-property)) @@ -166,10 +163,9 @@ This would be the place to check a syntax property for hiding. ;; Creates an stx out of the rule name and its components. ;; The location information of the rule spans that of its components. (define (rule-components->syntax rule-name/false #:srcloc [srcloc #f] . components) - (define flattened-elided-components (filter-not (λ(c) (eq? c elided)) (apply append components))) (datum->syntax #f (cons - (datum->syntax #f rule-name/false srcloc stx-with-original?-property) - flattened-elided-components) + (datum->syntax #f rule-name/false srcloc stx-with-original?-property) + (apply append components)) srcloc - stx-with-original?-property)) + stx-with-original?-property)) \ No newline at end of file diff --git a/brag/brag/elider/json-elider-toy.rkt b/brag/brag/elider/json-elider-toy.rkt index 3d55a38..8eed9e8 100644 --- a/brag/brag/elider/json-elider-toy.rkt +++ b/brag/brag/elider/json-elider-toy.rkt @@ -1,4 +1,4 @@ #lang brag -;; Simple baby example of JSON structure -json: ID <":"> ID +thing : foo +foo : <"bar"> diff --git a/brag/brag/elider/json-elider.rkt b/brag/brag/elider/json-elider.rkt index 224acd4..cdebc9e 100755 --- a/brag/brag/elider/json-elider.rkt +++ b/brag/brag/elider/json-elider.rkt @@ -14,4 +14,4 @@ array: "[" [json ("," json)*] "]" object: <"{"> [kvpair ("," kvpair)*] <"}"> -kvpair: <":"> json +kvpair: ":" diff --git a/brag/brag/elider/test-json-elider-toy.rkt b/brag/brag/elider/test-json-elider-toy.rkt new file mode 100644 index 0000000..c3072c3 --- /dev/null +++ b/brag/brag/elider/test-json-elider-toy.rkt @@ -0,0 +1,9 @@ +#lang br +(require "json-elider-toy.rkt" + brag/support + rackunit) + +(check-equal? + (syntax->datum + (parse (list "bar"))) + '(thing)) diff --git a/brag/brag/elider/test-json-elider.rkt b/brag/brag/elider/test-json-elider.rkt index bbb7e4d..89287ed 100755 --- a/brag/brag/elider/test-json-elider.rkt +++ b/brag/brag/elider/test-json-elider.rkt @@ -10,10 +10,10 @@ ":" (token 'STRING "'hello world'") "}"))) - '(json (object (kvpair (json (string "'hello world'")))))) + '(json (object (kvpair "message" (json (string "'hello world'")))))) -(check-equal? +#;(check-equal? (syntax->datum (parse "[[[{}]],[],[[{}]]]")) '(json (array #\[ (json (array #\[ (json (array #\[ (json (object)) #\])) #\])) #\, (json (array #\[ #\])) #\, (json (array #\[ (json (array #\[ (json (object )) #\])) #\])) #\]))) From 6d801934193ed77be651b61f02562889c2b1bd12 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 6 May 2016 12:58:15 -0700 Subject: [PATCH 04/13] lhs-id modifications ; tests pass --- brag/brag/rules/lexer.rkt | 5 ++--- brag/brag/rules/parser.rkt | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/brag/brag/rules/lexer.rkt b/brag/brag/rules/lexer.rkt index 877f563..32a205f 100755 --- a/brag/brag/rules/lexer.rkt +++ b/brag/brag/rules/lexer.rkt @@ -22,9 +22,6 @@ (:+ id-char))) - - - (define lex/1 (lexer-src-pos [(:: "'" @@ -64,6 +61,8 @@ (token-EOF lexeme)] [(:: id (:* whitespace) ":") (token-RULE_HEAD lexeme)] + [(:: "<" id ">" (:* whitespace) ":") + (token-RULE_HEAD_HIDDEN lexeme)] [id (token-ID lexeme)] diff --git a/brag/brag/rules/parser.rkt b/brag/brag/rules/parser.rkt index a9dc02d..5a318dc 100755 --- a/brag/brag/rules/parser.rkt +++ b/brag/brag/rules/parser.rkt @@ -17,6 +17,7 @@ token-PIPE token-REPEAT token-RULE_HEAD + token-RULE_HEAD_HIDDEN token-ID token-LIT token-EOF @@ -45,6 +46,7 @@ PIPE REPEAT RULE_HEAD + RULE_HEAD_HIDDEN ID LIT EOF)) @@ -84,7 +86,23 @@ (position-col $1-start-pos)) trimmed #f) - $2))]] + $2))] + + ;; angles indicate hiding. set hide value to #t + [(RULE_HEAD_HIDDEN pattern) + (begin + (begin + (define trimmed (cadr (regexp-match #px"<(\\w+)>\\s*:$" $1))) + (rule (position->pos $1-start-pos) + (position->pos $2-end-pos) + (lhs-id (position->pos $1-start-pos) + (pos (+ (position-offset $1-start-pos) + (string-length trimmed)) + (position-line $1-start-pos) + (position-col $1-start-pos)) + trimmed + #t) + $2)))]] [pattern [(implicit-pattern-sequence PIPE pattern) From f6181b90d72250cca79353bc406a3b53a1feb9d2 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 6 May 2016 13:22:38 -0700 Subject: [PATCH 05/13] lhs-id splicing works ; tests pass --- brag/brag/codegen/codegen.rkt | 8 +++++--- brag/brag/codegen/runtime.rkt | 22 ++++++++++++++-------- brag/brag/elider/json-elider.rkt | 2 +- brag/brag/elider/test-json-elider.rkt | 4 ++-- brag/brag/rules/stx.rkt | 24 +++++++++++++----------- 5 files changed, 35 insertions(+), 25 deletions(-) diff --git a/brag/brag/codegen/codegen.rkt b/brag/brag/codegen/codegen.rkt index c459fe1..f8ec6dc 100755 --- a/brag/brag/codegen/codegen.rkt +++ b/brag/brag/codegen/codegen.rkt @@ -42,7 +42,7 @@ #| MB: `rules` still carries 'hide syntax property |# - #;(report flattened-rules) + #;(report rules) #| @@ -54,7 +54,7 @@ (define generated-rule-codes (map flat-rule->yacc-rule flattened-rules)) #| - MB: `generated-rule-codes` loses the 'hide syntax property + MB: `generated-rule-codes` loses the 'hide syntax property (but not for lhs-ids) |# #;(report generated-rule-codes) @@ -179,6 +179,7 @@ ;; stx :== (name (U tokens rule-stx) ...) ;; (define (flat-rule->yacc-rule a-flat-rule) + ;; lhs-ids still carry 'hide property on #'name field #;(report a-flat-rule) (syntax-case a-flat-rule () [(rule-type origin name clauses ...) @@ -251,7 +252,8 @@ [(translated-action ...) translated-actions]) #`[(translated-pattern ...) (rule-components->syntax '#,rule-name/false translated-action ... - #:srcloc #,whole-rule-loc)])) + #:srcloc #,whole-rule-loc + #:splice? #,(syntax-property rule-name/false 'hide))])) diff --git a/brag/brag/codegen/runtime.rkt b/brag/brag/codegen/runtime.rkt index 03eed87..ebb8902 100755 --- a/brag/brag/codegen/runtime.rkt +++ b/brag/brag/codegen/runtime.rkt @@ -158,14 +158,20 @@ This would be the place to check a syntax property for hiding. (datum->syntax #f d (positions->srcloc start-pos end-pos) stx-with-original?-property)) - +(define splice-signal '@) ;; rule-components->syntax: (U symbol false) (listof stx) ... #:srcloc (U #f (list src line column offset span)) -> stx ;; Creates an stx out of the rule name and its components. ;; The location information of the rule spans that of its components. -(define (rule-components->syntax rule-name/false #:srcloc [srcloc #f] . components) - (datum->syntax #f - (cons - (datum->syntax #f rule-name/false srcloc stx-with-original?-property) - (apply append components)) - srcloc - stx-with-original?-property)) \ No newline at end of file +(define (rule-components->syntax rule-name/false #:srcloc [srcloc #f] #:splice? [splice #f] . componentss) + (let ([componentss (append-map (λ(cs) + (if (and (pair? cs) (syntax-property (car cs) 'splice)) + (list (cdr (syntax->list (car cs)))) + (list cs))) componentss)]) + (syntax-property + (datum->syntax #f + (cons + (datum->syntax #f rule-name/false srcloc stx-with-original?-property) + (apply append componentss)) + srcloc + stx-with-original?-property) + 'splice splice))) \ No newline at end of file diff --git a/brag/brag/elider/json-elider.rkt b/brag/brag/elider/json-elider.rkt index cdebc9e..677a02d 100755 --- a/brag/brag/elider/json-elider.rkt +++ b/brag/brag/elider/json-elider.rkt @@ -14,4 +14,4 @@ array: "[" [json ("," json)*] "]" object: <"{"> [kvpair ("," kvpair)*] <"}"> -kvpair: ":" + : ":" diff --git a/brag/brag/elider/test-json-elider.rkt b/brag/brag/elider/test-json-elider.rkt index 89287ed..c7e48d0 100755 --- a/brag/brag/elider/test-json-elider.rkt +++ b/brag/brag/elider/test-json-elider.rkt @@ -10,10 +10,10 @@ ":" (token 'STRING "'hello world'") "}"))) - '(json (object (kvpair "message" (json (string "'hello world'")))))) + '(json (object ":"))) -#;(check-equal? +(check-equal? (syntax->datum (parse "[[[{}]],[],[[{}]]]")) '(json (array #\[ (json (array #\[ (json (array #\[ (json (object)) #\])) #\])) #\, (json (array #\[ #\])) #\, (json (array #\[ (json (array #\[ (json (object )) #\])) #\])) #\]))) diff --git a/brag/brag/rules/stx.rkt b/brag/brag/rules/stx.rkt index 0317fcf..80197b5 100755 --- a/brag/brag/rules/stx.rkt +++ b/brag/brag/rules/stx.rkt @@ -21,17 +21,19 @@ (define (rule->stx source a-rule) (define id-stx - (datum->syntax #f - (string->symbol (lhs-id-val (rule-lhs a-rule))) - (list source - (pos-line (lhs-id-start (rule-lhs a-rule))) - (pos-col (lhs-id-start (rule-lhs a-rule))) - (pos-offset (lhs-id-start (rule-lhs a-rule))) - (if (and (number? (pos-offset (lhs-id-start (rule-lhs a-rule)))) - (number? (pos-offset (lhs-id-end (rule-lhs a-rule))))) - (- (pos-offset (lhs-id-end (rule-lhs a-rule))) - (pos-offset (lhs-id-start (rule-lhs a-rule)))) - #f)))) + (syntax-property + (datum->syntax #f + (string->symbol (lhs-id-val (rule-lhs a-rule))) + (list source + (pos-line (lhs-id-start (rule-lhs a-rule))) + (pos-col (lhs-id-start (rule-lhs a-rule))) + (pos-offset (lhs-id-start (rule-lhs a-rule))) + (if (and (number? (pos-offset (lhs-id-start (rule-lhs a-rule)))) + (number? (pos-offset (lhs-id-end (rule-lhs a-rule))))) + (- (pos-offset (lhs-id-end (rule-lhs a-rule))) + (pos-offset (lhs-id-start (rule-lhs a-rule)))) + #f))) + 'hide (lhs-id-hide (rule-lhs a-rule)))) (define pattern-stx (pattern->stx source (rule-pattern a-rule))) (define line (pos-line (rule-start a-rule))) (define column (pos-col (rule-start a-rule))) From 992fccdb1d6cf3f998e0faaaff7f536aa2869d39 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 6 May 2016 13:24:15 -0700 Subject: [PATCH 06/13] change lhs-id key from 'hide to 'splice --- brag/brag/codegen/codegen.rkt | 6 +++--- brag/brag/rules/rule-structs.rkt | 2 +- brag/brag/rules/stx.rkt | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/brag/brag/codegen/codegen.rkt b/brag/brag/codegen/codegen.rkt index f8ec6dc..b58f8df 100755 --- a/brag/brag/codegen/codegen.rkt +++ b/brag/brag/codegen/codegen.rkt @@ -54,7 +54,7 @@ (define generated-rule-codes (map flat-rule->yacc-rule flattened-rules)) #| - MB: `generated-rule-codes` loses the 'hide syntax property (but not for lhs-ids) + MB: `generated-rule-codes` loses the 'hide syntax property (but lhs-ids carry 'splice) |# #;(report generated-rule-codes) @@ -179,7 +179,7 @@ ;; stx :== (name (U tokens rule-stx) ...) ;; (define (flat-rule->yacc-rule a-flat-rule) - ;; lhs-ids still carry 'hide property on #'name field + ;; lhs-ids still carry 'splice property on #'name field #;(report a-flat-rule) (syntax-case a-flat-rule () [(rule-type origin name clauses ...) @@ -253,7 +253,7 @@ #`[(translated-pattern ...) (rule-components->syntax '#,rule-name/false translated-action ... #:srcloc #,whole-rule-loc - #:splice? #,(syntax-property rule-name/false 'hide))])) + #:splice? #,(syntax-property rule-name/false 'splice))])) diff --git a/brag/brag/rules/rule-structs.rkt b/brag/brag/rules/rule-structs.rkt index b4d64af..33a2e8d 100755 --- a/brag/brag/rules/rule-structs.rkt +++ b/brag/brag/rules/rule-structs.rkt @@ -12,7 +12,7 @@ (struct rule (start end lhs pattern) #:transparent) -(struct lhs-id (start end val hide) +(struct lhs-id (start end val splice) #:transparent) diff --git a/brag/brag/rules/stx.rkt b/brag/brag/rules/stx.rkt index 80197b5..a0f7b84 100755 --- a/brag/brag/rules/stx.rkt +++ b/brag/brag/rules/stx.rkt @@ -33,7 +33,7 @@ (- (pos-offset (lhs-id-end (rule-lhs a-rule))) (pos-offset (lhs-id-start (rule-lhs a-rule)))) #f))) - 'hide (lhs-id-hide (rule-lhs a-rule)))) + 'splice (lhs-id-splice (rule-lhs a-rule)))) (define pattern-stx (pattern->stx source (rule-pattern a-rule))) (define line (pos-line (rule-start a-rule))) (define column (pos-col (rule-start a-rule))) From 07350988e7875f7cf5f8ddda40d5afef007e0929 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 6 May 2016 13:32:36 -0700 Subject: [PATCH 07/13] make hider/splicer tests --- brag/brag/codegen/codegen.rkt | 3 ++- brag/brag/codegen/runtime.rkt | 6 +++--- brag/brag/elider/json-elider-toy.rkt | 4 ---- brag/brag/elider/test-json-elider-toy.rkt | 9 --------- .../json-elider.rkt => examples/baby-json-hider.rkt} | 5 ++--- .../test-baby-json-hider.rkt} | 4 ++-- 6 files changed, 9 insertions(+), 22 deletions(-) delete mode 100644 brag/brag/elider/json-elider-toy.rkt delete mode 100644 brag/brag/elider/test-json-elider-toy.rkt rename brag/brag/{elider/json-elider.rkt => examples/baby-json-hider.rkt} (78%) rename brag/brag/{elider/test-json-elider.rkt => test/test-baby-json-hider.rkt} (90%) diff --git a/brag/brag/codegen/codegen.rkt b/brag/brag/codegen/codegen.rkt index b58f8df..75837c9 100755 --- a/brag/brag/codegen/codegen.rkt +++ b/brag/brag/codegen/codegen.rkt @@ -247,7 +247,8 @@ (with-syntax ([$1-start-pos (datum->syntax (first translated-patterns) '$1-start-pos)] [$n-end-pos (format-id (last translated-patterns) "$~a-end-pos" (length translated-patterns))]) #`(positions->srcloc $1-start-pos $n-end-pos)))) - + + ;; move 'splice property into function because name is datum-ized (with-syntax ([(translated-pattern ...) translated-patterns] [(translated-action ...) translated-actions]) #`[(translated-pattern ...) diff --git a/brag/brag/codegen/runtime.rkt b/brag/brag/codegen/runtime.rkt index ebb8902..12212ca 100755 --- a/brag/brag/codegen/runtime.rkt +++ b/brag/brag/codegen/runtime.rkt @@ -163,15 +163,15 @@ This would be the place to check a syntax property for hiding. ;; Creates an stx out of the rule name and its components. ;; The location information of the rule spans that of its components. (define (rule-components->syntax rule-name/false #:srcloc [srcloc #f] #:splice? [splice #f] . componentss) - (let ([componentss (append-map (λ(cs) + (let ([spliced-componentss (append-map (λ(cs) (if (and (pair? cs) (syntax-property (car cs) 'splice)) - (list (cdr (syntax->list (car cs)))) + (list (cdr (syntax->list (car cs)))) ; pop off the rule name and splice its components into this rule (list cs))) componentss)]) (syntax-property (datum->syntax #f (cons (datum->syntax #f rule-name/false srcloc stx-with-original?-property) - (apply append componentss)) + (apply append spliced-componentss)) srcloc stx-with-original?-property) 'splice splice))) \ No newline at end of file diff --git a/brag/brag/elider/json-elider-toy.rkt b/brag/brag/elider/json-elider-toy.rkt deleted file mode 100644 index 8eed9e8..0000000 --- a/brag/brag/elider/json-elider-toy.rkt +++ /dev/null @@ -1,4 +0,0 @@ -#lang brag - -thing : foo -foo : <"bar"> diff --git a/brag/brag/elider/test-json-elider-toy.rkt b/brag/brag/elider/test-json-elider-toy.rkt deleted file mode 100644 index c3072c3..0000000 --- a/brag/brag/elider/test-json-elider-toy.rkt +++ /dev/null @@ -1,9 +0,0 @@ -#lang br -(require "json-elider-toy.rkt" - brag/support - rackunit) - -(check-equal? - (syntax->datum - (parse (list "bar"))) - '(thing)) diff --git a/brag/brag/elider/json-elider.rkt b/brag/brag/examples/baby-json-hider.rkt similarity index 78% rename from brag/brag/elider/json-elider.rkt rename to brag/brag/examples/baby-json-hider.rkt index 677a02d..eb2097f 100755 --- a/brag/brag/elider/json-elider.rkt +++ b/brag/brag/examples/baby-json-hider.rkt @@ -1,8 +1,7 @@ #lang brag ;; Simple baby example of JSON structure -json: number - | string +json: number | string | array | object @@ -14,4 +13,4 @@ array: "[" [json ("," json)*] "]" object: <"{"> [kvpair ("," kvpair)*] <"}"> - : ":" +: ":" diff --git a/brag/brag/elider/test-json-elider.rkt b/brag/brag/test/test-baby-json-hider.rkt similarity index 90% rename from brag/brag/elider/test-json-elider.rkt rename to brag/brag/test/test-baby-json-hider.rkt index c7e48d0..81af1a1 100755 --- a/brag/brag/elider/test-json-elider.rkt +++ b/brag/brag/test/test-baby-json-hider.rkt @@ -1,5 +1,5 @@ -#lang br -(require "json-elider.rkt" +#lang racket/base +(require brag/examples/baby-json-hider brag/support rackunit) From 7a4999ee36cf2e5e9d1951085a9d8a5ad3f1fa3c Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 6 May 2016 13:34:43 -0700 Subject: [PATCH 08/13] cleanup --- brag/brag/codegen/codegen.rkt | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/brag/brag/codegen/codegen.rkt b/brag/brag/codegen/codegen.rkt index 75837c9..2ef8499 100755 --- a/brag/brag/codegen/codegen.rkt +++ b/brag/brag/codegen/codegen.rkt @@ -38,26 +38,10 @@ ;; We flatten the rules so we can use the yacc-style ruleset that parser-tools ;; supports. - - #| - MB: `rules` still carries 'hide syntax property - |# - #;(report rules) - - - #| - MB: `flattened-rules` still carries 'hide syntax property - |# (define flattened-rules (flatten-rules rules)) - #;(report flattened-rules) (define generated-rule-codes (map flat-rule->yacc-rule flattened-rules)) - #| - MB: `generated-rule-codes` loses the 'hide syntax property (but lhs-ids carry 'splice) - |# - #;(report generated-rule-codes) - ;; The first rule, by default, is the start rule. (define rule-ids (for/list ([a-rule (in-list rules)]) (rule-id a-rule))) @@ -179,8 +163,6 @@ ;; stx :== (name (U tokens rule-stx) ...) ;; (define (flat-rule->yacc-rule a-flat-rule) - ;; lhs-ids still carry 'splice property on #'name field - #;(report a-flat-rule) (syntax-case a-flat-rule () [(rule-type origin name clauses ...) (begin From 87f5b186a2ba09f78ef0345a9ecef05e3e224546 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 6 May 2016 13:43:02 -0700 Subject: [PATCH 09/13] more cleanup --- brag/brag/codegen/codegen.rkt | 2 +- brag/brag/codegen/flatten.rkt | 2 +- brag/brag/codegen/runtime.rkt | 2 +- brag/brag/rules/parser.rkt | 4 ++-- brag/brag/rules/stx.rkt | 2 +- brag/brag/test/test-all.rkt | 2 +- brag/brag/test/test-parser.rkt | 2 +- brag/info.rkt | 2 +- 8 files changed, 9 insertions(+), 9 deletions(-) diff --git a/brag/brag/codegen/codegen.rkt b/brag/brag/codegen/codegen.rkt index 2ef8499..544c5a0 100755 --- a/brag/brag/codegen/codegen.rkt +++ b/brag/brag/codegen/codegen.rkt @@ -1,4 +1,4 @@ -#lang br +#lang racket/base (require (for-template racket/base) racket/list diff --git a/brag/brag/codegen/flatten.rkt b/brag/brag/codegen/flatten.rkt index a6a9fb6..e1d9baf 100755 --- a/brag/brag/codegen/flatten.rkt +++ b/brag/brag/codegen/flatten.rkt @@ -1,4 +1,4 @@ -#lang br +#lang racket/base (require brag/rules/stx-types (for-syntax racket/base)) diff --git a/brag/brag/codegen/runtime.rkt b/brag/brag/codegen/runtime.rkt index 12212ca..03235da 100755 --- a/brag/brag/codegen/runtime.rkt +++ b/brag/brag/codegen/runtime.rkt @@ -1,4 +1,4 @@ -#lang br +#lang racket/base (require racket/match racket/list diff --git a/brag/brag/rules/parser.rkt b/brag/brag/rules/parser.rkt index 5a318dc..1c74802 100755 --- a/brag/brag/rules/parser.rkt +++ b/brag/brag/rules/parser.rkt @@ -1,4 +1,4 @@ -#lang br +#lang racket/base (require parser-tools/yacc parser-tools/lex racket/list @@ -88,7 +88,7 @@ #f) $2))] - ;; angles indicate hiding. set hide value to #t + ;; angles indicate splicing. set splice value to #t [(RULE_HEAD_HIDDEN pattern) (begin (begin diff --git a/brag/brag/rules/stx.rkt b/brag/brag/rules/stx.rkt index a0f7b84..c481911 100755 --- a/brag/brag/rules/stx.rkt +++ b/brag/brag/rules/stx.rkt @@ -1,4 +1,4 @@ -#lang br +#lang racket/base (require "rule-structs.rkt" parser-tools/lex diff --git a/brag/brag/test/test-all.rkt b/brag/brag/test/test-all.rkt index 8917aec..f23fabe 100755 --- a/brag/brag/test/test-all.rkt +++ b/brag/brag/test/test-all.rkt @@ -1,4 +1,4 @@ -#lang br +#lang racket/base (require "test-0n1.rkt" diff --git a/brag/brag/test/test-parser.rkt b/brag/brag/test/test-parser.rkt index 94adba1..22fcd98 100755 --- a/brag/brag/test/test-parser.rkt +++ b/brag/brag/test/test-parser.rkt @@ -1,4 +1,4 @@ -#lang br +#lang racket/base (require rackunit diff --git a/brag/info.rkt b/brag/info.rkt index 810a1d4..45a38e2 100755 --- a/brag/info.rkt +++ b/brag/info.rkt @@ -1,6 +1,6 @@ #lang setup/infotab -(define deps '("base" "beautiful-racket-lib" "parser-tools-lib" "rackunit-lib" "python-tokenizer")) +(define deps '("base" "parser-tools-lib" "rackunit-lib" "python-tokenizer")) (define build-deps '("at-exp-lib" "parser-tools-doc" "racket-doc" "scribble-lib")) (define collection 'multi) From feec0f85d5da14a3a9ec5fa2d974d3ed0c4f455f Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 6 May 2016 13:48:55 -0700 Subject: [PATCH 10/13] better regexp --- brag/brag/rules/parser.rkt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/brag/brag/rules/parser.rkt b/brag/brag/rules/parser.rkt index 1c74802..a00f44f 100755 --- a/brag/brag/rules/parser.rkt +++ b/brag/brag/rules/parser.rkt @@ -91,8 +91,7 @@ ;; angles indicate splicing. set splice value to #t [(RULE_HEAD_HIDDEN pattern) (begin - (begin - (define trimmed (cadr (regexp-match #px"<(\\w+)>\\s*:$" $1))) + (define trimmed (regexp-match #px"<(.+)>\\s*:$" $1)) (rule (position->pos $1-start-pos) (position->pos $2-end-pos) (lhs-id (position->pos $1-start-pos) @@ -102,7 +101,7 @@ (position-col $1-start-pos)) trimmed #t) - $2)))]] + $2))]] [pattern [(implicit-pattern-sequence PIPE pattern) From 0f9e8018ead7f9e180210d04cf208ad5e9c485d2 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 6 May 2016 13:50:01 -0700 Subject: [PATCH 11/13] whoops --- brag/brag/rules/parser.rkt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brag/brag/rules/parser.rkt b/brag/brag/rules/parser.rkt index a00f44f..d348980 100755 --- a/brag/brag/rules/parser.rkt +++ b/brag/brag/rules/parser.rkt @@ -91,7 +91,7 @@ ;; angles indicate splicing. set splice value to #t [(RULE_HEAD_HIDDEN pattern) (begin - (define trimmed (regexp-match #px"<(.+)>\\s*:$" $1)) + (define trimmed (cadr (regexp-match #px"<(.+)>\\s*:$" $1))) (rule (position->pos $1-start-pos) (position->pos $2-end-pos) (lhs-id (position->pos $1-start-pos) From fd5c53f019c3dc48e3d8294631ca9ba80d26591a Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 6 May 2016 14:46:04 -0700 Subject: [PATCH 12/13] victory --- beautiful-racket/br/demo/txtadv/expander.rkt | 55 +++++--------------- beautiful-racket/br/demo/txtadv/parser.rkt | 18 +++---- brag/brag/codegen/runtime.rkt | 4 +- brag/brag/test/test-baby-json-hider.rkt | 2 +- 4 files changed, 23 insertions(+), 56 deletions(-) diff --git a/beautiful-racket/br/demo/txtadv/expander.rkt b/beautiful-racket/br/demo/txtadv/expander.rkt index 2c64400..b07c10f 100644 --- a/beautiful-racket/br/demo/txtadv/expander.rkt +++ b/beautiful-racket/br/demo/txtadv/expander.rkt @@ -30,61 +30,30 @@ (parameterize ([cmd-line-mode? #t]) (do-place))))) + (provide txtadv-program) -(define #'(txtadv-program _section ...) - #'(module-begin _section ...)) +(define #'txtadv-program #'module-begin) (provide verb-section) -(define-inverting #'(verb-section _verb-item ...) +(define #'(verb-section + ((_name0 . _transitive0?) + (_name . _transitive?) ... _desc) ...) (inject-syntax ([#'in-verbs (shared-syntax 'in-verbs)]) #'(define-verbs in-verbs - _verb-item ...))) - - -(provide verb-item) -(define-inverting #'(verb-item (_name0 _transitive0?) (_name _transitive?) ... _desc) - #`[_name0 #,@(if (syntax->datum #'_transitive0?) #'(_) #'()) (= _name ...) _desc]) - -(provide verb-name) -(define-cases #'verb-name - [#'(_ _id) #'(_id #f)] - [#'(_ _id _underscore) #'(_id #t)]) + [_name0 #,@(if (syntax->datum #'_transitive0?) #'(_) #'()) (= _name ...) _desc] ...))) (provide everywhere-section) -(define-inverting #'(everywhere-section [_name _desc] ...) +(define #'(everywhere-section [_id _desc] ...) #'(define-everywhere everywhere-actions - ([_name _desc] ...))) - -(provide id-desc) -(define-inverting #'(id-desc _id _desc) - #'(_id _desc)) + ([_id _desc] ...))) (provide things-section) -(define-inverting #'(things-section _thing ...) - #'(begin _thing ...)) - -(provide thing-item) -(define-inverting #'(thing-item _thingname (_actionname _actiondesc) ...) - #'(define-thing _thingname [_actionname _actiondesc] ...)) +(define #'(things-section (_thingname (_actionname _actiondesc) ...) ...) + #'(begin (define-thing _thingname [_actionname _actiondesc] ...) ...)) (provide places-section) -(define-inverting #'(places-section _placeitem ...) - #'(begin _placeitem ...)) - -(provide place-item) -(define-inverting #'(place-item _place-id _place-desc [_place-item ...] [_actionname _actiondesc] ...) - #'(define-place _place-id _place-desc [_place-item ...] ([_actionname _actiondesc] ...))) - -(provide place-descrip) -(define #'(place-descrip _desc) #'_desc) - -(provide place-items) -(define-inverting #'(place-items _id ...) #'(_id ...)) - -(provide place-name) -(define-cases #'place-name - [#'(_ _id) #'_id]) - +(define #'(places-section (_place-id _place-desc [_place-item ...] [_actionname _actiondesc] ...) ...) + #'(begin (define-place _place-id _place-desc [_place-item ...] ([_actionname _actiondesc] ...)) ...)) (provide s-exp) (define-cases-inverting #'s-exp diff --git a/beautiful-racket/br/demo/txtadv/parser.rkt b/beautiful-racket/br/demo/txtadv/parser.rkt index 9a0798c..91c08f1 100644 --- a/beautiful-racket/br/demo/txtadv/parser.rkt +++ b/beautiful-racket/br/demo/txtadv/parser.rkt @@ -4,28 +4,24 @@ txtadv-program : verb-section everywhere-section things-section places-section s verb-section : <"===VERBS==="> verb-item+ -verb-item : verb-name+ s-exp + : verb-name+ s-exp -verb-name : [<",">] ID ["_"] + : [<",">] ID ["_"] everywhere-section : <"===EVERYWHERE==="> id-desc+ things-section : <"===THINGS==="> thing-item+ -thing-item : DASHED-NAME id-desc+ + : DASHED-NAME id-desc+ places-section : <"===PLACES==="> place-item+ -place-item : DASHED-NAME place-descrip place-items id-desc+ + : DASHED-NAME STRING place-items id-desc+ -place-descrip : STRING ; `place-desc` is already used in expander + : <"["> ([<",">] ID)* <"]"> -place-items : <"["> place-name* <"]"> ; `place-things` is already used +start-section : <"===START==="> ID -place-name : [<",">] ID - -start-section : <"===START==="> place-name - -id-desc : ID s-exp + : ID s-exp s-exp : ID | STRING | <"("> s-exp* <")"> \ No newline at end of file diff --git a/brag/brag/codegen/runtime.rkt b/brag/brag/codegen/runtime.rkt index 03235da..bfa28da 100755 --- a/brag/brag/codegen/runtime.rkt +++ b/brag/brag/codegen/runtime.rkt @@ -165,7 +165,9 @@ This would be the place to check a syntax property for hiding. (define (rule-components->syntax rule-name/false #:srcloc [srcloc #f] #:splice? [splice #f] . componentss) (let ([spliced-componentss (append-map (λ(cs) (if (and (pair? cs) (syntax-property (car cs) 'splice)) - (list (cdr (syntax->list (car cs)))) ; pop off the rule name and splice its components into this rule + (list (list (syntax-case (car cs) () + [(rule-name c ...) + #'(c ...)]))) (list cs))) componentss)]) (syntax-property (datum->syntax #f diff --git a/brag/brag/test/test-baby-json-hider.rkt b/brag/brag/test/test-baby-json-hider.rkt index 81af1a1..fb64dd6 100755 --- a/brag/brag/test/test-baby-json-hider.rkt +++ b/brag/brag/test/test-baby-json-hider.rkt @@ -10,7 +10,7 @@ ":" (token 'STRING "'hello world'") "}"))) - '(json (object ":"))) + '(json (object (":")))) (check-equal? From 245a488ac0322691e46f52ebeb83c7fae10c66e0 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 6 May 2016 14:50:11 -0700 Subject: [PATCH 13/13] note --- beautiful-racket/br/demo/txtadv/expander.rkt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/beautiful-racket/br/demo/txtadv/expander.rkt b/beautiful-racket/br/demo/txtadv/expander.rkt index b07c10f..6c714ab 100644 --- a/beautiful-racket/br/demo/txtadv/expander.rkt +++ b/beautiful-racket/br/demo/txtadv/expander.rkt @@ -31,6 +31,9 @@ (do-place))))) +;; ============================================================== +;; Process parse trees from the reader: + (provide txtadv-program) (define #'txtadv-program #'module-begin)