import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.Arrays;
import edu.uci.eecs.specCompiler.specExtraction.Construct;
import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
public static void main(String[] argvs)
throws ParseException, TokenMgrError {
try {
- File f = new File("./grammer/spec.txt");
+ File f = new File("./grammer/spec1.txt");
FileInputStream fis = new FileInputStream(f);
SpecParser parser = new SpecParser(fis);
- /*
+
ArrayList<String> content = new ArrayList<String>();
ArrayList<Construct> constructs = new ArrayList<Construct>();
ArrayList<String> headers = new ArrayList<String>();
for (int i = 0; i < constructs.size(); i++) {
System.out.println(constructs.get(i));
}
- */
- parser.Test();
+
+ //parser.Test();
System.out.println("Parsing finished!");
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
+
+ private static ArrayList<String> breakLines(String all) {
+ String lines[] = all.split("[\\r\\n]+");
+ return new ArrayList<String>(Arrays.asList(lines));
+ }
+
+
public static ArrayList<VariableDeclaration> getTemplateArg(String line)
throws ParseException {
InputStream input = new ByteArrayInputStream(line.getBytes());
-<*> SKIP :
+<IN_POTENTIAL_SPEC, IN_SPEC> SKIP :
{
" "
|
"/*": IN_COMMENT
}
+<DEFAULT> TOKEN: {
+ <ANY: ~[]>
+}
+
<*> SKIP : {
// "//" comment for the specification
<"//" (~["\n", "\r"])* (["\n", "\r"])>
}
-<IN_SPEC, DEFAULT> TOKEN :
+<IN_SPEC> TOKEN :
{
/* Specification & C/C++ shared tokens */
// Reserved keywords
}
{
{ type = ""; }
- ("const"
+ (<CONST>
{ type = "const"; }
)?
(((str = <STRUCT>.image | str = <CLASS>.image | str = <UNSIGNED>.image) { type = type + " " + str; })?
type = name.fullName;
})
)
- ((str = "const".image {
+ ((str = <CONST>.image {
if (!type.equals(""))
type = type + " " + str;
else
}
{
(str = <IDENTIFIER>.image {res = str;})
- ("<" str = Type() { res = res + "<" + str; }
- ("," str = Type() { res = res + ", " + str; })* ">"
+ (<OPEN_BRACKET> str = Type() { res = res + "<" + str; }
+ (<COMMA> str = Type() { res = res + ", " + str; })* <CLOSE_BRACKET>
{ res = res + ">"; }
)?
{
params = new ArrayList<VariableDeclaration>();
}
<TEMPLATE>
- "<"
+ <OPEN_BRACKET>
(type = <IDENTIFIER>.image
name = <IDENTIFIER>.image
{
}
)
- ("," type = <IDENTIFIER>.image
+ (<COMMA> type = <IDENTIFIER>.image
name = <IDENTIFIER>.image
{
params.add(new VariableDeclaration(type, name));
}
)*
- ">"
+ <CLOSE_BRACKET>
{
//System.out.println(params);
return params;
{
typeParams = new ArrayList<VariableDeclaration >();
}
- "("
+ <OPEN_PAREN>
((varDecl = TypeParam() {typeParams.add(varDecl);})
((<COMMA> varDecl = TypeParam() {typeParams.add(varDecl);}))*)?
- ")"
+ <CLOSE_PAREN>
{
return typeParams;
}
void Parse(File f, ArrayList<String> content, ArrayList<Construct> constructs, ArrayList<String> headers) :
{
Construct inst;
- ArrayList<String> code;
+ StringBuilder sb;
+ boolean flushSB;
}
{
{
_file = f;
_content = content;
_constructs = constructs;
+ sb = new StringBuilder();
+ }
+ (
+ (inst = ParseSpec()
+ {
+ _constructs.add(inst);
+ }
+ ) |
+ //((code = C_CPP_CODE(headers)) { _content.addAll(code); })
+ (
+ flushSB = OriginalCode(sb)
+ {
+ if (flushSB) {
+ sb = new StringBuilder();
+ }
+ }
+ )
+ )*
+ // For the last piece of code
+ {
+ _content.add(sb.toString());
+ }
+ <EOF>
+}
+
+// If true, there's a new line and sb should be flushed
+boolean OriginalCode(StringBuilder sb) :
+{
+ String str;
+}
+{
+ str = <ANY>.image
+ {
+ if (!str.equals("\n")) {
+ sb.append(str);
+ return false;
+ } else {
+ _content.add(sb.toString());
+ return true;
+ }
}
- ((inst = ParseSpec() { _constructs.add(inst); }) |
- ((code = C_CPP_CODE(headers)) { _content.addAll(code); })
- )* <EOF>
}
Construct ParseSpec() :
<GLOBAL_DEFINE>
(<DECLARE_STRUCT> (declareStruct = C_CPP_CODE(null) {
declareStructs.add(declareStruct); }))*
- (<DECLARE_VAR> ((declareVar = TypeParam() ";" {
+ (<DECLARE_VAR> ((declareVar = TypeParam() <SEMI_COLON> {
declareVars.add(declareVar); } )*))?
(<INIT_VAR> (code = C_CPP_CODE(null) { initVar = code; } ))?
(<DEFINE_FUNC> (defineFunc = C_CPP_CODE(null) { defineFuncs.add(defineFunc); }))*