# entry point.
LANG = C;
@Global_define:
+ @DeclareStruct:
@DeclareVar:
@InitVar:
@DefineFunc:
b) Interface construct
@Begin
- @Interface: ...
+ @Interface_decl: ...
@Commit_point_set:
IDENTIFIER | IDENTIFIER ...
@Condition: ... (Optional)
@Begin
@Interface_define: <Interface_Name>
@End
+
+ g) Interface declare & define construct
+ @Begin
+ @Interface_decl_define: <Interface_Name>
+ @Commit_point_set:
+ IDENTIFIER | IDENTIFIER ...
+ @Condition: ... (Optional)
+ @HB_Condition:
+ IDENTIFIER :: <C_CPP_Condition>
+ @HB_Condition: ...
+ @ID: ... (Optional, use default ID)
+ @Check: (Optional)
+ ...
+ @Action: (Optional)
+ ...
+ @Post_action: (Optional)
+ @Post_check: (Optional)
+ @End
+
*/
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>();
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();
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");
}
<OPTIONS: "@Options:">
|
<GLOBAL_DEFINE: "@Global_define:">
+|
+ <DECLARE_STRUCT: "@DeclareStruct:">
|
<DECLARE_VAR: "@DeclareVar:">
|
<STRUCT: "struct">
|
<CLASS: "class">
+|
+ <UNSIGNED: "unsigned">
|
<TEMPLATE: "template">
+|
+ <INLINE: "inline">
+|
+ <STATIC: "static">
|
<#DIGIT: ["0"-"9"]>
|
<COLON: ":">
|
<DOUBLECOLON: "::">
+|
+ <DOUBLELESSTHAN: "<<">
+|
+ <DOUBLEGREATERTHAN: ">>">
+|
+ <TRIPLEGREATERTHAN: ">>>">
+|
+ <PLUS_EQUALS: "+=">
+|
+ <MINUS_EQUALS: "-=">
+|
+ <TIMES_EQUALS: "*=">
+|
+ <DIVIDE_EQUALS: "/=">
+|
+ <MOD_EQUALS: "%=">
+|
+ <XOR_EQUALS: "^=">
+|
+ <OR_EQUALS: "|=">
+|
+ <AND_EQUALS: "&=">
+
|
<SEMI_COLON: ";">
|
("const"
{ type = "const"; }
)?
- (((str = <STRUCT>.image | str = <CLASS>.image) { type = type + " " + str; })?
+ (((str = <STRUCT>.image | str = <CLASS>.image | str = <UNSIGNED>.image) { type = type + " " + str; })?
(
name = ParseQualifiedName() {
if (!type.equals(""))
{
System.out.println(func);
}
+
}
String ParameterizedName() :
}
{
(str = <IDENTIFIER>.image {res = str;})
- ("<" str = <IDENTIFIER>.image { res = res + "<" + str; }
- ("," str = <IDENTIFIER>.image { res = res + ", " + str; })* ">"
+ ("<" str = Type() { res = res + "<" + str; }
+ ("," str = Type() { res = res + ", " + str; })* ">"
{ res = res + ">"; }
)?
{
FunctionHeader FuncDecl() :
{
+ ArrayList<VariableDeclaration> templateList;
String ret;
QualifiedName funcName;
ArrayList<VariableDeclaration> args;
}
{
+aa
+ (<STATIC> | <INLINE>)*
ret = Type()
funcName = ParseQualifiedName()
args = FormalParamList()
}
}
-ArrayList<String> TemplateParamList() :
+ArrayList<VariableDeclaration> TemplateParamList() :
{
- ArrayList<String> params;
- String str;
+ ArrayList<VariableDeclaration> params;
+ String type;
+ String name;
}
{
{
- params = new ArrayList<String>();
+ params = new ArrayList<VariableDeclaration>();
}
<TEMPLATE>
"<"
- (str = <IDENTIFIER>.image
- str = <IDENTIFIER>.image {params.add(str);})
+ (type = <IDENTIFIER>.image
+ name = <IDENTIFIER>.image
+ {
+ params.add(new VariableDeclaration(type, name));
+ }
+ )
- ("," str = <IDENTIFIER>.image
- str = <IDENTIFIER>.image {params.add(str);})*
+ ("," type = <IDENTIFIER>.image
+ name = <IDENTIFIER>.image
+ {
+ params.add(new VariableDeclaration(type, name));
+ }
+ )*
">"
{
//System.out.println(params);
(
LOOKAHEAD(2)
(
- t = <CONST> | t = <STRUCT> | t = <CLASS> |
+ t = <CONST> | t = <STRUCT> | t = <CLASS> | t = <UNSIGNED> |
(t = <TEMPLATE> { inTemplate = true; })|
- (header = <INCLUDE>.image {
+ t = <STATIC> | t = <INLINE> |
+ (t = <INCLUDE>
+ {
+ header = t.image;
+ newLine = true;
if (headers != null) {
headers.add(header.substring(header.lastIndexOf(' ') + 1));
}
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> { newLine = true; } )
| t = <STRING_LITERAL> | t = <CHARACTER_LITERAL> |
t = <INTEGER_LITERAL> | t = <FLOATING_POINT_LITERAL> |
(t = <DEFINE> { newLine = true; } )
)
{
- text = text + " " + t.image;
+ if (text.equals("")) {
+ text = t.image;
+ } else {
+ text = text + " " + t.image;
+ }
if (newLine) {
content.add(text);
text = "";
newLine = false;
+ inTemplate = false;
}
}
)+
{
+ if (content.size() == 0) {
+ content.add(text);
+ }
return content;
}
}
SequentialDefineSubConstruct Global_define() :
{
- ArrayList<String> initVar, defineFunc, code;
+ ArrayList<String> initVar, defineFunc, code, declareStruct;
ArrayList<ArrayList<String>> defineFuncs;
ArrayList<VariableDeclaration> declareVars;
+ ArrayList<ArrayList<String>> declareStructs;
VariableDeclaration declareVar;
}
{
{
- declareVars = null;
+ declareVars = new ArrayList<VariableDeclaration>();
initVar = null;
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() ";" {
declareVars.add(declareVar); } )*))?
(<INIT_VAR> (code = C_CPP_CODE(null) { initVar = code; } ))?
(<DEFINE_FUNC> (defineFunc = C_CPP_CODE(null) { defineFuncs.add(defineFunc); }))*
{
SequentialDefineSubConstruct res = new
- SequentialDefineSubConstruct(declareVars, initVar, defineFuncs);
+ SequentialDefineSubConstruct(declareStructs, declareVars, initVar, defineFuncs);
//System.out.println(res);
return res;
}
{
hbConditionLabel = "";
}
- interfaceName = <IDENTIFIER>.image (<OPEN_BRACKET> hbConditionLabel =
- <IDENTIFIER>.image <CLOSE_BRACKET>)?
+ interfaceName = <IDENTIFIER>.image (<OPEN_PAREN> hbConditionLabel =
+ <IDENTIFIER>.image <CLOSE_PAREN>)?
{
return new ConditionalInterface(interfaceName, hbConditionLabel);
}
}
{
(clusterName= <IDENTIFIER>.image)
- <EQUALS> <OPEN_PAREN>
+ <EQUALS> <OPEN_BRACE>
(condInterface = Conditional_interface()
{ inst.addInterface2Cluster(clusterName, condInterface); }
)
(<COMMA> condInterface = Conditional_interface()
{ inst.addInterface2Cluster(clusterName, condInterface); }
)*
- <CLOSE_PAREN>
+ <CLOSE_BRACE>
}
void Interface_clusters(GlobalConstruct inst) :
{
{
res = null;
- action = null;
+ action = new ArrayList<String>();
condition = "";
idCode = "";
check = "";
postCheck = "";
commitPointSet = new ArrayList<String>();
hbConditions = new HashMap<String, String>();
-
- action = null;
- postAction = null;
+ postAction = new ArrayList<String>();
}
<BEGIN>
<INTERFACE> (interfaceName = <IDENTIFIER>.image)
(<CONDITION> (content = C_CPP_CODE(null) { condition = stringArray2String(content); }))?
(
<HB_CONDITION>
- (hbLabel = <IDENTIFIER>.image)
+ (hbLabel = <IDENTIFIER>.image) <DOUBLECOLON>
(content = C_CPP_CODE(null) { hbCondition = stringArray2String(content); })
{
if (hbConditions.containsKey(hbLabel)) {