Demeter
 

DAJ Examples

CD Library

Managing a collection of CD's.

  • The .cd file :
                  {
                    parser lookahead=2;
                  }
    
                  import java.util.*;
    
                  DiscCollection = CDList EOF. 
                  CD = "("*s*l "CDTitle."<title> Title "by" <artist> Artist ["genre" <genre> Genre
                        ] "released" <year>Year "songs"<songs>List(Song)")" ".".
                  Title = Ident. 
                  Artist : Band | Singer | MultiArtist common Name.
                  MultiArtist = "MultiArtis".
                  Band = "band" List(Musicians).
                  Name = Ident. 
                  Musicians = Person ["known as"NickName]. 
                  NickName = Ident. 
                  Person = "Name"First ["Middle" Second] "Last" Last. 
                  First = Ident. 
                  Second = Ident. 
                  Last = Ident. 
                  Singer = Person.
                  Genre : Pop | Rock | Jazz | Classical | Alternative | Soundtrack | Live.
                  Pop = "Pop".
                  Rock = "Rock".
                  Jazz = "Jazz".
                  Classical = "Classical".
                  Alternative  = "Alternative".
                  Soundtrack = "Soundtrack".
                  Live= .
                  Year = Integer.
                  Song = ["track"TrackNo] Title ["duration"Time].
                  TrackNo = Integer. 
                  Time = Minutes ":" Seconds. 
                  Minutes = Integer. 
                  Seconds = <dd> Double. 
    
    
                  CDList ~ "(" CD {CD}  ")".
                  List(X) ~ "(" X {","X} ")".
    
              
  • A sampe .trv file for the above class dictionary:
                aspect ToAll {
                declare strategy: eachCD: "from DiscCollection to *";
    
                declare traversal:
                  void listAll(): eachCD(CDPrinter);
                  }
    
              
  • And a sample Visitor.
              class CDPrinter {
                void before (Minutes minutes){
                  System.out.print("\t"+ minutes.integer.intValue());
                }
    
                void after (Minutes minutes){
                  System.out.print(":");
                }
    
                void before (TrackNo tNo) {
                  System.out.print(tNo.integer.intValue());
                }
    
                void before (Title title){
                  System.out.print("\t"+title.ident+"\t");
                }
    
                void before (Seconds seconds){
                  System.out.println(seconds.dd.toString());
                }
    
                void before (Song song){
                  System.out.print("\t ->");
                }
                void before(CD cd) {
                  System.out.println("---- Begin CD ------");
                }
    
                void after(CD cd) {
                  System.out.println("---- End CD ------");
                }
              }
    
              
  • Sample main.
              import java.io.File;
    
              class Main {
                public static void main(String[] args) {
                  System.out.println(" **** START **** ");
                  try {
                    DiscCollection cdcollection = DiscCollection.parse(new File(args[0]));
                    cdcollection.listAll();
                  } catch (Exception e) {
                    System.err.println(e);
                  }
                  System.out.println(" **** END **** ");
                }
              }