diff --git a/Test/TestParser.hs b/Test/TestParser.hs index 63f4dc1..8e8db60 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -52,6 +52,12 @@ testClassWithMethodAndField = TestCase $ testClassWithConstructor = TestCase $ assertEqual "expect class with constructor" [Class "WithConstructor" [MethodDeclaration "void" "" [] (Block [])] []] $ parse [CLASS,IDENTIFIER "WithConstructor",LBRACKET,IDENTIFIER "WithConstructor",LBRACE,RBRACE,LBRACKET,RBRACKET,RBRACKET] +testConstructorWithParams = TestCase $ + assertEqual "expect constructor with params" [Class "WithParams" [MethodDeclaration "void" "" [ParameterDeclaration "int" "p1"] (Block [])] []] $ + parse [CLASS,IDENTIFIER "WithParams",LBRACKET,IDENTIFIER "WithParams",LBRACE,INT,IDENTIFIER "p1",RBRACE,LBRACKET,RBRACKET,RBRACKET] +testConstructorWithStatements = TestCase $ + assertEqual "expect constructor with statement" [Class "WithConstructor" [MethodDeclaration "void" "" [] (Block [Return Nothing])] []] $ + parse [CLASS,IDENTIFIER "WithConstructor",LBRACKET,IDENTIFIER "WithConstructor",LBRACE,RBRACE,LBRACKET,RETURN,SEMICOLON,RBRACKET,RBRACKET] testEmptyBlock = TestCase $ assertEqual "expect empty block" [Block []] $ parseStatement [LBRACKET,RBRACKET] @@ -220,6 +226,8 @@ tests = TestList [ testEmptyMethodWithParams, testClassWithMethodAndField, testClassWithConstructor, + testConstructorWithParams, + testConstructorWithStatements, testEmptyBlock, testBlockWithLocalVarDecl, testBlockWithMultipleLocalVarDecls, diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index d51b376..45206a5 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -127,8 +127,8 @@ classorinterfacetype : simplename { $1 } classmemberdeclaration : fielddeclaration { $1 } | methoddeclaration { $1 } -constructordeclaration : constructordeclarator constructorbody { case $1 of (classname, parameters) -> MethodDecl $ MethodDeclaration "void" "" parameters $2 } - | modifiers constructordeclarator constructorbody { case $2 of (classname, parameters) -> MethodDecl $ MethodDeclaration "void" "" parameters $3 } +constructordeclaration : constructordeclarator constructorbody { MethodDecl $ MethodDeclaration "void" "" $1 $2 } + | modifiers constructordeclarator constructorbody { MethodDecl $ MethodDeclaration "void" "" $2 $3 } fielddeclaration : type variabledeclarators SEMICOLON { FieldDecls $ map (convertDeclarator $1) $2 } | modifiers type variabledeclarators SEMICOLON { FieldDecls $ map (convertDeclarator $2) $3 } @@ -138,12 +138,12 @@ methoddeclaration : methodheader methodbody { case $1 of (returnType, (name, par block : LBRACKET RBRACKET { Block [] } | LBRACKET blockstatements RBRACKET { Block $2 } -constructordeclarator : simplename LBRACE RBRACE { ($1, []) } - | simplename LBRACE formalparameterlist RBRACE { ($1, $3) } +constructordeclarator : simplename LBRACE RBRACE { [] } + | simplename LBRACE formalparameterlist RBRACE { $3 } constructorbody : LBRACKET RBRACKET { Block [] } -- | LBRACKET explicitconstructorinvocation RBRACKET { } - -- | LBRACKET blockstatements RBRACKET { } + | LBRACKET blockstatements RBRACKET { Block $2 } -- | LBRACKET explicitconstructorinvocation blockstatements RBRACKET { } methodheader : type methoddeclarator { ($1, $2) }