1
1
mirror of https://github.com/github/semantic.git synced 2024-11-28 10:15:55 +03:00

case/when/else

This commit is contained in:
Timothy Clem 2017-07-07 14:22:17 -07:00
parent 7d8978f764
commit 39b7c05e50
8 changed files with 66 additions and 21 deletions

View File

@ -24,14 +24,14 @@ instance Show1 Else where liftShowsPrec = genericLiftShowsPrec
-- TODO: Alternative definition would flatten if/else if/else chains: data If a = If ![(a, a)] !(Maybe a)
-- | A pattern-matching or computed jump control-flow statement, like 'switch' in C or JavaScript, or 'case' in Ruby or Haskell.
data Match with a = Match { matchSubject :: !a, matchPatterns :: ![with a] }
data Match a = Match { matchSubject :: !a, matchPatterns :: !a }
deriving (Eq, Foldable, Functor, GAlign, Generic1, Show, Traversable)
instance Eq1 with => Eq1 (Match with) where liftEq = genericLiftEq
instance Show1 with => Show1 (Match with) where liftShowsPrec = genericLiftShowsPrec
instance Eq1 Match where liftEq = genericLiftEq
instance Show1 Match where liftShowsPrec = genericLiftShowsPrec
-- | A pattern in a pattern-matching or computed jump control-flow statement, like 'case' in C or JavaScript, 'when' in Ruby, or the left-hand side of '->' in the body of Haskell 'case' expressions.
newtype Pattern a = Pattern a
data Pattern a = Pattern { pattern :: !a, patternBody :: !a }
deriving (Eq, Foldable, Functor, GAlign, Generic1, Show, Traversable)
instance Eq1 Pattern where liftEq = genericLiftEq

View File

@ -56,6 +56,8 @@ type Syntax = '[
, Statement.Finally
, Statement.ForEach
, Statement.If
, Statement.Match
, Statement.Pattern
, Statement.Retry
, Statement.Return
, Statement.ScopeEntry
@ -89,6 +91,7 @@ statement = -- handleError $
<|> unless
<|> while'
<|> until'
<|> case'
<|> emptyStatement
<|> assignment'
<|> unary
@ -255,6 +258,13 @@ until' =
for :: Assignment
for = makeTerm <$> symbol For <*> children (Statement.ForEach <$> some identifier <*> statement <*> statements)
case' :: Assignment
case' = makeTerm <$> symbol Case <*> children (Statement.Match <$> statement <*> when)
where
when = makeTerm <$> symbol When <*> children (Statement.Pattern <$> (makeTerm <$> location <*> some pattern) <*> (when <|> else' <|> statements))
else' = makeTerm <$> symbol Else <*> children (many statement)
pattern = symbol Pattern *> children ((symbol SplatArgument *> children statement) <|> statement)
subscript :: Assignment
subscript = makeTerm <$> symbol ElementReference <*> children (Expression.Subscript <$> statement <*> many argument)

View File

@ -1,5 +1,6 @@
case foo
when bar
when qux
when bar, *a
baz
else
end

View File

@ -4,5 +4,11 @@
(When
(Args
(Identifier))
(Identifier)
(Else))))+}
(When
(Args
(Identifier))
(Args
(Other "splat_argument"
(Identifier)))
(Identifier)
(Else)))))+}

View File

@ -4,5 +4,11 @@
(When
(Args
(Identifier))
(Identifier)
(Else))))-}
(When
(Args
(Identifier))
(Args
(Other "splat_argument"
(Identifier)))
(Identifier)
(Else)))))-}

View File

@ -3,8 +3,16 @@
(Identifier)
(When
(Args
(Identifier))
(Identifier)
{ (Else)
->(Else
(Identifier)) })))
{ (Identifier)
->(Identifier) })
{ (When
(Args
(Identifier))
(Args
(Other "splat_argument"
(Identifier)))
(Identifier)
(Else))
->(Identifier) }
{+(Else
(Identifier))+})))

View File

@ -3,8 +3,16 @@
(Identifier)
(When
(Args
(Identifier))
(Identifier)
{ (Else
(Identifier))
->(Else) })))
{ (Identifier)
->(Identifier) })
{ (Identifier)
->(When
(Args
(Identifier))
(Args
(Other "splat_argument"
(Identifier)))
(Identifier)
(Else)) }
{-(Else
(Identifier))-})))

View File

@ -4,5 +4,11 @@
(When
(Args
(Identifier))
(Identifier)
(Else))))
(When
(Args
(Identifier))
(Args
(Other "splat_argument"
(Identifier)))
(Identifier)
(Else)))))