From 2f81f8266186dd6ef3666343c3ae7dd0537bb06e Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Wed, 8 May 2024 10:20:55 +0200 Subject: [PATCH] parser implement localvardeclaration --- Test/TestParser.hs | 10 +++++++++- src/Parser/JavaParser.y | 17 +++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index b078def..1a3484b 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -54,6 +54,12 @@ testClassWithConstructor = TestCase $ parse [CLASS,IDENTIFIER "WithConstructor",LBRACKET,IDENTIFIER "WithConstructor",LBRACE,RBRACE,LBRACKET,RBRACKET,RBRACKET] +testEmptyBlock = TestCase $ assertEqual "expect empty block" (Block []) $ parseBlock [LBRACKET,RBRACKET] +testBlockWithLocalVarDecl = TestCase $ + assertEqual "expect block with local var delcaration" (Block [LocalVariableDeclaration $ VariableDeclaration "int" "localvar" Nothing]) $ + parseBlock [LBRACKET,INT,IDENTIFIER "localvar",SEMICOLON,RBRACKET] + + tests = TestList [ testSingleEmptyClass, testTwoEmptyClasses, @@ -68,5 +74,7 @@ tests = TestList [ testEmptyMethodWithParam, testEmptyMethodWithParams, testClassWithMethodAndField, - testClassWithConstructor + testClassWithConstructor, + testEmptyBlock, + testBlockWithLocalVarDecl ] \ No newline at end of file diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index d323009..a0a7a3c 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -1,10 +1,11 @@ { -module Parser.JavaParser (parse) where +module Parser.JavaParser (parse, parseBlock) where import Ast import Parser.Lexer } %name parse +%name parseBlock block %tokentype { Token } %error { parseError } %errorhandlertype explist @@ -135,7 +136,7 @@ fielddeclaration : type variabledeclarators SEMICOLON { FieldDecls $ map (conve methoddeclaration : methodheader methodbody { case $1 of (returnType, (name, parameters)) -> MethodDecl (MethodDeclaration returnType name parameters $2) } block : LBRACKET RBRACKET { Block [] } - -- | LBRACKET blockstatements RBRACKET { } + | LBRACKET blockstatements RBRACKET { $2 } constructordeclarator : simplename LBRACE RBRACE { ($1, []) } | simplename LBRACE formalparameterlist RBRACE { ($1, $3) } @@ -159,8 +160,8 @@ variabledeclarators : variabledeclarator { [$1] } methodbody : block { $1 } | SEMICOLON { Block [] } -blockstatements : blockstatement { } - | blockstatements blockstatement { } +blockstatements : blockstatement { Block $1 } + -- | blockstatements blockstatement { } formalparameterlist : formalparameter { [$1] } | formalparameterlist COMMA formalparameter { $1 ++ [$3] } @@ -183,8 +184,8 @@ referencetype : classorinterfacetype { $1 } variabledeclarator : variabledeclaratorid { Declarator $1 Nothing } -- | variabledeclaratorid ASSIGN variableinitializer { Declarator $1 Nothing } -- TODO -blockstatement : localvariabledeclarationstatement { } - | statement { } +blockstatement : localvariabledeclarationstatement { $1 } + -- | statement { } formalparameter : type variabledeclaratorid { ParameterDeclaration $1 $2 } @@ -197,7 +198,7 @@ variabledeclaratorid : IDENTIFIER { $1 } variableinitializer : expression { } -localvariabledeclarationstatement : localvariabledeclaration SEMICOLON { } +localvariabledeclarationstatement : localvariabledeclaration SEMICOLON { $1 } statement : statementwithouttrailingsubstatement{ } | ifthenstatement { } @@ -210,7 +211,7 @@ expression : assignmentexpression { } integraltype : INT { "int" } | CHAR { "char" } -localvariabledeclaration : type variabledeclarators { } +localvariabledeclaration : type variabledeclarators { map LocalVariableDeclaration $ map (convertDeclarator $1) $2 } statementwithouttrailingsubstatement : block { } | emptystatement { }