more
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / specExtraction / ParserUtils.java
index c70aa94..201bb8c 100644 (file)
@@ -1,7 +1,15 @@
 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.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) {
                int i, j;
@@ -21,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++) {
@@ -29,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();
+               }
+       }
 }