more
[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 specification out, and remember its location, including the
22  * file name and the line number, to help the code generation process.
23  * </p>
24  * 
25  * @author peizhaoo
26  * 
27  */
28 public class SpecExtractor {
29         public final HashMap<File, SourceFileInfo> srcFilesInfo;
30
31         public SpecExtractor() {
32                 srcFilesInfo = new HashMap<File, SourceFileInfo>();
33         }
34         
35         public ArrayList<Construct> getConstructs() {
36                 ArrayList<Construct> constructs = new ArrayList<Construct>();
37                 for (File f : srcFilesInfo.keySet()) {
38                         constructs.addAll(srcFilesInfo.get(f).constructs);
39                 }
40                 return constructs;
41         }
42         
43         /**
44          * <p>
45          * Given a list of files, it scans each file and add found SpecConstrcut to
46          * the _constructs list.
47          * </p>
48          * 
49          * @param files
50          * @throws SpecNotMatchException
51          */
52         public void extract(File[] files) {
53                 for (int i = 0; i < files.length; i++)
54                         extract(files[i]);
55         }
56
57         public void extract(File file) {
58                 if (srcFilesInfo.containsKey(file))
59                         return;
60                 SourceFileInfo srcFileInfo;
61                 try {
62                         srcFileInfo = SpecParser.ParseFile(file);
63                         for (int i = 0; i < srcFileInfo.content.size(); i++) {
64                                 System.out.println(srcFileInfo.content.get(i));
65                         }
66                         ParserUtils.write2File(srcFileInfo.file, srcFileInfo.content);
67                         srcFilesInfo.put(file, srcFileInfo);
68                 } catch (ParseException e) {
69                         e.printStackTrace();
70                 } catch (TokenMgrError e) {
71                         e.printStackTrace();
72                 }
73         }
74
75         
76 }