myPatterns: pattern matching in custom notations for Java & JavaScript

Nic Volanschi <nic.volanschi@free.fr>
Tue, 29 Jun 2010 20:43:16 +0200

          From comp.compilers

Related articles
myPatterns: pattern matching in custom notations for Java & JavaScript nic.volanschi@free.fr (Nic Volanschi) (2010-06-29)
| List of all articles for this month |
From: Nic Volanschi <nic.volanschi@free.fr>
Newsgroups: comp.compilers
Date: Tue, 29 Jun 2010 20:43:16 +0200
Organization: Compilers Central
Keywords: tools, lex, available
Posted-Date: 30 Jun 2010 09:50:34 EDT

I am looking for (feedback from) potential users of an open source
library for Java and JavaScript, called myPatterns, implementing a
new concept: pattern matching in "custom notations".


What are custom notations?


Everyone knows pattern matching of strings using regular expressions.
myPatterns implements a more general notion of pattern matching:
- working not only on strings, but on any program data: numbers,
    objects, collections, etc.
- using patterns written either in a pre-defined notation, subsuming
    regular expressions and JSON, on in user-defined notations.


When designing a class API, programmers using this library can also
design very easily a custom syntax for matching objects of that class,
i.e. for selecting objects of a certain shape, and decomposing them
into smaller pieces.


For more details, see http://mypatterns.free.fr/


-------------------------------------------------------------------------------


Example


A balance method for red-black trees can be written very concisely
using a custom notation in which a red-rooted tree is written
"(left label right)" and a black-rooted tree is written
"[left label right]":


static final String pats[] = {"[((%a %x %b) %y %c) %z %d]",
                                                            "[(%a %x (%b %y %c)) %z %d]",
                                                            "[%a %x ((%b %y %c) %z %d)]",
                                                            "[%a %x (%b %y (%c %z %d))]"};


rbTree balance() {
        Subst s = Matchbox.matchAny(this, pats);
        if(s != null)
                return new rbTree(red,
                                                    new rbTree(black, (rbTree)s.get('a'),
                                                                          (Integer)s.get('x'),
(rbTree)s.get('b')),
                                                    (Integer)s.get('y'),
                                                    new rbTree(black, (rbTree)s.get('c'),
                                                                          (Integer)s.get('z'),
(rbTree)s.get('d')));
        return this;
}



Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.