1
1
mirror of https://github.com/github/semantic.git synced 2024-12-22 22:31:36 +03:00

We don’t need to preserve the node name.

This commit is contained in:
Rob Rix 2016-02-13 21:27:27 -05:00
parent 11cbaf009c
commit acae391e50
2 changed files with 7 additions and 8 deletions

View File

@ -18,7 +18,7 @@ type Parser = Source Char -> IO (Term Text Info)
-- | Given a source string, the term's range, production name, and -- | Given a source string, the term's range, production name, and
-- | production/child pairs, construct the term. -- | production/child pairs, construct the term.
type Constructor = Source Char -> Range -> String -> [(String, Term Text Info)] -> Term Text Info type Constructor = Source Char -> Range -> String -> [Term Text Info] -> Term Text Info
-- | Categories that are treated as keyed nodes. -- | Categories that are treated as keyed nodes.
keyedCategories :: Set.Set Category keyedCategories :: Set.Set Category
@ -35,10 +35,10 @@ termConstructor mapping source range name = (Info range categories :<) . constru
where where
categories = mapping name categories = mapping name
construct [] = Leaf . pack . toList $ slice range source construct [] = Leaf . pack . toList $ slice range source
construct children | categories `intersect` fixedCategories = Fixed $ fmap snd children construct children | categories `intersect` fixedCategories = Fixed children
construct children | categories `intersect` keyedCategories = Keyed . Map.fromList $ assignKey <$> children construct children | categories `intersect` keyedCategories = Keyed . Map.fromList $ assignKey <$> children
construct children = Indexed $ snd <$> children construct children = Indexed children
intersect a b = not . Set.null $ Set.intersection a b intersect a b = not . Set.null $ Set.intersection a b
assignKey (_, node@(Info _ categories :< Fixed (key : _))) | Set.member Pair categories = (getSubstring key, node) assignKey node@(Info _ categories :< Fixed (key : _)) | Set.member Pair categories = (getSubstring key, node)
assignKey (_, node) = (getSubstring node, node) assignKey node = (getSubstring node, node)
getSubstring (Info range _ :< _) = pack . toList $ slice range source getSubstring (Info range _ :< _) = pack . toList $ slice range source

View File

@ -69,8 +69,7 @@ defaultCategoryForNodeName name = case name of
documentToTerm :: Constructor -> Ptr TSDocument -> Parser documentToTerm :: Constructor -> Ptr TSDocument -> Parser
documentToTerm constructor document contents = alloca $ \ root -> do documentToTerm constructor document contents = alloca $ \ root -> do
ts_document_root_node_p document root ts_document_root_node_p document root
(_, term) <- toTerm root toTerm root
return term
where toTerm node = do where toTerm node = do
name <- ts_node_p_name node document name <- ts_node_p_name node document
name <- peekCString name name <- peekCString name
@ -79,7 +78,7 @@ documentToTerm constructor document contents = alloca $ \ root -> do
-- Note: The strict application here is semantically important. Without it, we may not evaluate the range until after weve exited the scope that `node` was allocated within, meaning `alloca` will free it & other stack data may overwrite it. -- Note: The strict application here is semantically important. Without it, we may not evaluate the range until after weve exited the scope that `node` was allocated within, meaning `alloca` will free it & other stack data may overwrite it.
range <- return $! Range { start = fromIntegral $ ts_node_p_start_char node, end = fromIntegral $ ts_node_p_end_char node } range <- return $! Range { start = fromIntegral $ ts_node_p_start_char node, end = fromIntegral $ ts_node_p_end_char node }
return (name, constructor contents range name children) return $! constructor contents range name children
getChild node n out = do getChild node n out = do
_ <- ts_node_p_named_child node n out _ <- ts_node_p_named_child node n out
toTerm out toTerm out