From e04c475c5533db5eb8479e745102dc8c2bb858c6 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Mon, 6 May 2024 10:16:57 +0200 Subject: [PATCH 01/14] parser add custom type fields --- Test/TestParser.hs | 7 ++++++- src/Parser/JavaParser.y | 12 ++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index ce3f2cc..3fd30f0 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -18,11 +18,16 @@ testBooleanField = TestCase $ testIntField = TestCase $ assertEqual "expect class with int field" [Class "WithInt" [] [VariableDeclaration "int" "value" Nothing]] $ parse [CLASS,IDENTIFIER "WithInt",LBRACKET,INT,IDENTIFIER "value",SEMICOLON,RBRACKET] +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] + tests = TestList [ testSingleEmptyClass, testTwoEmptyClasses, testBooleanField, - testIntField + testIntField, + testCustomTypeField ] \ No newline at end of file diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index bfe74b2..bcfec2f 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -80,14 +80,14 @@ compilationunit : typedeclarations { $1 } typedeclarations : typedeclaration { [$1] } | typedeclarations typedeclaration { $1 ++ [$2] } -name : qualifiedname { } - | simplename { } +name : simplename { $1 } + -- | qualifiedname { } typedeclaration : classdeclaration { $1 } qualifiedname : name DOT IDENTIFIER { } -simplename : IDENTIFIER { } +simplename : IDENTIFIER { $1 } classdeclaration : CLASS IDENTIFIER classbody { case $3 of (methods, fields) -> Class $2 methods fields } -- | modifiers CLASS IDENTIFIER classbody { case $4 of (methods, fields) -> Class $3 methods fields } @@ -120,7 +120,7 @@ classtype : classorinterfacetype{ } classbodydeclaration : classmemberdeclaration { $1 } -- | constructordeclaration { FieldDecl $ VariableDeclaration "int" "a" Nothing } -- TODO -classorinterfacetype : name{ } +classorinterfacetype : name { $1 } classmemberdeclaration : fielddeclaration { $1 } -- | methoddeclaration { } @@ -150,7 +150,7 @@ methodheader : type methoddeclarator { } | modifiers VOID methoddeclarator { } type : primitivetype { $1 } - -- | referencetype { } + | referencetype { $1 } variabledeclarators : variabledeclarator { [$1] } -- | variabledeclarators COMMA variabledeclarator { $1 ++ [$3] } @@ -176,7 +176,7 @@ methoddeclarator : IDENTIFIER LBRACE RBRACE { } primitivetype : BOOLEAN { "boolean" } | numerictype { $1 } -referencetype : classorinterfacetype { } +referencetype : classorinterfacetype { $1 } variabledeclarator : variabledeclaratorid { Declarator $1 Nothing } From 32cce1f137d4ab41439fa3648ce866a8cb0a4b17 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Mon, 6 May 2024 10:33:37 +0200 Subject: [PATCH 02/14] 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 { } From bea9a039a8a60a93f5763d7e6f2d59ac9ff10e6c Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Mon, 6 May 2024 10:09:50 +0200 Subject: [PATCH 03/14] parser add modifier --- Test/TestParser.hs | 6 +++++- src/Parser/JavaParser.y | 7 ++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index 35f25ab..b979794 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -27,6 +27,9 @@ testMultipleDeclarationSameLine = TestCase $ 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] +testWithModifier = TestCase $ + assertEqual "expect class with int field" [Class "WithInt" [] [VariableDeclaration "int" "value" Nothing]] $ + parse [ABSTRACT,CLASS,IDENTIFIER "WithInt",LBRACKET,PUBLIC,INT,IDENTIFIER "value",SEMICOLON,RBRACKET] tests = TestList [ @@ -35,5 +38,6 @@ tests = TestList [ testBooleanField, testIntField, testCustomTypeField, - testMultipleDeclarations + testMultipleDeclarations, + testWithModifier ] \ No newline at end of file diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index 69fe9f2..8d772eb 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -90,7 +90,7 @@ qualifiedname : name DOT IDENTIFIER { } simplename : IDENTIFIER { $1 } classdeclaration : CLASS IDENTIFIER classbody { case $3 of (methods, fields) -> Class $2 methods fields } - -- | modifiers CLASS IDENTIFIER classbody { case $4 of (methods, fields) -> Class $3 methods fields } + | modifiers CLASS IDENTIFIER classbody { case $4 of (methods, fields) -> Class $3 methods fields } classbody : LBRACKET RBRACKET { ([], []) } | LBRACKET classbodydeclarations RBRACKET { $2 } @@ -129,7 +129,7 @@ constructordeclaration : constructordeclarator constructorbody { } | modifiers constructordeclarator constructorbody { } fielddeclaration : type variabledeclarators SEMICOLON { FieldDecls $ map (convertDeclarator $1) $2 } - -- | modifiers type variabledeclarators SEMICOLON {} + | modifiers type variabledeclarators SEMICOLON { FieldDecls $ map (convertDeclarator $2) $3 } methoddeclaration : methodheader methodbody { } @@ -365,9 +365,6 @@ data MethodOrFieldDeclaration = MethodDecl MethodDeclaration data Declarator = Declarator Identifier (Maybe Expression) --- convertDeclaratorList :: [DataType] -> MethodOrFieldDeclaration --- convertDeclaratorList = FieldDecls $ map - convertDeclarator :: DataType -> Declarator -> VariableDeclaration convertDeclarator dataType (Declarator id assigment) = VariableDeclaration dataType id assigment From d3ea0d6d7bc0d3e19c32bdc9cc78422f5dbee270 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Mon, 6 May 2024 23:34:03 +0200 Subject: [PATCH 04/14] parser add empty methods --- Test/TestParser.hs | 6 +++++- src/Parser/JavaParser.y | 24 ++++++++++++------------ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index b979794..4e0694b 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -30,6 +30,9 @@ testMultipleDeclarations = TestCase $ testWithModifier = TestCase $ assertEqual "expect class with int field" [Class "WithInt" [] [VariableDeclaration "int" "value" Nothing]] $ parse [ABSTRACT,CLASS,IDENTIFIER "WithInt",LBRACKET,PUBLIC,INT,IDENTIFIER "value",SEMICOLON,RBRACKET] +testMethod = TestCase $ + assertEqual "expect class with method" [Class "WithMethod" [MethodDeclaration "int" "foo" [] (Block [])] []] $ + parse [CLASS,IDENTIFIER "WithMethod",LBRACKET,INT,IDENTIFIER "foo",LBRACE,RBRACE,SEMICOLON,RBRACKET] tests = TestList [ @@ -39,5 +42,6 @@ tests = TestList [ testIntField, testCustomTypeField, testMultipleDeclarations, - testWithModifier + testWithModifier, + testMethod ] \ No newline at end of file diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index 8d772eb..d48b7e2 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -123,7 +123,7 @@ classbodydeclaration : classmemberdeclaration { $1 } classorinterfacetype : name { $1 } classmemberdeclaration : fielddeclaration { $1 } - -- | methoddeclaration { } + | methoddeclaration { $1 } constructordeclaration : constructordeclarator constructorbody { } | modifiers constructordeclarator constructorbody { } @@ -131,10 +131,10 @@ constructordeclaration : constructordeclarator constructorbody { } fielddeclaration : type variabledeclarators SEMICOLON { FieldDecls $ map (convertDeclarator $1) $2 } | modifiers type variabledeclarators SEMICOLON { FieldDecls $ map (convertDeclarator $2) $3 } -methoddeclaration : methodheader methodbody { } +methoddeclaration : methodheader methodbody { case $1 of (returnType, (name, parameters)) -> MethodDecl (MethodDeclaration returnType name parameters $2) } -block : LBRACKET RBRACKET { } - | LBRACKET blockstatements RBRACKET { } +block : LBRACKET RBRACKET { Block [] } + -- | LBRACKET blockstatements RBRACKET { } constructordeclarator : simplename LBRACE RBRACE { } | simplename LBRACE formalparameterlist RBRACE { } @@ -144,10 +144,10 @@ constructorbody : LBRACKET RBRACKET { } | LBRACKET blockstatements RBRACKET { } | LBRACKET explicitconstructorinvocation blockstatements RBRACKET { } -methodheader : type methoddeclarator { } - | modifiers type methoddeclarator { } - | VOID methoddeclarator { } - | modifiers VOID methoddeclarator { } +methodheader : type methoddeclarator { ($1, $2) } + -- | modifiers type methoddeclarator { } + -- | VOID methoddeclarator { } + -- | modifiers VOID methoddeclarator { } type : primitivetype { $1 } | referencetype { $1 } @@ -155,8 +155,8 @@ type : primitivetype { $1 } variabledeclarators : variabledeclarator { [$1] } | variabledeclarators COMMA variabledeclarator { $1 ++ [$3] } -methodbody : block { } - | SEMICOLON { } +methodbody : block { $1 } + | SEMICOLON { Block [] } blockstatements : blockstatement { } | blockstatements blockstatement { } @@ -170,8 +170,8 @@ explicitconstructorinvocation : THIS LBRACE RBRACE SEMICOLON { } classtypelist : classtype { } | classtypelist COMMA classtype { } -methoddeclarator : IDENTIFIER LBRACE RBRACE { } - | IDENTIFIER LBRACE formalparameterlist RBRACE { } +methoddeclarator : IDENTIFIER LBRACE RBRACE { ($1, []) } + -- | IDENTIFIER LBRACE formalparameterlist RBRACE { } primitivetype : BOOLEAN { "boolean" } | numerictype { $1 } From e3fa3efd952e14cfe51c702ebde07b655b869d20 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Mon, 6 May 2024 23:51:33 +0200 Subject: [PATCH 05/14] add void methods --- Test/TestParser.hs | 13 +++++++++++-- src/Parser/JavaParser.y | 8 ++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index 4e0694b..85b899b 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -30,9 +30,16 @@ testMultipleDeclarations = TestCase $ testWithModifier = TestCase $ assertEqual "expect class with int field" [Class "WithInt" [] [VariableDeclaration "int" "value" Nothing]] $ parse [ABSTRACT,CLASS,IDENTIFIER "WithInt",LBRACKET,PUBLIC,INT,IDENTIFIER "value",SEMICOLON,RBRACKET] -testMethod = TestCase $ + +testEmptyMethod = TestCase $ assertEqual "expect class with method" [Class "WithMethod" [MethodDeclaration "int" "foo" [] (Block [])] []] $ parse [CLASS,IDENTIFIER "WithMethod",LBRACKET,INT,IDENTIFIER "foo",LBRACE,RBRACE,SEMICOLON,RBRACKET] +testEmptyPrivateMethod = TestCase $ + assertEqual "expect class with method" [Class "WithMethod" [MethodDeclaration "int" "foo" [] (Block [])] []] $ + parse [CLASS,IDENTIFIER "WithMethod",LBRACKET,PRIVATE,INT,IDENTIFIER "foo",LBRACE,RBRACE,LBRACKET,RBRACKET,RBRACKET] +testEmptyVoidMethod = TestCase $ + assertEqual "expect class with method" [Class "WithMethod" [MethodDeclaration "void" "foo" [] (Block [])] []] $ + parse [CLASS,IDENTIFIER "WithMethod",LBRACKET,VOID,IDENTIFIER "foo",LBRACE,RBRACE,LBRACKET,RBRACKET,RBRACKET] tests = TestList [ @@ -43,5 +50,7 @@ tests = TestList [ testCustomTypeField, testMultipleDeclarations, testWithModifier, - testMethod + testEmptyMethod, + testEmptyPrivateMethod, + testEmptyVoidMethod ] \ No newline at end of file diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index d48b7e2..ec3788c 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -144,10 +144,10 @@ constructorbody : LBRACKET RBRACKET { } | LBRACKET blockstatements RBRACKET { } | LBRACKET explicitconstructorinvocation blockstatements RBRACKET { } -methodheader : type methoddeclarator { ($1, $2) } - -- | modifiers type methoddeclarator { } - -- | VOID methoddeclarator { } - -- | modifiers VOID methoddeclarator { } +methodheader : type methoddeclarator { ($1, $2) } + | modifiers type methoddeclarator { ($2, $3) } + | VOID methoddeclarator { ("void", $2) } + | modifiers VOID methoddeclarator { ("void", $3)} type : primitivetype { $1 } | referencetype { $1 } From 158197b44007e20b4aeb633c17bd96329646564b Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Tue, 7 May 2024 15:03:04 +0200 Subject: [PATCH 06/14] parser implement methods with params --- Test/TestParser.hs | 14 +++++++++++++- src/Parser/JavaParser.y | 8 ++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index 85b899b..8e1aaa7 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -40,6 +40,15 @@ testEmptyPrivateMethod = TestCase $ testEmptyVoidMethod = TestCase $ assertEqual "expect class with method" [Class "WithMethod" [MethodDeclaration "void" "foo" [] (Block [])] []] $ parse [CLASS,IDENTIFIER "WithMethod",LBRACKET,VOID,IDENTIFIER "foo",LBRACE,RBRACE,LBRACKET,RBRACKET,RBRACKET] +testEmptyMethodWithParam = TestCase $ + assertEqual "expect class with method with param" [Class "WithParam" [MethodDeclaration "void" "foo" [ParameterDeclaration "int" "param"] (Block [])] []] $ + parse [CLASS,IDENTIFIER "WithParam",LBRACKET,VOID,IDENTIFIER "foo",LBRACE,INT,IDENTIFIER "param",RBRACE,SEMICOLON,RBRACKET] +testEmptyMethodWithParams = TestCase $ + assertEqual "expect class with multiple params" [Class "WithParams" [MethodDeclaration "void" "foo" [ParameterDeclaration "int" "p1",ParameterDeclaration "Custom" "p2"] (Block [])] []] $ + parse [CLASS,IDENTIFIER "WithParams",LBRACKET,VOID,IDENTIFIER "foo",LBRACE,INT,IDENTIFIER "p1",COMMA,IDENTIFIER "Custom",IDENTIFIER "p2",RBRACE,SEMICOLON,RBRACKET] +testClassWithMethodAndField = TestCase $ + assertEqual "expect class with method and field" [Class "WithMethodAndField" [MethodDeclaration "void" "foo" [] (Block []), MethodDeclaration "int" "bar" [] (Block [])] [VariableDeclaration "int" "value" Nothing]] $ + parse [CLASS,IDENTIFIER "WithMethodAndField",LBRACKET,VOID,IDENTIFIER "foo",LBRACE,RBRACE,LBRACKET,RBRACKET,INT,IDENTIFIER "value",SEMICOLON,INT,IDENTIFIER "bar",LBRACE,RBRACE,SEMICOLON,RBRACKET] tests = TestList [ @@ -52,5 +61,8 @@ tests = TestList [ testWithModifier, testEmptyMethod, testEmptyPrivateMethod, - testEmptyVoidMethod + testEmptyVoidMethod, + testEmptyMethodWithParam, + testEmptyMethodWithParams, + testClassWithMethodAndField ] \ No newline at end of file diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index ec3788c..8edb2df 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -161,8 +161,8 @@ methodbody : block { $1 } blockstatements : blockstatement { } | blockstatements blockstatement { } -formalparameterlist : formalparameter { } - | formalparameterlist COMMA formalparameter{ } +formalparameterlist : formalparameter { [$1] } + | formalparameterlist COMMA formalparameter { $1 ++ [$3] } explicitconstructorinvocation : THIS LBRACE RBRACE SEMICOLON { } | THIS LBRACE argumentlist RBRACE SEMICOLON { } @@ -171,7 +171,7 @@ classtypelist : classtype { } | classtypelist COMMA classtype { } methoddeclarator : IDENTIFIER LBRACE RBRACE { ($1, []) } - -- | IDENTIFIER LBRACE formalparameterlist RBRACE { } + | IDENTIFIER LBRACE formalparameterlist RBRACE { ($1, $3) } primitivetype : BOOLEAN { "boolean" } | numerictype { $1 } @@ -185,7 +185,7 @@ variabledeclarator : variabledeclaratorid { Declarator $1 Nothing } blockstatement : localvariabledeclarationstatement { } | statement { } -formalparameter : type variabledeclaratorid { } +formalparameter : type variabledeclaratorid { ParameterDeclaration $1 $2 } argumentlist : expression { } | argumentlist COMMA expression { } From 5e8a2a90eda390cbe5272e209fdc085ad7d51077 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Wed, 8 May 2024 00:00:18 +0200 Subject: [PATCH 07/14] parser improve error messages --- src/Parser/JavaParser.y | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index 8edb2df..6c249ac 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -7,6 +7,7 @@ import Parser.Lexer %name parse %tokentype { Token } %error { parseError } +%errorhandlertype explist %token BOOLEAN { BOOLEAN } @@ -368,7 +369,7 @@ data Declarator = Declarator Identifier (Maybe Expression) convertDeclarator :: DataType -> Declarator -> VariableDeclaration convertDeclarator dataType (Declarator id assigment) = VariableDeclaration dataType id assigment -parseError :: [Token] -> a -parseError msg = error ("Parse error: " ++ show msg) +parseError :: ([Token], [String]) -> a +parseError (errortoken, expected) = error ("parse error on token: " ++ show errortoken ++ "\nexpected one of: " ++ show expected) } From fecc7eceaa9777139ce3dbbadfba0376bc51dc0f Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Wed, 8 May 2024 00:02:38 +0200 Subject: [PATCH 08/14] parser add constructor --- 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 8e1aaa7..b078def 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -49,6 +49,9 @@ testEmptyMethodWithParams = TestCase $ testClassWithMethodAndField = TestCase $ assertEqual "expect class with method and field" [Class "WithMethodAndField" [MethodDeclaration "void" "foo" [] (Block []), MethodDeclaration "int" "bar" [] (Block [])] [VariableDeclaration "int" "value" Nothing]] $ parse [CLASS,IDENTIFIER "WithMethodAndField",LBRACKET,VOID,IDENTIFIER "foo",LBRACE,RBRACE,LBRACKET,RBRACKET,INT,IDENTIFIER "value",SEMICOLON,INT,IDENTIFIER "bar",LBRACE,RBRACE,SEMICOLON,RBRACKET] +testClassWithConstructor = TestCase $ + assertEqual "expect class with constructor" [Class "WithConstructor" [MethodDeclaration "WithConstructor" "" [] (Block [])] []] $ + parse [CLASS,IDENTIFIER "WithConstructor",LBRACKET,IDENTIFIER "WithConstructor",LBRACE,RBRACE,LBRACKET,RBRACKET,RBRACKET] tests = TestList [ @@ -64,5 +67,6 @@ tests = TestList [ testEmptyVoidMethod, testEmptyMethodWithParam, testEmptyMethodWithParams, - testClassWithMethodAndField + testClassWithMethodAndField, + testClassWithConstructor ] \ No newline at end of file diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index 6c249ac..d323009 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -119,15 +119,15 @@ modifier : PUBLIC { } classtype : classorinterfacetype{ } classbodydeclaration : classmemberdeclaration { $1 } - -- | constructordeclaration { FieldDecl $ VariableDeclaration "int" "a" Nothing } -- TODO + | constructordeclaration { $1 } classorinterfacetype : name { $1 } classmemberdeclaration : fielddeclaration { $1 } | methoddeclaration { $1 } -constructordeclaration : constructordeclarator constructorbody { } - | modifiers constructordeclarator constructorbody { } +constructordeclaration : constructordeclarator constructorbody { case $1 of (classname, parameters) -> MethodDecl $ MethodDeclaration classname "" parameters $2 } + | modifiers constructordeclarator constructorbody { case $2 of (classname, parameters) -> MethodDecl $ MethodDeclaration classname "" parameters $3 } fielddeclaration : type variabledeclarators SEMICOLON { FieldDecls $ map (convertDeclarator $1) $2 } | modifiers type variabledeclarators SEMICOLON { FieldDecls $ map (convertDeclarator $2) $3 } @@ -137,13 +137,13 @@ methoddeclaration : methodheader methodbody { case $1 of (returnType, (name, par block : LBRACKET RBRACKET { Block [] } -- | LBRACKET blockstatements RBRACKET { } -constructordeclarator : simplename LBRACE RBRACE { } - | simplename LBRACE formalparameterlist RBRACE { } +constructordeclarator : simplename LBRACE RBRACE { ($1, []) } + | simplename LBRACE formalparameterlist RBRACE { ($1, $3) } -constructorbody : LBRACKET RBRACKET { } - | LBRACKET explicitconstructorinvocation RBRACKET { } - | LBRACKET blockstatements RBRACKET { } - | LBRACKET explicitconstructorinvocation blockstatements RBRACKET { } +constructorbody : LBRACKET RBRACKET { Block [] } + -- | LBRACKET explicitconstructorinvocation RBRACKET { } + -- | LBRACKET blockstatements RBRACKET { } + -- | LBRACKET explicitconstructorinvocation blockstatements RBRACKET { } methodheader : type methoddeclarator { ($1, $2) } | modifiers type methoddeclarator { ($2, $3) } From 2f81f8266186dd6ef3666343c3ae7dd0537bb06e Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Wed, 8 May 2024 10:20:55 +0200 Subject: [PATCH 09/14] 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 { } From a4b933d6593b90fc0f9288f4c614e2dff446156c Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Wed, 8 May 2024 10:30:50 +0200 Subject: [PATCH 10/14] parser implement multiple blockstatements --- Test/TestParser.hs | 6 +++++- src/Parser/JavaParser.y | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index 1a3484b..28bdd00 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -58,6 +58,9 @@ testEmptyBlock = TestCase $ assertEqual "expect empty block" (Block []) $ parseB testBlockWithLocalVarDecl = TestCase $ assertEqual "expect block with local var delcaration" (Block [LocalVariableDeclaration $ VariableDeclaration "int" "localvar" Nothing]) $ parseBlock [LBRACKET,INT,IDENTIFIER "localvar",SEMICOLON,RBRACKET] +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] tests = TestList [ @@ -76,5 +79,6 @@ tests = TestList [ testClassWithMethodAndField, testClassWithConstructor, testEmptyBlock, - testBlockWithLocalVarDecl + testBlockWithLocalVarDecl, + testBlockWithMultipleLocalVarDecls ] \ No newline at end of file diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index a0a7a3c..5c423b5 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -161,7 +161,7 @@ methodbody : block { $1 } | SEMICOLON { Block [] } blockstatements : blockstatement { Block $1 } - -- | blockstatements blockstatement { } + | blockstatements blockstatement { case $1 of Block stmts -> Block (stmts ++ $2)} formalparameterlist : formalparameter { [$1] } | formalparameterlist COMMA formalparameter { $1 ++ [$3] } From e8151ad2f09b609a1e99d4be048c7fe069b5e3e9 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Wed, 8 May 2024 10:37:40 +0200 Subject: [PATCH 11/14] 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 { } From ebfb912183f6b5c7cc716a3096f429b407456235 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Wed, 8 May 2024 11:05:02 +0200 Subject: [PATCH 12/14] parser add emptystatement --- Test/TestParser.hs | 7 +++++-- src/Parser/JavaParser.y | 18 +++++++++--------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index c000d6b..53a08a6 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -64,7 +64,9 @@ testBlockWithMultipleLocalVarDecls = TestCase $ testNestedBlocks = TestCase $ assertEqual "expect block with block inside" (Block [Block []]) $ parseBlock [LBRACKET,LBRACKET,RBRACKET,RBRACKET] - +testBlockWithEmptyStatement = TestCase $ + assertEqual "expect empty block" (Block []) $ + parseBlock [LBRACKET,SEMICOLON,SEMICOLON,RBRACKET] tests = TestList [ testSingleEmptyClass, @@ -84,5 +86,6 @@ tests = TestList [ testEmptyBlock, testBlockWithLocalVarDecl, testBlockWithMultipleLocalVarDecls, - testNestedBlocks + testNestedBlocks, + testBlockWithEmptyStatement ] \ No newline at end of file diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index fe309da..d2cf382 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -136,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 { $2 } + | LBRACKET blockstatements RBRACKET { Block $2 } constructordeclarator : simplename LBRACE RBRACE { ($1, []) } | simplename LBRACE formalparameterlist RBRACE { ($1, $3) } @@ -160,8 +160,8 @@ variabledeclarators : variabledeclarator { [$1] } methodbody : block { $1 } | SEMICOLON { Block [] } -blockstatements : blockstatement { Block $1 } - | blockstatements blockstatement { case $1 of Block stmts -> Block (stmts ++ $2)} +blockstatements : blockstatement { $1 } + | blockstatements blockstatement { $1 ++ $2} formalparameterlist : formalparameter { [$1] } | formalparameterlist COMMA formalparameter { $1 ++ [$3] } @@ -185,7 +185,7 @@ variabledeclarator : variabledeclaratorid { Declarator $1 Nothing } -- | variabledeclaratorid ASSIGN variableinitializer { Declarator $1 Nothing } -- TODO blockstatement : localvariabledeclarationstatement { $1 } - | statement { [$1] } + | statement { $1 } formalparameter : type variabledeclaratorid { ParameterDeclaration $1 $2 } @@ -213,8 +213,8 @@ integraltype : INT { "int" } localvariabledeclaration : type variabledeclarators { map LocalVariableDeclaration $ map (convertDeclarator $1) $2 } -statementwithouttrailingsubstatement : block { $1 } - -- | emptystatement { } +statementwithouttrailingsubstatement : block { [$1] } + | emptystatement { [] } -- | expressionstatement { } -- | returnstatement { } @@ -227,7 +227,7 @@ whilestatement : WHILE LBRACE expression RBRACE statement { } assignmentexpression : conditionalexpression { } | assignment{ } -emptystatement : SEMICOLON { } +emptystatement : SEMICOLON { Block [] } expressionstatement : statementexpression SEMICOLON { } @@ -338,8 +338,8 @@ andexpression : equalityexpression { } | andexpression AND equalityexpression { } equalityexpression : relationalexpression { } - | equalityexpression EQUAL relationalexpression { } - | equalityexpression NOTEQUAL relationalexpression { } + -- | equalityexpression EQUAL relationalexpression { } + -- | equalityexpression NOTEQUAL relationalexpression { } relationalexpression : shiftexpression { } | relationalexpression LESS shiftexpression { } From c8ce7f4b43ba4aa23f0a213b6cda139ef95c0586 Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Wed, 8 May 2024 13:16:00 +0200 Subject: [PATCH 13/14] parser implement int initializiation --- Test/TestParser.hs | 11 +++- src/Parser/JavaParser.y | 139 ++++++++++++++++++++-------------------- 2 files changed, 80 insertions(+), 70 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index 53a08a6..7a79ba8 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -68,6 +68,13 @@ testBlockWithEmptyStatement = TestCase $ assertEqual "expect empty block" (Block []) $ parseBlock [LBRACKET,SEMICOLON,SEMICOLON,RBRACKET] +testExpressionIntLiteral = TestCase $ + assertEqual "expect IntLiteral" (IntegerLiteral 3) $ + parseExpression [INTEGERLITERAL 3] +testFieldWithInitialization = TestCase $ + assertEqual "expect Class with initialized field" [Class "WithInitField" [] [VariableDeclaration "int" "number" $ Just $ IntegerLiteral 3]] $ + parse [CLASS,IDENTIFIER "WithInitField",LBRACKET,INT,IDENTIFIER "number",ASSIGN,INTEGERLITERAL 3,SEMICOLON,RBRACKET] + tests = TestList [ testSingleEmptyClass, testTwoEmptyClasses, @@ -87,5 +94,7 @@ tests = TestList [ testBlockWithLocalVarDecl, testBlockWithMultipleLocalVarDecls, testNestedBlocks, - testBlockWithEmptyStatement + testBlockWithEmptyStatement, + testExpressionIntLiteral, + testFieldWithInitialization ] \ No newline at end of file diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index d2cf382..9dd31c6 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -1,11 +1,12 @@ { -module Parser.JavaParser (parse, parseBlock) where +module Parser.JavaParser (parse, parseBlock, parseExpression) where import Ast import Parser.Lexer } %name parse %name parseBlock block +%name parseExpression expression %tokentype { Token } %error { parseError } %errorhandlertype explist @@ -182,7 +183,7 @@ referencetype : classorinterfacetype { $1 } variabledeclarator : variabledeclaratorid { Declarator $1 Nothing } - -- | variabledeclaratorid ASSIGN variableinitializer { Declarator $1 Nothing } -- TODO + | variabledeclaratorid ASSIGN variableinitializer { Declarator $1 (Just $3) } blockstatement : localvariabledeclarationstatement { $1 } | statement { $1 } @@ -196,7 +197,7 @@ numerictype : integraltype { $1 } variabledeclaratorid : IDENTIFIER { $1 } -variableinitializer : expression { } +variableinitializer : expression { $1 } localvariabledeclarationstatement : localvariabledeclaration SEMICOLON { $1 } @@ -206,7 +207,7 @@ statement : statementwithouttrailingsubstatement{ $1 } -- | whilestatement { } -expression : assignmentexpression { } +expression : assignmentexpression { $1 } integraltype : INT { "int" } | CHAR { "char" } @@ -224,8 +225,8 @@ ifthenelsestatement : IF LBRACE expression RBRACE statementnoshortif ELSE state whilestatement : WHILE LBRACE expression RBRACE statement { } -assignmentexpression : conditionalexpression { } - | assignment{ } +assignmentexpression : conditionalexpression { $1 } + -- | assignment { } emptystatement : SEMICOLON { Block [] } @@ -238,10 +239,10 @@ statementnoshortif : statementwithouttrailingsubstatement { } | ifthenelsestatementnoshortif { } | whilestatementnoshortif { } -conditionalexpression : conditionalorexpression { } - | conditionalorexpression QUESMARK expression COLON conditionalexpression { } +conditionalexpression : conditionalorexpression { $1 } + -- | conditionalorexpression QUESMARK expression COLON conditionalexpression { } -assignment :lefthandside assignmentoperator assignmentexpression { } +assignment : lefthandside assignmentoperator assignmentexpression { } statementexpression : assignment { } @@ -257,23 +258,23 @@ ifthenelsestatementnoshortif :IF LBRACE expression RBRACE statementnoshortif whilestatementnoshortif : WHILE LBRACE expression RBRACE statementnoshortif { } -conditionalorexpression : conditionalandexpression { } - | conditionalorexpression LOGICALOR conditionalandexpression{ } +conditionalorexpression : conditionalandexpression { $1 } + -- | conditionalorexpression LOGICALOR conditionalandexpression{ } -lefthandside : name { } +lefthandside : name { $1 } assignmentoperator : ASSIGN{ } - | TIMESEQUAL { } - | DIVIDEEQUAL { } - | MODULOEQUAL { } - | PLUSEQUAL { } - | MINUSEQUAL { } - | SHIFTLEFTEQUAL { } - | SIGNEDSHIFTRIGHTEQUAL { } - | UNSIGNEDSHIFTRIGHTEQUAL { } - | ANDEQUAL { } - | XOREQUAL { } - | OREQUAL{ } + -- | TIMESEQUAL { } + -- | DIVIDEEQUAL { } + -- | MODULOEQUAL { } + -- | PLUSEQUAL { } + -- | MINUSEQUAL { } + -- | SHIFTLEFTEQUAL { } + -- | SIGNEDSHIFTRIGHTEQUAL { } + -- | UNSIGNEDSHIFTRIGHTEQUAL { } + -- | ANDEQUAL { } + -- | XOREQUAL { } + -- | OREQUAL{ } preincrementexpression : INCREMENT unaryexpression { } @@ -291,73 +292,73 @@ methodinvocation : name LBRACE RBRACE { } classinstancecreationexpression : NEW classtype LBRACE RBRACE { } | NEW classtype LBRACE argumentlist RBRACE { } -conditionalandexpression : inclusiveorexpression { } +conditionalandexpression : inclusiveorexpression { $1 } fieldaccess : primary DOT IDENTIFIER { } -unaryexpression : preincrementexpression { } - | predecrementexpression { } - | PLUS unaryexpression { } - | MINUS unaryexpression { } - | unaryexpressionnotplusminus { } +unaryexpression : unaryexpressionnotplusminus { $1 } + -- | predecrementexpression { } + -- | PLUS unaryexpression { } + -- | MINUS unaryexpression { } + -- | preincrementexpression { $1 } -postfixexpression : primary { } - | name { } - | postincrementexpression { } - | postdecrementexpression{ } +postfixexpression : primary { $1 } + -- | name { } + -- | postincrementexpression { } + -- | postdecrementexpression{ } -primary : primarynonewarray { } +primary : primarynonewarray { $1 } -inclusiveorexpression : exclusiveorexpression { } - | inclusiveorexpression OR exclusiveorexpression { } +inclusiveorexpression : exclusiveorexpression { $1 } + -- | inclusiveorexpression OR exclusiveorexpression { } -primarynonewarray : literal { } - | THIS { } - | LBRACE expression RBRACE { } - | classinstancecreationexpression { } - | fieldaccess { } - | methodinvocation { } +primarynonewarray : literal { $1 } + -- | THIS { } + -- | LBRACE expression RBRACE { } + -- | classinstancecreationexpression { } + -- | fieldaccess { } + -- | methodinvocation { } -unaryexpressionnotplusminus : postfixexpression { } - | TILDE unaryexpression { } - | EXCLMARK unaryexpression { } - | castexpression{ } +unaryexpressionnotplusminus : postfixexpression { $1 } + -- | TILDE unaryexpression { } + -- | EXCLMARK unaryexpression { } + -- | castexpression{ } -exclusiveorexpression : andexpression { } - | exclusiveorexpression XOR andexpression { } +exclusiveorexpression : andexpression { $1 } + -- | exclusiveorexpression XOR andexpression { } -literal : INTLITERAL { } - | BOOLLITERAL { } - | CHARLITERAL { } - | JNULL { } +literal : INTLITERAL { IntegerLiteral $1 } + -- | BOOLLITERAL { } + -- | CHARLITERAL { } + -- | JNULL { } castexpression : LBRACE primitivetype RBRACE unaryexpression { } | LBRACE expression RBRACE unaryexpressionnotplusminus{ } -andexpression : equalityexpression { } - | andexpression AND equalityexpression { } +andexpression : equalityexpression { $1 } + -- | andexpression AND equalityexpression { } -equalityexpression : relationalexpression { } +equalityexpression : relationalexpression { $1 } -- | equalityexpression EQUAL relationalexpression { } -- | equalityexpression NOTEQUAL relationalexpression { } -relationalexpression : shiftexpression { } - | relationalexpression LESS shiftexpression { } - | relationalexpression GREATER shiftexpression { } - | relationalexpression LESSEQUAL shiftexpression { } - | relationalexpression GREATEREQUAL shiftexpression { } - | relationalexpression INSTANCEOF referencetype { } +relationalexpression : shiftexpression { $1 } + -- | relationalexpression LESS shiftexpression { } + -- | relationalexpression GREATER shiftexpression { } + -- | relationalexpression LESSEQUAL shiftexpression { } + -- | relationalexpression GREATEREQUAL shiftexpression { } + -- | relationalexpression INSTANCEOF referencetype { } -shiftexpression : additiveexpression { } +shiftexpression : additiveexpression { $1 } -additiveexpression : multiplicativeexpression { } - | additiveexpression PLUS multiplicativeexpression { } - | additiveexpression MINUS multiplicativeexpression { } +additiveexpression : multiplicativeexpression { $1 } + -- | additiveexpression PLUS multiplicativeexpression { } + -- | additiveexpression MINUS multiplicativeexpression { } -multiplicativeexpression : unaryexpression { } - | multiplicativeexpression MUL unaryexpression { } - | multiplicativeexpression DIV unaryexpression { } - | multiplicativeexpression MOD unaryexpression { } +multiplicativeexpression : unaryexpression { $1 } + -- | multiplicativeexpression MUL unaryexpression { } + -- | multiplicativeexpression DIV unaryexpression { } + -- | multiplicativeexpression MOD unaryexpression { } { From c49b7f556c055432dc18236311c3b0cc65a09e7a Mon Sep 17 00:00:00 2001 From: Marvin Schlegel Date: Wed, 8 May 2024 13:29:10 +0200 Subject: [PATCH 14/14] parser implement varibale initialization --- Test/TestParser.hs | 10 +++++++++- src/Parser/JavaParser.y | 6 +++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Test/TestParser.hs b/Test/TestParser.hs index 7a79ba8..4a18581 100644 --- a/Test/TestParser.hs +++ b/Test/TestParser.hs @@ -74,6 +74,12 @@ testExpressionIntLiteral = TestCase $ testFieldWithInitialization = TestCase $ assertEqual "expect Class with initialized field" [Class "WithInitField" [] [VariableDeclaration "int" "number" $ Just $ IntegerLiteral 3]] $ parse [CLASS,IDENTIFIER "WithInitField",LBRACKET,INT,IDENTIFIER "number",ASSIGN,INTEGERLITERAL 3,SEMICOLON,RBRACKET] +testLocalBoolWithInitialization = TestCase $ + assertEqual "expect block with with initialized local var" (Block [LocalVariableDeclaration $ VariableDeclaration "boolean" "b" $ Just $ BooleanLiteral False]) $ + parseBlock [LBRACKET,BOOLEAN,IDENTIFIER "b",ASSIGN,BOOLLITERAL False,SEMICOLON,RBRACKET] +testFieldNullWithInitialization = TestCase $ + assertEqual "expect Class with initialized field" [Class "WithInitField" [] [VariableDeclaration "Object" "bar" $ Just NullLiteral]] $ + parse [CLASS,IDENTIFIER "WithInitField",LBRACKET,IDENTIFIER "Object",IDENTIFIER "bar",ASSIGN,NULLLITERAL,SEMICOLON,RBRACKET] tests = TestList [ testSingleEmptyClass, @@ -96,5 +102,7 @@ tests = TestList [ testNestedBlocks, testBlockWithEmptyStatement, testExpressionIntLiteral, - testFieldWithInitialization + testFieldWithInitialization, + testLocalBoolWithInitialization, + testFieldNullWithInitialization ] \ No newline at end of file diff --git a/src/Parser/JavaParser.y b/src/Parser/JavaParser.y index 9dd31c6..b0a8173 100644 --- a/src/Parser/JavaParser.y +++ b/src/Parser/JavaParser.y @@ -328,9 +328,9 @@ exclusiveorexpression : andexpression { $1 } -- | exclusiveorexpression XOR andexpression { } literal : INTLITERAL { IntegerLiteral $1 } - -- | BOOLLITERAL { } - -- | CHARLITERAL { } - -- | JNULL { } + | BOOLLITERAL { BooleanLiteral $1 } + | CHARLITERAL { CharacterLiteral $1 } + | JNULL { NullLiteral } castexpression : LBRACE primitivetype RBRACE unaryexpression { } | LBRACE expression RBRACE unaryexpressionnotplusminus{ }