import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
import edu.uci.eecs.specCompiler.specExtraction.EntryPointConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.ClassBeginConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.ClassEndConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.FunctionHeader;
public class SpecParser {
private static ArrayList<String> _content;
private static File _file;
private static ArrayList<Construct> _constructs;
+
public static void main(String[] argvs)
throws ParseException, TokenMgrError {
try {
+ String line = "int* A::B<sfd, _sdf>::id(const char * ch_ptr, int a)";
+ System.out.println(parseFuncHeader(line));
+
File f = new File("./grammer/spec.txt");
FileInputStream fis = new FileInputStream(f);
SpecParser parser = new SpecParser(fis);
+ /**
ArrayList<String> content = new ArrayList<String>();
ArrayList<Construct> constructs = new ArrayList<Construct>();
parser.Parse(f, content, constructs);
+ for (int i = 0; i < content.size(); i++) {
+ System.out.println(content.get(i));
+ }
+ */
+ parser.Test();
System.out.println("Parsing finished!");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
+ public static ArrayList<String> getTemplateArg(String line)
+ throws ParseException {
+ InputStream input = new ByteArrayInputStream(line.getBytes());
+ SpecParser parser = new SpecParser(input);
+ return parser.TemplateParamList();
+ }
+
+ public static FunctionHeader parseFuncHeader(String line)
+ throws ParseException {
+ InputStream input = new ByteArrayInputStream(line.getBytes());
+ SpecParser parser = new SpecParser(input);
+ return parser.FuncDecl();
+ }
+
+
public static String stringArray2String(ArrayList<String> content) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < content.size(); i++) {
<COMMIT_POINT_SET: "@Commit_point_set:">
|
<ENTRY_POINT: "@Entry_point">
+|
+ <CLASS_BEGIN: "@Class_begin">
+|
+ <CLASS_END: "@Class_end">
|
<INTERFACE_DEFINE: "@Interface_define:">
|
|
<STRUCT: "struct">
|
- <TYPENAME: "typename">
+ <CLASS: "class">
+|
+ <TEMPLATE: "template">
|
<#DIGIT: ["0"-"9"]>
|
("const"
{ type = "const"; }
)?
- (("struct" { type = type + " struct"; })?
- (str = <IDENTIFIER>.image {
+ (((str = <STRUCT>.image | str = <CLASS>.image) { type = type + " " + str; })?
+ (
+ str = QualifiedName() {
+ if (!type.equals(""))
+ type = type + " " + str;
+ else
+ type = str;
+ })
+ (
+ str = ParameterizedName() {
if (!type.equals(""))
type = type + " " + str;
else
type = str;
- }))
+ })
+ )
((str = "const".image {
if (!type.equals(""))
type = type + " " + str;
}
}
+void Test() :
+{}
+{
+ Type()
+ //FuncDecl()
+}
+
+String ParameterizedName() :
+{
+ String res = "";
+ String str;
+}
+{
+ (str = <IDENTIFIER>.image {res = str;})
+ ("<" str = <IDENTIFIER>.image { res = res + "<" + str; }
+ ("," str = <IDENTIFIER>.image { res = res + ", " + str; })* ">"
+ { res = res + ">"; }
+ )?
+ {
+ return res;
+ }
+}
+
+FunctionHeader FuncDecl() :
+{
+ String ret, qualifiedName, bareName;
+ ArrayList<String> args;
+}
+{
+ ret = Type()
+ qualifiedName = QualifiedName()
+ bareName = <IDENTIFIER>.image
+ args = FormalParamList()
+ {
+ FunctionHeader res = new FunctionHeader(ret, qualifiedName, bareName, args);
+ //System.out.println(res);
+ return res;
+ }
+}
+
+String QualifiedName() :
+{
+ String qualifiedName, str;
+}
+{
+ { qualifiedName = ""; }
+ (LOOKAHEAD(2) (str = ParameterizedName() { qualifiedName = qualifiedName +
+ str + "::"; } <DOUBLECOLON> ))*
+ {
+ return qualifiedName;
+ }
+}
+
+ArrayList<String> TemplateParamList() :
+{
+ ArrayList<String> params;
+ String str;
+}
+{
+ {
+ params = new ArrayList<String>();
+ }
+ <TEMPLATE>
+ "<"
+ (str = <IDENTIFIER>.image
+ str = <IDENTIFIER>.image {params.add(str);})
+
+ ("," str = <IDENTIFIER>.image
+ str = <IDENTIFIER>.image {params.add(str);})*
+ ">"
+ {
+ //System.out.println(params);
+ return params;
+ }
+}
+
ArrayList<String> FormalParamList() :
{
ArrayList<String> typeParams;
{
typeParams = new ArrayList<String>();
}
+ "("
(TypeParam(typeParams) (<COMMA> TypeParam(typeParams))*)?
+ ")"
{
- System.out.println(typeParams);
return typeParams;
}
}
String text;
Token t;
boolean newLine = false;
+ boolean inTemplate = false;
ArrayList<String> content;
}
{
(
LOOKAHEAD(2)
(
- t = <CONST> | t = <STRUCT> | t = <TYPENAME> |
+ t = <CONST> | t = <STRUCT> | t = <CLASS> |
+ (t = <TEMPLATE> { inTemplate = true; })|
t = <IDENTIFIER> | t = <POUND> |
(t = <OPEN_BRACE> { newLine = true; } ) |
(t = <CLOSE_BRACE> { newLine = true; } ) |
| t = <HB_SYMBOL> | t = <COMMA> |
t = <DOT> | t = <STAR> | t = <NEGATE> | t = <EXCLAMATION> | t = <AND> | t = <OR> | t = <MOD> | t = <PLUS> |
t = <PLUSPLUS> | t = <MINUS> | t = <MINUSMINUS> | t = <DIVIDE> | t = <BACKSLASH> |
- t = <LESS_THAN> | t = <GREATER_THAN> | t = <GREATER_EQUALS> | t = <LESS_EQUALS> |
+ t = <LESS_THAN> |
+ (t = <GREATER_THAN> { if (inTemplate) newLine = true; }) |
+ t = <GREATER_EQUALS> | t = <LESS_EQUALS> |
t = <LOGICAL_EQUALS> | t = <NOT_EQUALS> | t = <LOGICAL_AND> | t = <LOGICAL_OR> | t = <XOR> |
t = <QUESTION_MARK> | t = <COLON> | t = <DOUBLECOLON> |
(t = <SEMI_COLON> { newLine = true; } )
LOOKAHEAD(2) res = Commit_point_define() |
LOOKAHEAD(2) res = Commit_point_define_check() |
LOOKAHEAD(2) res = Entry_point() |
+ LOOKAHEAD(2) res = Class_begin() |
+ LOOKAHEAD(2) res = Class_end() |
LOOKAHEAD(2) res = Interface_define()
)
{
}
}
+ClassBeginConstruct Class_begin() :
+{}
+{
+
+ <BEGIN>
+ <CLASS_BEGIN>
+ <END>
+ {
+ return new ClassBeginConstruct(_file, _content.size());
+ }
+}
+
+ClassEndConstruct Class_end() :
+{}
+{
+
+ <BEGIN>
+ <CLASS_END>
+ <END>
+ {
+ return new ClassEndConstruct(_file, _content.size());
+ }
+}
+
InterfaceDefineConstruct Interface_define() :
{
String name;