From 158197b44007e20b4aeb633c17bd96329646564b Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Tue, 7 May 2024 15:03:04 +0200 Subject: [PATCH] parser implement methods with params --- Test/TestParser.hs | 14 +++++++++++++- src/Parser/JavaParser.y | 8 ++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index 85b899b..8e1aaa7 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -40,6 +40,15 @@ testEmptyPrivateMethod = TestCase $ testEmptyVoidMethod = TestCase $ assertEqual "expect class with method" [Class "WithMethod" [MethodDeclaration "void" "foo" [] (Block [])] []] $ parse [CLASS,IDENTIFIER "WithMethod",LBRACKET,VOID,IDENTIFIER "foo",LBRACE,RBRACE,LBRACKET,RBRACKET,RBRACKET] +testEmptyMethodWithParam = TestCase $ + assertEqual "expect class with method with param" [Class "WithParam" [MethodDeclaration "void" "foo" [ParameterDeclaration "int" "param"] (Block [])] []] $ + parse [CLASS,IDENTIFIER "WithParam",LBRACKET,VOID,IDENTIFIER "foo",LBRACE,INT,IDENTIFIER "param",RBRACE,SEMICOLON,RBRACKET] +testEmptyMethodWithParams = TestCase $ + assertEqual "expect class with multiple params" [Class "WithParams" [MethodDeclaration "void" "foo" [ParameterDeclaration "int" "p1",ParameterDeclaration "Custom" "p2"] (Block [])] []] $ + parse [CLASS,IDENTIFIER "WithParams",LBRACKET,VOID,IDENTIFIER "foo",LBRACE,INT,IDENTIFIER "p1",COMMA,IDENTIFIER "Custom",IDENTIFIER "p2",RBRACE,SEMICOLON,RBRACKET] +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] tests = TestList [ @@ -52,5 +61,8 @@ tests = TestList [ testWithModifier, testEmptyMethod, testEmptyPrivateMethod, - testEmptyVoidMethod + testEmptyVoidMethod, + testEmptyMethodWithParam, + testEmptyMethodWithParams, + testClassWithMethodAndField ] \ No newline at end of file diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index ec3788c..8edb2df 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -161,8 +161,8 @@ methodbody : block { $1 } blockstatements : blockstatement { } | blockstatements blockstatement { } -formalparameterlist : formalparameter { } - | formalparameterlist COMMA formalparameter{ } +formalparameterlist : formalparameter { [$1] } + | formalparameterlist COMMA formalparameter { $1 ++ [$3] } explicitconstructorinvocation : THIS LBRACE RBRACE SEMICOLON { } | THIS LBRACE argumentlist RBRACE SEMICOLON { } @@ -171,7 +171,7 @@ classtypelist : classtype { } | classtypelist COMMA classtype { } methoddeclarator : IDENTIFIER LBRACE RBRACE { ($1, []) } - -- | IDENTIFIER LBRACE formalparameterlist RBRACE { } + | IDENTIFIER LBRACE formalparameterlist RBRACE { ($1, $3) } primitivetype : BOOLEAN { "boolean" } | numerictype { $1 } @@ -185,7 +185,7 @@ variabledeclarator : variabledeclaratorid { Declarator $1 Nothing } blockstatement : localvariabledeclarationstatement { } | statement { } -formalparameter : type variabledeclaratorid { } +formalparameter : type variabledeclaratorid { ParameterDeclaration $1 $2 } argumentlist : expression { } | argumentlist COMMA expression { }