37d78efbb7c4f4c29004b2d16a04178bf5829a4c
[fleet.git] / src / edu / berkeley / fleet / doc / ShipDescription.java
1 package edu.berkeley.fleet.doc;
2
3 import java.io.*;
4 import java.util.*;
5
6 /** the Java representation of a .ship file */
7 public class ShipDescription implements Iterable<BenkoBoxDescription> {
8
9     public String getName() { return name; }
10     public String getSection(String sectionName) { return sections.get(sectionName); }
11     public Iterator<BenkoBoxDescription> iterator() { return benkoBoxes.iterator(); }
12
13     public ShipDescription(String name, BufferedReader r) throws IOException {
14         if (name.endsWith(".ship"))
15             name = name.substring(0, name.length() - ".ship".length());
16         while(name.indexOf('/') != -1) name = name.substring(name.indexOf('/')+1);
17         this.name = name;
18         String sectionName = null;
19         StringBuffer sb = new StringBuffer();
20         while(true) {
21             String s = r.readLine();
22             if (s==null || s.startsWith("==")) {
23                 if (sectionName != null) sections.put(sectionName, sb.toString());
24                 if (s==null) break;
25                 sb = new StringBuffer();
26                 sectionName = s.trim();
27                 while(sectionName.startsWith("="))
28                     sectionName = sectionName.substring(1);
29                 while(sectionName.endsWith("="))
30                     sectionName = sectionName.substring(0, sectionName.length()-1);
31                 sectionName = sectionName.trim().toLowerCase();
32                 continue;
33             }
34             sb.append(s+"\n");
35         }
36         for(String s : sections.keySet())
37             processSection(s);
38     }
39
40     // private //////////////////////////////////////////////////////////////////////////////
41
42     private String name;
43     private String texDocumentation;
44
45     // must keep proper ordering for FPGA (FIXME: should alphabetize when synthesizing)
46     private ArrayList<BenkoBoxDescription> benkoBoxes = new ArrayList<BenkoBoxDescription>();
47
48     private HashMap<String,String> sections = new HashMap<String,String>();
49
50     private void processSection(String section) throws IOException {
51         if (section.equals("")) {
52             BufferedReader br = new BufferedReader(new StringReader(sections.get(section)));
53             for(String s = br.readLine(); s != null; s = br.readLine()) {
54                 if (s.trim().length()==0) continue;
55                 String key = s.substring(0, s.indexOf(':')).trim();
56                 String val = s.substring(s.indexOf(':')+1).trim();
57                 if (key.toLowerCase().equals("ship"))
58                     name = val.trim();
59             }
60         } else if (section.equals("ports")) {
61             BufferedReader br = new BufferedReader(new StringReader(sections.get(section)));
62             boolean rightSide = false;
63             BenkoBoxDescription p = null;
64             for(String s = br.readLine(); s != null; s = br.readLine()) {
65                 if (s.trim().length()==0) { rightSide = true; continue; }
66
67                 String key = s.substring(0, s.indexOf(':')).trim();
68                 boolean tokenOnly = false;
69                 boolean inbox = false;
70                 key = key.replaceAll("  +", " ");
71                 if      (key.equals("token in"))  { tokenOnly = true;   inbox = true;  }
72                 else if (key.equals("token out")) { tokenOnly = true;   inbox = false; }
73                 else if (key.equals("data in"))   { tokenOnly = false;  inbox = true;  }
74                 else if (key.equals("data out"))  { tokenOnly = false;  inbox = false; }
75                 else if (key.equals("in"))        { tokenOnly = false;  inbox = true;  }
76                 else if (key.equals("out"))       { tokenOnly = false;  inbox = false; }
77                 else if (key.startsWith("constant")) {
78                     continue;
79                 } else if (key.startsWith("sibling")) {
80                     continue;
81                 } else if (key.startsWith("shortcut to")) {
82                     continue;
83                 }
84                 else throw new RuntimeException("unknown port type: \""+key+"\"");
85
86                 p = null;
87                 String val = s.substring(s.indexOf(':')+1).trim();
88                 String boxname = val.indexOf('.') != -1 ? val.substring(0, val.indexOf('.')) : val;
89                 String dest    = val.indexOf('.') != -1 ? val.substring(val.indexOf('.')+1)  : "";
90                 for (BenkoBoxDescription b : benkoBoxes)
91                     if (b.getName().equals(boxname)) { p = b; break; }
92                 if (p==null) p = new BenkoBoxDescription(this, boxname, tokenOnly, inbox);
93                 p.addDest(dest);
94             }
95         }
96     }
97
98     void add(BenkoBoxDescription b) { benkoBoxes.add(b); }
99
100 }