1 package edu.berkeley.fleet.doc;
6 /** the Java representation of a .ship file */
7 public class ShipDescription implements Iterable<BenkoBoxDescription> {
9 public String getName() { return name; }
10 public String getSection(String sectionName) { return sections.get(sectionName); }
11 public Iterator<BenkoBoxDescription> iterator() { return benkoBoxes.iterator(); }
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);
18 String sectionName = null;
19 StringBuffer sb = new StringBuffer();
21 String s = r.readLine();
22 if (s==null || s.startsWith("==")) {
23 if (sectionName != null) sections.put(sectionName, sb.toString());
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();
36 for(String s : sections.keySet())
40 // private //////////////////////////////////////////////////////////////////////////////
43 private String texDocumentation;
45 // must keep proper ordering for FPGA (FIXME: should alphabetize when synthesizing)
46 private ArrayList<BenkoBoxDescription> benkoBoxes = new ArrayList<BenkoBoxDescription>();
48 private HashMap<String,String> sections = new HashMap<String,String>();
51 public HashMap<String,Constant> constants = new HashMap<String,Constant>();
53 public Constant getConstant(String name) {
54 return constants.get(name);
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"))
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));
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; }
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")) {
102 else throw new RuntimeException("unknown port type: \""+key+"\"");
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);
116 void add(BenkoBoxDescription b) { benkoBoxes.add(b); }