Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/sablecc/sablecc/GenTokens.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.io.*;
13  
14  public class GenTokens extends DepthFirstAdapter
15  {
16      private MacroExpander macros;
17      private ResolveIds ids;
18      private File pkgDir;
19      private String pkgName;
20  
21  //    final GenTokens instance = this;
22      String text;
23  
24      public GenTokens(ResolveIds ids)
25      {
26          this.ids = ids;
27  
28          try
29          {
30              macros = new MacroExpander(
31                  new InputStreamReader(
32                  getClass().getResourceAsStream("tokens.txt")));
33          }
34          catch(IOException e)
35          {
36              throw new RuntimeException("unable to open tokens.txt.");
37          }
38  
39          pkgDir = new File(ids.pkgDir, "node");
40          pkgName = ids.pkgName.equals("") ? "node" : ids.pkgName + ".node";
41  
42          if(!pkgDir.exists())
43          {
44              if(!pkgDir.mkdir())
45              {
46                  throw new RuntimeException("Unable to create " + pkgDir.getAbsolutePath());
47              }
48          }
49      }
50  
51      public void inTokenDef1(TokenDef1 node)
52      {
53          String name = (String) ids.names.get(node);
54  
55          BufferedWriter file;
56  
57          try
58          {
59              file = new BufferedWriter(
60                  new FileWriter(
61                  new File(pkgDir, name + ".java")));
62          }
63          catch(IOException e)
64          {
65              throw new RuntimeException("Unable to create " + new File(pkgDir, name + ".java").getAbsolutePath());
66          }
67  
68          text = null;
69  
70          RegExp1 regExp = (RegExp1) node.getNode4();
71  
72          Concat1 concat = (Concat1) regExp.getNode1();
73          RegExpTails regExpTails = regExp.getNode2();
74  
75          if(regExpTails instanceof RegExpTails2)
76          {
77              UnExps unExps = concat.getNode1();
78  
79              if(unExps instanceof UnExps1)
80              {
81                  UnExps unExps_2 = ((UnExps1) unExps).getNode2();
82  
83                  if(unExps_2 instanceof UnExps2)
84                  {
85                      UnExp1 unExp = (UnExp1) ((UnExps1) unExps).getNode1();
86  
87                      Basic basic = unExp.getNode1();
88                      UnOpOpt unOpOpt = unExp.getNode2();
89  
90                      if((basic instanceof Basic3) &&
91                          (unOpOpt instanceof UnOpOpt2))
92                      {
93                          text = ((Basic3) basic).getNode1().getText();
94                          text = text.substring(1, text.length() - 1);
95                      }
96                      else if((basic instanceof Basic1) &&
97                          (unOpOpt instanceof UnOpOpt2))
98                      {
99                          PChar pChar = ((Basic1) basic).getNode1();
100 
101                         if(pChar instanceof PChar1)
102                         {
103                             text = ((PChar1) pChar).getNode1().getText();
104                             text = text.substring(1, text.length() - 1);
105                         }
106                     }
107                 }
108             }
109         }
110 
111         try
112         {
113             if(text == null)
114             {
115                 ids.fixedTokens.put(node, new Boolean(false));
116 
117                 macros.apply(file, "VariableTextToken", new String[] { pkgName,
118                     ids.pkgName.equals("") ? "analysis" : ids.pkgName + ".analysis",
119                     name});
120             }
121             else
122             {
123                 ids.fixedTokens.put(node, new Boolean(true));
124 
125                 macros.apply(file, "FixedTextToken", new String[] { pkgName,
126                     ids.pkgName.equals("") ? "analysis" : ids.pkgName + ".analysis",
127                     name, processText(text)});
128 
129                 ids.errorNames.put(node, "'" + text + "'");
130             }
131         }
132         catch(IOException e)
133         {
134             throw new RuntimeException("An error occured while writing to " +
135                 new File(pkgDir, name + ".java").getAbsolutePath());
136         }
137 
138         try
139         {
140             file.close();
141         }
142         catch(IOException e)
143         {
144         }
145     }
146 
147     private String processText(String s)
148     {
149         StringBuffer result = new StringBuffer();
150 
151         for(int i = 0; i < s.length(); i++)
152         {
153             char c = s.charAt(i);
154 
155             switch(c)
156             {
157             case '\b':
158                 {
159                     result.append("\\t");
160                     break;
161                 }
162             case '\t':
163                 {
164                     result.append("\\t");
165                     break;
166                 }
167             case '\n':
168                 {
169                     result.append("\\n");
170                     break;
171                 }
172             case '\f':
173                 {
174                     result.append("\\f");
175                     break;
176                 }
177             case '\r':
178                 {
179                     result.append("\\r");
180                     break;
181                 }
182             case '\"':
183                 {
184                     result.append("\\\"");
185                     break;
186                 }
187             case '\'':
188                 {
189                     result.append("\\\'");
190                     break;
191                 }
192             case '\\':
193                 {
194                     result.append("\\\\");
195                     break;
196                 }
197             default:
198                 {
199                     result.append(c);
200                 }
201             }
202         }
203 
204         return result.toString();
205     }
206 }
207 
208