Package COM.INFORMATIMAGO.COMMON-LISP.CESARUM.PMATCH


Sexp Pattern Matcher


License:

    AGPL3

    Copyright Pascal J. Bourguignon 2003 - 2012

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.
    If not, see <http://www.gnu.org/licenses/>


(collect-variables pat)
function
PAT:       A symbolic expression with the following syntax:
             (?v v)  expects a symbol (variable).
             (?c c)  expects a constant (non symbol atom).
             (?x x)  expects anything (one item).
             (?+ l)  expects anything (one or more items).
             (?* l)  expects anything (zero or more items).
             other   expects exactly other (can be a sublist).
RETURN:    A list of the symbol used in the various (?. sym) items,
           in no particular order, but with duplicates deleted.
(match pat exp &optional ms)
function
DO:        A pattern matcher accepting the following syntax:
             ?av        expects a symbol (variable).
             ?ac        expects a constant (non symbol atom).
             ?ax        expects anything (one item).
             (?v n)     expects a symbol (variable)     and bind it.
             (?c n)     expects a constant (non symbol atom)  and bind it.
             (?x n)     expects anything (one item)     and bind it.
             (?n n ...) expects anything (several item) and bind them.
             (?+ ...)   expects anything (one or more times).  AOB
             (?* ...)   expects anything (zero or more times). AOB
             (?? ...)   expects anything (zero or one time).
             ...        expects exactly ... (can be a sublist).
           AOB = All occurences bind.
RETURN:    A match-state structure.
SEE ALSO:  match-state-failed-p to check if the matching failed.
           match-state-dict     to get the binding dictionary.
(match-case sexp &rest clauses)
macro
SEXP:    A symbolic expression, evaluated.
CLAUSES: A list of (pattern &body body)
         The pattern must be litteral.
         Lexical variable names are extracted from it, and body is executed
         in a lexical environment where these names are bound to the matched
         subexpression of SEXP.
DO:      Execute the body of the clause whose pattern matches the SEXP,
         or whose pattern is a symbol string-equal to OTHERWISE.
EXAMPLE: (match-case expr
            ((add       (?x a) to   (?x b)) `(+ ,a ,b))
            ((multiply  (?x a) with (?x b)) `(* ,a ,b))
            ((substract (?x a) from (?x a)) 0)
            (otherwise                      :error))
(match-case* sexp clauses)
function
SEXP:    A symbolic expression, evaluated.
CLAUSES: A list of (pattern func) or (otherwise ofunc)
         The functions FUNC is called with one BINDINGS argument.
         The function OFUNC is called with no argument.
DO:      Call the function of the clause whose pattern matches the SEXP,
         or whose pattern is a symbol string-equal to OTHERWISE.
RETURN:  The result of the called function, and the pattern that matched.
EXAMPLE: (match-case* expr
            `(((add       (?x a) to   (?x b))
                ,(lambda (bindings) `(+ ,(aget bindings 'a) ,(aget bindings 'b)))
               ((multiply  (?x a) with (?x b))
                ,(lambda (bindings) `(* ,(aget bindings 'a) ,(aget bindings 'b))))
               ((substract (?x a) from (?x a))
                ,(constantly 0))
               (otherwise
                ,(lambda () (error "No matching pattern"))))))
(match-dict-map ms function)
function
DO:     Calls FUNCTION (lambda (symbol value) ...) with all successive bindings,
        (unless matching state is failed).
RETURN: The list of results of the FUNCTION.
(match-state-dict ms)
function
RETURN: The dictionary of MATCH-STATE.
(match-state-failed-p ms)
function
RETURN: Whether the match failed.