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