4031ce1412ae5069b84a81fa086c65a2b39297b2
[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(BufferedReader r) throws IOException {
14         String sectionName = "";
15         StringBuffer sb = new StringBuffer();
16         while(true) {
17             String s = r.readLine();
18             if (s==null) break;
19             if (s.startsWith("==")) {
20                 sections.put(sectionName, sb.toString());
21                 sb = new StringBuffer();
22                 sectionName = s.trim();
23                 while(sectionName.startsWith("="))
24                     sectionName = sectionName.substring(1);
25                 while(sectionName.endsWith("="))
26                     sectionName = sectionName.substring(0, sectionName.length()-1);
27                 sectionName = sectionName.trim().toLowerCase();
28                 continue;
29             }
30             sb.append(s+"\n");
31         }
32         for(String s : sections.keySet())
33             processSection(s);
34     }
35
36     // private //////////////////////////////////////////////////////////////////////////////
37
38     private String name;
39     private String texDocumentation;
40
41     // must keep proper ordering for FPGA (FIXME: should alphabetize when synthesizing)
42     private ArrayList<BenkoBoxDescription> benkoBoxes = new ArrayList<BenkoBoxDescription>();
43
44     private HashMap<String,String> sections = new HashMap<String,String>();
45
46     private void processSection(String section) throws IOException {
47         if (section.equals("")) {
48             BufferedReader br = new BufferedReader(new StringReader(sections.get(section)));
49             for(String s = br.readLine(); s != null; s = br.readLine()) {
50                 if (s.trim().length()==0) continue;
51                 String key = s.substring(0, s.indexOf(':')).trim();
52                 String val = s.substring(s.indexOf(':')+1).trim();
53                 if (key.toLowerCase().equals("ship"))
54                     name = val.trim();
55             }
56         } else if (section.equals("ports")) {
57             BufferedReader br = new BufferedReader(new StringReader(sections.get(section)));
58             boolean rightSide = false;
59             BenkoBoxDescription p = null;
60             for(String s = br.readLine(); s != null; s = br.readLine()) {
61                 if (s.trim().length()==0) { rightSide = true; continue; }
62
63                 String key = s.substring(0, s.indexOf(':')).trim();
64                 boolean tokenOnly = false;
65                 boolean inbox = false;
66                 key = key.replaceAll("  +", " ");
67                 if      (key.equals("token in"))  { tokenOnly = true;   inbox = true;  }
68                 else if (key.equals("token out")) { tokenOnly = true;   inbox = false; }
69                 else if (key.equals("data in"))   { tokenOnly = false;  inbox = true;  }
70                 else if (key.equals("data out"))  { tokenOnly = false;  inbox = false; }
71                 else if (key.equals("in"))        { tokenOnly = false;  inbox = true;  }
72                 else if (key.equals("out"))       { tokenOnly = false;  inbox = false; }
73                 else if (key.startsWith("constant")) {
74                     continue;
75                 } else if (key.startsWith("shortcut to")) {
76                     continue;
77                 }
78                 else throw new RuntimeException("unknown port type: \""+key+"\"");
79
80                 p = null;
81                 String val = s.substring(s.indexOf(':')+1).trim();
82                 String boxname = val.indexOf('.') != -1 ? val.substring(0, val.indexOf('.')) : val;
83                 String dest    = val.indexOf('.') != -1 ? val.substring(val.indexOf('.')+1)  : "";
84                 for (BenkoBoxDescription b : benkoBoxes)
85                     if (b.getName().equals(boxname)) { p = b; break; }
86                 if (p==null) p = new BenkoBoxDescription(this, boxname, tokenOnly, inbox);
87                 p.addDest(dest);
88             }
89         }
90     }
91
92     void add(BenkoBoxDescription b) { benkoBoxes.add(b); }
93
94 }