Matthew Butterick 9 years ago
commit 99951f2f07

@ -30,70 +30,38 @@
(parameterize ([cmd-line-mode? #t]) (parameterize ([cmd-line-mode? #t])
(do-place))))) (do-place)))))
;; ==============================================================
;; Process parse trees from the reader:
(provide txtadv-program) (provide txtadv-program)
(define #'(txtadv-program _section ...) (define #'txtadv-program #'module-begin)
#'(module-begin _section ...))
(provide verb-section) (provide verb-section)
(define-inverting #'(verb-section _heading _verb-item ...) (define #'(verb-section
((_name0 . _transitive0?)
(_name . _transitive?) ... _desc) ...)
(inject-syntax ([#'in-verbs (shared-syntax 'in-verbs)]) (inject-syntax ([#'in-verbs (shared-syntax 'in-verbs)])
#'(define-verbs in-verbs #'(define-verbs in-verbs
_verb-item ...))) [_name0 #,@(if (syntax->datum #'_transitive0?) #'(_) #'()) (= _name ...) _desc] ...)))
(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
;; 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) (provide everywhere-section)
(define-inverting #'(everywhere-section _heading [_name _desc] ...) (define #'(everywhere-section [_id _desc] ...)
#'(define-everywhere everywhere-actions #'(define-everywhere everywhere-actions
([_name _desc] ...))) ([_id _desc] ...)))
(provide id-desc)
(define-inverting #'(id-desc _id _desc)
#'(_id _desc))
(provide things-section) (provide things-section)
(define-inverting #'(things-section _heading _thing ...) (define #'(things-section (_thingname (_actionname _actiondesc) ...) ...)
#'(begin _thing ...)) #'(begin (define-thing _thingname [_actionname _actiondesc] ...) ...))
(provide thing-item)
(define-inverting #'(thing-item _thingname (_actionname _actiondesc) ...)
#'(define-thing _thingname [_actionname _actiondesc] ...))
(provide places-section) (provide places-section)
(define-inverting #'(places-section _heading _placeitem ...) (define #'(places-section (_place-id _place-desc [_place-item ...] [_actionname _actiondesc] ...) ...)
#'(begin _placeitem ...)) #'(begin (define-place _place-id _place-desc [_place-item ...] ([_actionname _actiondesc] ...)) ...))
(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]
[#'(_ _id) #'_id])
(provide s-exp) (provide s-exp)
(define-cases-inverting #'s-exp (define-cases-inverting #'s-exp
[#'(_ "(" _sx ... ")") #'(_sx ...)] [#'(_ _sx) #'_sx]
[#'(_ _sx) #'_sx]) [#'(_ _sx ... ) #'(_sx ...)])
;; todo: consolidate the game-starters. ;; todo: consolidate the game-starters.
@ -107,7 +75,7 @@
everywhere-actions))) everywhere-actions)))
(provide start-section) (provide start-section)
(define #'(start-section _heading _where) (define #'(start-section _where)
(inject-syntax ([#'in-verbs (shared-syntax 'in-verbs)]) (inject-syntax ([#'in-verbs (shared-syntax 'in-verbs)])
#'(init-game _where #'(init-game _where
in-verbs in-verbs

@ -2,30 +2,26 @@
txtadv-program : verb-section everywhere-section things-section places-section start-section 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-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+ <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-item> : DASHED-NAME STRING place-items id-desc+
place-descrip : STRING ; `place-desc` is already used in expander <place-items> : <"["> ([<",">] ID)* <"]">
place-items : "[" place-name* "]" ; `place-things` is already used start-section : <"===START==="> ID
place-name : [","] ID <id-desc> : ID s-exp
start-section : "===START===" place-name s-exp : ID | STRING | <"("> s-exp* <")">
id-desc : ID s-exp
s-exp : ID | STRING | "(" s-exp* ")"

@ -1,4 +1,4 @@
#lang br #lang racket/base
(require (for-template racket/base) (require (for-template racket/base)
racket/list racket/list
@ -38,26 +38,10 @@
;; We flatten the rules so we can use the yacc-style ruleset that parser-tools ;; We flatten the rules so we can use the yacc-style ruleset that parser-tools
;; supports. ;; supports.
#|
MB: `rules` still carries 'hide syntax property
|#
#;(report flattened-rules)
#|
MB: `flattened-rules` still carries 'hide syntax property
|#
(define flattened-rules (flatten-rules rules)) (define flattened-rules (flatten-rules rules))
#;(report flattened-rules)
(define generated-rule-codes (map flat-rule->yacc-rule flattened-rules)) (define generated-rule-codes (map flat-rule->yacc-rule flattened-rules))
#|
MB: `generated-rule-codes` loses the 'hide syntax property
|#
#;(report generated-rule-codes)
;; The first rule, by default, is the start rule. ;; The first rule, by default, is the start rule.
(define rule-ids (for/list ([a-rule (in-list rules)]) (define rule-ids (for/list ([a-rule (in-list rules)])
(rule-id a-rule))) (rule-id a-rule)))
@ -179,7 +163,6 @@
;; stx :== (name (U tokens rule-stx) ...) ;; stx :== (name (U tokens rule-stx) ...)
;; ;;
(define (flat-rule->yacc-rule a-flat-rule) (define (flat-rule->yacc-rule a-flat-rule)
#;(report a-flat-rule)
(syntax-case a-flat-rule () (syntax-case a-flat-rule ()
[(rule-type origin name clauses ...) [(rule-type origin name clauses ...)
(begin (begin
@ -216,6 +199,8 @@
(for/list ([translated-pattern (in-list translated-patterns)] (for/list ([translated-pattern (in-list translated-patterns)]
[primitive-pattern (syntax->list a-clause)] [primitive-pattern (syntax->list a-clause)]
[pos (in-naturals 1)]) [pos (in-naturals 1)])
(if (syntax-property primitive-pattern 'hide)
#'null
(with-syntax ([$X (with-syntax ([$X
(format-id translated-pattern "$~a" pos)] (format-id translated-pattern "$~a" pos)]
[$X-start-pos [$X-start-pos
@ -233,12 +218,10 @@
(syntax->list #'rest)])] (syntax->list #'rest)])]
[(id val) [(id val)
#'(list $X)] #'(list $X)]
;; move the 'hide syntax property into the translated-action
;; because syntax gets datum-ized
[(lit val) [(lit val)
#`(list (atomic-datum->syntax $X $X-start-pos $X-end-pos #,(syntax-property primitive-pattern 'hide)))] #'(list (atomic-datum->syntax $X $X-start-pos $X-end-pos))]
[(token val) [(token val)
#`(list (atomic-datum->syntax $X $X-start-pos $X-end-pos #,(syntax-property primitive-pattern 'hide)))])))) #'(list (atomic-datum->syntax $X $X-start-pos $X-end-pos))])))))
(define whole-rule-loc (define whole-rule-loc
(if (empty? translated-patterns) (if (empty? translated-patterns)
@ -247,11 +230,13 @@
[$n-end-pos (format-id (last translated-patterns) "$~a-end-pos" (length translated-patterns))]) [$n-end-pos (format-id (last translated-patterns) "$~a-end-pos" (length translated-patterns))])
#`(positions->srcloc $1-start-pos $n-end-pos)))) #`(positions->srcloc $1-start-pos $n-end-pos))))
;; move 'splice property into function because name is datum-ized
(with-syntax ([(translated-pattern ...) translated-patterns] (with-syntax ([(translated-pattern ...) translated-patterns]
[(translated-action ...) translated-actions]) [(translated-action ...) translated-actions])
#`[(translated-pattern ...) #`[(translated-pattern ...)
(rule-components->syntax '#,rule-name/false translated-action ... (rule-components->syntax '#,rule-name/false translated-action ...
#:srcloc #,whole-rule-loc)])) #:srcloc #,whole-rule-loc
#:splice? #,(syntax-property rule-name/false 'splice))]))

@ -1,4 +1,4 @@
#lang br #lang racket/base
(require brag/rules/stx-types (require brag/rules/stx-types
(for-syntax racket/base)) (for-syntax racket/base))

@ -1,4 +1,4 @@
#lang br #lang racket/base
(require racket/match (require racket/match
racket/list racket/list
@ -150,26 +150,30 @@ This would be the place to check a syntax property for hiding.
(define stx-with-original?-property (define stx-with-original?-property
(read-syntax #f (open-input-string "meaningless-string"))) (read-syntax #f (open-input-string "meaningless-string")))
(define elided (gensym))
;; atomic-datum->syntax: datum position position ;; atomic-datum->syntax: datum position position
;; Helper that does the ugly work in wrapping a datum into a syntax ;; Helper that does the ugly work in wrapping a datum into a syntax
;; with source location. ;; with source location.
(define (atomic-datum->syntax d start-pos end-pos [hide? #f]) (define (atomic-datum->syntax d start-pos end-pos)
(if hide? (datum->syntax #f d (positions->srcloc start-pos end-pos) stx-with-original?-property))
elided
(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 ;; 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. ;; Creates an stx out of the rule name and its components.
;; The location information of the rule spans that of 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 (rule-components->syntax rule-name/false #:srcloc [srcloc #f] #:splice? [splice #f] . componentss)
(define flattened-elided-components (filter-not (λ(c) (eq? c elided)) (apply append components))) (let ([spliced-componentss (append-map (λ(cs)
(if (and (pair? cs) (syntax-property (car cs) 'splice))
(list (list (syntax-case (car cs) ()
[(rule-name c ...)
#'(c ...)])))
(list cs))) componentss)])
(syntax-property
(datum->syntax #f (datum->syntax #f
(cons (cons
(datum->syntax #f rule-name/false srcloc stx-with-original?-property) (datum->syntax #f rule-name/false srcloc stx-with-original?-property)
flattened-elided-components) (apply append spliced-componentss))
srcloc srcloc
stx-with-original?-property)) stx-with-original?-property)
'splice splice)))

@ -1,4 +0,0 @@
#lang brag
;; Simple baby example of JSON structure
json: ID <":"> ID

@ -1,8 +1,7 @@
#lang brag #lang brag
;; Simple baby example of JSON structure ;; Simple baby example of JSON structure
json: number json: number | string
| string
| array | array
| object | object
@ -14,4 +13,4 @@ array: "[" [json ("," json)*] "]"
object: <"{"> [kvpair ("," kvpair)*] <"}"> object: <"{"> [kvpair ("," kvpair)*] <"}">
kvpair: <ID> <":"> json <kvpair>: <ID> ":" <json>

@ -22,9 +22,6 @@
(:+ id-char))) (:+ id-char)))
(define lex/1 (define lex/1
(lexer-src-pos (lexer-src-pos
[(:: "'" [(:: "'"
@ -64,6 +61,8 @@
(token-EOF lexeme)] (token-EOF lexeme)]
[(:: id (:* whitespace) ":") [(:: id (:* whitespace) ":")
(token-RULE_HEAD lexeme)] (token-RULE_HEAD lexeme)]
[(:: "<" id ">" (:* whitespace) ":")
(token-RULE_HEAD_HIDDEN lexeme)]
[id [id
(token-ID lexeme)] (token-ID lexeme)]

@ -1,4 +1,4 @@
#lang br #lang racket/base
(require parser-tools/yacc (require parser-tools/yacc
parser-tools/lex parser-tools/lex
racket/list racket/list
@ -17,6 +17,7 @@
token-PIPE token-PIPE
token-REPEAT token-REPEAT
token-RULE_HEAD token-RULE_HEAD
token-RULE_HEAD_HIDDEN
token-ID token-ID
token-LIT token-LIT
token-EOF token-EOF
@ -45,6 +46,7 @@
PIPE PIPE
REPEAT REPEAT
RULE_HEAD RULE_HEAD
RULE_HEAD_HIDDEN
ID ID
LIT LIT
EOF)) EOF))
@ -84,6 +86,21 @@
(position-col $1-start-pos)) (position-col $1-start-pos))
trimmed trimmed
#f) #f)
$2))]
;; angles indicate splicing. set splice value to #t
[(RULE_HEAD_HIDDEN pattern)
(begin
(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)
(pos (+ (position-offset $1-start-pos)
(string-length trimmed))
(position-line $1-start-pos)
(position-col $1-start-pos))
trimmed
#t)
$2))]] $2))]]
[pattern [pattern

@ -12,7 +12,7 @@
(struct rule (start end lhs pattern) (struct rule (start end lhs pattern)
#:transparent) #:transparent)
(struct lhs-id (start end val hide) (struct lhs-id (start end val splice)
#:transparent) #:transparent)

@ -1,4 +1,4 @@
#lang br #lang racket/base
(require "rule-structs.rkt" (require "rule-structs.rkt"
parser-tools/lex parser-tools/lex
@ -21,6 +21,7 @@
(define (rule->stx source a-rule) (define (rule->stx source a-rule)
(define id-stx (define id-stx
(syntax-property
(datum->syntax #f (datum->syntax #f
(string->symbol (lhs-id-val (rule-lhs a-rule))) (string->symbol (lhs-id-val (rule-lhs a-rule)))
(list source (list source
@ -31,7 +32,8 @@
(number? (pos-offset (lhs-id-end (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-end (rule-lhs a-rule)))
(pos-offset (lhs-id-start (rule-lhs a-rule)))) (pos-offset (lhs-id-start (rule-lhs a-rule))))
#f)))) #f)))
'splice (lhs-id-splice (rule-lhs a-rule))))
(define pattern-stx (pattern->stx source (rule-pattern a-rule))) (define pattern-stx (pattern->stx source (rule-pattern a-rule)))
(define line (pos-line (rule-start a-rule))) (define line (pos-line (rule-start a-rule)))
(define column (pos-col (rule-start a-rule))) (define column (pos-col (rule-start a-rule)))

@ -1,4 +1,4 @@
#lang br #lang racket/base
(require "test-0n1.rkt" (require "test-0n1.rkt"

@ -1,5 +1,5 @@
#lang br #lang racket/base
(require "json-elider.rkt" (require brag/examples/baby-json-hider
brag/support brag/support
rackunit) rackunit)
@ -10,7 +10,7 @@
":" ":"
(token 'STRING "'hello world'") (token 'STRING "'hello world'")
"}"))) "}")))
'(json (object (kvpair (json (string "'hello world'")))))) '(json (object (":"))))
(check-equal? (check-equal?

@ -1,4 +1,4 @@
#lang br #lang racket/base
(require rackunit (require rackunit

Loading…
Cancel
Save