edits
[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 /**
14  * <p> Utility functions for parsing the specifications </p>
15  * @author peizhaoo
16  *
17  */
18 public class ParserUtils {
19         public static String trimSpace(String line) {
20                 int i, j;
21                 char ch;
22                 for (i = 0; i < line.length(); i++) {
23                         ch = line.charAt(i);
24                         if (ch != ' ' && ch != '\t')
25                                 break;
26                 }
27                 for (j = line.length() - 1; j >= 0; j--) {
28                         ch = line.charAt(j);
29                         if (ch != ' ' && ch != '\t')
30                                 break;
31                 }
32                 if (i > j)
33                         return "";
34                 else
35                         return line.substring(i, j + 1);
36         }
37
38         public static String array2Str(ArrayList code) {
39                 StringBuilder sb = new StringBuilder();
40                 for (int i = 0; i < code.size(); i++) {
41                         sb.append(code.get(i) + "\n");
42                 }
43                 return sb.toString();
44         }
45
46         public static String getClassName(String classDefineLine) {
47                 IDExtractor extractor = new IDExtractor(classDefineLine,
48                                 classDefineLine.length() - 1);
49                 return extractor.getPrevID();
50         }
51
52         public static String getTemplateStr(String templateLine) {
53                 String templateStr = null;
54                 ArrayList<VariableDeclaration> templateArgs;
55                 try {
56                         templateArgs = UtilParser.getTemplateArg(templateLine);
57                         templateStr = "<" + templateArgs.get(0).name;
58                         for (int i = 1; i < templateArgs.size(); i++) {
59                                 templateStr = templateStr + ", " + templateArgs.get(i).name;
60                         }
61                         templateStr = templateStr + ">";
62                 } catch (ParseException e) {
63                         e.printStackTrace();
64                 }
65
66                 return templateStr;
67         }
68
69         public static void write2File(File file, ArrayList<String> content) {
70                 String newFileName = Environment.GENERATED_FILE_DIR + "/"
71                                 + file.getParentFile().getName() + "/" + file.getName();
72
73                 File newFile = new File(newFileName);
74                 newFile.getParentFile().mkdirs();
75                 if (!newFile.exists()) {
76                         try {
77                                 newFile.createNewFile();
78                         } catch (IOException e) {
79                                 e.printStackTrace();
80                         }
81                 }
82                 try {
83                         BufferedWriter bw = new BufferedWriter(new FileWriter(newFile));
84                         for (int i = 0; i < content.size(); i++) {
85                                 bw.write(content.get(i) + "\n");
86                         }
87                         bw.flush();
88                 } catch (IOException e) {
89                         e.printStackTrace();
90                 }
91         }
92 }