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