edits
[cdsspec-compiler.git] / src / edu / uci / eecs / specExtraction / VariableDeclaration.java
1 package edu.uci.eecs.specExtraction;
2
3 import java.io.File;
4 import java.util.regex.Matcher;
5 import java.util.regex.Pattern;
6
7 /**
8  * <p>
9  * This class represents a variable declaration in C/C++, in which there exist a
10  * type and a name.
11  * </p>
12  * 
13  * @author Peizhao Ou
14  * 
15  */
16 public class VariableDeclaration {
17         public final String type;
18         public final String name;
19
20         public VariableDeclaration(String type, String name) {
21                 this.type = SpecUtils.trimSpace(type);
22                 this.name = SpecUtils.trimSpace(name);
23         }
24
25         public VariableDeclaration(File file, int lineNum, String line)
26                         throws WrongAnnotationException {
27                 // "([<>\*\w\s]+)\s?(\w+)\s;"
28                 Pattern regexp = Pattern.compile("([<>&\\*\\w\\s]+)\\s?(\\w+)\\s?;");
29                 Matcher matcher = regexp.matcher(line);
30                 if (matcher.find()) {
31                         type = matcher.group(1);
32                         name = matcher.group(2);
33                 } else {
34                         type = null;
35                         name = null;
36                         WrongAnnotationException.err(file, lineNum, "The declaration: \""
37                                         + line + "\" has wrong syntax.");
38                 }
39         }
40
41         public String toString() {
42                 return type + ": " + name;
43         }
44 }