parser implement localvardeclaration

This commit is contained in:
Marvin Schlegel 2024-05-08 10:20:55 +02:00
parent ea18431b77
commit 58367a95e6
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] 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 [ tests = TestList [
testSingleEmptyClass, testSingleEmptyClass,
testTwoEmptyClasses, testTwoEmptyClasses,
@ -68,5 +74,7 @@ tests = TestList [
testEmptyMethodWithParam, testEmptyMethodWithParam,
testEmptyMethodWithParams, testEmptyMethodWithParams,
testClassWithMethodAndField, 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 Ast
import Parser.Lexer import Parser.Lexer
} }
%name parse %name parse
%name parseBlock block
%tokentype { Token } %tokentype { Token }
%error { parseError } %error { parseError }
%errorhandlertype explist %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) } methoddeclaration : methodheader methodbody { case $1 of (returnType, (name, parameters)) -> MethodDecl (MethodDeclaration returnType name parameters $2) }
block : LBRACKET RBRACKET { Block [] } block : LBRACKET RBRACKET { Block [] }
-- | LBRACKET blockstatements RBRACKET { } | LBRACKET blockstatements RBRACKET { $2 }
constructordeclarator : simplename LBRACE RBRACE { ($1, []) } constructordeclarator : simplename LBRACE RBRACE { ($1, []) }
| simplename LBRACE formalparameterlist RBRACE { ($1, $3) } | simplename LBRACE formalparameterlist RBRACE { ($1, $3) }
@ -159,8 +160,8 @@ variabledeclarators : variabledeclarator { [$1] }
methodbody : block { $1 } methodbody : block { $1 }
| SEMICOLON { Block [] } | SEMICOLON { Block [] }
blockstatements : blockstatement { } blockstatements : blockstatement { Block $1 }
| blockstatements blockstatement { } -- | blockstatements blockstatement { }
formalparameterlist : formalparameter { [$1] } formalparameterlist : formalparameter { [$1] }
| formalparameterlist COMMA formalparameter { $1 ++ [$3] } | formalparameterlist COMMA formalparameter { $1 ++ [$3] }
@ -183,8 +184,8 @@ referencetype : classorinterfacetype { $1 }
variabledeclarator : variabledeclaratorid { Declarator $1 Nothing } variabledeclarator : variabledeclaratorid { Declarator $1 Nothing }
-- | variabledeclaratorid ASSIGN variableinitializer { Declarator $1 Nothing } -- TODO -- | variabledeclaratorid ASSIGN variableinitializer { Declarator $1 Nothing } -- TODO
blockstatement : localvariabledeclarationstatement { } blockstatement : localvariabledeclarationstatement { $1 }
| statement { } -- | statement { }
formalparameter : type variabledeclaratorid { ParameterDeclaration $1 $2 } formalparameter : type variabledeclaratorid { ParameterDeclaration $1 $2 }
@ -197,7 +198,7 @@ variabledeclaratorid : IDENTIFIER { $1 }
variableinitializer : expression { } variableinitializer : expression { }
localvariabledeclarationstatement : localvariabledeclaration SEMICOLON { } localvariabledeclarationstatement : localvariabledeclaration SEMICOLON { $1 }
statement : statementwithouttrailingsubstatement{ } statement : statementwithouttrailingsubstatement{ }
| ifthenstatement { } | ifthenstatement { }
@ -210,7 +211,7 @@ expression : assignmentexpression { }
integraltype : INT { "int" } integraltype : INT { "int" }
| CHAR { "char" } | CHAR { "char" }
localvariabledeclaration : type variabledeclarators { } localvariabledeclaration : type variabledeclarators { map LocalVariableDeclaration $ map (convertDeclarator $1) $2 }
statementwithouttrailingsubstatement : block { } statementwithouttrailingsubstatement : block { }
| emptystatement { } | emptystatement { }