Skip to content

Commit

Permalink
Unflatten decorated signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
flbulgarelli committed Apr 14, 2022
1 parent 70707cc commit e70a19c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
24 changes: 12 additions & 12 deletions spec/JavaSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,25 @@ spec = do
run [text|
class Foo {
private void hello() {}
}|] `shouldBe` Class "Foo" Nothing (Sequence [
Decorator [Private] (SubroutineSignature "hello" [] "void" []),
Decorator [Private] (SimpleMethod "hello" [] None)])
}|] `shouldBe` Class "Foo" Nothing (Decorator [Private] (Sequence [
(SubroutineSignature "hello" [] "void" []),
(SimpleMethod "hello" [] None)]))

it "parses Class with static, private Methods" $ do
run [text|
class Foo {
private static void hello() {}
}|] `shouldBe` Class "Foo" Nothing (Sequence [
Decorator [Private, Static] (SubroutineSignature "hello" [] "void" []),
Decorator [Private, Static] (SimpleMethod "hello" [] None)])
}|] `shouldBe` Class "Foo" Nothing (Decorator [Private, Static] (Sequence [
(SubroutineSignature "hello" [] "void" []),
(SimpleMethod "hello" [] None)]))

it "parses Class with Methods with simple annotations" $ do
run [text|
class Foo {
@AfterSave public void hello() {}
}|] `shouldBe` Class "Foo" Nothing (Sequence [
Decorator [Annotation (Reference "AfterSave")] (SubroutineSignature "hello" [] "void" []),
Decorator [Annotation (Reference "AfterSave")] (SimpleMethod "hello" [] None)])
}|] `shouldBe` Class "Foo" Nothing (Decorator [Annotation (Reference "AfterSave")] (Sequence [
(SubroutineSignature "hello" [] "void" []),
(SimpleMethod "hello" [] None)]))

it "parses Class with protected, abstract Methods" $ do
run [text|
Expand Down Expand Up @@ -406,9 +406,9 @@ spec = do
it "parses attributes" $ do
run [text|class Foo {
private int foo = 4;
}|] `shouldBe` Class "Foo" Nothing (Sequence [
Decorator [Private] (VariableSignature "foo" "int" []),
Decorator [Private] (Attribute "foo" (MuNumber 4))])
}|] `shouldBe` Class "Foo" Nothing (Decorator [Private] (Sequence [
(VariableSignature "foo" "int" []),
(Attribute "foo" (MuNumber 4))]))

it "parses attribute access" $ do
run [text|class Foo {
Expand Down
19 changes: 11 additions & 8 deletions src/Language/Mulang/Parsers/Java.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ muClass (ClassDecl _ name _ superclass interfaces (ClassBody body)) =
muInterface (InterfaceDecl _ name _ interfaces (InterfaceBody body)) =
Interface (i name) (map muRefType interfaces) (compactConcatMap muMemberDecl body)

muClassTypeDecl clazz@(ClassDecl modifiers name args _ _ _) = muModifiers modifiers . muDeclaration name args $ muClass clazz
muClassTypeDecl clazz@(ClassDecl modifiers name args _ _ _) = decorate modifiers . muDeclaration name args $ muClass clazz
muClassTypeDecl (EnumDecl modifiers name _ (EnumBody constants _)) =
muModifiers modifiers $ Enumeration (i name) (map muEnumConstant constants)
decorate modifiers $ Enumeration (i name) (map muEnumConstant constants)

muImplements interface = Implement $ Reference (muRefType interface)

Expand All @@ -55,18 +55,18 @@ muDecl (MemberDecl memberDecl) = muMemberDecl memberDecl
muDecl (InitDecl _ block) = [muBlock block]

muMemberDecl :: MemberDecl -> [Expression]
muMemberDecl (FieldDecl modifiers typ varDecls) = map (muModifiers modifiers) . concatMap (variableToAttribute.muVarDecl typ) $ varDecls
muMemberDecl (FieldDecl modifiers typ varDecls) = decorateMany modifiers . concatMap (variableToAttribute.muVarDecl typ) $ varDecls
muMemberDecl (MethodDecl (elem (Annotation (MarkerAnnotation (Name [Ident "Test"]))) -> True) _ Nothing (Ident name) [] _ body)
= return $ Test (MuString name) (muMethodBody body)
muMemberDecl (MethodDecl (elem Static -> True) _ Nothing (Ident "main") [_] _ body)
= return $ EntryPoint "main" (muMethodBody body)
muMemberDecl (MethodDecl modifiers typeParams typ name params _ (MethodBody Nothing))
= return $ muModifiers modifiers $ muMethodSignature name params typ typeParams
= return $ decorate modifiers $ muMethodSignature name params typ typeParams
muMemberDecl (MethodDecl (elem Public -> True) _ _ (Ident "equals") params _ body)
= return $ PrimitiveMethod O.Equal [SimpleEquation (map muFormalParam params) (muMethodBody body)]
muMemberDecl (MethodDecl (elem Public -> True) _ _ (Ident "hashCode") params _ body)
= return $ PrimitiveMethod O.Hash [SimpleEquation (map muFormalParam params) (muMethodBody body)]
muMemberDecl (MethodDecl modifiers typeParams returnType name params _ body) = map (muModifiers modifiers) [
muMemberDecl (MethodDecl modifiers typeParams returnType name params _ body) = decorateMany modifiers [
muMethodSignature name params returnType typeParams,
SimpleMethod (i name) (map muFormalParam params) (muMethodBody body)]
muMemberDecl e@(ConstructorDecl _ _ _ _params _ _constructorBody) = return . debug $ e
Expand All @@ -76,9 +76,12 @@ muMemberDecl (MemberInterfaceDecl decl) =
muMethodSignature name params returnType typeParams = SubroutineSignature (i name) (map muFormalParamType params) (muReturnType returnType) (map muTypeParam typeParams)
muTypeParam (TypeParam (Ident i) _) = i

muModifiers :: [Modifier] -> Expression -> Expression
muModifiers modifiers | null nonPublicModifiers = id
| otherwise = Decorator (map muModifier nonPublicModifiers)
decorate :: [Modifier] -> Expression -> Expression
decorate ms = head . decorateMany ms . return

decorateMany :: [Modifier] -> [Expression] -> [Expression]
decorateMany modifiers es | null nonPublicModifiers = es
| otherwise = [Decorator (map muModifier nonPublicModifiers) (compact es)]
where
nonPublicModifiers = filter (/=Public) modifiers

Expand Down

0 comments on commit e70a19c

Please sign in to comment.