2008-06-30 21:54:22 -04:00
|
|
|
; tree regular expression pattern matching
|
|
|
|
; by Jeff Bezanson
|
|
|
|
|
|
|
|
(define (unique lst)
|
2009-01-31 20:53:58 -05:00
|
|
|
(if (null? lst)
|
2008-06-30 21:54:22 -04:00
|
|
|
()
|
2009-01-31 20:53:58 -05:00
|
|
|
(cons (car lst)
|
2019-08-09 10:18:36 -04:00
|
|
|
(filter (lambda (x) (not (eq x (car lst))))
|
|
|
|
(unique (cdr lst))))))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
|
|
|
; list of special pattern symbols that cannot be variable names
|
|
|
|
(define metasymbols '(_ ...))
|
|
|
|
|
|
|
|
; expression tree pattern matching
|
|
|
|
; matches expr against pattern p and returns an assoc list ((var . expr) (var . expr) ...)
|
|
|
|
; mapping variables to captured subexpressions, or #f if no match.
|
|
|
|
; when a match succeeds, __ is always bound to the whole matched expression.
|
|
|
|
;
|
|
|
|
; p is an expression in the following pattern language:
|
|
|
|
;
|
|
|
|
; _ match anything, not captured
|
|
|
|
; <func> any scheme function; matches if (func expr) returns #t
|
|
|
|
; <var> match anything and capture as <var>. future occurrences of <var> in the pattern
|
|
|
|
; must match the same thing.
|
|
|
|
; (head <p1> <p2> etc) match an s-expr with 'head' matched literally, and the rest of the
|
|
|
|
; subpatterns matched recursively.
|
|
|
|
; (-/ <ex>) match <ex> literally
|
|
|
|
; (-^ <p>) complement of pattern <p>
|
|
|
|
; (-- <var> <p>) match <p> and capture as <var> if match succeeds
|
|
|
|
;
|
|
|
|
; regular match constructs:
|
|
|
|
; ... match any number of anything
|
|
|
|
; (-$ <p1> <p2> etc) match any of subpatterns <p1>, <p2>, etc
|
|
|
|
; (-* <p>) match any number of <p>
|
|
|
|
; (-? <p>) match 0 or 1 of <p>
|
|
|
|
; (-+ <p>) match at least 1 of <p>
|
|
|
|
; all of these can be wrapped in (-- var ) for capturing purposes
|
|
|
|
; This is NP-complete. Be careful.
|
|
|
|
;
|
|
|
|
(define (match- p expr state)
|
2009-01-31 20:53:58 -05:00
|
|
|
(cond ((symbol? p)
|
2019-08-09 10:18:36 -04:00
|
|
|
(cond ((eq p '_) state)
|
|
|
|
(#t
|
|
|
|
(let ((capt (assq p state)))
|
|
|
|
(if capt
|
|
|
|
(and (equal? expr (cdr capt)) state)
|
|
|
|
(cons (cons p expr) state))))))
|
|
|
|
|
|
|
|
((procedure? p)
|
|
|
|
(and (p expr) state))
|
|
|
|
|
|
|
|
((pair? p)
|
|
|
|
(cond ((eq (car p) '-/) (and (equal? (cadr p) expr) state))
|
|
|
|
((eq (car p) '-^) (and (not (match- (cadr p) expr state)) state))
|
|
|
|
((eq (car p) '--)
|
|
|
|
(and (match- (caddr p) expr state)
|
|
|
|
(cons (cons (cadr p) expr) state)))
|
|
|
|
((eq (car p) '-$) ; greedy alternation for toplevel pattern
|
|
|
|
(match-alt (cdr p) () (list expr) state #f 1))
|
|
|
|
(#t
|
|
|
|
(and (pair? expr)
|
|
|
|
(equal? (car p) (car expr))
|
|
|
|
(match-seq (cdr p) (cdr expr) state (length (cdr expr)))))))
|
|
|
|
|
|
|
|
(#t
|
|
|
|
(and (equal? p expr) state))))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
|
|
|
; match an alternation
|
|
|
|
(define (match-alt alt prest expr state var L)
|
2009-01-31 20:53:58 -05:00
|
|
|
(if (null? alt) #f ; no alternatives left
|
|
|
|
(let ((subma (match- (car alt) (car expr) state)))
|
2019-08-09 10:18:36 -04:00
|
|
|
(or (and subma
|
|
|
|
(match-seq prest (cdr expr)
|
|
|
|
(if var
|
|
|
|
(cons (cons var (car expr))
|
|
|
|
subma)
|
|
|
|
subma)
|
|
|
|
(- L 1)))
|
|
|
|
(match-alt (cdr alt) prest expr state var L)))))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
|
|
|
; match generalized kleene star (try consuming min to max)
|
|
|
|
(define (match-star- p prest expr state var min max L sofar)
|
|
|
|
(cond ; case 0: impossible to match
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
((> min max) #f)
|
2009-01-31 20:53:58 -05:00
|
|
|
; case 1: only allowed to match 0 subexpressions
|
2008-06-30 21:54:22 -04:00
|
|
|
((= max 0) (match-seq prest expr
|
|
|
|
(if var (cons (cons var (reverse sofar)) state)
|
2019-08-09 10:18:36 -04:00
|
|
|
state)
|
2008-06-30 21:54:22 -04:00
|
|
|
L))
|
2009-01-31 20:53:58 -05:00
|
|
|
; case 2: must match at least 1
|
2008-06-30 21:54:22 -04:00
|
|
|
((> min 0)
|
|
|
|
(and (match- p (car expr) state)
|
|
|
|
(match-star- p prest (cdr expr) state var (- min 1) (- max 1) (- L 1)
|
|
|
|
(cons (car expr) sofar))))
|
2009-01-31 20:53:58 -05:00
|
|
|
; otherwise, must match either 0 or between 1 and max subexpressions
|
|
|
|
(#t
|
2008-06-30 21:54:22 -04:00
|
|
|
(or (match-star- p prest expr state var 0 0 L sofar)
|
|
|
|
(match-star- p prest expr state var 1 max L sofar)))))
|
2019-08-09 10:18:36 -04:00
|
|
|
(define (match-star p prest expr state var min max L)
|
2008-06-30 21:54:22 -04:00
|
|
|
(match-star- p prest expr state var min max L ()))
|
|
|
|
|
|
|
|
; match sequences of expressions
|
|
|
|
(define (match-seq p expr state L)
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(cond ((not state) #f)
|
2019-08-09 10:18:36 -04:00
|
|
|
((null? p) (if (null? expr) state #f))
|
|
|
|
(#t
|
|
|
|
(let ((subp (car p))
|
|
|
|
(var #f))
|
|
|
|
(if (and (pair? subp)
|
|
|
|
(eq (car subp) '--))
|
|
|
|
(begin (set! var (cadr subp))
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
(set! subp (caddr subp)))
|
2019-08-09 10:18:36 -04:00
|
|
|
#f)
|
|
|
|
(let ((head (if (pair? subp) (car subp) ())))
|
|
|
|
(cond ((eq subp '...)
|
|
|
|
(match-star '_ (cdr p) expr state var 0 L L))
|
|
|
|
((eq head '-*)
|
|
|
|
(match-star (cadr subp) (cdr p) expr state var 0 L L))
|
|
|
|
((eq head '-+)
|
|
|
|
(match-star (cadr subp) (cdr p) expr state var 1 L L))
|
|
|
|
((eq head '-?)
|
|
|
|
(match-star (cadr subp) (cdr p) expr state var 0 1 L))
|
|
|
|
((eq head '-$)
|
|
|
|
(match-alt (cdr subp) (cdr p) expr state var L))
|
|
|
|
(#t
|
|
|
|
(and (pair? expr)
|
|
|
|
(match-seq (cdr p) (cdr expr)
|
|
|
|
(match- (car p) (car expr) state)
|
|
|
|
(- L 1))))))))))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
|
|
|
(define (match p expr) (match- p expr (list (cons '__ expr))))
|
|
|
|
|
|
|
|
; given a pattern p, return the list of capturing variables it uses
|
|
|
|
(define (patargs- p)
|
2009-01-31 20:53:58 -05:00
|
|
|
(cond ((and (symbol? p)
|
2008-06-30 21:54:22 -04:00
|
|
|
(not (member p metasymbols)))
|
|
|
|
(list p))
|
2019-08-09 10:18:36 -04:00
|
|
|
|
2009-01-31 20:53:58 -05:00
|
|
|
((pair? p)
|
2008-06-30 21:54:22 -04:00
|
|
|
(if (eq (car p) '-/)
|
|
|
|
()
|
2019-08-09 10:18:36 -04:00
|
|
|
(unique (apply append (map patargs- (cdr p))))))
|
|
|
|
|
2009-01-31 20:53:58 -05:00
|
|
|
(#t ())))
|
2008-06-30 21:54:22 -04:00
|
|
|
(define (patargs p)
|
|
|
|
(cons '__ (patargs- p)))
|
|
|
|
|
|
|
|
; try to transform expr using a pattern-lambda from plist
|
|
|
|
; returns the new expression, or expr if no matches
|
|
|
|
(define (apply-patterns plist expr)
|
2009-01-31 20:53:58 -05:00
|
|
|
(if (null? plist) expr
|
|
|
|
(if (procedure? plist)
|
2019-08-09 10:18:36 -04:00
|
|
|
(let ((enew (plist expr)))
|
|
|
|
(if (not enew)
|
|
|
|
expr
|
|
|
|
enew))
|
|
|
|
(let ((enew ((car plist) expr)))
|
|
|
|
(if (not enew)
|
|
|
|
(apply-patterns (cdr plist) expr)
|
|
|
|
enew)))))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
|
|
|
; top-down fixed-point macroexpansion. this is a typical algorithm,
|
|
|
|
; but it may leave some structure that matches a pattern unexpanded.
|
|
|
|
; the advantage is that non-terminating cases cannot arise as a result
|
|
|
|
; of expression composition. in other words, if the outer loop terminates
|
|
|
|
; on all inputs for a given set of patterns, then the whole algorithm
|
|
|
|
; terminates. pattern sets that violate this should be easier to detect,
|
|
|
|
; for example
|
|
|
|
; (pattern-lambda (/ 2 3) '(/ 3 2)), (pattern-lambda (/ 3 2) '(/ 2 3))
|
|
|
|
; TODO: ignore quoted expressions
|
|
|
|
(define (pattern-expand plist expr)
|
2009-01-31 20:53:58 -05:00
|
|
|
(if (not (pair? expr))
|
2008-06-30 21:54:22 -04:00
|
|
|
expr
|
2009-01-31 20:53:58 -05:00
|
|
|
(let ((enew (apply-patterns plist expr)))
|
2019-08-09 10:18:36 -04:00
|
|
|
(if (eq enew expr)
|
2009-01-31 20:53:58 -05:00
|
|
|
; expr didn't change; move to subexpressions
|
2019-08-09 10:18:36 -04:00
|
|
|
(cons (car expr)
|
|
|
|
(map (lambda (subex) (pattern-expand plist subex)) (cdr expr)))
|
|
|
|
; expr changed; iterate
|
|
|
|
(pattern-expand plist enew)))))
|