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:
parent
7d8978f764
commit
39b7c05e50
@ -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
|
||||
|
@ -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)
|
||||
|
||||
|
3
test/fixtures/ruby/when-else.A.rb
vendored
3
test/fixtures/ruby/when-else.A.rb
vendored
@ -1,5 +1,6 @@
|
||||
case foo
|
||||
when bar
|
||||
when qux
|
||||
when bar, *a
|
||||
baz
|
||||
else
|
||||
end
|
||||
|
10
test/fixtures/ruby/when-else.diff+A.txt
vendored
10
test/fixtures/ruby/when-else.diff+A.txt
vendored
@ -4,5 +4,11 @@
|
||||
(When
|
||||
(Args
|
||||
(Identifier))
|
||||
(Identifier)
|
||||
(Else))))+}
|
||||
(When
|
||||
(Args
|
||||
(Identifier))
|
||||
(Args
|
||||
(Other "splat_argument"
|
||||
(Identifier)))
|
||||
(Identifier)
|
||||
(Else)))))+}
|
||||
|
10
test/fixtures/ruby/when-else.diff-A.txt
vendored
10
test/fixtures/ruby/when-else.diff-A.txt
vendored
@ -4,5 +4,11 @@
|
||||
(When
|
||||
(Args
|
||||
(Identifier))
|
||||
(Identifier)
|
||||
(Else))))-}
|
||||
(When
|
||||
(Args
|
||||
(Identifier))
|
||||
(Args
|
||||
(Other "splat_argument"
|
||||
(Identifier)))
|
||||
(Identifier)
|
||||
(Else)))))-}
|
||||
|
18
test/fixtures/ruby/when-else.diffA-B.txt
vendored
18
test/fixtures/ruby/when-else.diffA-B.txt
vendored
@ -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))+})))
|
||||
|
18
test/fixtures/ruby/when-else.diffB-A.txt
vendored
18
test/fixtures/ruby/when-else.diffB-A.txt
vendored
@ -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))-})))
|
||||
|
10
test/fixtures/ruby/when-else.parseA.txt
vendored
10
test/fixtures/ruby/when-else.parseA.txt
vendored
@ -4,5 +4,11 @@
|
||||
(When
|
||||
(Args
|
||||
(Identifier))
|
||||
(Identifier)
|
||||
(Else))))
|
||||
(When
|
||||
(Args
|
||||
(Identifier))
|
||||
(Args
|
||||
(Other "splat_argument"
|
||||
(Identifier)))
|
||||
(Identifier)
|
||||
(Else)))))
|
||||
|
Loading…
Reference in New Issue
Block a user