Copyright | (c) 2019-2021 Vaclav Svejcar |
---|---|
License | BSD-3-Clause |
Maintainer | vaclav.svejcar@gmail.com |
Stability | experimental |
Portability | POSIX |
Safe Haskell | None |
Language | Haskell2010 |
This module contains data types and function used for analysis and type safe representation of source code files.
Synopsis
- data LineType
- type CodeLine = (LineType, Text)
- newtype SourceCode = SourceCode [CodeLine]
- fromText :: a -> (Text -> State a LineType) -> Text -> SourceCode
- toText :: SourceCode -> Text
- firstMatching :: (CodeLine -> Maybe a) -> SourceCode -> Maybe (Int, a)
- lastMatching :: (CodeLine -> Maybe a) -> SourceCode -> Maybe (Int, a)
- stripStart :: SourceCode -> SourceCode
- stripEnd :: SourceCode -> SourceCode
- cut :: Int -> Int -> SourceCode -> SourceCode
Data Types
Represents type of the line in source code.
newtype SourceCode Source #
Represents analyzed source code.
Instances
Eq SourceCode Source # | |
Defined in Headroom.SourceCode (==) :: SourceCode -> SourceCode -> Bool # (/=) :: SourceCode -> SourceCode -> Bool # | |
Show SourceCode Source # | |
Defined in Headroom.SourceCode showsPrec :: Int -> SourceCode -> ShowS # show :: SourceCode -> String # showList :: [SourceCode] -> ShowS # | |
Semigroup SourceCode Source # | |
Defined in Headroom.SourceCode (<>) :: SourceCode -> SourceCode -> SourceCode # sconcat :: NonEmpty SourceCode -> SourceCode # stimes :: Integral b => b -> SourceCode -> SourceCode # | |
Monoid SourceCode Source # | |
Defined in Headroom.SourceCode mempty :: SourceCode # mappend :: SourceCode -> SourceCode -> SourceCode # mconcat :: [SourceCode] -> SourceCode # |
Functions
:: a | initial state of analyzing function |
-> (Text -> State a LineType) | function that analyzes currently processed line |
-> Text | raw source code to analyze |
-> SourceCode | analyzed |
Converts Text
into SourceCode
using the given function to analyze
each line's LineType
. The analyzing function can hold any state that is
accumulated as the text is processed, for example to hold some info about
already processed lines.
:: SourceCode | source code to convert back to plain text |
-> Text | resulting plain text |
Converts analyzed SourceCode
back into Text
.
:: (CodeLine -> Maybe a) | predicate (and transform) function |
-> SourceCode | source code to search in |
-> Maybe (Int, a) | first matching line (if found) |
Finds very first line matching given predicate and optionally performs some operation over it.
:: (CodeLine -> Maybe a) | predicate (and transform) function |
-> SourceCode | source code to search in |
-> Maybe (Int, a) | last matching line (if found) |
Finds very last line matching given predicate and optionally performs some operation over it.
:: SourceCode | source code to strip |
-> SourceCode | stripped source code |
Strips empty lines at the beginning of source code.
>>>
stripStart $ SourceCode [(Code, ""), (Code, "foo"), (Code, "")]
SourceCode [(Code,"foo"),(Code,"")]
:: SourceCode | source code to strip |
-> SourceCode | stripped source code |
Strips empty lines at the end of source code.
>>>
stripEnd $ SourceCode [(Code, ""), (Code, "foo"), (Code, "")]
SourceCode [(Code,""),(Code,"foo")]
:: Int | index of first line to be included into the snippet |
-> Int | index of the first line after the snippet |
-> SourceCode | source code to cut |
-> SourceCode | cut snippet |
Cuts snippet from the source code using the given start and end position.
>>>
cut 1 3 $ SourceCode [(Code, "1"), (Code, "2"),(Code, "3"),(Code, "4")]
SourceCode [(Code,"2"),(Code,"3")]