add lots of stuff
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / specExtraction / ParserUtils.java
1 package edu.uci.eecs.specCompiler.specExtraction;
2
3 import java.util.ArrayList;
4
5 import edu.uci.eecs.specCompiler.codeGenerator.SemanticsChecker;
6 import edu.uci.eecs.specCompiler.grammerParser.ParseException;
7 import edu.uci.eecs.specCompiler.grammerParser.SpecParser;
8
9 public class ParserUtils {
10         public static String trimSpace(String line) {
11                 int i, j;
12                 char ch;
13                 for (i = 0; i < line.length(); i++) {
14                         ch = line.charAt(i);
15                         if (ch != ' ' && ch != '\t')
16                                 break;
17                 }
18                 for (j = line.length() - 1; j >= 0; j--) {
19                         ch = line.charAt(j);
20                         if (ch != ' ' && ch != '\t')
21                                 break;
22                 }
23                 if (i > j)
24                         return "";
25                 else
26                         return line.substring(i, j + 1);
27         }
28
29         public static String array2Str(ArrayList code) {
30                 StringBuilder sb = new StringBuilder();
31                 for (int i = 0; i < code.size(); i++) {
32                         sb.append(code.get(i) + "\n");
33                 }
34                 return sb.toString();
35         }
36
37         public static String getClassName(String classDefineLine) {
38                 IDExtractor extractor = new IDExtractor(classDefineLine,
39                                 classDefineLine.length() - 1);
40                 return extractor.getPrevID();
41         }
42
43         public static String getTemplateStr(String templateLine) {
44                 String templateStr = null;
45                 try {
46                         ArrayList<String> args = SpecParser.getTemplateArg(templateLine);
47                         templateStr = "<" + args.get(1);
48                         for (int i = 1; i < args.size() / 2; i++) {
49                                 templateStr = templateStr + ", " + args.get(i * 2 + 1);
50                         }
51                         templateStr = templateStr + ">";
52                 } catch (ParseException e) {
53                         e.printStackTrace();
54                 }
55                 return templateStr;
56         }
57 }