From 301b87c9ac878827823b46543f5b6602fba24e8a Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Mon, 6 May 2024 10:33:37 +0200 Subject: [PATCH] parser implement multiple declarations --- Test/TestParser.hs | 10 ++++++++-- src/Parser/JavaParser.y | 12 ++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index 3fd30f0..35f25ab 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -21,7 +21,12 @@ testIntField = TestCase $ testCustomTypeField = TestCase $ assertEqual "expect class with foo field" [Class "WithFoo" [] [VariableDeclaration "Foo" "value" Nothing]] $ parse [CLASS,IDENTIFIER "WithFoo",LBRACKET,IDENTIFIER "Foo",IDENTIFIER "value",SEMICOLON,RBRACKET] - +testMultipleDeclarationSameLine = TestCase $ + assertEqual "expect class with two int fields" [Class "TwoInts" [] [VariableDeclaration "int" "num1" Nothing, VariableDeclaration "int" "num2" Nothing]] $ + parse [CLASS,IDENTIFIER "TwoInts",LBRACKET,INT,IDENTIFIER "num1",COMMA,IDENTIFIER "num2",SEMICOLON,RBRACKET] +testMultipleDeclarations = TestCase $ + assertEqual "expect class with int and char field" [Class "Multiple" [] [VariableDeclaration "int" "value" Nothing, VariableDeclaration "char" "letter" Nothing]] $ + parse [CLASS,IDENTIFIER "Multiple",LBRACKET,INT,IDENTIFIER "value",SEMICOLON,CHAR,IDENTIFIER "letter",SEMICOLON,RBRACKET] tests = TestList [ @@ -29,5 +34,6 @@ tests = TestList [ testTwoEmptyClasses, testBooleanField, testIntField, - testCustomTypeField + testCustomTypeField, + testMultipleDeclarations ] \ No newline at end of file diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index bcfec2f..69fe9f2 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -103,11 +103,11 @@ classbodydeclarations : classbodydeclaration { MethodDecl method -> ([method], []) FieldDecls fields -> ([], fields) } - -- | classbodydeclarations classbodydeclaration { - -- case ($1, $2) of - -- ((methods, fields), MethodDecl method) -> ((methods ++ [method]), fields) - -- ((methods, fields), FieldDecl field) -> (methods, (fields ++ [field])) - -- } + | classbodydeclarations classbodydeclaration { + case ($1, $2) of + ((methods, fields), MethodDecl method) -> ((methods ++ [method]), fields) + ((methods, fields), FieldDecls newFields) -> (methods, (fields ++ newFields)) + } modifier : PUBLIC { } | PROTECTED { } @@ -153,7 +153,7 @@ type : primitivetype { $1 } | referencetype { $1 } variabledeclarators : variabledeclarator { [$1] } - -- | variabledeclarators COMMA variabledeclarator { $1 ++ [$3] } + | variabledeclarators COMMA variabledeclarator { $1 ++ [$3] } methodbody : block { } | SEMICOLON { }