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

Streamline block expression

This commit is contained in:
Timothy Clem 2016-11-04 15:45:26 -07:00
parent 463d075879
commit 2b195508fd
4 changed files with 5 additions and 7 deletions

View File

@ -240,9 +240,7 @@ toTermName source term = case unwrap term of
S.ConditionalAssignment id _ -> toTermName' id
S.Until expr _ -> toTermName' expr
S.Unless expr _ -> termNameFromSource expr
S.BlockExpression maybeExpr children -> case maybeExpr of
Just expr -> termNameFromSource expr
Nothing -> fromMaybe "branch" $ (toCategoryName . category) . extract <$> head children
S.BlockExpression clauses -> termNameFromChildren term clauses
S.Rescue args _ -> intercalate ", " $ toTermName' <$> args
where toTermName' = toTermName source
termNameFromChildren term children = termNameFromRange (unionRangesFrom (range term) (range <$> children))

View File

@ -94,7 +94,7 @@ termConstructor source sourceSpan name range children
("while_statement", expr : rest ) -> S.While expr rest
("while_statement", _ ) -> S.Error children
("yield", _) -> S.Yield (listToMaybe children)
_ | name `elem` blocks -> S.BlockExpression Nothing children
_ | name `elem` blocks -> S.BlockExpression children
_ | name `elem` operators -> S.Operator children
_ | name `elem` functions -> case children of
[ body ] -> S.AnonymousFunction [] [body]

View File

@ -129,6 +129,6 @@ syntaxToTermField syntax = case syntax of
S.Yield expr -> [ "yieldExpression" .= expr ]
S.Until expr body -> [ "untilExpr" .= expr ] <> [ "untilBody" .= body ]
S.Unless expr clauses -> [ "unless" .= expr ] <> childrenFields clauses
S.BlockExpression condition expressions -> [ "condition" .= condition ] <> childrenFields expressions
S.BlockExpression expressions -> childrenFields expressions
S.Rescue args expressions -> [ "args" .= args ] <> childrenFields expressions
where childrenFields c = [ "children" .= c ]

View File

@ -81,8 +81,8 @@ data Syntax a f
| Until { untilExpr :: f, untilBody :: [f] }
-- | An unless statement with an expression and maybe more expression clauses.
| Unless f [f]
-- | A block expression might have a conditional expression and always has a list of expressions (e.g. begin, else, ensure in Ruby).
| BlockExpression (Maybe f) [f]
-- | A block expression has a list of expressions (e.g. begin, else, ensure in Ruby).
| BlockExpression [f]
-- | A rescue block has a list of arguments to rescue and a list of expressions.
| Rescue [f] [f]
deriving (Eq, Foldable, Functor, Generic, Generic1, Mergeable, Ord, Show, Traversable, ToJSON)