diff --git a/Test/TestParser.hs b/Test/TestParser.hs index 8e1aaa7..b078def 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -49,6 +49,9 @@ testEmptyMethodWithParams = TestCase $ testClassWithMethodAndField = TestCase $ assertEqual "expect class with method and field" [Class "WithMethodAndField" [MethodDeclaration "void" "foo" [] (Block []), MethodDeclaration "int" "bar" [] (Block [])] [VariableDeclaration "int" "value" Nothing]] $ parse [CLASS,IDENTIFIER "WithMethodAndField",LBRACKET,VOID,IDENTIFIER "foo",LBRACE,RBRACE,LBRACKET,RBRACKET,INT,IDENTIFIER "value",SEMICOLON,INT,IDENTIFIER "bar",LBRACE,RBRACE,SEMICOLON,RBRACKET] +testClassWithConstructor = TestCase $ + assertEqual "expect class with constructor" [Class "WithConstructor" [MethodDeclaration "WithConstructor" "" [] (Block [])] []] $ + parse [CLASS,IDENTIFIER "WithConstructor",LBRACKET,IDENTIFIER "WithConstructor",LBRACE,RBRACE,LBRACKET,RBRACKET,RBRACKET] tests = TestList [ @@ -64,5 +67,6 @@ tests = TestList [ testEmptyVoidMethod, testEmptyMethodWithParam, testEmptyMethodWithParams, - testClassWithMethodAndField + testClassWithMethodAndField, + testClassWithConstructor ] \ No newline at end of file diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index 6c249ac..d323009 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -119,15 +119,15 @@ modifier : PUBLIC { } classtype : classorinterfacetype{ } classbodydeclaration : classmemberdeclaration { $1 } - -- | constructordeclaration { FieldDecl $ VariableDeclaration "int" "a" Nothing } -- TODO + | constructordeclaration { $1 } classorinterfacetype : name { $1 } classmemberdeclaration : fielddeclaration { $1 } | methoddeclaration { $1 } -constructordeclaration : constructordeclarator constructorbody { } - | modifiers constructordeclarator constructorbody { } +constructordeclaration : constructordeclarator constructorbody { case $1 of (classname, parameters) -> MethodDecl $ MethodDeclaration classname "" parameters $2 } + | modifiers constructordeclarator constructorbody { case $2 of (classname, parameters) -> MethodDecl $ MethodDeclaration classname "" parameters $3 } fielddeclaration : type variabledeclarators SEMICOLON { FieldDecls $ map (convertDeclarator $1) $2 } | modifiers type variabledeclarators SEMICOLON { FieldDecls $ map (convertDeclarator $2) $3 } @@ -137,13 +137,13 @@ methoddeclaration : methodheader methodbody { case $1 of (returnType, (name, par block : LBRACKET RBRACKET { Block [] } -- | LBRACKET blockstatements RBRACKET { } -constructordeclarator : simplename LBRACE RBRACE { } - | simplename LBRACE formalparameterlist RBRACE { } +constructordeclarator : simplename LBRACE RBRACE { ($1, []) } + | simplename LBRACE formalparameterlist RBRACE { ($1, $3) } -constructorbody : LBRACKET RBRACKET { } - | LBRACKET explicitconstructorinvocation RBRACKET { } - | LBRACKET blockstatements RBRACKET { } - | LBRACKET explicitconstructorinvocation blockstatements RBRACKET { } +constructorbody : LBRACKET RBRACKET { Block [] } + -- | LBRACKET explicitconstructorinvocation RBRACKET { } + -- | LBRACKET blockstatements RBRACKET { } + -- | LBRACKET explicitconstructorinvocation blockstatements RBRACKET { } methodheader : type methoddeclarator { ($1, $2) } | modifiers type methoddeclarator { ($2, $3) }