bb203fbfb40dc457ccb78898b224e2b99fc015bd
[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     // FIXME
51     public HashMap<String,Constant> constants = new HashMap<String,Constant>();
52
53     public Constant getConstant(String name) {
54         return constants.get(name);
55     }
56
57     private void processSection(String section) throws IOException {
58         if (section.equals("")) {
59             BufferedReader br = new BufferedReader(new StringReader(sections.get(section)));
60             for(String s = br.readLine(); s != null; s = br.readLine()) {
61                 if (s.trim().length()==0) continue;
62                 String key = s.substring(0, s.indexOf(':')).trim();
63                 String val = s.substring(s.indexOf(':')+1).trim();
64                 if (key.toLowerCase().equals("ship"))
65                     name = val.trim();
66             }
67         } else if (section.equals("constants")) {
68             BufferedReader br = new BufferedReader(new StringReader(sections.get(section)));
69             for(String s = br.readLine(); s != null; s = br.readLine()) {
70                 if (s.indexOf(':')==-1) continue;
71                 String key = s.substring(0, s.indexOf(':')).trim();
72                 if (key.startsWith("constant")) {
73                     String constname = key.substring("constant".length()+1).trim();
74                     String val       = s.substring(s.indexOf(':')+1).trim();
75                     constants.put(constname, new Constant(val));
76                 }
77             }
78         } else if (section.equals("ports")) {
79             BufferedReader br = new BufferedReader(new StringReader(sections.get(section)));
80             boolean rightSide = false;
81             BenkoBoxDescription p = null;
82             for(String s = br.readLine(); s != null; s = br.readLine()) {
83                 if (s.trim().length()==0) { rightSide = true; continue; }
84
85                 String key = s.substring(0, s.indexOf(':')).trim();
86                 boolean tokenOnly = false;
87                 boolean inbox = false;
88                 key = key.replaceAll("  +", " ");
89                 if      (key.equals("token in"))  { tokenOnly = true;   inbox = true;  }
90                 else if (key.equals("token out")) { tokenOnly = true;   inbox = false; }
91                 else if (key.equals("data in"))   { tokenOnly = false;  inbox = true;  }
92                 else if (key.equals("data out"))  { tokenOnly = false;  inbox = false; }
93                 else if (key.equals("in"))        { tokenOnly = false;  inbox = true;  }
94                 else if (key.equals("out"))       { tokenOnly = false;  inbox = false; }
95                 else if (key.startsWith("constant")) {
96                     String constname = key.substring("constant".length()+1).trim();
97                     String val       = s.substring(s.indexOf(':')+1).trim();
98                     p.constants.put(constname, new Constant(val));
99                 } else if (key.startsWith("shortcut to")) {
100                     continue;
101                 }
102                 else throw new RuntimeException("unknown port type: \""+key+"\"");
103
104                 p = null;
105                 String val = s.substring(s.indexOf(':')+1).trim();
106                 String boxname = val.indexOf('.') != -1 ? val.substring(0, val.indexOf('.')) : val;
107                 String dest    = val.indexOf('.') != -1 ? val.substring(val.indexOf('.')+1)  : "";
108                 for (BenkoBoxDescription b : benkoBoxes)
109                     if (b.getName().equals(boxname)) { p = b; break; }
110                 if (p==null) p = new BenkoBoxDescription(this, boxname, tokenOnly, inbox);
111                 p.addDest(dest);
112             }
113         }
114     }
115
116     void add(BenkoBoxDescription b) { benkoBoxes.add(b); }
117 }