From 307cdda0de0ae6af728a07f54ee38e1708fdc2d9 Mon Sep 17 00:00:00 2001 From: joshvera Date: Tue, 14 Jun 2016 09:53:35 -0700 Subject: [PATCH] Add cases for methods and args --- src/Category.hs | 2 ++ src/Parser.hs | 9 +++++++-- src/Renderer/JSON.hs | 3 ++- src/Renderer/Split.hs | 9 +++++++++ src/Syntax.hs | 1 + src/Term/Arbitrary.hs | 2 +- 6 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/Category.hs b/src/Category.hs index f2c17e70c..93e9b1cff 100644 --- a/src/Category.hs +++ b/src/Category.hs @@ -27,6 +27,8 @@ data Category | ExpressionStatements -- | A method call on an object. | MethodCall + -- | A method's arguments. + | Args -- | A string literal. | StringLiteral -- | An integer literal. diff --git a/src/Parser.hs b/src/Parser.hs index e463cbcc1..b3c7816b7 100644 --- a/src/Parser.hs +++ b/src/Parser.hs @@ -62,6 +62,9 @@ isAssignment = flip Set.member (Set.singleton Category.Assignment) isMemberAccess :: Category -> Bool isMemberAccess = flip Set.member (Set.singleton Category.MemberAccess) +isArgs :: Category -> Bool +isArgs = flip Set.member (Set.Singleton Category.Args) + -- | Given a function that maps production names to sets of categories, produce -- | a Constructor. termConstructor :: Constructor @@ -81,8 +84,10 @@ termConstructor source info children = cofree (info :< syntax) (id:params:body:[]) | (info :< _) <- runCofree id, isIdentifier (category info) -> Syntax.Function (Just id) (Just params) body x -> error $ "Expected a function declaration but got: " <> show x - construct children | isFunctionCall (category info), (x:xs) <- children = - Syntax.FunctionCall x xs + construct children | isFunctionCall (category info) = case runCofree <$> children of + [ (_ :< Syntax.MemberAccess{..}), params@(_ :< Args{}) ] -> Syntax.MethodCall memberId property (cofree params) + (x:xs) -> Syntax.FunctionCall (cofree x) (cofree <$> xs) + construct children | isArgs (category info) = Args children construct children | isFixed (category info) = Fixed children construct children | isKeyed (category info) = Keyed . Map.fromList $ assignKey <$> children construct children = Indexed children diff --git a/src/Renderer/JSON.hs b/src/Renderer/JSON.hs index 1906b2af1..ef52828e2 100644 --- a/src/Renderer/JSON.hs +++ b/src/Renderer/JSON.hs @@ -73,7 +73,8 @@ termFields Info{..} syntax = "range" .= characterRange : "category" .= category Keyed c -> childrenFields c Syntax.FunctionCall identifier params -> [ "identifier" .= identifier ] <> [ "params" .= params ] Syntax.Function identifier params c -> [ "identifier" .= identifier ] <> [ "params" .= params ] <> childrenFields c - Syntax.MethodCall targetId methodId params -> [ "targetIdentifier" .= targetId ] <> [ "methodId" .= methodId ] <> [ "params" .= params ] + Syntax.MethodCall targetId methodId args -> [ "targetIdentifier" .= targetId ] <> [ "methodId" .= methodId ] <> [ "args" .= args ] + Syntax.Args c -> childrenFields c where childrenFields c = [ "children" .= c ] patchFields :: KeyValue kv => SplitPatch (Term leaf Info) -> [kv] diff --git a/src/Renderer/Split.hs b/src/Renderer/Split.hs index f47c2d7b7..acbacd224 100644 --- a/src/Renderer/Split.hs +++ b/src/Renderer/Split.hs @@ -48,6 +48,9 @@ styleName category = "category-" <> case category of Category.FunctionCall -> "function_call" Category.Function -> "function" Category.MethodCall -> "method_call" + Category.Args -> "arguments" + Category.Assignment -> "assignment" + Category.MemberAccess -> "member_access" Identifier -> "identifier" Params -> "parameters" ExpressionStatements -> "expression_statements" @@ -108,6 +111,12 @@ instance ToMarkup f => ToMarkup (Renderable (Source Char, Info, Syntax a (f, Ran contentElements source characterRange (catMaybes [identifier, params, Just expressions]) Syntax.MethodCall targetId methodId methodParams -> ul . mconcat $ wrapIn li <$> contentElements source characterRange [targetId, methodId, methodParams] + Syntax.Args children -> ul . mconcat $ wrapIn li <$> + contentElements source characterRange children + Syntax.MemberAccess memberId property -> ul . mconcat $ wrapIn li <$> + contentElements source characterRange [memberId, property] + Syntax.Assignment memberId value -> ul . mconcat $ wrapIn li <$> + contentElements source characterRange [memberId, value] contentElements :: (Foldable t, ToMarkup f) => Source Char -> Range -> t (f, Range) -> [Markup] contentElements source range children = let (elements, next) = foldr' (markupForContextAndChild source) ([], end range) children in diff --git a/src/Syntax.hs b/src/Syntax.hs index 851939d1f..63cb0ffda 100644 --- a/src/Syntax.hs +++ b/src/Syntax.hs @@ -29,4 +29,5 @@ data Syntax -- | A method call consisting of its target, the method name, and the parameters passed to the method. -- | e.g. in Javascript console.log('hello') represents a method call. | MethodCall { targetId :: f, methodId :: f, methodParams :: f } + | Args [f] deriving (Functor, Show, Eq, Foldable, Traversable) diff --git a/src/Term/Arbitrary.hs b/src/Term/Arbitrary.hs index e58d13d99..2781373be 100644 --- a/src/Term/Arbitrary.hs +++ b/src/Term/Arbitrary.hs @@ -52,5 +52,5 @@ instance (Eq leaf, Eq annotation, Arbitrary leaf, Arbitrary annotation) => Arbit Fixed f -> Fixed <$> (List.subsequences f >>= recursivelyShrink) Keyed k -> Keyed . Map.fromList <$> (List.subsequences (Map.toList k) >>= recursivelyShrink) FunctionCall identifier children -> FunctionCall <$> shrink identifier <*> (List.subsequences children >>= recursivelyShrink) - Function identifier params children -> FunctionCall <$> shrink identifier <*> shrink params <*> (List.subsequences children >>= recursivelyShrink) + Function identifier params children -> Function <$> shrink identifier <*> shrink params <*> shrink children MethodCall targetId methodId params -> MethodCall <$> shrink targetId <*> shrink methodId <*> shrink params