From 8f35aa4d14e55bbffdd09ef39d96c5edb6a9f18f Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Mon, 15 May 2017 15:34:53 -0400 Subject: [PATCH] Extract & generalize toAST. --- src/TreeSitter.hs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/TreeSitter.hs b/src/TreeSitter.hs index 090285a7f..e5836aecf 100644 --- a/src/TreeSitter.hs +++ b/src/TreeSitter.hs @@ -64,13 +64,6 @@ parseRubyToAST source = do ts_document_free document pure ast - where toAST :: Node -> IO (A.RoseF (A.Node Ruby.Grammar) Node) - toAST node@Node{..} = do - let count = fromIntegral nodeChildCount - children <- allocaArray count $ \ childNodesPtr -> do - _ <- with nodeTSNode (\ nodePtr -> ts_node_copy_child_nodes nullPtr nodePtr childNodesPtr (fromIntegral count)) - peekArray count childNodesPtr - pure $ A.RoseF (toEnum (fromIntegral nodeSymbol) :. nodeRange node :. nodeSpan node :. Nil) children -- | Parse Ruby to a list of Terms, printing any assignment errors to stdout. Intended for use in ghci, e.g.: -- @@ -84,6 +77,14 @@ parseRubyToTerm source = do _ -> traverse_ (putStrLn . ($ "") . A.showError source) errors >> pure Nothing +toAST :: Enum grammar => Node -> IO (A.RoseF (A.Node grammar) Node) +toAST node@Node{..} = do + let count = fromIntegral nodeChildCount + children <- allocaArray count $ \ childNodesPtr -> do + _ <- with nodeTSNode (\ nodePtr -> ts_node_copy_child_nodes nullPtr nodePtr childNodesPtr (fromIntegral count)) + peekArray count childNodesPtr + pure $ A.RoseF (toEnum (fromIntegral nodeSymbol) :. nodeRange node :. nodeSpan node :. Nil) children + anaM :: (Corecursive t, Monad m, Traversable (Base t)) => (a -> m (Base t a)) -> a -> m t anaM g = a where a = pure . embed <=< traverse a <=< g