Overview Tutorial

download    Version 1.1 - Version with improved language extension analyzer

In this section we propose a complete solution providing a Java Extension to Matching: JEM. This has been inspired by approaches like XTatic or AxE which are in fact designed following classical structural decomposition mechanisms available in functional languages like Objective-Caml for example.

Architecture

Code sample

The following function implements a naive pretty printer for an Xml based document.


   public static void print(IDocument stream, PrintStream printer) {
     match (stream) {
     case <_ as tag att [ANY* as content]> {
          printer.print(<"+tag);
          for(int i = 0; i < att.size(); i++) {
              String name = att.getName(i);
              String value = att.getValue(i);
	      printer.print(" "+name+"='"+value+"'");
	  }
	  if (content.isEmpty()) {
             printer.println("/>");
	  } else {
             printer.println(">");
	     print(content,printer);
	     printer.println("</"+tag+">");
          }
       }
     case CDATA as string {
	  printer.println("<![CDATA["+string+"]]>"); 
       }
     default {
          IDocumentIterator iterator = stream.getIterator()
	  while(iterator.hasMoreElements()) {
              print(iterator.nextElement(),printer);
         }
       }
     }
   }
        

Transformed code sample

This code is analysed and a pure Java code generation is performed. Such use case is transformed as shown in the next code fragment (Java packages has been removed in in order to increase readability).


   public static void print(IDocument stream, PrintStream printer) {
     String[] $Automata_0$ = new String[]{
		"rO0ABX...AoAAAABAAAAB3g=",
		"rO0ABX...AoAAAABAAAAAXg=",};
     AutomataUtilities.ResultOfMatching $Result_0$ = 
                AutomataUtilities.match(stream, $Automata_0$);
     switch ($Result_0$.getIndex()) {
     case 0: {
         String tag = $Result_0$.getString("tag");
         IAttributes att = $Result_0$.getAttributes("att");
         IDocument content = $Result_0$.getDocument("content");
         assert tag != null;
         printer.print("<" + tag);
         for (int i = 0; i < att.size(); i++) {
              String name = att.getName(i);
              String value = att.getValue(i);
              printer.print(" "+name+"='"+value+"'");
         }
         if (content.isEmpty()) {
            printer.println("/>");
         } else {
            printer.println(">");
            print(" ", content, printer);
            printer.println("</" + tagname + ">");
         }

         break;
       }
     case 1: {
         String string = $Result_0$.getString("string");
         assert string != null;
         printer.println("<![CDATA[" + string + "]]>");

         break;
       }
     default: {
         IDocumentIterator iterator = stream.getIterator()
         while(iterator.hasMoreElements()) {
            print(iterator.nextElement(),printer);
         }
       }
     }
   }