1 package edu.uci.eecs.specCompiler.specExtraction;
3 import java.io.BufferedWriter;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.util.ArrayList;
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;
14 * <p> Utility functions for parsing the specifications </p>
18 public class ParserUtils {
19 public static String trimSpace(String line) {
22 for (i = 0; i < line.length(); i++) {
24 if (ch != ' ' && ch != '\t')
27 for (j = line.length() - 1; j >= 0; j--) {
29 if (ch != ' ' && ch != '\t')
35 return line.substring(i, j + 1);
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");
46 public static String getClassName(String classDefineLine) {
47 IDExtractor extractor = new IDExtractor(classDefineLine,
48 classDefineLine.length() - 1);
49 return extractor.getPrevID();
52 public static String getTemplateStr(String templateLine) {
53 String templateStr = null;
54 ArrayList<VariableDeclaration> templateArgs;
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;
61 templateStr = templateStr + ">";
62 } catch (ParseException e) {
69 public static void write2File(File file, ArrayList<String> content) {
70 String newFileName = Environment.GENERATED_FILE_DIR + "/"
71 + file.getParentFile().getName() + "/" + file.getName();
73 File newFile = new File(newFileName);
74 newFile.getParentFile().mkdirs();
75 if (!newFile.exists()) {
77 newFile.createNewFile();
78 } catch (IOException e) {
83 BufferedWriter bw = new BufferedWriter(new FileWriter(newFile));
84 for (int i = 0; i < content.size(); i++) {
85 bw.write(content.get(i) + "\n");
88 } catch (IOException e) {