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;
13 public class ParserUtils {
14 public static String trimSpace(String line) {
17 for (i = 0; i < line.length(); i++) {
19 if (ch != ' ' && ch != '\t')
22 for (j = line.length() - 1; j >= 0; j--) {
24 if (ch != ' ' && ch != '\t')
30 return line.substring(i, j + 1);
33 public static String array2Str(ArrayList code) {
34 StringBuilder sb = new StringBuilder();
35 for (int i = 0; i < code.size(); i++) {
36 sb.append(code.get(i) + "\n");
41 public static String getClassName(String classDefineLine) {
42 IDExtractor extractor = new IDExtractor(classDefineLine,
43 classDefineLine.length() - 1);
44 return extractor.getPrevID();
47 public static String getTemplateStr(String templateLine) {
48 String templateStr = null;
49 ArrayList<VariableDeclaration> templateArgs;
51 templateArgs = UtilParser.getTemplateArg(templateLine);
52 templateStr = "<" + templateArgs.get(0).name;
53 for (int i = 1; i < templateArgs.size(); i++) {
54 templateStr = templateStr + ", " + templateArgs.get(i).name;
56 templateStr = templateStr + ">";
57 } catch (ParseException e) {
64 public static void write2File(File file, ArrayList<String> content) {
65 String newFileName = Environment.GENERATED_FILE_DIR + "/"
66 + file.getParentFile().getName() + "/" + file.getName();
68 File newFile = new File(newFileName);
69 newFile.getParentFile().mkdirs();
70 if (!newFile.exists()) {
72 newFile.createNewFile();
73 } catch (IOException e) {
78 BufferedWriter bw = new BufferedWriter(new FileWriter(newFile));
79 for (int i = 0; i < content.size(); i++) {
80 bw.write(content.get(i) + "\n");
83 } catch (IOException e) {