hpython-0.1: Syntax tree and DSL for Python

Copyright(C) CSIRO 2017-2018
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) #

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 #

HasNewlines (Blank a) Source # 
Instance details

Defined in Language.Python.Optics.Newlines

Methods

_Newlines :: Traversal' (Blank a) Newline Source #

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

Methods

trailingWhitespace :: Lens' (UnOp a) [Whitespace] Source #

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

Methods

trailingWhitespace :: Lens' (Ident v a) [Whitespace] Source #

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

Methods

trailingWhitespace :: Lens' (Expr v a) [Whitespace] Source #

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

Methods

trailingWhitespace :: Lens' (SetItem v a) [Whitespace] Source #

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

Methods

trailingWhitespace :: Lens' (CompFor v a) [Whitespace] Source #

HasTrailingWhitespace (CompIf v a) Source # 
Instance details

Defined in Language.Python.Syntax.Expr

Methods

trailingWhitespace :: Lens' (CompIf v a) [Whitespace] Source #

HasTrailingWhitespace (Param v a) Source # 
Instance details

Defined in Language.Python.Syntax.Expr

Methods

trailingWhitespace :: Lens' (Param v a) [Whitespace] Source #

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

Defined in Language.Python.Syntax.Import

Methods

trailingWhitespace :: Lens' (ImportAs e v a) [Whitespace] Source #

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)

Methods

trailingNewline :: Traversal' (s v a) Newline Source #

setTrailingNewline :: s v a -> Newline -> s v a Source #

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 #

Constructors

MkIndent 

Fields

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

Methods

_Newlines :: Traversal' Indent Newline Source #

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 #

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 #

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

Methods

_Newlines :: Traversal' (Indents a) Newline Source #

indentsValue :: forall a. Lens' (Indents a) [Indent] Source #

indentsAnn :: forall a a. Lens (Indents a) (Indents a) a 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.