parser implement localvardeclaration

This commit is contained in:
Marvin Schlegel 2024-05-08 10:20:55 +02:00
parent fecc7eceaa
commit 2f81f82661
2 changed files with 18 additions and 9 deletions

View File

@ -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
]

View File

@ -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 { }