From e435574b9f37701310de4a56714eebaa536818a9 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Tue, 10 May 2016 12:50:23 -0700 Subject: [PATCH] repair parser & parser tests --- brag/brag/rules/parser.rkt | 18 ++++++------ brag/brag/test/test-parser.rkt | 54 ++++++++++++++++++++++++---------- 2 files changed, 48 insertions(+), 24 deletions(-) diff --git a/brag/brag/rules/parser.rkt b/brag/brag/rules/parser.rkt index 579d249..b359f5b 100755 --- a/brag/brag/rules/parser.rkt +++ b/brag/brag/rules/parser.rkt @@ -90,34 +90,34 @@ #f) $2))] - ;; bang indicates hiding. set hide value to "hide" - [(RULE_HEAD_HIDDEN pattern) + [(RULE_HEAD_HIDDEN pattern) ; bang indicates hiding (begin (define trimmed (cadr (regexp-match #px"!(\\S+)\\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)) + (string-length trimmed) + (string-length "!")) (position-line $1-start-pos) (position-col $1-start-pos)) trimmed - ''hide) ; symbols won't work for these signals + ''hide) ; symbol needs to be double quoted in this case $2))] - ;; atsign indicates splicing. set hide value to "splice" - [(RULE_HEAD_SPLICED pattern) + [(RULE_HEAD_SPLICED pattern) ;atsign indicates splicinh (begin (define trimmed (cadr (regexp-match #px"@(\\S+)\\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)) + (string-length trimmed) + (string-length "@")) (position-line $1-start-pos) (position-col $1-start-pos)) trimmed - ''splice) + ''splice) ; symbol needs to be double quoted in this case $2))]] [pattern @@ -194,7 +194,7 @@ ;; only works for nonterminals on the right side (meaningless with terminals) (if (token-id? $2) (error 'brag "Can't use splice operator with terminal") - (pattern-id (position->pos $2-start-pos) + (pattern-id (position->pos $1-start-pos) (position->pos $2-end-pos) $2 'splice))]]) diff --git a/brag/brag/test/test-parser.rkt b/brag/brag/test/test-parser.rkt index 22fcd98..a075a17 100755 --- a/brag/brag/test/test-parser.rkt +++ b/brag/brag/test/test-parser.rkt @@ -25,13 +25,39 @@ (lhs-id (p 1) (p 5) "expr" #f) (pattern-token (p 8) (p 13) "COLON" #f)))) -(check-equal? (grammar-parser (tokenize (open-input-string "expr : COLON"))) - (list (rule (p 1) (p 21) +(check-equal? (grammar-parser (tokenize (open-input-string "!expr : COLON"))) + (list (rule (p 1) (p 14) + (lhs-id (p 1) (p 6) "expr" ''hide) + (pattern-token (p 9) (p 14) "COLON" #f)))) + +(check-equal? (grammar-parser (tokenize (open-input-string "@expr : COLON"))) + (list (rule (p 1) (p 14) + (lhs-id (p 1) (p 6) "expr" ''splice) + (pattern-token (p 9) (p 14) "COLON" #f)))) + +(check-equal? (grammar-parser (tokenize (open-input-string "expr : !COLON COLON"))) + (list (rule (p 1) (p 20) + (lhs-id (p 1) (p 5) "expr" #f) + (pattern-seq (p 8) (p 20) + (list + (pattern-token (p 8) (p 14) "COLON" 'hide) + (pattern-token (p 15) (p 20) "COLON" #f)))))) + +(check-equal? (grammar-parser (tokenize (open-input-string "expr : !thing COLON"))) + (list (rule (p 1) (p 20) + (lhs-id (p 1) (p 5) "expr" #f) + (pattern-seq (p 8) (p 20) + (list + (pattern-id (p 8) (p 14) "thing" 'hide) + (pattern-token (p 15) (p 20) "COLON" #f)))))) + +(check-equal? (grammar-parser (tokenize (open-input-string "expr : @thing COLON"))) + (list (rule (p 1) (p 20) (lhs-id (p 1) (p 5) "expr" #f) - (pattern-seq (p 8) (p 21) + (pattern-seq (p 8) (p 20) (list - (pattern-token (p 8) (p 15) "COLON" #t) - (pattern-token (p 16) (p 21) "COLON" #f)))))) + (pattern-id (p 8) (p 14) "thing" 'splice) + (pattern-token (p 15) (p 20) "COLON" #f)))))) (check-equal? (grammar-parser (tokenize (open-input-string "expr : 'hello'*"))) (list (rule (p 1) (p 16) @@ -47,11 +73,11 @@ 1 (pattern-lit (p 8) (p 15) "hello" #f))))) -(check-equal? (grammar-parser (tokenize (open-input-string "expr : [<'hello'>]"))) - (list (rule (p 1) (p 19) +(check-equal? (grammar-parser (tokenize (open-input-string "expr : [!'hello']"))) + (list (rule (p 1) (p 18) (lhs-id (p 1) (p 5) "expr" #f) - (pattern-maybe (p 8) (p 19) - (pattern-lit (p 9) (p 18) "hello" #t))))) + (pattern-maybe (p 8) (p 18) + (pattern-lit (p 9) (p 17) "hello" 'hide))))) (check-equal? (grammar-parser (tokenize (open-input-string "expr : COLON | BLAH"))) (list (rule (p 1) (p 20) @@ -70,13 +96,12 @@ (list (pattern-token (p 23) (p 26) "BAZ" #f) (pattern-id (p 27) (p 31) "expr" #f)))))))) -(check-equal? (grammar-parser (tokenize (open-input-string "expr : one two "))) - (list (rule (p 1) (p 23) +(check-equal? (grammar-parser (tokenize (open-input-string "expr : one two !three"))) + (list (rule (p 1) (p 22) (lhs-id (p 1) (p 5) "expr" #f) - (pattern-seq (p 8) (p 23) (list (pattern-id (p 8) (p 11) "one" #f) + (pattern-seq (p 8) (p 22) (list (pattern-id (p 8) (p 11) "one" #f) (pattern-id (p 12) (p 15) "two" #f) - (pattern-id (p 16) (p 23) "three" #t)))))) - + (pattern-id (p 16) (p 22) "three" 'hide)))))) (check-equal? (grammar-parser (tokenize (open-input-string "expr : (one two three)"))) (list (rule (p 1) (p 23) @@ -85,7 +110,6 @@ (pattern-id (p 13) (p 16) "two" #f) (pattern-id (p 17) (p 22) "three" #f)))))) - (check-equal? (grammar-parser (tokenize (open-input-string "expr : one two* three"))) (list (rule (p 1) (p 22) (lhs-id (p 1) (p 5) "expr" #f)