changes
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / specExtraction / SpecExtractor.java
1 package edu.uci.eecs.specCompiler.specExtraction;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileNotFoundException;
6 import java.io.FileReader;
7 import java.io.IOException;
8 import java.io.LineNumberReader;
9 import java.util.ArrayList;
10 import java.util.HashMap;
11 import java.util.HashSet;
12
13 import edu.uci.eecs.specCompiler.grammerParser.ParseException;
14 import edu.uci.eecs.specCompiler.grammerParser.SpecParser;
15 import edu.uci.eecs.specCompiler.grammerParser.TokenMgrError;
16
17 /**
18  * <p>
19  * This class represents the specification extractor of the specification. The
20  * main function of this class is to read C/C++11 source files and extract the
21  * corresponding specifications, and record corresponding information such as
22  * location, e.g., the file name and the line number, to help the code
23  * generation process.
24  * </p>
25  * 
26  * @author peizhaoo
27  * 
28  */
29 public class SpecExtractor {
30         public final HashMap<File, SourceFileInfo> srcFilesInfo;
31
32         public SpecExtractor() {
33                 srcFilesInfo = new HashMap<File, SourceFileInfo>();
34         }
35
36         public ArrayList<Construct> getConstructs() {
37                 ArrayList<Construct> constructs = new ArrayList<Construct>();
38                 for (File f : srcFilesInfo.keySet()) {
39                         constructs.addAll(srcFilesInfo.get(f).constructs);
40                 }
41                 return constructs;
42         }
43
44         /**
45          * <p>
46          * Given a list of files, it scans each file and add found SpecConstrcut to
47          * the _constructs list.
48          * </p>
49          * 
50          * @param files
51          * @throws SpecNotMatchException
52          */
53         public void extract(File[] files) {
54                 for (int i = 0; i < files.length; i++)
55                         extract(files[i]);
56         }
57
58         public void extract(File file) {
59                 if (srcFilesInfo.containsKey(file))
60                         return;
61                 SourceFileInfo srcFileInfo;
62                 try {
63                         srcFileInfo = SpecParser.ParseFile(file);
64                         for (int i = 0; i < srcFileInfo.content.size(); i++) {
65                                 System.out.println(srcFileInfo.content.get(i));
66                         }
67                         ParserUtils.write2File(srcFileInfo.file, srcFileInfo.content);
68                         srcFilesInfo.put(file, srcFileInfo);
69                 } catch (ParseException e) {
70                         e.printStackTrace();
71                 } catch (TokenMgrError e) {
72                         e.printStackTrace();
73                 }
74         }
75
76 }