~ubuntu-branches/ubuntu/vivid/sablecc/vivid

« back to all changes in this revision

Viewing changes to src/org/sablecc/sablecc/GenProds.java

  • Committer: Bazaar Package Importer
  • Author(s): Etienne M. Gagnon
  • Date: 2004-01-25 13:28:01 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040125132801-0so74ui506b30b1q
Tags: 2.18.2-1
* New upstream release.
* Keep the org.sablecc.ant.taskdef.Sablecc ant task of the upstream
  package, closes: #213020.
* Improve man page.
* Update Standards-Version to 3.6.1.

Show diffs side-by-side

added added

removed removed

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