more
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / specExtraction / ParserUtils.java
index 0ce533288dd6af681b8f55d8b1a3af1aab24b827..201bb8c5829bbc43d553cb414c886583d43a15b4 100644 (file)
@@ -1,9 +1,14 @@
 package edu.uci.eecs.specCompiler.specExtraction;
 
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
 import java.util.ArrayList;
 
-import edu.uci.eecs.specCompiler.grammerParser.ParseException;
-import edu.uci.eecs.specCompiler.grammerParser.SpecParser;
+import edu.uci.eecs.specCompiler.codeGenerator.Environment;
+import edu.uci.eecs.specCompiler.grammerParser.utilParser.ParseException;
+import edu.uci.eecs.specCompiler.grammerParser.utilParser.UtilParser;
 
 public class ParserUtils {
        public static String trimSpace(String line) {
@@ -24,7 +29,7 @@ public class ParserUtils {
                else
                        return line.substring(i, j + 1);
        }
-       
+
        public static String array2Str(ArrayList code) {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < code.size(); i++) {
@@ -32,4 +37,51 @@ public class ParserUtils {
                }
                return sb.toString();
        }
+
+       public static String getClassName(String classDefineLine) {
+               IDExtractor extractor = new IDExtractor(classDefineLine,
+                               classDefineLine.length() - 1);
+               return extractor.getPrevID();
+       }
+
+       public static String getTemplateStr(String templateLine) {
+               String templateStr = null;
+               ArrayList<VariableDeclaration> templateArgs;
+               try {
+                       templateArgs = UtilParser.getTemplateArg(templateLine);
+                       templateStr = "<" + templateArgs.get(0).name;
+                       for (int i = 1; i < templateArgs.size(); i++) {
+                               templateStr = templateStr + ", " + templateArgs.get(i).name;
+                       }
+                       templateStr = templateStr + ">";
+               } catch (ParseException e) {
+                       e.printStackTrace();
+               }
+
+               return templateStr;
+       }
+
+       public static void write2File(File file, ArrayList<String> content) {
+               String newFileName = Environment.GENERATED_FILE_DIR + "/"
+                               + file.getParentFile().getName() + "/" + file.getName();
+
+               File newFile = new File(newFileName);
+               newFile.getParentFile().mkdirs();
+               if (!newFile.exists()) {
+                       try {
+                               newFile.createNewFile();
+                       } catch (IOException e) {
+                               e.printStackTrace();
+                       }
+               }
+               try {
+                       BufferedWriter bw = new BufferedWriter(new FileWriter(newFile));
+                       for (int i = 0; i < content.size(); i++) {
+                               bw.write(content.get(i) + "\n");
+                       }
+                       bw.flush();
+               } catch (IOException e) {
+                       e.printStackTrace();
+               }
+       }
 }