Common Lisp 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/>

README:

FUNCTION

Public

COLLECT-VARIABLES (PAT)

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 (MAKE-MATCH-STATE)))

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 CLAUSES)

SEXP: A symbolic expression, evaluated. CLAUSES: A list of (pattern func) or (otherwise ofunc) The functions FUNC is called with one BINDING 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)

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)

RETURN: The dictionary of MATCH-STATE.

MATCH-STATE-FAILED-P (MS)

RETURN: Whether the match failed.

Private

DICT-BOUNDP (DICT NAME)

PRIVATE

DICT-VALUE (DICT NAME)

PRIVATE

EVALUATE (INSTANCE)

PRIVATE

EXP-CONSTANT-P (EXP)

PRIVATE

EXP-VARIABLE-P (EXP)

PRIVATE

GENERATE-ALL-FOLLOWS (EXP)

RETURN: a list of possible follows from shortest to longuest.

INSTANCIATE (MS SKELETON)

PRIVATE PRE: (not (match-state-failed-p ms)) DO: Instanciate the skeleton, substituting all occurence of (: var) with the value bound to var in the binding dictionary of MS, Occurences of (:@ var) are split in line like ,@ in backquotes. Then all remaining (: form) are evaluated (with eval) from the deepest first.

MATCH-DICT-ADD-BINDING (MS PAT EXP)

PRIVATE

MATCH-STATE-FAIL (MS RAISON)

PRIVATE

MATCH-STATE-RETRY (MS)

PRIVATE

PAT-ALTERNATIVE-P (PAT)

PRIVATE

PAT-ANONYMOUS-CONSTANT-P (PAT)

PRIVATE

PAT-ANONYMOUS-EXPRESSION-P (PAT)

PRIVATE

PAT-ANONYMOUS-VARIABLE-P (PAT)

PRIVATE

PAT-CONSTANT-P (PAT)

PRIVATE

PAT-EXPRESSION-P (PAT)

PRIVATE

PAT-NAMED-P (PAT)

PRIVATE

PAT-OPTIONAL-P (PAT)

PRIVATE

PAT-OPTIONAL-REPEAT-P (PAT)

PRIVATE

PAT-REPEAT-P (PAT)

PRIVATE

PAT-SQUELETON-EVAL-P (PAT)

PRIVATE

PAT-VARIABLE-P (PAT)

PRIVATE

SUBST-BINDINGS (EXPR DICT)

PRIVATE

Undocumented

MAKE-MATCH-STATE (&KEY DICT)

MACRO

Public

MATCH-CASE (SEXP &REST CLAUSES)

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

Private

WITH-GENSYMS (SYMS &BODY BODY)

DO: Replaces given symbols with gensyms. Useful for creating macros. NOTE: This version by Paul Graham in On Lisp.

Undocumented

DEFPATTERN (NAME PATTERN)