1
1
mirror of https://github.com/github/semantic.git synced 2024-12-21 13:51:44 +03:00

Add a Function constructor to Syntax

This commit is contained in:
joshvera 2016-06-10 13:11:32 -04:00
parent 129ece7820
commit 91bf955125
4 changed files with 13 additions and 0 deletions

View File

@ -17,6 +17,8 @@ data Category
| Pair
-- | A call to a function.
| FunctionCall
-- | A function declaration
| Function
-- | A string literal.
| StringLiteral
-- | An integer literal.

View File

@ -23,6 +23,7 @@ toTermName term = case runCofree term of
(_ :< Indexed children) -> fromMaybe "EmptyIndexedNode" $ (toCategoryName . category) . extract <$> head children
(_ :< Fixed children) -> fromMaybe "EmptyFixedNode" $ (toCategoryName . category) . extract <$> head children
(_ :< Syntax.FunctionCall i _) -> toTermName i
(_ :< Syntax.Function _) -> "anonymous function"
class HasCategory a where
toCategoryName :: a -> Text

View File

@ -30,6 +30,10 @@ fixedCategories = Set.fromList [ BinaryOperator, Pair ]
functionCallCategories :: Set.Set Category
functionCallCategories = Set.singleton Category.FunctionCall
-- | Categories that are treated as functionCall nodes.
functionCategories :: Set.Set Category
functionCategories = Set.singleton Category.Function
-- | Should these categories be treated as keyed nodes?
isKeyed :: Category -> Bool
isKeyed = flip Set.member keyedCategories
@ -41,6 +45,9 @@ isFixed = flip Set.member fixedCategories
isFunctionCall :: Category -> Bool
isFunctionCall = flip Set.member functionCallCategories
isFunction :: Category -> Bool
isFunction = flip Set.member functionCategories
-- | Given a function that maps production names to sets of categories, produce
-- | a Constructor.
termConstructor :: Constructor
@ -50,6 +57,7 @@ termConstructor source info children = cofree (info :< syntax)
construct :: [Term Text Info] -> Syntax Text (Term Text Info)
construct [] = Leaf . pack . toString $ slice (characterRange info) source
construct children | isFunctionCall (category info), (x:xs) <- children = Syntax.FunctionCall x xs
construct children | isFunction (category info) = Syntax.Function children
construct children | isFixed (category info) = Fixed children
construct children | isKeyed (category info) = Keyed . Map.fromList $ assignKey <$> children
construct children = Indexed children

View File

@ -19,4 +19,6 @@ data Syntax
| Keyed (OrderedMap T.Text f)
-- | A function call has an identifier where f is a (Leaf a) and a list of arguments.
| FunctionCall f [f]
-- | A function has a list of expressions.
| Function [f]
deriving (Functor, Show, Eq, Foldable, Traversable)