hpython-0.2: Python language tools

Copyright(C) CSIRO 2017-2019
LicenseBSD3
MaintainerIsaac Elliott <isaace71295@gmail.com>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Language.Python.Syntax.Whitespace

Contents

Description

 
Synopsis

Whitespace

data Newline Source #

A newline is either a carriage return, a line feed, or a carriage return followed by a line feed.

Constructors

CR 
LF 
CRLF 

data Whitespace Source #

Whitespace is either a space, a tab, a newline that continues the logical line (Continued), a newline that ends the logical line (Newline), or a Whitespace.

Despite not literally being whitespace, comments inside enclosed forms are treated as whitespace. See https://docs.python.org/3.5/reference/lexical_analysis.html#implicit-line-joining

Example and counterexample of comments as whitespace

( 1 +
  # here's a comment
 2 +
 3 # another comment
)
 
x = 5 + 5
# this line is not considered whitespace
y = x * 2
[ 1
, 2 # I'm whitespace
, 3
# also whitespace
]

data Blank a Source #

Lines which are "blank", meaning that they contain, if anything, only whitespace and/or a comment.

Constructors

Blank 
Instances
Functor Blank Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

Methods

fmap :: (a -> b) -> Blank a -> Blank b #

(<$) :: a -> Blank b -> Blank a #

Foldable Blank Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

Methods

fold :: Monoid m => Blank m -> m #

foldMap :: Monoid m => (a -> m) -> Blank a -> m #

foldr :: (a -> b -> b) -> b -> Blank a -> b #

foldr' :: (a -> b -> b) -> b -> Blank a -> b #

foldl :: (b -> a -> b) -> b -> Blank a -> b #

foldl' :: (b -> a -> b) -> b -> Blank a -> b #

foldr1 :: (a -> a -> a) -> Blank a -> a #

foldl1 :: (a -> a -> a) -> Blank a -> a #

toList :: Blank a -> [a] #

null :: Blank a -> Bool #

length :: Blank a -> Int #

elem :: Eq a => a -> Blank a -> Bool #

maximum :: Ord a => Blank a -> a #

minimum :: Ord a => Blank a -> a #

sum :: Num a => Blank a -> a #

product :: Num a => Blank a -> a #

Traversable Blank Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

Methods

traverse :: Applicative f => (a -> f b) -> Blank a -> f (Blank b) #

sequenceA :: Applicative f => Blank (f a) -> f (Blank a) #

mapM :: Monad m => (a -> m b) -> Blank a -> m (Blank b) #

sequence :: Monad m => Blank (m a) -> m (Blank a) #

HasAnn Blank Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

Methods

annot :: Lens' (Blank a) (Ann a) Source #

Eq a => Eq (Blank a) Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

Methods

(==) :: Blank a -> Blank a -> Bool #

(/=) :: Blank a -> Blank a -> Bool #

Show a => Show (Blank a) Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

Methods

showsPrec :: Int -> Blank a -> ShowS #

show :: Blank a -> String #

showList :: [Blank a] -> ShowS #

Generic (Blank a) Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

Associated Types

type Rep (Blank a) :: Type -> Type #

Methods

from :: Blank a -> Rep (Blank a) x #

to :: Rep (Blank a) x -> Blank a #

HasNewlines (Blank a) Source # 
Instance details

Defined in Language.Python.Optics.Newlines

type Rep (Blank a) Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

type Rep (Blank a) = D1 (MetaData "Blank" "Language.Python.Syntax.Whitespace" "hpython-0.2-7fNN6PEHntyHCqZaN2NGK0" False) (C1 (MetaCons "Blank" PrefixI True) (S1 (MetaSel (Just "_blankAnn") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 (Ann a)) :*: (S1 (MetaSel (Just "_blankWhitespaces") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [Whitespace]) :*: S1 (MetaSel (Just "_blankComment") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 (Maybe (Comment a))))))

class HasTrailingWhitespace s where Source #

Every syntactic element contains the whitespace that immediately follows it.

This type class lets us access this trailing whitespace in many different types throughout hpython.

Instances
HasTrailingWhitespace At Source # 
Instance details

Defined in Language.Python.Syntax.Punctuation

HasTrailingWhitespace Equals Source # 
Instance details

Defined in Language.Python.Syntax.Punctuation

HasTrailingWhitespace Colon Source # 
Instance details

Defined in Language.Python.Syntax.Punctuation

HasTrailingWhitespace Comma Source # 
Instance details

Defined in Language.Python.Syntax.Punctuation

HasTrailingWhitespace Dot Source # 
Instance details

Defined in Language.Python.Syntax.Punctuation

HasTrailingWhitespace a => HasTrailingWhitespace (NonEmpty a) Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

HasTrailingWhitespace (StringLiteral a) Source # 
Instance details

Defined in Language.Python.Syntax.Strings

HasTrailingWhitespace (Semicolon a) Source # 
Instance details

Defined in Language.Python.Syntax.Punctuation

HasTrailingWhitespace (UnOp a) Source # 
Instance details

Defined in Language.Python.Syntax.Operator.Unary

HasTrailingWhitespace (BinOp a) Source # 
Instance details

Defined in Language.Python.Syntax.Operator.Binary

HasTrailingWhitespace s => HasTrailingWhitespace (CommaSep1' s) Source # 
Instance details

Defined in Language.Python.Syntax.CommaSep

HasTrailingWhitespace s => HasTrailingWhitespace (CommaSep1 s) Source # 
Instance details

Defined in Language.Python.Syntax.CommaSep

HasTrailingWhitespace (AugAssign a) Source # 
Instance details

Defined in Language.Python.Syntax.AugAssign

HasTrailingWhitespace (Ident v a) Source # 
Instance details

Defined in Language.Python.Syntax.Ident

HasTrailingWhitespace (ModuleName v a) Source # 
Instance details

Defined in Language.Python.Syntax.ModuleNames

HasTrailingWhitespace (RelativeModuleName v a) Source # 
Instance details

Defined in Language.Python.Syntax.ModuleNames

HasTrailingWhitespace (ImportTargets v a) Source # 
Instance details

Defined in Language.Python.Syntax.Import

HasTrailingWhitespace (Expr v a) Source # 
Instance details

Defined in Language.Python.Syntax.Expr

HasTrailingWhitespace (TupleItem v a) Source # 
Instance details

Defined in Language.Python.Syntax.Expr

HasTrailingWhitespace (SetItem v a) Source # 
Instance details

Defined in Language.Python.Syntax.Expr

HasTrailingWhitespace (ListItem v a) Source # 
Instance details

Defined in Language.Python.Syntax.Expr

HasTrailingWhitespace (Subscript v a) Source # 
Instance details

Defined in Language.Python.Syntax.Expr

HasTrailingWhitespace (DictItem v a) Source # 
Instance details

Defined in Language.Python.Syntax.Expr

HasTrailingWhitespace (CompFor v a) Source # 
Instance details

Defined in Language.Python.Syntax.Expr

HasTrailingWhitespace (CompIf v a) Source # 
Instance details

Defined in Language.Python.Syntax.Expr

HasTrailingWhitespace (Param v a) Source # 
Instance details

Defined in Language.Python.Syntax.Expr

HasTrailingWhitespace (e v a) => HasTrailingWhitespace (ImportAs e v a) Source # 
Instance details

Defined in Language.Python.Syntax.Import

HasTrailingWhitespace (Comprehension e v a) Source # 
Instance details

Defined in Language.Python.Syntax.Expr

class HasTrailingNewline (s :: [*] -> * -> *) where Source #

A statement-containing thing may have a trailing newline

Some forms always have a trailing newline, which is why this class isn't just trailingNewline :: Lens' (s v a) (Maybe Newline)

Indentation

data IndentLevel Source #

Python has rules regarding the expansion of tabs into spaces and how to go about computing indentation after this is done.

See https://docs.python.org/3.5/reference/lexical_analysis.html#indentation

This data structure implements those rules as a monoid.

newtype Indent Source #

Instances
IsList Indent Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

Associated Types

type Item Indent :: Type #

Eq Indent Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

Methods

(==) :: Indent -> Indent -> Bool #

(/=) :: Indent -> Indent -> Bool #

Ord Indent Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

Show Indent Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

Semigroup Indent Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

Monoid Indent Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

HasNewlines Indent Source # 
Instance details

Defined in Language.Python.Optics.Newlines

type Item Indent Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

indentWhitespaces :: Iso' Indent [Whitespace] Source #

An Indent is isomorphic to a list of Whitespace

indentIt :: [Whitespace] -> Indents a -> Indents a Source #

Indent some indentation by a chunk

dedentIt :: Indents a -> Indents a Source #

Deent some indentation by a chunk

data Indents a Source #

A possibly annotated list of Indents.

Constructors

Indents 

Fields

Instances
Functor Indents Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

Methods

fmap :: (a -> b) -> Indents a -> Indents b #

(<$) :: a -> Indents b -> Indents a #

Foldable Indents Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

Methods

fold :: Monoid m => Indents m -> m #

foldMap :: Monoid m => (a -> m) -> Indents a -> m #

foldr :: (a -> b -> b) -> b -> Indents a -> b #

foldr' :: (a -> b -> b) -> b -> Indents a -> b #

foldl :: (b -> a -> b) -> b -> Indents a -> b #

foldl' :: (b -> a -> b) -> b -> Indents a -> b #

foldr1 :: (a -> a -> a) -> Indents a -> a #

foldl1 :: (a -> a -> a) -> Indents a -> a #

toList :: Indents a -> [a] #

null :: Indents a -> Bool #

length :: Indents a -> Int #

elem :: Eq a => a -> Indents a -> Bool #

maximum :: Ord a => Indents a -> a #

minimum :: Ord a => Indents a -> a #

sum :: Num a => Indents a -> a #

product :: Num a => Indents a -> a #

Traversable Indents Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

Methods

traverse :: Applicative f => (a -> f b) -> Indents a -> f (Indents b) #

sequenceA :: Applicative f => Indents (f a) -> f (Indents a) #

mapM :: Monad m => (a -> m b) -> Indents a -> m (Indents b) #

sequence :: Monad m => Indents (m a) -> m (Indents a) #

Eq1 Indents Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

Methods

liftEq :: (a -> b -> Bool) -> Indents a -> Indents b -> Bool #

Ord1 Indents Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

Methods

liftCompare :: (a -> b -> Ordering) -> Indents a -> Indents b -> Ordering #

HasAnn Indents Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

Methods

annot :: Lens' (Indents a) (Ann a) Source #

Eq a => Eq (Indents a) Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

Methods

(==) :: Indents a -> Indents a -> Bool #

(/=) :: Indents a -> Indents a -> Bool #

Show a => Show (Indents a) Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

Methods

showsPrec :: Int -> Indents a -> ShowS #

show :: Indents a -> String #

showList :: [Indents a] -> ShowS #

Generic (Indents a) Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

Associated Types

type Rep (Indents a) :: Type -> Type #

Methods

from :: Indents a -> Rep (Indents a) x #

to :: Rep (Indents a) x -> Indents a #

Semigroup a => Semigroup (Indents a) Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

Methods

(<>) :: Indents a -> Indents a -> Indents a #

sconcat :: NonEmpty (Indents a) -> Indents a #

stimes :: Integral b => b -> Indents a -> Indents a #

HasNewlines (Indents a) Source # 
Instance details

Defined in Language.Python.Optics.Newlines

type Rep (Indents a) Source # 
Instance details

Defined in Language.Python.Syntax.Whitespace

type Rep (Indents a) = D1 (MetaData "Indents" "Language.Python.Syntax.Whitespace" "hpython-0.2-7fNN6PEHntyHCqZaN2NGK0" False) (C1 (MetaCons "Indents" PrefixI True) (S1 (MetaSel (Just "_indentsValue") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [Indent]) :*: S1 (MetaSel (Just "_indentsAnn") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 (Ann a))))

indentsAnn :: forall a a. Lens (Indents a) (Indents a) (Ann a) (Ann a) Source #

subtractStart :: Indents a -> Indents a -> Maybe (Indents a) Source #

Subtract the first argument from the beginning of the second

Returns Nothing if the first list is not a prefix of the second.