From e8151ad2f09b609a1e99d4be048c7fe069b5e3e9 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Wed, 8 May 2024 10:37:40 +0200 Subject: [PATCH] parser implement nested blocks --- Test/TestParser.hs | 6 +++++- src/Parser/JavaParser.y | 18 +++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index 28bdd00..c000d6b 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -61,6 +61,9 @@ testBlockWithLocalVarDecl = TestCase $ testBlockWithMultipleLocalVarDecls = TestCase $ assertEqual "expect block with multiple local var declarations" (Block [LocalVariableDeclaration $ VariableDeclaration "int" "var1" Nothing, LocalVariableDeclaration $ VariableDeclaration "boolean" "var2" Nothing]) $ parseBlock [LBRACKET,INT,IDENTIFIER "var1",SEMICOLON,BOOLEAN,IDENTIFIER "var2",SEMICOLON,RBRACKET] +testNestedBlocks = TestCase $ + assertEqual "expect block with block inside" (Block [Block []]) $ + parseBlock [LBRACKET,LBRACKET,RBRACKET,RBRACKET] tests = TestList [ @@ -80,5 +83,6 @@ tests = TestList [ testClassWithConstructor, testEmptyBlock, testBlockWithLocalVarDecl, - testBlockWithMultipleLocalVarDecls + testBlockWithMultipleLocalVarDecls, + testNestedBlocks ] \ No newline at end of file diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index 5c423b5..fe309da 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -185,7 +185,7 @@ variabledeclarator : variabledeclaratorid { Declarator $1 Nothing } -- | variabledeclaratorid ASSIGN variableinitializer { Declarator $1 Nothing } -- TODO blockstatement : localvariabledeclarationstatement { $1 } - -- | statement { } + | statement { [$1] } formalparameter : type variabledeclaratorid { ParameterDeclaration $1 $2 } @@ -200,10 +200,10 @@ variableinitializer : expression { } localvariabledeclarationstatement : localvariabledeclaration SEMICOLON { $1 } -statement : statementwithouttrailingsubstatement{ } - | ifthenstatement { } - | ifthenelsestatement { } - | whilestatement { } +statement : statementwithouttrailingsubstatement{ $1 } + -- | ifthenstatement { } + -- | ifthenelsestatement { } + -- | whilestatement { } expression : assignmentexpression { } @@ -213,10 +213,10 @@ integraltype : INT { "int" } localvariabledeclaration : type variabledeclarators { map LocalVariableDeclaration $ map (convertDeclarator $1) $2 } -statementwithouttrailingsubstatement : block { } - | emptystatement { } - | expressionstatement { } - | returnstatement { } +statementwithouttrailingsubstatement : block { $1 } + -- | emptystatement { } + -- | expressionstatement { } + -- | returnstatement { } ifthenstatement : IF LBRACE expression RBRACE statement { }