1
1
mirror of https://github.com/github/semantic.git synced 2025-01-02 12:23:08 +03:00

Implement unless as a Negate syntax

This commit is contained in:
Timothy Clem 2016-11-07 13:12:53 -08:00
parent 363fb62bd7
commit cafcd8fd91
5 changed files with 24 additions and 15 deletions

View File

@ -219,7 +219,9 @@ toTermName source term = case unwrap term of
S.Return expr -> maybe "empty" toTermName' expr
S.Yield expr -> maybe "empty" toTermName' expr
S.Error _ -> termNameFromSource term
S.If expr _ -> termNameFromSource expr
S.If expr _ -> case unwrap expr of
(S.Negate condition) -> termNameFromSource condition
_ -> termNameFromSource expr
S.For clauses _ -> termNameFromChildren term clauses
S.While expr _ -> toTermName' expr
S.DoWhile _ expr -> toTermName' expr
@ -239,7 +241,7 @@ toTermName source term = case unwrap term of
S.Export (Just identifier) expr -> "{ " <> intercalate ", " (termNameFromSource <$> expr) <> " }" <> " from " <> toTermName' identifier
S.ConditionalAssignment id _ -> toTermName' id
S.Until expr _ -> toTermName' expr
S.Unless expr _ -> termNameFromSource expr
S.Negate expr -> toTermName' expr
S.Rescue args _ -> intercalate ", " $ toTermName' <$> args
where toTermName' = toTermName source
termNameFromChildren term children = termNameFromRange (unionRangesFrom (range term) (range <$> children))

View File

@ -25,6 +25,16 @@ termConstructor
-> IO (Term (S.Syntax Text) (Record '[Range, Category, SourceSpan])) -- ^ The resulting term, in IO.
termConstructor source sourceSpan name range children
| name == "ERROR" = withDefaultInfo (S.Error children)
| name == "unless_modifier" = case children of
[ lhs, rhs ] -> do
condition <- withDefaultInfo (S.Negate rhs)
withDefaultInfo $ S.If condition [lhs]
_ -> withDefaultInfo $ S.Error children
| name == "unless_statement" = case children of
( expr : rest ) -> do
condition <- withDefaultInfo (S.Negate expr)
withDefaultInfo $ S.If condition rest
_ -> withDefaultInfo $ S.Error children
| otherwise = withDefaultInfo $ case (name, children) of
("array", _ ) -> S.Array children
("assignment", [ identifier, value ]) -> S.Assignment identifier value
@ -75,7 +85,7 @@ termConstructor source sourceSpan name range children
("member_access", [ base, property ]) -> S.MemberAccess base property
("member_access", _ ) -> S.Error children
("method_declaration", _ ) -> case children of
identifier : params : body | category (extract params) == Params -> S.Method identifier (toList (unwrap params)) body
identifier : params : body | Params <- category (extract params) -> S.Method identifier (toList (unwrap params)) body
identifier : body -> S.Method identifier [] body
_ -> S.Error children
("module_declaration", identifier : body ) -> S.Module identifier body
@ -90,9 +100,6 @@ termConstructor source sourceSpan name range children
("rescue_modifier", [lhs, rhs] ) -> S.Rescue [lhs] [rhs]
("rescue_modifier", _ ) -> S.Error children
("return_statement", _ ) -> S.Return (listToMaybe children)
("unless_modifier", [ lhs, condition ]) -> S.Unless condition [lhs]
("unless_modifier", _ ) -> S.Error children
("unless_statement", expr : rest ) -> S.Unless expr rest
("unless_statement", _ ) -> S.Error children
("until_modifier", [ lhs, condition ]) -> S.Until condition [lhs]
("until_modifier", _ ) -> S.Error children

View File

@ -128,6 +128,6 @@ syntaxToTermField syntax = case syntax of
S.ConditionalAssignment id value -> [ "conditionalIdentifier" .= id ] <> [ "value" .= value ]
S.Yield expr -> [ "yieldExpression" .= expr ]
S.Until expr body -> [ "untilExpr" .= expr ] <> [ "untilBody" .= body ]
S.Unless expr clauses -> [ "unless" .= expr ] <> childrenFields clauses
S.Negate expr -> [ "negate" .= expr ]
S.Rescue args expressions -> [ "args" .= args ] <> childrenFields expressions
where childrenFields c = [ "children" .= c ]

View File

@ -80,8 +80,8 @@ data Syntax a f
| ConditionalAssignment { conditionalAssignmentId :: f, value :: f }
| Yield (Maybe f)
| Until { untilExpr :: f, untilBody :: [f] }
-- | An unless statement with an expression and maybe more expression clauses.
| Unless f [f]
-- | A negation has a single expression.
| Negate 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)

View File

@ -149,7 +149,7 @@
}
]
},
"summary": "Replaced the 'x' identifier with the 'foo' identifier"
"summary": "Replaced the 'x' identifier with the 'foo' identifier in the foo unless statement"
},
{
"span": {
@ -164,7 +164,7 @@
]
}
},
"summary": "Added the 'bar' identifier"
"summary": "Added the 'bar' identifier in the foo unless statement"
},
{
"span": {
@ -179,7 +179,7 @@
]
}
},
"summary": "Added the 'bat' identifier"
"summary": "Added the 'bat' identifier in the foo unless statement"
},
{
"span": {
@ -253,7 +253,7 @@
}
]
},
"summary": "Replaced the 'foo' identifier with the 'x' identifier"
"summary": "Replaced the 'foo' identifier with the 'x' identifier in the x unless statement"
},
{
"span": {
@ -268,7 +268,7 @@
]
}
},
"summary": "Deleted the 'bar' identifier"
"summary": "Deleted the 'bar' identifier in the x unless statement"
},
{
"span": {
@ -283,7 +283,7 @@
]
}
},
"summary": "Deleted the 'bat' identifier"
"summary": "Deleted the 'bat' identifier in the x unless statement"
},
{
"span": {