1
1
mirror of https://github.com/github/semantic.git synced 2024-12-21 05:41:54 +03:00

Merge pull request #1204 from github/python-slice-expressions

Assign slice expressions
This commit is contained in:
Rick Winfrey 2017-07-13 10:26:40 -07:00 committed by GitHub
commit 1efabaed9a
6 changed files with 25 additions and 11 deletions

View File

@ -82,6 +82,13 @@ data Subscript a
instance Eq1 Subscript where liftEq = genericLiftEq
instance Show1 Subscript where liftShowsPrec = genericLiftShowsPrec
-- | Enumeration (e.g. a[1:10:1] in Python (start at index 1, stop at index 10, step 1 element from start to stop))
data Enumeration a = Enumeration { enumerationStart :: !a, enumerationEnd :: !a, enumerationStep :: !a }
deriving (Eq, Foldable, Functor, GAlign, Generic1, Show, Traversable)
instance Eq1 Enumeration where liftEq = genericLiftEq
instance Show1 Enumeration where liftShowsPrec = genericLiftShowsPrec
-- | ScopeResolution (e.g. import a.b in Python or a::b in C++)
data ScopeResolution a
= ScopeResolution ![a]

View File

@ -42,14 +42,6 @@ newtype Float a = Float { floatContent :: ByteString }
instance Eq1 Data.Syntax.Literal.Float where liftEq = genericLiftEq
instance Show1 Data.Syntax.Literal.Float where liftShowsPrec = genericLiftShowsPrec
data Range a = Range { rangeStart :: !a, rangeEnd :: !a }
deriving (Eq, Foldable, Functor, GAlign, Generic1, Show, Traversable)
instance Eq1 Range where liftEq = genericLiftEq
instance Show1 Range where liftShowsPrec = genericLiftShowsPrec
-- Strings, symbols
newtype String a = String { stringElements :: [a] }

View File

@ -39,6 +39,7 @@ type Syntax =
, Expression.Bitwise
, Expression.Call
, Expression.Comparison
, Expression.Enumeration
, Expression.ScopeResolution
, Expression.MemberAccess
, Expression.Subscript
@ -164,6 +165,7 @@ expression = argument
<|> memberAccess
<|> notOperator
<|> parameter
<|> slice
<|> subscript
<|> statement
<|> tuple
@ -423,6 +425,12 @@ memberAccess = makeTerm <$> symbol Attribute <*> children (Expression.MemberAcce
subscript :: Assignment
subscript = makeTerm <$> symbol Subscript <*> children (Expression.Subscript <$> expression <*> many expression)
slice :: Assignment
slice = makeTerm <$> symbol Slice <*> children
(Expression.Enumeration <$> ((emptyTerm <* symbol AnonColon <* source) <|> (expression <* symbol AnonColon <* source))
<*> ((emptyTerm <* symbol AnonColon <* source) <|> (expression <* symbol AnonColon <* source) <|> (expression <|> emptyTerm))
<*> (expression <|> emptyTerm))
call :: Assignment
call = makeTerm <$> symbol Call <*> children (Expression.Call <$> identifier <*> (symbol ArgumentList *> children (many expression)
<|> some comprehension) <*> emptyTerm)

View File

@ -33,6 +33,7 @@ type Syntax = '[
, Expression.Boolean
, Expression.Call
, Expression.Comparison
, Expression.Enumeration
, Expression.MemberAccess
, Expression.ScopeResolution
, Expression.Subscript
@ -43,7 +44,6 @@ type Syntax = '[
, Literal.Integer
, Literal.KeyValue
, Literal.Null
, Literal.Range
, Literal.String
, Literal.Symbol
, Literal.TextElement
@ -148,7 +148,7 @@ literal =
<|> makeTerm <$> symbol Grammar.Float <*> (Literal.Float <$> source)
<|> makeTerm <$> symbol Grammar.Nil <*> (Literal.Null <$ source)
-- TODO: Do we want to represent the difference between .. and ...
<|> makeTerm <$> symbol Range <*> children (Literal.Range <$> statement <*> statement)
<|> makeTerm <$> symbol Range <*> children (Expression.Enumeration <$> statement <*> statement <*> emptyTerm)
<|> makeTerm <$> symbol Array <*> children (Literal.Array <$> many statement)
<|> makeTerm <$> symbol Hash <*> children (Literal.Hash <$> many pair)
-- TODO: Give subshell it's own literal and allow interpolation
@ -334,7 +334,6 @@ assignment'
<|> makeTerm <$> symbol DestructuredLeftAssignment <*> children (many expr)
<|> argument
unary :: Assignment
unary = symbol Unary >>= \ location ->
makeTerm location . Expression.Complement <$> children ( symbol AnonTilde *> statement )

4
test/fixtures/python/slice.A.py vendored Normal file
View File

@ -0,0 +1,4 @@
a[:]
b[5:]
b[5:6, ...]
c[::]

4
test/fixtures/python/slice.B.py vendored Normal file
View File

@ -0,0 +1,4 @@
c[4:5, ...]
a[:]
d[3:]
e[::]