1
1
mirror of https://github.com/github/semantic.git synced 2024-12-22 06:11:49 +03:00

Add cases for methods and args

This commit is contained in:
joshvera 2016-06-14 09:53:35 -07:00
parent a3b3bee219
commit 307cdda0de
6 changed files with 22 additions and 4 deletions

View File

@ -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.

View File

@ -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

View File

@ -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]

View File

@ -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

View File

@ -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)

View File

@ -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