c80ce51dcfe844b3016b3e5aade8080089b1cc4f
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / specExtraction / ParserUtils.java
1 package edu.uci.eecs.specCompiler.specExtraction;
2
3 import java.io.BufferedWriter;
4 import java.io.File;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.util.ArrayList;
8
9 import edu.uci.eecs.specCompiler.codeGenerator.Environment;
10 import edu.uci.eecs.specCompiler.grammerParser.utilParser.ParseException;
11 import edu.uci.eecs.specCompiler.grammerParser.utilParser.UtilParser;
12
13 public class ParserUtils {
14         public static String trimSpace(String line) {
15                 int i, j;
16                 char ch;
17                 for (i = 0; i < line.length(); i++) {
18                         ch = line.charAt(i);
19                         if (ch != ' ' && ch != '\t')
20                                 break;
21                 }
22                 for (j = line.length() - 1; j >= 0; j--) {
23                         ch = line.charAt(j);
24                         if (ch != ' ' && ch != '\t')
25                                 break;
26                 }
27                 if (i > j)
28                         return "";
29                 else
30                         return line.substring(i, j + 1);
31         }
32
33         public static String array2Str(ArrayList code) {
34                 StringBuilder sb = new StringBuilder();
35                 for (int i = 0; i < code.size(); i++) {
36                         sb.append(code.get(i) + "\n");
37                 }
38                 return sb.toString();
39         }
40
41         public static String getClassName(String classDefineLine) {
42                 IDExtractor extractor = new IDExtractor(classDefineLine,
43                                 classDefineLine.length() - 1);
44                 return extractor.getPrevID();
45         }
46
47         public static String getTemplateStr(String templateLine) {
48                 String templateStr = null;
49                 ArrayList<VariableDeclaration> templateArgs;
50                 try {
51                         templateArgs = UtilParser.getTemplateArg(templateLine);
52                         templateStr = "<" + templateArgs.get(0).name;
53                         for (int i = 1; i < templateArgs.size(); i++) {
54                                 templateStr = templateStr + ", " + templateArgs.get(i).name;
55                         }
56                         templateStr = templateStr + ">";
57                 } catch (ParseException e) {
58                         e.printStackTrace();
59                 }
60                 
61                 return templateStr;
62         }
63         
64         public static void write2File(File file, ArrayList<String> content) {
65                 String newFileName = Environment.GENERATED_FILE_DIR + "/" + file.getName();
66                 File newFile = new File(newFileName);
67                 newFile.getParentFile().mkdirs();
68                 if (!newFile.exists()) {
69                         try {
70                                 newFile.createNewFile();
71                         } catch (IOException e) {
72                                 e.printStackTrace();
73                         }
74                 }
75                 try {
76                         BufferedWriter bw = new BufferedWriter(new FileWriter(newFile));
77                         for (int i = 0; i < content.size(); i++) {
78                                 bw.write(content.get(i) + "\n");
79                         }
80                         bw.flush();
81                 } catch (IOException e) {
82                         e.printStackTrace();
83                 }
84         }
85 }