+++ /dev/null
-/* spec-compiler.jj Grammer definition for the specification */
-
-
-/*
- SPEC constructs:
- Each construct should be embraced by /DOUBLE_STAR ... STAR/ annotation.
- Within there, any line beginning with a "#" is a comment of the annotation.
- Each constrcut should begin with @Begin and end with @End. Otherwise, the
- annotation would be considered as normal comments of the source.
-
- a) Global construct
- @DeclareState: C/C++ variable declaration; // Declare the state structure
- @InitState: C/C++ statements; // Specify how to initialize the state
- @CopyState: // A function on how to copy an existing state (Not sure if we
- // need this because we might be able to auto this
- @Commutativity: Method1 <-> Method2 (Guard) // Admissibility condition.
- // Allow specify 0-many rules
-
- b) Interface construct
- @Interface: InterfaceName // Required; a label to represent the interface
- @LocalState: // Optional; to calculate the accumulative local state before this
- // method call in a customized fashion. If not specified here, the
- // local state would be default, which is the result of the
- // execution on the subset of method calls in the sequential order
- @PreCondition: // Optional; checking code
- @LocalSideEffect: // Optional; to calculate the side effect this method call
- // have on the local state in a customized fashion. If this
- // field is not stated, it means we don't care about it.
- @SideEffect: // Optional; to calculate the side effect on the global state. When
- // the "@LocalSideEffect" specification is ommitted, we also impose the
- // same side effect on the set of method calls that happen before this
- // method call in the sequential order.
- @PostCondition: // Optional; checking code
-
-
- c) Ordering point construct
- @OPDefine: condition // If the specified condition satisfied, the atomic
- // operation right before is an ordering point
-
- @PotentialOP(Label): condition // If the specified condition satisfied, the
- // atomic operation right before is a potential
- // ordering point, and we label it with a tag
-
- @OPCheck(Label): condition // If the specified condition satisfied, the
- // potential ordering point defined earlier with the
- // same tag becomes an ordering point
-
- @OPClear: condition // If the specified condition satisfied, all the
- // ordering points and potential ordering points will be
- // cleared
-
- @OPClearDefine: condition // If the specified condition satisfied, all the
- // ordering points and potential ordering points will
- // be cleared, and the atomic operation right before
- // becomes an ordering point. This is a syntax sugar
- // as the combination of an "OPClear" and "OPDefine"
- // statement
-
-*/
-
-
-
-options {
- STATIC = false;
- JAVA_UNICODE_ESCAPE = true;
-}
-
-PARSER_BEGIN(SpecParser)
-package edu.uci.eecs.specCompiler.grammerParser;
-
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.InputStream;
-import java.io.ByteArrayInputStream;
-import java.io.File;
-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;
-import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
-import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
-import edu.uci.eecs.specCompiler.specExtraction.CommutativityRule;
-import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
-import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
-import edu.uci.eecs.specCompiler.specExtraction.CPClearConstruct;
-import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
-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;
-import edu.uci.eecs.specCompiler.specExtraction.QualifiedName;
-import edu.uci.eecs.specCompiler.specExtraction.SourceFileInfo;
-import edu.uci.eecs.specCompiler.specExtraction.VariableDeclaration;
-
- 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 {
- 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>();
- parser.Parse(f, content, constructs, headers);
- for (int i = 0; i < content.size(); i++) {
- System.out.println(content.get(i));
- }
-
- for (int i = 0; i < constructs.size(); i++) {
- System.out.println(constructs.get(i));
- }
-
-
- //parser.Test();
- System.out.println("Parsing finished!");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
-
- public static SourceFileInfo ParseFile(File f)
- throws ParseException, TokenMgrError {
- try {
- InputStream input = new FileInputStream(f);
- SpecParser parser = new SpecParser(input);
- ArrayList<String> content = new ArrayList<String>(),
- headers = new ArrayList<String>();
- ArrayList<Construct> constructs = new ArrayList<Construct>();
- parser.Parse(f, content, constructs, headers);
- return new SourceFileInfo(f, content, headers, constructs);
- } 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());
- 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();
- if (content.size() == 1)
- return content.get(0);
- for (int i = 0; i < content.size(); i++) {
- sb.append(content.get(i) + "\n");
- }
- return sb.toString();
- }
-
- /**
- boolean spaceSeparator(Token t) {
- switch (t.image) {
- case "[":
- case "]":
- case "=":
- case "(":
- case ")":
- case ",":
- case ".":
- case "*":
- case "~":
- case "!":
- case "&":
- case "|":
- case "%":
- case "+":
- case "-":
- case "/":
- case "<":
- case ">":
- case "<=":
- case ">=":
- case "==":
- case "!=":
- case "&&":
- case "||":
- case "^":
- case "?":
- case ":":
- case "::":
- case "<<":
- case ">>":
- case ">>>":
- case "+=":
- case "-=":
- case "*=":
- case "/=":
- case "%=":
- case "^=":
- case "&=":
- case ";":
- return false;
- default:
- return true;
- }
- }
- */
-
- }
-PARSER_END(SpecParser)
-
-
-
-<IN_POTENTIAL_SPEC, IN_SPEC> SKIP :
-{
- " "
-|
- "\n"
-|
- "\r"
-|
- "\r\n"
-|
- "\t"
-}
-
-SKIP : {
- "/**" : IN_POTENTIAL_SPEC
-}
-
-<IN_POTENTIAL_SPEC> TOKEN : {
- <BEGIN: "@Begin"> : IN_SPEC
-}
-
-<IN_SPEC> SKIP : {
- "*/" : DEFAULT
-}
-
-SKIP : {
- "/*": IN_COMMENT
-}
-
-<DEFAULT> TOKEN: {
- <ANY: ~[]>
-}
-
-<*> SKIP : {
- // "//" comment for the specification
- <"//" (~["\n", "\r"])* (["\n", "\r"])>
-}
-
-<IN_COMMENT, IN_POTENTIAL_SPEC> SKIP : {
- "*/": DEFAULT
-}
-
-<IN_COMMENT, IN_POTENTIAL_SPEC> SKIP : { < ~[] > }
-
-<IN_SPEC> SKIP :
-{
- // "#" comment for the specification
- <"#" (~["\n", "\r"])* (["\n", "\r"])>
-}
-
-
-<IN_SPEC> TOKEN : {
- <END: "@End">
-|
- <OPTIONS: "@Options:">
-|
- <GLOBAL_DEFINE: "@Global_define:">
-|
- <DECLARE_STRUCT: "@DeclareStruct:">
-|
- <DECLARE_VAR: "@DeclareVar:">
-|
- <INIT_VAR: "@InitVar:">
-|
- <CLEANUP: "@Finalize:">
-|
- <DEFINE_FUNC: "@DefineFunc:">
-|
- <INTERFACE_CLUSTER: "@Interface_cluster:">
-|
- <HAPPENS_BEFORE: "@Happens_before:">
-|
- <COMMUTATIVITY: "@Commutativity:">
-|
- <INTERFACE: "@Interface:">
-|
- <COMMIT_POINT_SET: "@Commit_point_set:">
-|
- <ENTRY_POINT: "@Entry_point">
-|
- <CLASS_BEGIN: "@Class_begin">
-|
- <CLASS_END: "@Class_end">
-|
- <INTERFACE_DEFINE: "@Interface_define:">
-|
- <CONDITION: "@Condition:">
-|
- <HB_CONDITION: "@HB_condition:">
-|
- <ID: "@ID:">
-|
- <CHECK: "@Check:">
-|
- <ACTION: "@Action:">
-|
- <CODE: "@Code:">
-|
- <POST_ACTION: "@Post_action:">
-|
- <POST_CHECK: "@Post_check:">
-|
- <POTENTIAL_COMMIT_POINT_DEFINE: "@Potential_commit_point_define:">
-|
- <POTENTIAL_ADDITIONAL_ORDERING_POINT_DEFINE: "@Potential_additional_ordering_point_define:">
-|
- <LABEL: "@Label:">
-|
- <COMMIT_POINT_DEFINE_CHECK: "@Commit_point_define_check:">
-|
- <ADDITIONAL_ORDERING_POINT_DEFINE_CHECK: "@Additional_ordering_point_define_check:">
-|
- <COMMIT_POINT_DEFINE: "@Commit_point_define:">
-|
- <ADDITIONAL_ORDERING_POINT_DEFINE: "@Additional_ordering_point_define:">
-|
- <COMMIT_POINT_CLEAR: "@Commit_point_clear:">
-|
- <POTENTIAL_COMMIT_POINT_LABEL: "@Potential_commit_point_label:">
-|
- <POTENTIAL_ADDITIONAL_ORDERING_POINT_LABEL: "@Potential_additional_ordering_point_label:">
-}
-
-
-<IN_SPEC> TOKEN :
-{
-/* Specification & C/C++ shared tokens */
-// Reserved keywords
- <CONST: "const">
-|
- <STRUCT: "struct">
-|
- <CLASS: "class">
-|
- <UNSIGNED: "unsigned">
-|
- <TEMPLATE: "template">
-|
- <INLINE: "inline">
-|
- <STATIC: "static">
-|
- <FOR: "for">
-|
- <#DIGIT: ["0"-"9"]>
-|
- <#LETTER: ["a"-"z", "A"-"Z"]>
-|
- <IDENTIFIER: (<LETTER> | "_") (<LETTER> | <DIGIT> | "_")*>
-|
- <POUND: "#">
-|
- <OPEN_BRACKET: "[">
-|
- <CLOSE_BRACKET: "]">
-|
- <EQUALS: "=">
-|
- <OPEN_PAREN: "(">
-|
- <CLOSE_PAREN: ")">
-|
- <OPEN_BRACE: "{">
-|
- <CLOSE_BRACE: "}">
-|
- <HB_SYMBOL: "->">
-|
- <COMMUTATIVITY_SYMBOL: "<->">
-|
- <COMMA: ",">
-|
-/* C/C++ only token*/
- <DOT: ".">
-|
- <DOLLAR: "$">
-|
- <STAR: "*">
-|
- <NEGATE: "~">
-|
- <EXCLAMATION: "!">
-|
- <AND: "&">
-|
- <OR: "|">
-|
- <MOD: "%">
-|
- <PLUS: "+">
-|
- <PLUSPLUS: "++">
-|
- <MINUS: "-">
-|
- <MINUSMINUS: "--">
-|
- <DIVIDE: "/">
-|
- <BACKSLASH: "\\">
-|
- <LESS_THAN: "<">
-|
- <GREATER_THAN: ">">
-|
- <GREATER_EQUALS: ">=">
-|
- <LESS_EQUALS: "<=">
-|
- <LOGICAL_EQUALS: "==">
-|
- <NOT_EQUALS: "!=">
-|
- <LOGICAL_AND: "&&">
-|
- <LOGICAL_OR: "||">
-|
- <XOR: "^">
-|
- <QUESTION_MARK: "?">
-|
- <COLON: ":">
-|
- <DOUBLECOLON: "::">
-|
- <DOUBLELESSTHAN: "<<">
-|
- <DOUBLEGREATERTHAN: ">>">
-|
- <TRIPLEGREATERTHAN: ">>>">
-|
- <PLUS_EQUALS: "+=">
-|
- <MINUS_EQUALS: "-=">
-|
- <TIMES_EQUALS: "*=">
-|
- <DIVIDE_EQUALS: "/=">
-|
- <MOD_EQUALS: "%=">
-|
- <XOR_EQUALS: "^=">
-|
- <OR_EQUALS: "|=">
-|
- <AND_EQUALS: "&=">
-|
- <SEMI_COLON: ";">
-|
- <STRING_LITERAL:
- "\""
- ((~["\"","\\","\n","\r"])
- | ("\\"
- ( ["n","t","b","r","f","\\","'","\""]
- | ["0"-"7"] ( ["0"-"7"] )?
- | ["0"-"3"] ["0"-"7"]
- ["0"-"7"]
- )
- )
- )*
- "\"">
-|
- <CHARACTER_LITERAL:
- "'"
- ((~["'","\\","\n","\r"])
- | ("\\"
- (["n","t","b","r","f","\\","'","\""]
- | ["0"-"7"] ( ["0"-"7"] )?
- | ["0"-"3"] ["0"-"7"]
- ["0"-"7"]
- )
- )
- )
- "'">
-|
- < INTEGER_LITERAL:
- <DECIMAL_LITERAL> (["l","L"])?
- | <HEX_LITERAL> (["l","L"])?
- | <OCTAL_LITERAL> (["l","L"])?>
-|
- < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
-|
- < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
-|
- < #OCTAL_LITERAL: "0" (["0"-"7"])* >
-|
- < FLOATING_POINT_LITERAL:
- <DECIMAL_FLOATING_POINT_LITERAL>
- | <HEXADECIMAL_FLOATING_POINT_LITERAL> >
-|
- < #DECIMAL_FLOATING_POINT_LITERAL:
- (["0"-"9"])+ "." (["0"-"9"])* (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
- | "." (["0"-"9"])+ (<DECIMAL_EXPONENT>)? (["f","F","d","D"])?
- | (["0"-"9"])+ <DECIMAL_EXPONENT> (["f","F","d","D"])?
- | (["0"-"9"])+ (<DECIMAL_EXPONENT>)? ["f","F","d","D"]>
-|
- < #DECIMAL_EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
-|
- < #HEXADECIMAL_FLOATING_POINT_LITERAL:
- "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])+ (".")? <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?
- | "0" ["x", "X"] (["0"-"9","a"-"f","A"-"F"])* "." (["0"-"9","a"-"f","A"-"F"])+ <HEXADECIMAL_EXPONENT> (["f","F","d","D"])?>
-|
- < #HEXADECIMAL_EXPONENT: ["p","P"] (["+","-"])? (["0"-"9"])+ >
-|
- < #SPACE: (" " | "\t")+>
-|
- < #TO_END_OF_LINE: (~["\n"])+>
-|
- /* Macro token */
- <INCLUDE: "#" (<SPACE>)? "include" <SPACE> (<STRING_LITERAL> | "<" (<LETTER> | <DOT>)+ ">")>
-|
- <DEFINE: "#" (<SPACE>)? <TO_END_OF_LINE>>
-}
-
-String Type() :
-{
- String type;
- String str;
- QualifiedName name;
-}
-{
- { type = ""; }
- (<CONST>
- { type = "const"; }
- )?
- (((str = <STRUCT>.image | str = <CLASS>.image | str = <UNSIGNED>.image) { type = type + " " + str; })?
- (
- name = ParseQualifiedName() {
- if (!type.equals(""))
- type = type + " " + name.fullName;
- else
- type = name.fullName;
- })
- )
- ((str = <CONST>.image {
- if (!type.equals(""))
- type = type + " " + str;
- else
- type = str;
- }) |
- (str = <STAR>.image {
- if (!type.equals(""))
- type = type + " " + str;
- else
- type = str;
- }) |
- (str = <AND>.image {
- if (!type.equals(""))
- type = type + " " + str;
- else
- type = str;
- })
- )*
- {
- return type;
- }
-}
-
-
-String ParameterizedName() :
-{
- String res = "";
- String str;
-}
-{
- (str = <IDENTIFIER>.image {res = str;})
- (<OPEN_BRACKET> str = Type() { res = res + "<" + str; }
- (<COMMA> str = Type() { res = res + ", " + str; })* <CLOSE_BRACKET>
- { res = res + ">"; }
- )?
- {
- return res;
- }
-}
-
-FunctionHeader FuncDecl() :
-{
- String ret;
- QualifiedName funcName;
- ArrayList<VariableDeclaration> args;
-}
-{
- (<STATIC> | <INLINE>)*
- ret = Type()
- funcName = ParseQualifiedName()
- args = FormalParamList()
- {
- FunctionHeader res = new FunctionHeader(ret, funcName, args);
- //System.out.println(res);
- return res;
- }
-}
-
-QualifiedName ParseQualifiedName() :
-{
- String qualifiedName, str;
-}
-{
- { qualifiedName = ""; }
- (str = ParameterizedName() { qualifiedName = qualifiedName + str; } )
- ( <DOUBLECOLON> (str = ParameterizedName() { qualifiedName = qualifiedName +
- "::" + str; } ))*
- {
- QualifiedName res = new QualifiedName(qualifiedName);
- //System.out.println(res);
- return res;
- }
-}
-
-ArrayList<VariableDeclaration> TemplateParamList() :
-{
- ArrayList<VariableDeclaration> params;
- String type;
- String name;
-}
-{
- {
- params = new ArrayList<VariableDeclaration>();
- }
- <TEMPLATE>
- <OPEN_BRACKET>
- (type = <IDENTIFIER>.image
- name = <IDENTIFIER>.image
- {
- params.add(new VariableDeclaration(type, name));
- }
- )
-
- (<COMMA> type = <IDENTIFIER>.image
- name = <IDENTIFIER>.image
- {
- params.add(new VariableDeclaration(type, name));
- }
- )*
- <CLOSE_BRACKET>
- {
- //System.out.println(params);
- return params;
- }
-}
-
-ArrayList<VariableDeclaration > FormalParamList() :
-{
- ArrayList<VariableDeclaration > typeParams;
- VariableDeclaration varDecl;
-}
-{
- {
- typeParams = new ArrayList<VariableDeclaration >();
- }
- <OPEN_PAREN>
- ((varDecl = TypeParam() {typeParams.add(varDecl);})
- ((<COMMA> varDecl = TypeParam() {typeParams.add(varDecl);}))*)?
- <CLOSE_PAREN>
- {
- return typeParams;
- }
-}
-
-VariableDeclaration TypeParam() :
-{
- String type, param;
-}
-{
- (type = Type()) (param = <IDENTIFIER>.image)
- {
- return new VariableDeclaration(type, param);
- }
-}
-
-
-
-ArrayList<String> C_CPP_CODE(ArrayList<String> headers) :
-{
- String text;
- Token t;
- boolean newLine = false;
- boolean newSpace = true;
- boolean inTemplate = false;
- boolean inForLoop = false;
- ArrayList<String> content;
- String header;
-}
-{
- {
- text = "";
- t = new Token();
- content = new ArrayList<String>();
- }
- (
- LOOKAHEAD(2)
- (
- t = <CONST> | t = <STRUCT> | t = <CLASS> | t = <UNSIGNED> |
- (t = <TEMPLATE> { inTemplate = true; })|
- t = <STATIC> | t = <INLINE> |
- (t = <FOR> { inForLoop = true; })|
- (t = <INCLUDE>
- {
- header = t.image;
- newLine = true;
- if (headers != null) {
- headers.add(header.substring(header.lastIndexOf(' ') + 1));
- }
- })
- | t = <IDENTIFIER> | t = <POUND> |
- (t = <OPEN_BRACE> { newLine = true; } ) |
- (t = <CLOSE_BRACE> { newLine = true; inForLoop = false;} ) |
- t = <EQUALS> | t = <OPEN_PAREN> | t = <CLOSE_PAREN> |
- t = <OPEN_BRACKET> | t = <CLOSE_BRACKET>
- | t = <HB_SYMBOL> | t = <COMMA> |
- t = <DOT> | t = <STAR> | t = <DOLLAR> | 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> { 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 = <DOUBLELESSTHAN> |
- t = <DOUBLEGREATERTHAN> |
- t = <TRIPLEGREATERTHAN> |
-
- t = <PLUS_EQUALS> |
- t = <MINUS_EQUALS> |
- t = <TIMES_EQUALS> |
- t = <DIVIDE_EQUALS> |
- t = <MOD_EQUALS> |
- t = <XOR_EQUALS> |
- t = <OR_EQUALS> |
- t = <AND_EQUALS> |
-
- (t = <SEMI_COLON> { if (!inForLoop) newLine = true; } )
- | t = <STRING_LITERAL> | t = <CHARACTER_LITERAL> |
- t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL> |
- (t = <DEFINE> { newLine = true; } )
- )
- {
- if (text.equals("")) {
- text = t.image;
- newSpace = true;
- } else {
- text = text + " " + t.image;
- /*
- if (newSpace && spaceSeparator(t)) {
- text = text + " " + t.image;
- } else {
- text = text + t.image;
- if (spaceSeparator(t))
- newSpace = true;
- }*/
- }
- if (newLine) {
- content.add(text);
- text = "";
- newLine = false;
- inTemplate = false;
- }
- }
- )+
-
- {
- if (content.size() == 0) {
- content.add(text);
- }
- return content;
- }
-}
-
-
-void Parse(File f, ArrayList<String> content, ArrayList<Construct> constructs, ArrayList<String> headers) :
-{
- Construct inst;
- 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;
- }
- }
-}
-
-Construct ParseSpec() :
-{
- Construct res;
-}
-{
- (
- LOOKAHEAD(2) res = Global_construct() |
- LOOKAHEAD(2) res = Interface() |
- LOOKAHEAD(2) res = Potential_commit_point_define() |
- LOOKAHEAD(2) res = Commit_point_define() |
- LOOKAHEAD(2) res = Commit_point_define_check() |
- LOOKAHEAD(2) res = Potential_additional_ordering_point_define() |
- LOOKAHEAD(2) res = Additional_ordering_point_define() |
- LOOKAHEAD(2) res = Additional_ordering_point_define_check() |
- LOOKAHEAD(2) res = Commit_point_clear() |
- LOOKAHEAD(2) res = Entry_point() |
- LOOKAHEAD(2) res = Class_begin() |
- LOOKAHEAD(2) res = Class_end() |
- LOOKAHEAD(2) res = Interface_define()
- )
- {
- //System.out.println(res);
- return res;
- }
-}
-
-GlobalConstruct Global_construct() :
-{
- GlobalConstruct res;
- SequentialDefineSubConstruct code;
- HashMap<String, String> options;
- String key, value;
-}
-{
- {
- res = null;
- options = new HashMap<String, String>();
- }
- <BEGIN>
- (<OPTIONS>
- ((key = <IDENTIFIER>.image)
- <EQUALS>
- (value = <IDENTIFIER>.image)
- {
- if (options.containsKey(key)) {
- throw new ParseException("Duplicate options!");
- }
- options.put(key, value);
- }
- <SEMI_COLON>
- )*
- )?
- (code = Global_define())
- { res = new GlobalConstruct(_file, _content.size(), code, options); }
- (Interface_clusters(res))?
- (Happens_before(res))?
- (Commutativity(res))?
- <END>
- {
- res.unfoldInterfaceCluster();
- return res;
- }
-}
-
-SequentialDefineSubConstruct Global_define() :
-{
- ArrayList<String> initVar, cleanup, defineFunc, code, declareStruct;
- ArrayList<ArrayList<String>> defineFuncs;
- ArrayList<VariableDeclaration> declareVars;
- ArrayList<ArrayList<String>> declareStructs;
- VariableDeclaration declareVar;
-
-}
-{
- {
- declareVars = new ArrayList<VariableDeclaration>();
- initVar = new ArrayList<String>();
- cleanup = new ArrayList<String>();
- defineFuncs = new ArrayList<ArrayList<String>>();
- declareStructs = new ArrayList<ArrayList<String>>();
- }
- <GLOBAL_DEFINE>
- (<DECLARE_STRUCT> (declareStruct = C_CPP_CODE(null) {
- declareStructs.add(declareStruct); }))*
- (<DECLARE_VAR> ((declareVar = TypeParam() <SEMI_COLON> {
- declareVars.add(declareVar); } )*))?
- (<INIT_VAR> (code = C_CPP_CODE(null) { initVar = code; } ))?
- (<CLEANUP> (code = C_CPP_CODE(null) { cleanup = code; } ))?
- (<DEFINE_FUNC> (defineFunc = C_CPP_CODE(null) { defineFuncs.add(defineFunc); }))*
- {
- SequentialDefineSubConstruct res = new
- SequentialDefineSubConstruct(declareStructs, declareVars, initVar, cleanup, defineFuncs);
- //System.out.println(res);
- return res;
- }
-}
-
-ConditionalInterface Conditional_interface() :
-{
- String interfaceName, hbConditionLabel;
-}
-{
- {
- hbConditionLabel = "";
- }
- interfaceName = <IDENTIFIER>.image (<OPEN_PAREN> hbConditionLabel =
- <IDENTIFIER>.image <CLOSE_PAREN>)?
- {
- return new ConditionalInterface(interfaceName, hbConditionLabel);
- }
-}
-
-void Interface_cluster(GlobalConstruct inst) :
-{
- String clusterName;
- ConditionalInterface condInterface;
-}
-{
- (clusterName= <IDENTIFIER>.image)
- <EQUALS> <OPEN_BRACE>
- (condInterface = Conditional_interface()
- { inst.addInterface2Cluster(clusterName, condInterface); }
- )
- (<COMMA> condInterface = Conditional_interface()
- { inst.addInterface2Cluster(clusterName, condInterface); }
- )*
- <CLOSE_BRACE>
-}
-
-void Interface_clusters(GlobalConstruct inst) :
-{}
-{
- <INTERFACE_CLUSTER> (Interface_cluster(inst))+
-}
-
-void Happens_before(GlobalConstruct inst) :
-{
- ConditionalInterface left, right;
-}
-{
- <HAPPENS_BEFORE>
- (
- left = Conditional_interface() <HB_SYMBOL> right = Conditional_interface()
- { inst.addHBCondition(left, right); }
- )+
-}
-
-void Commutativity(GlobalConstruct inst) :
-{
- String method1, method2, condition;
- ArrayList<String> content;
-}
-{
- {
- content = new ArrayList<String>();
- }
-
- (
- <COMMUTATIVITY>
- method1 = <IDENTIFIER>.image <COMMUTATIVITY_SYMBOL>
- method2 = <IDENTIFIER>.image
- <COLON>
- content = C_CPP_CODE(null)
- { condition = stringArray2String(content); }
- {
- inst.addCommutativityRule(method1, method2, condition);
- }
- )+
-}
-
-InterfaceConstruct Interface() :
-{
- InterfaceConstruct res;
- String interfaceName, condition, idCode, check,
- postCheck, commitPoint, hbLabel, hbCondition;
- ArrayList<String> commitPointSet;
- ArrayList<String> action, postAction;
- HashMap<String, String> hbConditions;
- ArrayList<String> content;
-}
-{
- {
- res = null;
- action = new ArrayList<String>();
- condition = "";
- idCode = "";
- check = "";
- postCheck = "";
- commitPointSet = new ArrayList<String>();
- hbConditions = new HashMap<String, String>();
- postAction = new ArrayList<String>();
- }
- <BEGIN>
- <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> (content = C_CPP_CODE(null) { condition = stringArray2String(content); }))?
- (
- <HB_CONDITION>
- (hbLabel = <IDENTIFIER>.image) <DOUBLECOLON>
- (content = C_CPP_CODE(null) { hbCondition = stringArray2String(content); })
- {
- if (hbConditions.containsKey(hbLabel)) {
- throw new ParseException(interfaceName + " has" +
- "duplicate happens-before condtion labels");
- }
- hbConditions.put(hbLabel, hbCondition);
- }
- )*
- (<ID> (content = C_CPP_CODE(null) { idCode = stringArray2String(content); }))?
- (<CHECK> (content = C_CPP_CODE(null) { check = stringArray2String(content); }))?
- (<ACTION> action = C_CPP_CODE(null))?
- (<POST_ACTION> (postAction = C_CPP_CODE(null) ))?
- (<POST_CHECK> (content = C_CPP_CODE(null) { postCheck = stringArray2String(content); }))?
- <END>
- {
- res = new InterfaceConstruct(_file, _content.size(), interfaceName, commitPointSet, condition,
- hbConditions, idCode, check, action, postAction, postCheck);
- return res;
- }
-}
-
-
-PotentialCPDefineConstruct Potential_commit_point_define() :
-{
- PotentialCPDefineConstruct res;
- String label, condition;
- ArrayList<String> content;
-}
-{
-
- { res = null; }
- <BEGIN>
- <POTENTIAL_COMMIT_POINT_DEFINE> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
- <LABEL> (label = <IDENTIFIER>.image)
- <END>
- {
- res = new PotentialCPDefineConstruct(_file, _content.size(), label, condition);
- return res;
- }
-}
-
-PotentialCPDefineConstruct Potential_additional_ordering_point_define() :
-{
- PotentialCPDefineConstruct res;
- String label, condition;
- ArrayList<String> content;
-}
-{
-
- { res = null; }
- <BEGIN>
- <POTENTIAL_ADDITIONAL_ORDERING_POINT_DEFINE> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
- <LABEL> (label = <IDENTIFIER>.image)
- <END>
- {
- // Set the boolean flag isAdditionalOrderingPoint to be true
- res = new PotentialCPDefineConstruct(_file, _content.size(), true, label, condition);
- return res;
- }
-}
-
-
-CPDefineConstruct Commit_point_define() :
-{
- CPDefineConstruct res;
- String label, potentialCPLabel, condition;
- ArrayList<String> content;
-}
-{
-
- { res = null; }
- <BEGIN>
- <COMMIT_POINT_DEFINE> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
- <POTENTIAL_COMMIT_POINT_LABEL> (potentialCPLabel = <IDENTIFIER>.image)
- <LABEL> (label = <IDENTIFIER>.image)
- <END>
- {
- res = new CPDefineConstruct(_file, _content.size(), false, label, potentialCPLabel, condition);
- return res;
- }
-}
-
-CPDefineConstruct Additional_ordering_point_define() :
-{
- CPDefineConstruct res;
- String label, potentialCPLabel, condition;
- ArrayList<String> content;
-}
-{
-
- { res = null; }
- <BEGIN>
- <ADDITIONAL_ORDERING_POINT_DEFINE> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
- <POTENTIAL_ADDITIONAL_ORDERING_POINT_LABEL> (potentialCPLabel = <IDENTIFIER>.image)
- <LABEL> (label = <IDENTIFIER>.image)
- <END>
- {
- // Set the boolean flag isAdditionalOrderingPoint to be true
- res = new CPDefineConstruct(_file, _content.size(), true, label, potentialCPLabel, condition);
- return res;
- }
-}
-
-CPClearConstruct Commit_point_clear() :
-{
- CPClearConstruct res;
- String label, condition;
- ArrayList<String> content;
-}
-{
-
- { res = null; }
- <BEGIN>
- <COMMIT_POINT_CLEAR> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
- <LABEL> (label = <IDENTIFIER>.image)
- <END>
- {
- res = new CPClearConstruct(_file, _content.size(), label, condition);
- return res;
- }
-}
-
-
-CPDefineCheckConstruct Commit_point_define_check() :
-{
- CPDefineCheckConstruct res;
- String label, condition;
- ArrayList<String> content;
-}
-{
-
- { res = null; }
- <BEGIN>
- <COMMIT_POINT_DEFINE_CHECK> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
- <LABEL> (label = <IDENTIFIER>.image)
- <END>
- {
- res = new CPDefineCheckConstruct(_file, _content.size(), false, label, condition);
- return res;
- }
-}
-
-CPDefineCheckConstruct Additional_ordering_point_define_check() :
-{
- CPDefineCheckConstruct res;
- String label, condition;
- ArrayList<String> content;
-}
-{
-
- { res = null; }
- <BEGIN>
- <ADDITIONAL_ORDERING_POINT_DEFINE_CHECK> (content = C_CPP_CODE(null) { condition = stringArray2String(content); })
- <LABEL> (label = <IDENTIFIER>.image)
- <END>
- {
- // Set the boolean flag isAdditionalOrderingPoint to be true
- res = new CPDefineCheckConstruct(_file, _content.size(), true, label, condition);
- return res;
- }
-}
-
-EntryPointConstruct Entry_point() :
-{}
-{
-
- <BEGIN>
- <ENTRY_POINT>
- <END>
- {
- return new EntryPointConstruct(_file, _content.size());
- }
-}
-
-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;
-}
-{
- <BEGIN>
- <INTERFACE_DEFINE> (name = <IDENTIFIER>.image)
- <END>
- {
- return new InterfaceDefineConstruct(_file, _content.size(), name);
- }
-}