Source code: org/sablecc/sablecc/GenProds.java
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * This file is part of SableCC. *
3 * See the file "LICENSE" for copyright information and the *
4 * terms and conditions for copying, distribution and *
5 * modification of SableCC. *
6 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
7
8 package org.sablecc.sablecc;
9
10 import org.sablecc.sablecc.analysis.*;
11 import org.sablecc.sablecc.node.*;
12 import java.util.*;
13 import com.sun.java.util.collections.*;
14 import java.io.*;
15
16 public class GenProds extends DepthFirstAdapter
17 {
18 private MacroExpander macros;
19 private ResolveIds ids;
20 private File pkgDir;
21 private String pkgName;
22 private Map hiddenProds = new TypedTreeMap(
23 StringComparator.instance,
24 StringCast.instance,
25 NodeCast.instance);
26
27 public GenProds(ResolveIds ids)
28 {
29 this.ids = ids;
30
31 try
32 {
33 macros = new MacroExpander(
34 new InputStreamReader(
35 getClass().getResourceAsStream("productions.txt")));
36 }
37 catch(IOException e)
38 {
39 throw new RuntimeException("unable to open productions.txt.");
40 }
41
42 pkgDir = new File(ids.pkgDir, "node");
43 pkgName = ids.pkgName.equals("") ? "node" : ids.pkgName + ".node";
44
45 if(!pkgDir.exists())
46 {
47 if(!pkgDir.mkdir())
48 {
49 throw new RuntimeException("Unable to create " + pkgDir.getAbsolutePath());
50 }
51 }
52 }
53
54 public void inProd1(Prod1 node)
55 {
56 String name = (String) ids.names.get(node);
57
58 createProduction(name);
59 }
60
61 private void createProduction(String name)
62 {
63 BufferedWriter file;
64
65 try
66 {
67 file = new BufferedWriter(
68 new FileWriter(
69 new File(pkgDir, name + ".java")));
70 }
71 catch(IOException e)
72 {
73 throw new RuntimeException("Unable to create " + new File(pkgDir, name + ".java").getAbsolutePath());
74 }
75
76 try
77 {
78 macros.apply(file, "Production", new String[] {pkgName, name});
79 }
80 catch(IOException e)
81 {
82 throw new RuntimeException("An error occured while writing to " +
83 new File(pkgDir, name + ".java").getAbsolutePath());
84 }
85
86 try
87 {
88 file.close();
89 }
90 catch(IOException e)
91 {
92 }
93 }
94
95 public void inUnOp1(UnOp1 node)
96 {
97 if(node.getParent().getParent() instanceof Elem1)
98 {
99 Elem1 elem = (Elem1) node.getParent().getParent();
100
101 String name = (String) ids.elemTypes.get(elem);
102
103 if(hiddenProds.put("X" + name, elem) == null)
104 {
105 createProduction("X" + name);
106
107 createAlternative(
108 "X1" + name,
109 "HiddenAlternative2",
110 new String[] {pkgName,
111 ids.pkgName.equals("") ? "analysis" : ids.pkgName + ".analysis",
112 "X1" + name,
113 "X" + name,
114 name,
115 GenAlts.nodeName(name),
116 GenAlts.nodeName("X" + name)});
117
118 createAlternative(
119 "X2" + name,
120 "HiddenAlternative1",
121 new String[] {pkgName,
122 ids.pkgName.equals("") ? "analysis" : ids.pkgName + ".analysis",
123 "X2" + name,
124 "X" + name,
125 name,
126 GenAlts.nodeName(name)});
127 }
128 }
129 }
130
131 public void inUnOp3(UnOp3 node)
132 {
133 if(node.getParent().getParent() instanceof Elem1)
134 {
135 Elem1 elem = (Elem1) node.getParent().getParent();
136
137 String name = (String) ids.elemTypes.get(elem);
138
139 if(hiddenProds.put("X" + name, elem) == null)
140 {
141 createProduction("X" + name);
142
143 createAlternative(
144 "X1" + name,
145 "HiddenAlternative2",
146 new String[] {pkgName,
147 ids.pkgName.equals("") ? "analysis" : ids.pkgName + ".analysis",
148 "X1" + name,
149 "X" + name,
150 name,
151 GenAlts.nodeName(name),
152 GenAlts.nodeName("X" + name)});
153
154 createAlternative(
155 "X2" + name,
156 "HiddenAlternative1",
157 new String[] {pkgName,
158 ids.pkgName.equals("") ? "analysis" : ids.pkgName + ".analysis",
159 "X2" + name,
160 "X" + name,
161 name,
162 GenAlts.nodeName(name)});
163 }
164 }
165 }
166
167 private void createAlternative(String name, String macro, String[] arg)
168 {
169 BufferedWriter file;
170
171 try
172 {
173 file = new BufferedWriter(
174 new FileWriter(
175 new File(pkgDir, name + ".java")));
176 }
177 catch(IOException e)
178 {
179 throw new RuntimeException("Unable to create " + new File(pkgDir, name + ".java").getAbsolutePath());
180 }
181
182 try
183 {
184 macros.apply(file, macro, arg);
185 }
186 catch(IOException e)
187 {
188 throw new RuntimeException("An error occured while writing to " +
189 new File(pkgDir, name + ".java").getAbsolutePath());
190 }
191
192 try
193 {
194 file.close();
195 }
196 catch(IOException e)
197 {
198 }
199 }
200 }
201
202