small change
[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
12 import edu.uci.eecs.specCompiler.grammerParser.ParseException;
13 import edu.uci.eecs.specCompiler.grammerParser.SpecParser;
14 import edu.uci.eecs.specCompiler.grammerParser.TokenMgrError;
15
16 /**
17  * <p>
18  * This class represents the specification extractor of the specification. The
19  * main function of this class is to read C/C++11 source files and extract the
20  * corresponding specification out, and remember its location, including the
21  * file name and the line number, to help the code generation process.
22  * </p>
23  * 
24  * @author peizhaoo
25  * 
26  */
27 public class SpecExtractor {
28         public final ArrayList<Construct> constructs;
29         
30         public final HashMap<File, ArrayList<String>> contents;
31         
32
33         public SpecExtractor() {
34                 constructs = new ArrayList<Construct>();
35                 contents = new HashMap<File, ArrayList<String>>();
36         }
37         
38         /**
39          * <p>
40          * Given a list of files, it scans each file and add found SpecConstrcut to
41          * the _constructs list.
42          * </p>
43          * 
44          * @param files
45          * @throws SpecNotMatchException
46          */
47         public void extract(File[] files) {
48                 for (int i = 0; i < files.length; i++)
49                         extract(files[i]);
50         }
51
52         public void extract(File file) {
53                 if (contents.containsKey(file))
54                         return;
55                 ArrayList<String> content = new ArrayList<String>();
56                 ArrayList<Construct> localConstructs = new ArrayList<Construct>();
57                 try {
58                         SpecParser.ParseFile(file, content, localConstructs);
59                         contents.put(file, content);
60                         constructs.addAll(localConstructs);
61                 } catch (ParseException e) {
62                         e.printStackTrace();
63                 } catch (TokenMgrError e) {
64                         e.printStackTrace();
65                 }
66         }
67
68         
69 }