mirror of
https://github.com/github/semantic.git
synced 2024-12-19 12:51:52 +03:00
Merge pull request #688 from github/if-syntax
Add If constructor to Syntax
This commit is contained in:
commit
2ac77b0afa
@ -60,6 +60,8 @@ data Category
|
||||
| VarDecl
|
||||
-- | A switch expression.
|
||||
| Switch
|
||||
-- | A if/else expression.
|
||||
| If
|
||||
-- | A for expression.
|
||||
| For
|
||||
-- | A while expression.
|
||||
|
@ -98,6 +98,7 @@ toTermName source term = case unwrap term of
|
||||
S.Pair a b -> toTermName' a <> ": " <> toTermName' b
|
||||
S.Return expr -> maybe "empty" toTermName' expr
|
||||
S.Error _ _ -> termNameFromSource term
|
||||
S.If expr _ _ -> termNameFromSource expr
|
||||
S.For _ _ -> termNameFromChildren term
|
||||
S.While expr _ -> toTermName' expr
|
||||
S.DoWhile _ expr -> toTermName' expr
|
||||
@ -211,6 +212,7 @@ instance HasCategory Category where
|
||||
C.Finally -> "finally statement"
|
||||
C.Class -> "class"
|
||||
C.Method -> "method"
|
||||
C.If -> "if statement"
|
||||
|
||||
instance (HasCategory leaf, HasField fields Category) => HasCategory (Term leaf (Record fields)) where
|
||||
toCategoryName = toCategoryName . category . extract
|
||||
|
@ -9,7 +9,7 @@ import Info
|
||||
import qualified Syntax as S
|
||||
import Term
|
||||
import qualified Data.Set as Set
|
||||
import Source
|
||||
import Source hiding (uncons)
|
||||
import SourceSpan
|
||||
|
||||
-- | A function that takes a source blob and returns an annotated AST.
|
||||
@ -91,6 +91,11 @@ termConstructor source sourceSpan info = cofree . construct
|
||||
construct children | Pair == (category info) = withDefaultInfo $ S.Fixed children
|
||||
construct children | C.Error == category info =
|
||||
withDefaultInfo $ S.Error sourceSpan children
|
||||
construct children | If == category info, Just (expr, clauses) <- uncons children =
|
||||
withDefaultInfo $ case clauses of
|
||||
[clause1, clause2] -> S.If expr clause1 (Just clause2)
|
||||
[clause] -> S.If expr clause Nothing
|
||||
_ -> S.Error sourceSpan children
|
||||
construct children | For == category info, Just (exprs, body) <- unsnoc children =
|
||||
withDefaultInfo $ S.For exprs body
|
||||
construct children | While == category info, [expr, body] <- children =
|
||||
|
@ -71,6 +71,7 @@ termFields info syntax = "range" .= characterRange info : "category" .= category
|
||||
S.Args c -> childrenFields c
|
||||
S.Assignment assignmentId property -> [ "assignmentIdentifier" .= assignmentId ] <> [ "property" .= property ]
|
||||
S.MemberAccess memberId value -> [ "memberIdentifier" .= memberId ] <> [ "value" .= value ]
|
||||
S.If expr clause maybeClause -> [ "if" .= expr ] <> [ "ifBody" .= clause ] <> [ "elseBody" .= maybeClause ]
|
||||
S.For exprs body -> [ "forExpressions" .= exprs ] <> [ "forBody" .= body ]
|
||||
S.While expr body -> [ "whileExpr" .= expr ] <> [ "whileBody" .= body ]
|
||||
S.DoWhile expr body -> [ "doWhileExpr" .= expr ] <> [ "doWhileBody" .= body ]
|
||||
|
@ -72,6 +72,7 @@ styleName category = "category-" <> case category of
|
||||
ArrayLiteral -> "array"
|
||||
C.Class -> "class_statement"
|
||||
C.Method -> "method"
|
||||
C.If -> "if_statement"
|
||||
Other string -> string
|
||||
|
||||
-- | Pick the class name for a split patch.
|
||||
@ -83,7 +84,7 @@ splitPatchToClassName patch = stringValue $ "patch " <> case patch of
|
||||
|
||||
-- | Render a diff as an HTML split diff.
|
||||
split :: (HasField fields Category, HasField fields Cost, HasField fields Range) => Renderer (Record fields)
|
||||
split blobs diff = SplitOutput $ TL.toStrict . renderHtml
|
||||
split blobs diff = SplitOutput . TL.toStrict . renderHtml
|
||||
. docTypeHtml
|
||||
. ((head $ link ! A.rel "stylesheet" ! A.href "style.css") <>)
|
||||
. body
|
||||
|
@ -69,6 +69,7 @@ data Syntax a f
|
||||
| Class f (Maybe f) [f]
|
||||
-- | A method definition with an identifier, params, and a list of expressions.
|
||||
| Method f [f] [f]
|
||||
| If f f (Maybe f)
|
||||
deriving (Eq, Foldable, Functor, Generic, Generic1, Mergeable, Ord, Show, Traversable)
|
||||
|
||||
|
||||
|
@ -51,6 +51,7 @@ categoriesForLanguage language name = case (language, name) of
|
||||
(JavaScript, "class") -> Class
|
||||
(JavaScript, "catch") -> Catch
|
||||
(JavaScript, "finally") -> Finally
|
||||
(JavaScript, "if_statement") -> If
|
||||
|
||||
(Ruby, "hash") -> Object
|
||||
_ -> defaultCategoryForNodeName name
|
||||
|
Loading…
Reference in New Issue
Block a user