|
|
@ -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
|
|
|
|