elide
token added with no functional changes; all tests still pass
This commit is contained in:
parent
894c9780d8
commit
831d5cca35
|
@ -260,7 +260,7 @@
|
||||||
(let loop ([a-pattern a-pattern]
|
(let loop ([a-pattern a-pattern]
|
||||||
[implicit implicit]
|
[implicit implicit]
|
||||||
[explicit explicit])
|
[explicit explicit])
|
||||||
(syntax-case a-pattern (id lit token choice repeat maybe seq)
|
(syntax-case a-pattern (id lit token choice repeat maybe elide seq)
|
||||||
[(id val)
|
[(id val)
|
||||||
(values implicit explicit)]
|
(values implicit explicit)]
|
||||||
[(lit val)
|
[(lit val)
|
||||||
|
@ -279,6 +279,8 @@
|
||||||
(loop #'val implicit explicit)]
|
(loop #'val implicit explicit)]
|
||||||
[(maybe val)
|
[(maybe val)
|
||||||
(loop #'val implicit explicit)]
|
(loop #'val implicit explicit)]
|
||||||
|
[(elide val)
|
||||||
|
(loop #'val implicit explicit)]
|
||||||
[(seq vals ...)
|
[(seq vals ...)
|
||||||
(for/fold ([implicit implicit]
|
(for/fold ([implicit implicit]
|
||||||
[explicit explicit])
|
[explicit explicit])
|
||||||
|
@ -342,7 +344,7 @@
|
||||||
(define (pattern-collect-used-ids a-pattern acc)
|
(define (pattern-collect-used-ids a-pattern acc)
|
||||||
(let loop ([a-pattern a-pattern]
|
(let loop ([a-pattern a-pattern]
|
||||||
[acc acc])
|
[acc acc])
|
||||||
(syntax-case a-pattern (id lit token choice repeat maybe seq)
|
(syntax-case a-pattern (id lit token choice repeat maybe elide seq)
|
||||||
[(id val)
|
[(id val)
|
||||||
(cons #'val acc)]
|
(cons #'val acc)]
|
||||||
[(lit val)
|
[(lit val)
|
||||||
|
@ -357,6 +359,8 @@
|
||||||
(loop #'val acc)]
|
(loop #'val acc)]
|
||||||
[(maybe val)
|
[(maybe val)
|
||||||
(loop #'val acc)]
|
(loop #'val acc)]
|
||||||
|
[(elide val)
|
||||||
|
(loop #'val acc)]
|
||||||
[(seq vals ...)
|
[(seq vals ...)
|
||||||
(for/fold ([acc acc])
|
(for/fold ([acc acc])
|
||||||
([v (in-list (syntax->list #'(vals ...)))])
|
([v (in-list (syntax->list #'(vals ...)))])
|
||||||
|
@ -385,7 +389,7 @@
|
||||||
a-leaf)
|
a-leaf)
|
||||||
|
|
||||||
(define (process-pattern a-pattern)
|
(define (process-pattern a-pattern)
|
||||||
(syntax-case a-pattern (id lit token choice repeat maybe seq)
|
(syntax-case a-pattern (id lit token choice repeat maybe elide seq)
|
||||||
[(id val)
|
[(id val)
|
||||||
(free-id-table-ref toplevel-rule-table #'val)]
|
(free-id-table-ref toplevel-rule-table #'val)]
|
||||||
[(lit val)
|
[(lit val)
|
||||||
|
@ -407,6 +411,8 @@
|
||||||
(process-pattern #'val)])]
|
(process-pattern #'val)])]
|
||||||
[(maybe val)
|
[(maybe val)
|
||||||
(make-leaf)]
|
(make-leaf)]
|
||||||
|
[(elide val)
|
||||||
|
(make-leaf)]
|
||||||
[(seq vals ...)
|
[(seq vals ...)
|
||||||
(begin
|
(begin
|
||||||
(define an-and-node (sat:make-and))
|
(define an-and-node (sat:make-and))
|
||||||
|
|
|
@ -72,7 +72,7 @@
|
||||||
[origin (syntax-case a-rule (rule) [(rule name (pat-head rest ...)) #'pat-head])])
|
[origin (syntax-case a-rule (rule) [(rule name (pat-head rest ...)) #'pat-head])])
|
||||||
(syntax-case a-rule (rule)
|
(syntax-case a-rule (rule)
|
||||||
[(rule name pat)
|
[(rule name pat)
|
||||||
(syntax-case #'pat (id inferred-id lit token choice repeat maybe seq)
|
(syntax-case #'pat (id inferred-id lit token choice repeat maybe elide seq)
|
||||||
|
|
||||||
;; The primitive types stay as they are:
|
;; The primitive types stay as they are:
|
||||||
[(id val)
|
[(id val)
|
||||||
|
@ -123,6 +123,16 @@
|
||||||
[])
|
[])
|
||||||
inferred-rules)))]
|
inferred-rules)))]
|
||||||
|
|
||||||
|
[(elide sub-pat)
|
||||||
|
(begin
|
||||||
|
(define-values (inferred-rules new-sub-pats)
|
||||||
|
(lift-nonprimitive-pattern #'sub-pat))
|
||||||
|
(with-syntax ([(sub-pat ...) new-sub-pats])
|
||||||
|
(cons #'(head origin name
|
||||||
|
[sub-pat ...]
|
||||||
|
[])
|
||||||
|
inferred-rules)))]
|
||||||
|
|
||||||
[(seq sub-pat ...)
|
[(seq sub-pat ...)
|
||||||
(begin
|
(begin
|
||||||
(define-values (inferred-rules new-sub-pats)
|
(define-values (inferred-rules new-sub-pats)
|
||||||
|
@ -139,7 +149,7 @@
|
||||||
|
|
||||||
;; Returns true if the pattern looks primitive
|
;; Returns true if the pattern looks primitive
|
||||||
(define (primitive-pattern? a-pat)
|
(define (primitive-pattern? a-pat)
|
||||||
(syntax-case a-pat (id lit token choice repeat maybe seq)
|
(syntax-case a-pat (id lit token choice repeat maybe elide seq)
|
||||||
[(id val)
|
[(id val)
|
||||||
#t]
|
#t]
|
||||||
[(lit val)
|
[(lit val)
|
||||||
|
@ -152,6 +162,8 @@
|
||||||
#f]
|
#f]
|
||||||
[(maybe sub-pat)
|
[(maybe sub-pat)
|
||||||
#f]
|
#f]
|
||||||
|
[(elide sub-pat)
|
||||||
|
#f]
|
||||||
[(seq sub-pat ...)
|
[(seq sub-pat ...)
|
||||||
(andmap primitive-pattern? (syntax->list #'(sub-pat ...)))]))
|
(andmap primitive-pattern? (syntax->list #'(sub-pat ...)))]))
|
||||||
|
|
||||||
|
|
Binary file not shown.
16
beautiful-racket-ragg/br/ragg/elider/json-elider.rkt
Executable file
16
beautiful-racket-ragg/br/ragg/elider/json-elider.rkt
Executable file
|
@ -0,0 +1,16 @@
|
||||||
|
#lang br/ragg
|
||||||
|
|
||||||
|
;; Simple baby example of JSON structure
|
||||||
|
json: number | string
|
||||||
|
| array
|
||||||
|
| object
|
||||||
|
|
||||||
|
number: NUMBER
|
||||||
|
|
||||||
|
string: STRING
|
||||||
|
|
||||||
|
array: "[" [json ("," json)*] "]"
|
||||||
|
|
||||||
|
object: "{" [kvpair ("," kvpair)*] "}"
|
||||||
|
|
||||||
|
kvpair: ID <":"> json
|
25
beautiful-racket-ragg/br/ragg/elider/test-json-elider.rkt
Executable file
25
beautiful-racket-ragg/br/ragg/elider/test-json-elider.rkt
Executable file
|
@ -0,0 +1,25 @@
|
||||||
|
#lang racket/base
|
||||||
|
(require "json-elider.rkt"
|
||||||
|
br/ragg/support
|
||||||
|
rackunit)
|
||||||
|
|
||||||
|
(check-equal?
|
||||||
|
(syntax->datum
|
||||||
|
(parse (list "{"
|
||||||
|
(token 'ID "message")
|
||||||
|
":"
|
||||||
|
(token 'STRING "'hello world'")
|
||||||
|
"}")))
|
||||||
|
'(json (object "{"
|
||||||
|
(kvpair "message" (json (string "'hello world'")))
|
||||||
|
"}")))
|
||||||
|
|
||||||
|
|
||||||
|
#;(check-equal?
|
||||||
|
(syntax->datum
|
||||||
|
(parse "[[[{}]],[],[[{}]]]"))
|
||||||
|
'(json (array #\[ (json (array #\[ (json (array #\[ (json (object #\{ #\})) #\])) #\])) #\, (json (array #\[ #\])) #\, (json (array #\[ (json (array #\[ (json (object #\{ #\})) #\])) #\])) #\])))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
(define name "ragg")
|
(define name "ragg")
|
||||||
(define categories '(devtools))
|
(define categories '(devtools))
|
||||||
(define can-be-loaded-with 'all)
|
(define can-be-loaded-with 'all)
|
||||||
(define required-core-version "5.3.1")
|
|
||||||
(define version "1.0")
|
(define version "1.0")
|
||||||
(define repositories '("4.x"))
|
(define repositories '("4.x"))
|
||||||
(define scribblings '(("br-ragg.scrbl")))
|
(define scribblings '(("br-ragg.scrbl")))
|
||||||
|
|
|
@ -39,10 +39,14 @@
|
||||||
(token-LPAREN lexeme)]
|
(token-LPAREN lexeme)]
|
||||||
["["
|
["["
|
||||||
(token-LBRACKET lexeme)]
|
(token-LBRACKET lexeme)]
|
||||||
|
["<"
|
||||||
|
(token-LANGLE lexeme)]
|
||||||
[")"
|
[")"
|
||||||
(token-RPAREN lexeme)]
|
(token-RPAREN lexeme)]
|
||||||
["]"
|
["]"
|
||||||
(token-RBRACKET lexeme)]
|
(token-RBRACKET lexeme)]
|
||||||
|
[">"
|
||||||
|
(token-RANGLE lexeme)]
|
||||||
["|"
|
["|"
|
||||||
(token-PIPE lexeme)]
|
(token-PIPE lexeme)]
|
||||||
[(:or "+" "*")
|
[(:or "+" "*")
|
||||||
|
|
|
@ -12,6 +12,8 @@
|
||||||
token-RPAREN
|
token-RPAREN
|
||||||
token-LBRACKET
|
token-LBRACKET
|
||||||
token-RBRACKET
|
token-RBRACKET
|
||||||
|
token-LANGLE ; for elider
|
||||||
|
token-RANGLE ; for elider
|
||||||
token-PIPE
|
token-PIPE
|
||||||
token-REPEAT
|
token-REPEAT
|
||||||
token-RULE_HEAD
|
token-RULE_HEAD
|
||||||
|
@ -19,7 +21,7 @@
|
||||||
token-LIT
|
token-LIT
|
||||||
token-EOF
|
token-EOF
|
||||||
grammar-parser
|
grammar-parser
|
||||||
|
|
||||||
current-source
|
current-source
|
||||||
current-parser-error-handler
|
current-parser-error-handler
|
||||||
|
|
||||||
|
@ -32,12 +34,15 @@
|
||||||
[struct-out pattern-choice]
|
[struct-out pattern-choice]
|
||||||
[struct-out pattern-repeat]
|
[struct-out pattern-repeat]
|
||||||
[struct-out pattern-maybe]
|
[struct-out pattern-maybe]
|
||||||
|
[struct-out pattern-elide]
|
||||||
[struct-out pattern-seq])
|
[struct-out pattern-seq])
|
||||||
|
|
||||||
(define-tokens tokens (LPAREN
|
(define-tokens tokens (LPAREN
|
||||||
RPAREN
|
RPAREN
|
||||||
LBRACKET
|
LBRACKET
|
||||||
RBRACKET
|
RBRACKET
|
||||||
|
LANGLE
|
||||||
|
RANGLE
|
||||||
PIPE
|
PIPE
|
||||||
REPEAT
|
REPEAT
|
||||||
RULE_HEAD
|
RULE_HEAD
|
||||||
|
@ -52,17 +57,17 @@
|
||||||
(src-pos)
|
(src-pos)
|
||||||
(start rules)
|
(start rules)
|
||||||
(end EOF)
|
(end EOF)
|
||||||
|
|
||||||
(grammar
|
(grammar
|
||||||
[rules
|
[rules
|
||||||
[(rules*) $1]]
|
[(rules*) $1]]
|
||||||
|
|
||||||
[rules*
|
[rules*
|
||||||
[(rule rules*)
|
[(rule rules*)
|
||||||
(cons $1 $2)]
|
(cons $1 $2)]
|
||||||
[()
|
[()
|
||||||
'()]]
|
'()]]
|
||||||
|
|
||||||
;; I have a separate token type for rule identifiers to avoid the
|
;; I have a separate token type for rule identifiers to avoid the
|
||||||
;; shift/reduce conflict that happens with the implicit sequencing
|
;; shift/reduce conflict that happens with the implicit sequencing
|
||||||
;; of top-level rules. i.e. the parser can't currently tell, when
|
;; of top-level rules. i.e. the parser can't currently tell, when
|
||||||
|
@ -80,7 +85,7 @@
|
||||||
(position-col $1-start-pos))
|
(position-col $1-start-pos))
|
||||||
trimmed)
|
trimmed)
|
||||||
$2))]]
|
$2))]]
|
||||||
|
|
||||||
[pattern
|
[pattern
|
||||||
[(implicit-pattern-sequence PIPE pattern)
|
[(implicit-pattern-sequence PIPE pattern)
|
||||||
(if (pattern-choice? $3)
|
(if (pattern-choice? $3)
|
||||||
|
@ -92,7 +97,7 @@
|
||||||
(list $1 $3)))]
|
(list $1 $3)))]
|
||||||
[(implicit-pattern-sequence)
|
[(implicit-pattern-sequence)
|
||||||
$1]]
|
$1]]
|
||||||
|
|
||||||
[implicit-pattern-sequence
|
[implicit-pattern-sequence
|
||||||
[(repeatable-pattern implicit-pattern-sequence)
|
[(repeatable-pattern implicit-pattern-sequence)
|
||||||
(if (pattern-seq? $2)
|
(if (pattern-seq? $2)
|
||||||
|
@ -104,7 +109,7 @@
|
||||||
(list $1 $2)))]
|
(list $1 $2)))]
|
||||||
[(repeatable-pattern)
|
[(repeatable-pattern)
|
||||||
$1]]
|
$1]]
|
||||||
|
|
||||||
[repeatable-pattern
|
[repeatable-pattern
|
||||||
[(atomic-pattern REPEAT)
|
[(atomic-pattern REPEAT)
|
||||||
(cond [(string=? $2 "*")
|
(cond [(string=? $2 "*")
|
||||||
|
@ -119,7 +124,7 @@
|
||||||
(error 'grammar-parse "unknown repetition operator ~e" $2)])]
|
(error 'grammar-parse "unknown repetition operator ~e" $2)])]
|
||||||
[(atomic-pattern)
|
[(atomic-pattern)
|
||||||
$1]]
|
$1]]
|
||||||
|
|
||||||
[atomic-pattern
|
[atomic-pattern
|
||||||
[(LIT)
|
[(LIT)
|
||||||
(pattern-lit (position->pos $1-start-pos)
|
(pattern-lit (position->pos $1-start-pos)
|
||||||
|
@ -134,15 +139,20 @@
|
||||||
(pattern-id (position->pos $1-start-pos)
|
(pattern-id (position->pos $1-start-pos)
|
||||||
(position->pos $1-end-pos)
|
(position->pos $1-end-pos)
|
||||||
$1))]
|
$1))]
|
||||||
|
|
||||||
[(LBRACKET pattern RBRACKET)
|
[(LBRACKET pattern RBRACKET)
|
||||||
(pattern-maybe (position->pos $1-start-pos)
|
(pattern-maybe (position->pos $1-start-pos)
|
||||||
(position->pos $3-end-pos)
|
(position->pos $3-end-pos)
|
||||||
$2)]
|
$2)]
|
||||||
|
|
||||||
|
[(LANGLE pattern RANGLE)
|
||||||
|
(pattern-elide (position->pos $1-start-pos)
|
||||||
|
(position->pos $3-end-pos)
|
||||||
|
$2)]
|
||||||
|
|
||||||
[(LPAREN pattern RPAREN)
|
[(LPAREN pattern RPAREN)
|
||||||
(relocate-pattern $2 (position->pos $1-start-pos) (position->pos $3-end-pos))]])
|
(relocate-pattern $2 (position->pos $1-start-pos) (position->pos $3-end-pos))]])
|
||||||
|
|
||||||
|
|
||||||
(error (lambda (tok-ok? tok-name tok-value start-pos end-pos)
|
(error (lambda (tok-ok? tok-name tok-value start-pos end-pos)
|
||||||
((current-parser-error-handler) tok-ok? tok-name tok-value (position->pos start-pos) (position->pos end-pos))))))
|
((current-parser-error-handler) tok-ok? tok-name tok-value (position->pos start-pos) (position->pos end-pos))))))
|
||||||
|
@ -152,22 +162,24 @@
|
||||||
;; Rewrites the pattern's start and end pos accordingly.
|
;; Rewrites the pattern's start and end pos accordingly.
|
||||||
(define (relocate-pattern a-pat start-pos end-pos)
|
(define (relocate-pattern a-pat start-pos end-pos)
|
||||||
(match a-pat
|
(match a-pat
|
||||||
[(pattern-id _ _ v)
|
[(pattern-id _ _ v)
|
||||||
(pattern-id start-pos end-pos v)]
|
(pattern-id start-pos end-pos v)]
|
||||||
[(pattern-token _ _ v)
|
[(pattern-token _ _ v)
|
||||||
(pattern-token start-pos end-pos v)]
|
(pattern-token start-pos end-pos v)]
|
||||||
[(pattern-lit _ _ v)
|
[(pattern-lit _ _ v)
|
||||||
(pattern-lit start-pos end-pos v)]
|
(pattern-lit start-pos end-pos v)]
|
||||||
[(pattern-choice _ _ vs)
|
[(pattern-choice _ _ vs)
|
||||||
(pattern-choice start-pos end-pos vs)]
|
(pattern-choice start-pos end-pos vs)]
|
||||||
[(pattern-repeat _ _ m v)
|
[(pattern-repeat _ _ m v)
|
||||||
(pattern-repeat start-pos end-pos m v)]
|
(pattern-repeat start-pos end-pos m v)]
|
||||||
[(pattern-maybe _ _ v)
|
[(pattern-maybe _ _ v)
|
||||||
(pattern-maybe start-pos end-pos v)]
|
(pattern-maybe start-pos end-pos v)]
|
||||||
[(pattern-seq _ _ vs)
|
[(pattern-elide _ _ v)
|
||||||
(pattern-seq start-pos end-pos vs)]
|
(pattern-elide start-pos end-pos v)]
|
||||||
[else
|
[(pattern-seq _ _ vs)
|
||||||
(error 'relocate-pattern "Internal error when relocating ~s\n" a-pat)]))
|
(pattern-seq start-pos end-pos vs)]
|
||||||
|
[else
|
||||||
|
(error 'relocate-pattern "Internal error when relocating ~s\n" a-pat)]))
|
||||||
|
|
||||||
|
|
||||||
; token-id: string -> boolean
|
; token-id: string -> boolean
|
||||||
|
@ -194,9 +206,9 @@
|
||||||
|
|
||||||
;; When bad things happen, we need to emit errors with source location.
|
;; When bad things happen, we need to emit errors with source location.
|
||||||
(struct exn:fail:parse-grammar exn:fail (srclocs)
|
(struct exn:fail:parse-grammar exn:fail (srclocs)
|
||||||
#:transparent
|
#:transparent
|
||||||
#:property prop:exn:srclocs (lambda (instance)
|
#:property prop:exn:srclocs (lambda (instance)
|
||||||
(exn:fail:parse-grammar-srclocs instance)))
|
(exn:fail:parse-grammar-srclocs instance)))
|
||||||
|
|
||||||
(define current-parser-error-handler
|
(define current-parser-error-handler
|
||||||
(make-parameter
|
(make-parameter
|
||||||
|
|
|
@ -42,6 +42,9 @@
|
||||||
(struct pattern-maybe pattern (val)
|
(struct pattern-maybe pattern (val)
|
||||||
#:transparent)
|
#:transparent)
|
||||||
|
|
||||||
|
(struct pattern-elide pattern (val)
|
||||||
|
#:transparent)
|
||||||
|
|
||||||
(struct pattern-seq pattern (vals)
|
(struct pattern-seq pattern (vals)
|
||||||
#:transparent)
|
#:transparent)
|
||||||
|
|
||||||
|
|
|
@ -13,4 +13,5 @@
|
||||||
(define (choice stx) (raise-syntax-error #f "Used out of context of rules" stx))
|
(define (choice stx) (raise-syntax-error #f "Used out of context of rules" stx))
|
||||||
(define (repeat stx) (raise-syntax-error #f "Used out of context of rules" stx))
|
(define (repeat stx) (raise-syntax-error #f "Used out of context of rules" stx))
|
||||||
(define (maybe stx) (raise-syntax-error #f "Used out of context of rules" stx))
|
(define (maybe stx) (raise-syntax-error #f "Used out of context of rules" stx))
|
||||||
|
(define (elide stx) (raise-syntax-error #f "Used out of context of rules" stx))
|
||||||
(define (seq stx) (raise-syntax-error #f "Used out of context of rules" stx))
|
(define (seq stx) (raise-syntax-error #f "Used out of context of rules" stx))
|
|
@ -71,6 +71,8 @@
|
||||||
`(repeat ,min ,(recur val))]
|
`(repeat ,min ,(recur val))]
|
||||||
[(struct pattern-maybe (start end val))
|
[(struct pattern-maybe (start end val))
|
||||||
`(maybe ,(recur val))]
|
`(maybe ,(recur val))]
|
||||||
|
[(struct pattern-elide (start end val))
|
||||||
|
`(elide ,(recur val))]
|
||||||
[(struct pattern-seq (start end vals))
|
[(struct pattern-seq (start end vals))
|
||||||
`(seq ,@(map recur vals))])
|
`(seq ,@(map recur vals))])
|
||||||
source-location))
|
source-location))
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#lang at-exp racket
|
#lang racket
|
||||||
(require rackunit)
|
(require rackunit)
|
||||||
(require "world.rkt")
|
(require "world.rkt")
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@
|
||||||
"get key"
|
"get key"
|
||||||
"You now have the key.\n")
|
"You now have the key.\n")
|
||||||
|
|
||||||
|
|
||||||
(check-cmd?
|
(check-cmd?
|
||||||
"n"
|
"n"
|
||||||
"You're standing in a meadow. There is a house to the north.\n")
|
"You're standing in a meadow. There is a house to the north.\n")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user