From d3ea0d6d7bc0d3e19c32bdc9cc78422f5dbee270 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Mon, 6 May 2024 23:34:03 +0200 Subject: [PATCH] parser add empty methods --- Test/TestParser.hs | 6 +++++- src/Parser/JavaParser.y | 24 ++++++++++++------------ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index b979794..4e0694b 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -30,6 +30,9 @@ testMultipleDeclarations = TestCase $ testWithModifier = TestCase $ assertEqual "expect class with int field" [Class "WithInt" [] [VariableDeclaration "int" "value" Nothing]] $ parse [ABSTRACT,CLASS,IDENTIFIER "WithInt",LBRACKET,PUBLIC,INT,IDENTIFIER "value",SEMICOLON,RBRACKET] +testMethod = TestCase $ + assertEqual "expect class with method" [Class "WithMethod" [MethodDeclaration "int" "foo" [] (Block [])] []] $ + parse [CLASS,IDENTIFIER "WithMethod",LBRACKET,INT,IDENTIFIER "foo",LBRACE,RBRACE,SEMICOLON,RBRACKET] tests = TestList [ @@ -39,5 +42,6 @@ tests = TestList [ testIntField, testCustomTypeField, testMultipleDeclarations, - testWithModifier + testWithModifier, + testMethod ] \ No newline at end of file diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index 8d772eb..d48b7e2 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -123,7 +123,7 @@ classbodydeclaration : classmemberdeclaration { $1 } classorinterfacetype : name { $1 } classmemberdeclaration : fielddeclaration { $1 } - -- | methoddeclaration { } + | methoddeclaration { $1 } constructordeclaration : constructordeclarator constructorbody { } | modifiers constructordeclarator constructorbody { } @@ -131,10 +131,10 @@ constructordeclaration : constructordeclarator constructorbody { } fielddeclaration : type variabledeclarators SEMICOLON { FieldDecls $ map (convertDeclarator $1) $2 } | modifiers type variabledeclarators SEMICOLON { FieldDecls $ map (convertDeclarator $2) $3 } -methoddeclaration : methodheader methodbody { } +methoddeclaration : methodheader methodbody { case $1 of (returnType, (name, parameters)) -> MethodDecl (MethodDeclaration returnType name parameters $2) } -block : LBRACKET RBRACKET { } - | LBRACKET blockstatements RBRACKET { } +block : LBRACKET RBRACKET { Block [] } + -- | LBRACKET blockstatements RBRACKET { } constructordeclarator : simplename LBRACE RBRACE { } | simplename LBRACE formalparameterlist RBRACE { } @@ -144,10 +144,10 @@ constructorbody : LBRACKET RBRACKET { } | LBRACKET blockstatements RBRACKET { } | LBRACKET explicitconstructorinvocation blockstatements RBRACKET { } -methodheader : type methoddeclarator { } - | modifiers type methoddeclarator { } - | VOID methoddeclarator { } - | modifiers VOID methoddeclarator { } +methodheader : type methoddeclarator { ($1, $2) } + -- | modifiers type methoddeclarator { } + -- | VOID methoddeclarator { } + -- | modifiers VOID methoddeclarator { } type : primitivetype { $1 } | referencetype { $1 } @@ -155,8 +155,8 @@ type : primitivetype { $1 } variabledeclarators : variabledeclarator { [$1] } | variabledeclarators COMMA variabledeclarator { $1 ++ [$3] } -methodbody : block { } - | SEMICOLON { } +methodbody : block { $1 } + | SEMICOLON { Block [] } blockstatements : blockstatement { } | blockstatements blockstatement { } @@ -170,8 +170,8 @@ explicitconstructorinvocation : THIS LBRACE RBRACE SEMICOLON { } classtypelist : classtype { } | classtypelist COMMA classtype { } -methoddeclarator : IDENTIFIER LBRACE RBRACE { } - | IDENTIFIER LBRACE formalparameterlist RBRACE { } +methoddeclarator : IDENTIFIER LBRACE RBRACE { ($1, []) } + -- | IDENTIFIER LBRACE formalparameterlist RBRACE { } primitivetype : BOOLEAN { "boolean" } | numerictype { $1 }