From da39e68253fdb76719573f28eecc23db2ee08dfe Mon Sep 17 00:00:00 2001
From: Peizhao Ou <peizhaoo@uci.edu>
Date: Fri, 11 Oct 2013 16:18:03 -0700
Subject: [PATCH] parsing passed

---
 grammer/spec-compiler.jj                      | 112 ++++++++++++++----
 .../CPDefineCheckConstruct.java               |  16 ++-
 .../specExtraction/CPDefineConstruct.java     |  19 ++-
 .../specExtraction/GlobalConstruct.java       |   6 +-
 .../specExtraction/InterfaceConstruct.java    |  52 ++++++++
 .../PotentialCPDefineConstruct.java           |  16 ++-
 6 files changed, 189 insertions(+), 32 deletions(-)

diff --git a/grammer/spec-compiler.jj b/grammer/spec-compiler.jj
index a006e7b..ca38b5f 100644
--- a/grammer/spec-compiler.jj
+++ b/grammer/spec-compiler.jj
@@ -69,6 +69,9 @@ package edu.uci.eecs.specCompiler.grammerParser;
 
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
 
 import edu.uci.eecs.specCompiler.specExtraction.Construct;
 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
@@ -301,7 +304,10 @@ Construct Start() :
 	LOOKAHEAD(3) res = Commit_point_define_check()
 	)
 	<EOF>
-	{ return res; }
+	{
+		System.out.println(res);
+		return res;
+	}
 }
 
 GlobalConstruct Global_construct() :
@@ -321,7 +327,6 @@ GlobalConstruct Global_construct() :
 	<TAIL>
 	{
 		res.unfoldInterfaceCluster();
-		System.out.println(res);
 		return res;
 	}
 }
@@ -425,73 +430,128 @@ void Happens_before(GlobalConstruct inst) :
 
 InterfaceConstruct Interface() :
 {
-	InterfaceConstruct res;	
+	InterfaceConstruct res;
+	String interfaceName, condition, idCode, check, action, postAction,
+		postCheck, commitPoint, hbLabel, hbCondition;
+	ArrayList<String> commitPointSet;
+	HashMap<String, String> hbConditions;
 }
 {
-	{ res = null; }
+	{
+		res = null;
+		condition = "";
+		idCode = "";
+		check = "";
+		action = "";
+		postAction = "";
+		postCheck = "";
+		commitPointSet = new ArrayList<String>();
+		hbConditions = new HashMap<String, String>();
+	}
 	<HEAD> 
 		<BEGIN>
-			<INTERFACE> <IDENTIFIER>
-			<COMMIT_POINT_SET> <IDENTIFIER> (<OR> <IDENTIFIER>)*
-			(<CONDITION> C_CPP_CODE())?
-			(<HB_CONDITION> <IDENTIFIER> C_CPP_CODE())*
-			(<ID> C_CPP_CODE())?
-			(<CHECK> C_CPP_CODE())?
-			(<ACTION> C_CPP_CODE())?
-			(<POST_ACTION> C_CPP_CODE())?
-			(<POST_CHECK> C_CPP_CODE())?
+			<INTERFACE> (interfaceName = <IDENTIFIER>.image)
+			<COMMIT_POINT_SET>
+				(commitPoint = <IDENTIFIER>.image
+				{ commitPointSet.add(commitPoint); }
+				)
+				(<OR>
+					(commitPoint = <IDENTIFIER>.image)
+					{
+						if (commitPointSet.contains(commitPoint)) {
+							throw new ParseException(interfaceName + " has" +
+								"duplicate commit point labels");
+						}
+						commitPointSet.add(commitPoint);
+					}
+				)*
+
+			(<CONDITION> (condition = C_CPP_CODE()))?
+			(
+				<HB_CONDITION>
+				(hbLabel = <IDENTIFIER>.image)
+				(hbCondition = C_CPP_CODE())
+				{
+					if (hbConditions.containsKey(hbLabel)) {
+						throw new ParseException(interfaceName + " has" +
+							"duplicate happens-before condtion labels");
+					}
+					hbConditions.put(hbLabel, hbCondition);
+				}
+			)*
+			(<ID> (idCode = C_CPP_CODE()))?
+			(<CHECK> (check = C_CPP_CODE()))?
+			(<ACTION> (action = C_CPP_CODE()))?
+			(<POST_ACTION> (postAction = C_CPP_CODE()))?
+			(<POST_CHECK> (postCheck = C_CPP_CODE()))?
 		<END>
 	<TAIL>
-	{ return res; }
+	{
+		res = new InterfaceConstruct(interfaceName, commitPointSet, condition,
+			hbConditions, idCode, check, action, postAction, postCheck);
+		return res;
+	}
 }
 
 PotentialCPDefineConstruct Potential_commit_point_define() :
 {
-	PotentialCPDefineConstruct res;	
+	PotentialCPDefineConstruct res;
+	String label, condition;
 }
 {
 
 	{ res = null; }
 	<HEAD> 
 		<BEGIN>
-			<POTENTIAL_COMMIT_POINT_DEFINE> C_CPP_CODE()
-			<LABEL> <IDENTIFIER>
+			<POTENTIAL_COMMIT_POINT_DEFINE> (condition = C_CPP_CODE())
+			<LABEL> (label = <IDENTIFIER>.image)
 		<END>
 	<TAIL>
-	{ return res; }
+	{
+		res = new PotentialCPDefineConstruct(label, condition); 
+		return res;
+	}
 }
 
 
 CPDefineConstruct Commit_point_define() :
 {
-	CPDefineConstruct res;	
+	CPDefineConstruct res;
+	String label, potentialCPLabel, condition;
 }
 {
 
 	{ res = null; }
 	<HEAD> 
 		<BEGIN>
-			<COMMIT_POINT_DEFINE> C_CPP_CODE()
-			<POTENTIAL_COMMIT_POINT_LABEL> <IDENTIFIER>
-			<LABEL> <IDENTIFIER>
+			<COMMIT_POINT_DEFINE> (condition = C_CPP_CODE())
+			<POTENTIAL_COMMIT_POINT_LABEL> (potentialCPLabel = <IDENTIFIER>.image)
+			<LABEL> (label = <IDENTIFIER>.image)
 		<END>
 	<TAIL>
-	{ return res; }
+	{
+		res = new CPDefineConstruct(label, potentialCPLabel, condition);
+		return res;
+	}
 }
 
 
 CPDefineCheckConstruct Commit_point_define_check() :
 {
 	CPDefineCheckConstruct res;	
+	String label, condition;
 }
 {
 
 	{ res = null; }
 	<HEAD> 
 		<BEGIN> 
-			<COMMIT_POINT_DEFINE_CHECK> C_CPP_CODE()
-			<LABEL> <IDENTIFIER>
+			<COMMIT_POINT_DEFINE_CHECK> (condition = C_CPP_CODE())
+			<LABEL> (label = <IDENTIFIER>.image)
 		<END>
 	<TAIL>
-	{ return res; }
+	{
+		res = new CPDefineCheckConstruct(label, condition);
+		return res;
+	}
 }
diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/CPDefineCheckConstruct.java b/src/edu/uci/eecs/specCompiler/specExtraction/CPDefineCheckConstruct.java
index 9edd764..ed504e1 100644
--- a/src/edu/uci/eecs/specCompiler/specExtraction/CPDefineCheckConstruct.java
+++ b/src/edu/uci/eecs/specCompiler/specExtraction/CPDefineCheckConstruct.java
@@ -1,5 +1,19 @@
 package edu.uci.eecs.specCompiler.specExtraction;
 
 public class CPDefineCheckConstruct extends Construct {
-
+	public final String label;
+	public final String condition;
+	
+	public CPDefineCheckConstruct(String label, String condition) {
+		this.label = label;
+		this.condition = condition;
+	}
+	
+	public String toString() {
+		StringBuffer res = new StringBuffer();
+		res.append("@Commit_point_define_check:\n");
+		res.append("Label: " + label + "\n");
+		res.append("Condition: " + condition + "\n");
+		return res.toString();
+	}
 }
diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/CPDefineConstruct.java b/src/edu/uci/eecs/specCompiler/specExtraction/CPDefineConstruct.java
index e351dc9..7d22728 100644
--- a/src/edu/uci/eecs/specCompiler/specExtraction/CPDefineConstruct.java
+++ b/src/edu/uci/eecs/specCompiler/specExtraction/CPDefineConstruct.java
@@ -1,5 +1,22 @@
 package edu.uci.eecs.specCompiler.specExtraction;
 
 public class CPDefineConstruct extends Construct {
-
+	public final String label;
+	public final String potentialCPLabel;
+	public final String condition;
+	
+	public CPDefineConstruct(String label, String potentialCPLabel, String condition) {
+		this.label = label;
+		this.potentialCPLabel = potentialCPLabel;
+		this.condition = condition;
+	}
+	
+	public String toString() {
+		StringBuffer res = new StringBuffer();
+		res.append("@Potential_commit_point_define:\n");
+		res.append("Label: " + label + "\n");
+		res.append("Potential_CP_label: " + potentialCPLabel + "\n");
+		res.append("Condition: " + condition + "\n");
+		return res.toString();
+	}
 }
diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/GlobalConstruct.java b/src/edu/uci/eecs/specCompiler/specExtraction/GlobalConstruct.java
index 88c2bfb..ef0a815 100644
--- a/src/edu/uci/eecs/specCompiler/specExtraction/GlobalConstruct.java
+++ b/src/edu/uci/eecs/specCompiler/specExtraction/GlobalConstruct.java
@@ -64,11 +64,11 @@ public class GlobalConstruct extends Construct {
 	}
 	
 	public String toString() {
-		StringBuilder sb = new StringBuilder();
-		sb.append("Code:\n");
+		StringBuilder sb = new StringBuilder("GlobalConstruct:\n");
+		sb.append("@Code:\n");
 		sb.append(code);
 		
-		sb.append("Happens_before:\n");
+		sb.append("@Happens_before:\n");
 		for (ConditionalInterface left : hbRelations.keySet()) {
 			HashSet<ConditionalInterface> rights = hbRelations.get(left);
 			sb.append(left + " -> ");
diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/InterfaceConstruct.java b/src/edu/uci/eecs/specCompiler/specExtraction/InterfaceConstruct.java
index e18bc39..c301c4e 100644
--- a/src/edu/uci/eecs/specCompiler/specExtraction/InterfaceConstruct.java
+++ b/src/edu/uci/eecs/specCompiler/specExtraction/InterfaceConstruct.java
@@ -1,5 +1,57 @@
 package edu.uci.eecs.specCompiler.specExtraction;
 
+import java.util.ArrayList;
+import java.util.HashMap;
+
 public class InterfaceConstruct extends Construct {
+	public final String name;
+	public final ArrayList<String> commitPointSet;
+	public final String condition;
+	public final HashMap<String, String> hbConditions;
+	public final String idCode;
+	public final String check;
+	public final String action;
+	public final String postAction;
+	public final String postCheck;
 
+	public InterfaceConstruct(String name, ArrayList<String> commitPointSet,
+			String condition, HashMap<String, String> hbConditions, String idCode,
+			String check, String action, String postAction, String postCheck) {
+		this.name = name;
+		this.commitPointSet = commitPointSet;
+		this.condition = condition;
+		this.hbConditions = hbConditions;
+		this.idCode = idCode;
+		this.check = check;
+		this.action = action;
+		this.postAction = postAction;
+		this.postCheck = postCheck;
+	}
+	
+	public String toString() {
+		StringBuilder sb = new StringBuilder("InterfaceConstruct:\n");
+		sb.append("@Interface: " + name + "\n");
+		sb.append("@Commit_point_set: ");
+		for (String commitPoint : commitPointSet) {
+			sb.append(commitPoint + " | ");
+		}
+		sb.append(".\n");
+		sb.append("@Condition: ");
+		sb.append(condition + "\n");
+		sb.append("@HBConditions: \n");
+		for (String hbLabel : hbConditions.keySet()) {
+			String hbCondition = hbConditions.get(hbLabel);
+			sb.append(hbLabel + " :: " + hbCondition + "\n");
+		}
+		sb.append("@ID: ");
+		sb.append(idCode + "\n");
+		sb.append("@Check: " + check + "\n");
+		sb.append("@Action:\n");
+		sb.append(action + "\n");
+		sb.append("@Post_action:\n");
+		sb.append(postAction + "\n");
+		sb.append("@Post_check: " + postCheck + "\n");
+		
+		return sb.toString();
+	}
 }
diff --git a/src/edu/uci/eecs/specCompiler/specExtraction/PotentialCPDefineConstruct.java b/src/edu/uci/eecs/specCompiler/specExtraction/PotentialCPDefineConstruct.java
index 946697e..3f68610 100644
--- a/src/edu/uci/eecs/specCompiler/specExtraction/PotentialCPDefineConstruct.java
+++ b/src/edu/uci/eecs/specCompiler/specExtraction/PotentialCPDefineConstruct.java
@@ -1,5 +1,19 @@
 package edu.uci.eecs.specCompiler.specExtraction;
 
 public class PotentialCPDefineConstruct extends Construct {
-
+	public final String label;
+	public final String condition;
+	
+	public PotentialCPDefineConstruct(String label, String condition) {
+		this.label = label;
+		this.condition = condition;
+	}
+	
+	public String toString() {
+		StringBuffer res = new StringBuffer();
+		res.append("@Potential_commit_point_define:\n");
+		res.append("Label: " + label + "\n");
+		res.append("Condition: " + condition + "\n");
+		return res.toString();
+	}
 }
-- 
2.34.1