Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
A collection of parsing algorithms with a common interface, operating on grammars represented as records with rank-2 field types.
Synopsis
- failureDescription :: forall s pos. (Ord s, TextualMonoid s, Position pos) => s -> ParseFailure pos s -> Int -> s
- simply :: (Only r (p (Only r) s) -> s -> Only r f) -> p (Only r) s r -> s -> f r
- type Grammar (g :: (Type -> Type) -> Type) p s = g (p g s)
- type GrammarBuilder (g :: (Type -> Type) -> Type) (g' :: (Type -> Type) -> Type) (p :: ((Type -> Type) -> Type) -> Type -> Type -> Type) (s :: Type) = g (p g' s) -> g (p g' s)
- type GrammarOverlay (g :: (Type -> Type) -> Type) (m :: Type -> Type) = g m -> g m -> g m
- type ParseResults s = Either (ParseFailure Pos s)
- data ParseFailure pos s = ParseFailure {
- failurePosition :: pos
- expectedAlternatives :: FailureDescription s
- errorAlternatives :: [String]
- data FailureDescription s = FailureDescription {
- staticDescriptions :: [String]
- literalDescriptions :: [s]
- newtype Ambiguous a = Ambiguous {
- getAmbiguous :: NonEmpty a
- type Pos = Down Int
- class Parsing m => DeterministicParsing (m :: Type -> Type) where
- class Alternative m => AmbiguousParsing m where
- class Alternative m => CommittedParsing m where
- type CommittedResults m :: Type -> Type
- commit :: m a -> m (CommittedResults m a)
- admit :: m (CommittedResults m a) -> m a
- class InputParsing m => TraceableParsing m where
- traceInput :: (ParserInput m -> String) -> m a -> m a
- traceAs :: Show (ParserInput m) => String -> m a -> m a
- class (DeterministicParsing m, InputCharParsing m, TokenParsing m) => LexicalParsing m where
- lexicalWhiteSpace :: m ()
- someLexicalSpace :: m ()
- lexicalComment :: m ()
- lexicalSemicolon :: m Char
- lexicalToken :: m a -> m a
- identifierToken :: m (ParserInput m) -> m (ParserInput m)
- isIdentifierStartChar :: Char -> Bool
- isIdentifierFollowChar :: Char -> Bool
- identifier :: m (ParserInput m)
- keyword :: ParserInput m -> m ()
- class InputParsing m => MultiParsing m where
- type ResultFunctor m :: Type -> Type
- type GrammarConstraint m (g :: (Type -> Type) -> Type) :: Constraint
- parseComplete :: (ParserInput m ~ s, GrammarConstraint m g, Eq s, FactorialMonoid s) => g m -> s -> g (ResultFunctor m)
- parsePrefix :: (ParserInput m ~ s, GrammarConstraint m g, Eq s, FactorialMonoid s) => g m -> s -> g (Compose (ResultFunctor m) ((,) s))
- class MultiParsing m => GrammarParsing m where
- type ParserGrammar m :: (Type -> Type) -> Type
- type GrammarFunctor m :: Type -> Type
- parsingResult :: ParserInput m -> GrammarFunctor m a -> ResultFunctor m (ParserInput m, a)
- nonTerminal :: (g ~ ParserGrammar m, GrammarConstraint m g) => (g (GrammarFunctor m) -> GrammarFunctor m a) -> m a
- selfReferring :: (g ~ ParserGrammar m, GrammarConstraint m g, Distributive g) => g m
- fixGrammar :: (g ~ ParserGrammar m, GrammarConstraint m g, Distributive g) => (g m -> g m) -> g m
- recursive :: m a -> m a
- chainRecursive :: (g ~ ParserGrammar m, f ~ GrammarFunctor m, GrammarConstraint m g) => (f a -> g f -> g f) -> m a -> m a -> m a
- chainLongestRecursive :: (g ~ ParserGrammar m, f ~ GrammarFunctor m, GrammarConstraint m g) => (f a -> g f -> g f) -> m a -> m a -> m a
- overlay :: (GrammarParsing m, g ~ ParserGrammar m, GrammarConstraint m g, Distributive g, Foldable f) => (g m -> g m) -> f (GrammarOverlay g m) -> g m
- class LookAheadParsing m => InputParsing (m :: Type -> Type) where
- type ParserInput (m :: Type -> Type)
- type ParserPosition (m :: Type -> Type)
- getInput :: m (ParserInput m)
- getSourcePos :: m (ParserPosition m)
- anyToken :: m (ParserInput m)
- take :: Int -> m (ParserInput m)
- satisfy :: (ParserInput m -> Bool) -> m (ParserInput m)
- notSatisfy :: (ParserInput m -> Bool) -> m ()
- scan :: state -> (state -> ParserInput m -> Maybe state) -> m (ParserInput m)
- string :: ParserInput m -> m (ParserInput m)
- takeWhile :: (ParserInput m -> Bool) -> m (ParserInput m)
- takeWhile1 :: (ParserInput m -> Bool) -> m (ParserInput m)
- class (CharParsing m, InputParsing m) => InputCharParsing (m :: Type -> Type) where
- satisfyCharInput :: (Char -> Bool) -> m (ParserInput m)
- notSatisfyChar :: (Char -> Bool) -> m ()
- scanChars :: state -> (state -> Char -> Maybe state) -> m (ParserInput m)
- takeCharsWhile :: (Char -> Bool) -> m (ParserInput m)
- takeCharsWhile1 :: (Char -> Bool) -> m (ParserInput m)
- class InputParsing m => ConsumedInputParsing (m :: Type -> Type) where
- match :: m a -> m (ParserInput m, a)
- class Ord p => Position p where
- class Parsing m => CharParsing (m :: Type -> Type) where
- class Alternative m => Parsing (m :: Type -> Type) where
- (<?>) :: m a -> String -> m a
- skipMany :: m a -> m ()
- skipSome :: m a -> m ()
- unexpected :: String -> m a
- notFollowedBy :: Show a => m a -> m ()
- class Parsing m => LookAheadParsing (m :: Type -> Type) where
- lookAhead :: m a -> m a
- class CharParsing m => TokenParsing (m :: Type -> Type) where
- concatMany :: (Alternative p, Monoid a) => p a -> p a
- concatSome :: (Alternative p, Semigroup a) => p a -> p a
Applying parsers
failureDescription :: forall s pos. (Ord s, TextualMonoid s, Position pos) => s -> ParseFailure pos s -> Int -> s Source #
Given the textual parse input, the parse failure on the input, and the number of lines preceding the failure to show, produce a human-readable failure description.
simply :: (Only r (p (Only r) s) -> s -> Only r f) -> p (Only r) s r -> s -> f r Source #
Apply the given parsing function (typically parseComplete
or parsePrefix
) to the given grammar-agnostic
parser and its input. A typical invocation might be
getCompose $ simply parsePrefix myParser myInput
Types
type Grammar (g :: (Type -> Type) -> Type) p s = g (p g s) Source #
A grammar is a record type g
whose fields are parsers of type p
on input streams of type s
. A value of a
Grammar
type is typically produced by applying fixGrammar
or overlay
to a GrammarBuilder
.
type GrammarBuilder (g :: (Type -> Type) -> Type) (g' :: (Type -> Type) -> Type) (p :: ((Type -> Type) -> Type) -> Type -> Type -> Type) (s :: Type) = g (p g' s) -> g (p g' s) Source #
A GrammarBuilder g g' p s
is an endomorphic function on a grammar g
, whose parsers of type p
build on
grammars of type g'
and parse an input stream of type s
. Grammar parameters g
and g'
are typically
identical in simple monolithic grammars, but when composing complex grammars the first grammar parameter g
would
be just a building block for the final grammar g'
.
type GrammarOverlay (g :: (Type -> Type) -> Type) (m :: Type -> Type) = g m -> g m -> g m Source #
A grammar overlay is a function that takes a final grammar self
and the parent grammar super
and builds a new
grammar from them. Use overlay
to apply a colection of overlays on top of a base grammar.
type ParseResults s = Either (ParseFailure Pos s) Source #
A parse results in either a ParseFailure
or the result of the appropriate type.
data ParseFailure pos s Source #
A ParseFailure
contains the offset of the parse failure and the list of things expected at that offset.
ParseFailure | |
|
Instances
data FailureDescription s Source #
An expected or erroneous input can be described using String
or using the input type
Instances
An Ambiguous
parse result, produced by the ambiguous
combinator, contains a NonEmpty
list of
alternative results.
Instances
Foldable Ambiguous Source # | |
Defined in Text.Grampa.Class fold :: Monoid m => Ambiguous m -> m # foldMap :: Monoid m => (a -> m) -> Ambiguous a -> m # foldMap' :: Monoid m => (a -> m) -> Ambiguous a -> m # foldr :: (a -> b -> b) -> b -> Ambiguous a -> b # foldr' :: (a -> b -> b) -> b -> Ambiguous a -> b # foldl :: (b -> a -> b) -> b -> Ambiguous a -> b # foldl' :: (b -> a -> b) -> b -> Ambiguous a -> b # foldr1 :: (a -> a -> a) -> Ambiguous a -> a # foldl1 :: (a -> a -> a) -> Ambiguous a -> a # toList :: Ambiguous a -> [a] # length :: Ambiguous a -> Int # elem :: Eq a => a -> Ambiguous a -> Bool # maximum :: Ord a => Ambiguous a -> a # minimum :: Ord a => Ambiguous a -> a # | |
Show1 Ambiguous Source # | |
Traversable Ambiguous Source # | |
Applicative Ambiguous Source # | |
Functor Ambiguous Source # | |
Monad Ambiguous Source # | |
Data a => Data (Ambiguous a) Source # | |
Defined in Text.Grampa.Class gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Ambiguous a -> c (Ambiguous a) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Ambiguous a) # toConstr :: Ambiguous a -> Constr # dataTypeOf :: Ambiguous a -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Ambiguous a)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Ambiguous a)) # gmapT :: (forall b. Data b => b -> b) -> Ambiguous a -> Ambiguous a # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Ambiguous a -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Ambiguous a -> r # gmapQ :: (forall d. Data d => d -> u) -> Ambiguous a -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Ambiguous a -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Ambiguous a -> m (Ambiguous a) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Ambiguous a -> m (Ambiguous a) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Ambiguous a -> m (Ambiguous a) # | |
Monoid a => Monoid (Ambiguous a) Source # | |
Semigroup a => Semigroup (Ambiguous a) Source # | |
Show a => Show (Ambiguous a) Source # | |
Eq a => Eq (Ambiguous a) Source # | |
Ord a => Ord (Ambiguous a) Source # | |
Defined in Text.Grampa.Class | |
AmbiguityDecidable (Ambiguous a) Source # | |
Defined in Text.Grampa.Internal ambiguityWitness :: Maybe (AmbiguityWitness (Ambiguous a)) |
Classes
Parsing
class Parsing m => DeterministicParsing (m :: Type -> Type) where #
Combinator methods for constructing deterministic parsers, i.e., parsers that can succeed with only a single result.
Nothing
(<<|>) :: m a -> m a -> m a infixl 3 #
Left-biased choice: if the left alternative succeeds, the right one is never tried.
takeOptional :: m a -> m (Maybe a) #
Like optional
, but never succeeds with Nothing
if the argument parser can succeed.
Like many
, but always consuming the longest matching sequence of input.
Like some
, but always consuming the longest matching sequence of input.
concatAll :: Monoid a => m a -> m a #
Like concatMany
, but always consuming the longest matching sequence of input.
Like skipMany
, but always consuming the longest matching sequence of input.
Instances
class Alternative m => AmbiguousParsing m where Source #
Parsers that can produce alternative parses and collect them into an Ambiguous
node
ambiguous :: m a -> m (Ambiguous a) Source #
Collect all alternative parses of the same length into a NonEmpty
list of results.
Instances
Ord s => AmbiguousParsing (Parser g s) Source # | |
(Applicative m, Eq (m ()), Ord s) => AmbiguousParsing (ParserT m g s) Source # | |
(AmbiguousParsing (p g s), Apply g) => AmbiguousParsing (Fixed p g s) Source # | |
class Alternative m => CommittedParsing m where Source #
Parsers that can temporarily package and delay failure, in a way dual to Parsec's try
combinator. Where Parsec
would require something like
alternatives = try intro1 *> expected1 <|> try intro2 *> expected2 <|> fallback
you can instead say
alternatives = admit $ intro1 *> commit expected1 <|> intro2 *> commit expected2 <|> commit fallback
A parsing failure inside an intro
parser leaves the other alternatives open, a failure inside an expected
parser bubbles up and out of the whole admit
block.
type CommittedResults m :: Type -> Type Source #
commit :: m a -> m (CommittedResults m a) Source #
Commits the argument parser to success.
admit :: m (CommittedResults m a) -> m a Source #
Admits a possible defeat of the argument parser.
Instances
(FactorialMonoid s, Ord s) => CommittedParsing (Parser g s) Source # | |
(FactorialMonoid s, Ord s) => CommittedParsing (Parser g s) Source # | |
Ord s => CommittedParsing (Parser g s) Source # | |
FactorialMonoid s => CommittedParsing (Parser g s) Source # | |
FactorialMonoid s => CommittedParsing (Parser g s) Source # | |
(Applicative m, Traversable m, Ord s) => CommittedParsing (ParserT m g s) Source # | |
(Apply g, CommittedParsing (p g s), CommittedResults (p g s) ~ ParseResults s) => CommittedParsing (Fixed p g s) Source # | |
class InputParsing m => TraceableParsing m where Source #
The class of parsers whose execution can be traced for debugging purposes
traceInput :: (ParserInput m -> String) -> m a -> m a Source #
Modify the argument parser to log its input whenever invoked.
traceAs :: Show (ParserInput m) => String -> m a -> m a Source #
Modify the argument parser to log the given description and its input whenever invoked.
Instances
class (DeterministicParsing m, InputCharParsing m, TokenParsing m) => LexicalParsing m where Source #
If a grammar is Lexical
, its parsers can instantiate the TokenParsing
class.
Nothing
lexicalWhiteSpace :: m () Source #
Always succeeds, consuming all white space and comments
someLexicalSpace :: m () Source #
Consumes all whitespace and comments, failing if there are none
lexicalComment :: m () Source #
Consumes a single comment, defaults to empty
lexicalSemicolon :: m Char Source #
Consumes a single semicolon and any trailing whitespace, returning the character |';'|. The method can be overridden for automatic semicolon insertion, but if it succeeds on semicolon or white space input it must consume it.
lexicalToken :: m a -> m a Source #
Applies the argument parser and consumes the trailing lexicalWhitespace
identifierToken :: m (ParserInput m) -> m (ParserInput m) Source #
Applies the argument parser, determines whether its result is a legal identifier, and consumes the trailing
lexicalWhitespace
isIdentifierStartChar :: Char -> Bool Source #
Determines whether the given character can start an identifier token, allows only a letter or underscore by default
isIdentifierFollowChar :: Char -> Bool Source #
Determines whether the given character can be any part of an identifier token, also allows numbers
identifier :: m (ParserInput m) Source #
Parses a valid identifier and consumes the trailing lexicalWhitespace
default identifier :: TextualMonoid (ParserInput m) => m (ParserInput m) Source #
keyword :: ParserInput m -> m () Source #
Parses the argument word whole, not followed by any identifier character, and consumes the trailing
lexicalWhitespace
default keyword :: (Show (ParserInput m), TextualMonoid (ParserInput m)) => ParserInput m -> m () Source #
Grammars
class InputParsing m => MultiParsing m where Source #
Choose one of the instances of this class to parse with.
type ResultFunctor m :: Type -> Type Source #
Some parser types produce a single result, others a list of results.
type GrammarConstraint m (g :: (Type -> Type) -> Type) :: Constraint Source #
type GrammarConstraint m g = Functor g
parseComplete :: (ParserInput m ~ s, GrammarConstraint m g, Eq s, FactorialMonoid s) => g m -> s -> g (ResultFunctor m) Source #
Given a rank-2 record of parsers and input, produce a record of parses of the complete input.
parsePrefix :: (ParserInput m ~ s, GrammarConstraint m g, Eq s, FactorialMonoid s) => g m -> s -> g (Compose (ResultFunctor m) ((,) s)) Source #
Given a rank-2 record of parsers and input, produce a record of prefix parses paired with the remaining input suffix.
Instances
class MultiParsing m => GrammarParsing m where Source #
Parsers that belong to this class can memoize the parse results to avoid exponential performance complexity.
type ParserGrammar m :: (Type -> Type) -> Type Source #
The record of grammar productions associated with the parser
type GrammarFunctor m :: Type -> Type Source #
For internal use by notTerminal
parsingResult :: ParserInput m -> GrammarFunctor m a -> ResultFunctor m (ParserInput m, a) Source #
Converts the intermediate to final parsing result.
nonTerminal :: (g ~ ParserGrammar m, GrammarConstraint m g) => (g (GrammarFunctor m) -> GrammarFunctor m a) -> m a Source #
Used to reference a grammar production, only necessary from outside the grammar itself
selfReferring :: (g ~ ParserGrammar m, GrammarConstraint m g, Distributive g) => g m Source #
Construct a grammar whose every production refers to itself.
fixGrammar :: (g ~ ParserGrammar m, GrammarConstraint m g, Distributive g) => (g m -> g m) -> g m Source #
Convert a self-referring grammar function to a grammar.
recursive :: m a -> m a Source #
Mark a parser that relies on primitive recursion to prevent an infinite loop in fixGrammar
.
:: (g ~ ParserGrammar m, f ~ GrammarFunctor m, GrammarConstraint m g) | |
=> (f a -> g f -> g f) | setter for the parsed results of each iteration |
-> m a | the non-recursive base case |
-> m a | the recursive case to iterate |
-> m a |
Convert a left-recursive parser to a non-left-recursive one. For example, you can replace the left-recursive production
foo = BinOp <$> foo <*> bar <|> baz
in the field foo
of grammar g
with
foo = chainRecursive (\x g-> g{foo = x}) baz (BinOp <$> foo <*> bar)
This method works on individual parsers left-recursive on themselves, not on grammars with mutually left-recursive productions. Use Text.Grampa.ContextFree.Memoizing.LeftRecursive for the latter.
chainLongestRecursive Source #
:: (g ~ ParserGrammar m, f ~ GrammarFunctor m, GrammarConstraint m g) | |
=> (f a -> g f -> g f) | setter for the parsed results of each iteration |
-> m a | the non-recursive base case |
-> m a | the recursive case to iterate |
-> m a |
Line chainRecursive
but produces only the longest possible parse. The modified example
foo = chainLongestRecursive (\x g-> g{foo = x}) baz (BinOp <$> foo <*> bar)
would be equivalent to the left-recursive production with biased choice
foo = BinOp <$> foo <*> bar <<|> baz
Instances
overlay :: (GrammarParsing m, g ~ ParserGrammar m, GrammarConstraint m g, Distributive g, Foldable f) => (g m -> g m) -> f (GrammarOverlay g m) -> g m Source #
Layers a sequence of GrammarOverlay
on top of a base GrammarBuilder
to produce a new grammar.
From the input-parsers library
class LookAheadParsing m => InputParsing (m :: Type -> Type) where #
Methods for parsing monoidal inputs
type ParserInput (m :: Type -> Type) #
The type of the input stream that the parser m
expects to parse.
type ParserPosition (m :: Type -> Type) #
type ParserPosition (m :: Type -> Type) = Down Int
getInput :: m (ParserInput m) #
Always sucessful parser that returns the entire remaining input without consuming it.
getSourcePos :: m (ParserPosition m) #
Retrieve the Position
reached by the parser in the input source.
anyToken :: m (ParserInput m) #
A parser that accepts any single atomic prefix of the input stream.
anyToken == satisfy (const True) anyToken == take 1
take :: Int -> m (ParserInput m) #
A parser that accepts exactly the given number of input atoms.
take n == count n anyToken
satisfy :: (ParserInput m -> Bool) -> m (ParserInput m) #
A parser that accepts an input atom only if it satisfies the given predicate.
notSatisfy :: (ParserInput m -> Bool) -> m () #
A parser that succeeds exactly when satisfy doesn't, equivalent to
notFollowedBy
.
satisfy
scan :: state -> (state -> ParserInput m -> Maybe state) -> m (ParserInput m) #
A stateful scanner. The predicate modifies a state argument, and each transformed state is passed to successive
invocations of the predicate on each token of the input until one returns Nothing
or the input ends.
This parser does not fail. It will return an empty string if the predicate returns Nothing
on the first
character.
Note: Because this parser does not fail, do not use it with combinators such as many
,
because such parsers loop until a failure occurs. Careless use will thus result in an infinite loop.
string :: ParserInput m -> m (ParserInput m) #
A parser that consumes and returns the given prefix of the input.
takeWhile :: (ParserInput m -> Bool) -> m (ParserInput m) #
A parser accepting the longest sequence of input atoms that match the given predicate; an optimized version of
concat
.
many
.
satisfy
.
Note: Because this parser does not fail, do not use it with combinators such as many
,
because such parsers loop until a failure occurs. Careless use will thus result in an infinite loop.
takeWhile1 :: (ParserInput m -> Bool) -> m (ParserInput m) #
Instances
class (CharParsing m, InputParsing m) => InputCharParsing (m :: Type -> Type) where #
Methods for parsing textual monoid inputs
satisfyCharInput :: (Char -> Bool) -> m (ParserInput m) #
Specialization of satisfy
on textual inputs, accepting an input character only if it satisfies the given
predicate, and returning the input atom that represents the character. Equivalent to fmap singleton
. Char.satisfy
notSatisfyChar :: (Char -> Bool) -> m () #
A parser that succeeds exactly when satisfy doesn't, equivalent to notFollowedBy . Char.satisfy
scanChars :: state -> (state -> Char -> Maybe state) -> m (ParserInput m) #
Stateful scanner like scan
, but specialized for TextualMonoid
inputs.
takeCharsWhile :: (Char -> Bool) -> m (ParserInput m) #
Specialization of takeWhile
on TextualMonoid
inputs, accepting the longest sequence of input characters that
match the given predicate; an optimized version of fmap fromString . many . Char.satisfy
.
Note: Because this parser does not fail, do not use it with combinators such as many
,
because such parsers loop until a failure occurs. Careless use will thus result in an infinite loop.
takeCharsWhile1 :: (Char -> Bool) -> m (ParserInput m) #
Specialization of takeWhile1
on TextualMonoid
inputs, accepting the longest sequence of input characters
that match the given predicate; an optimized version of fmap fromString . some . Char.satisfy
.
Instances
class InputParsing m => ConsumedInputParsing (m :: Type -> Type) where #
Parsers that keep track of the consumed input.
match :: m a -> m (ParserInput m, a) #
Return both the result of a parse and the portion of the input that the argument parser consumed.
Instances
class Ord p => Position p where #
A class for representing position values. The methods satisfy these laws:
move (distance pos1 pos2) pos1 == pos2 (pos1 < pos2) == (distance pos1 pos2 > 0)
Distance from the first position to the second
Move the position by the given distance.
offset :: FactorialMonoid s => s -> p -> Int #
Map the position into its offset from the beginning of the full input.
From the parsers library
class Parsing m => CharParsing (m :: Type -> Type) where #
Additional functionality needed to parse character streams.
Nothing
notChar c
parses any single character other than c
. Returns the parsed
character.
This parser succeeds for any character. Returns the parsed character.
Instances
class Alternative m => Parsing (m :: Type -> Type) where #
Additional functionality needed to describe parsers independent of input type.
(<?>) :: m a -> String -> m a infixr 0 #
Give a parser a name
A version of many that discards its input. Specialized because it can often be implemented more cheaply.
skipSome p
applies the parser p
one or more times, skipping
its result. (aka skipMany1 in parsec)
unexpected :: String -> m a #
Used to emit an error on an unexpected token
notFollowedBy :: Show a => m a -> m () #
notFollowedBy p
only succeeds when parser p
fails. This parser
does not consume any input. This parser can be used to implement the
'longest match' rule. For example, when recognizing keywords (for
example let
), we want to make sure that a keyword is not followed
by a legal identifier character, in which case the keyword is
actually an identifier (for example lets
). We can program this
behaviour as follows:
keywordLet = try $ string "let" <* notFollowedBy alphaNum
Instances
class Parsing m => LookAheadParsing (m :: Type -> Type) where #
Additional functionality needed to describe parsers independent of input type.
Instances
class CharParsing m => TokenParsing (m :: Type -> Type) where #
Additional functionality that is needed to tokenize input while ignoring whitespace.
Nothing
Usually, someSpace consists of one or more occurrences of a space
.
Some parsers may choose to recognize line comments or block (multi line)
comments as white space as well.
Called when we enter a nested pair of symbols. Overloadable to enable disabling layout
The token parser |semi| parses the character ';' and skips any trailing white space. Returns the character ';'. Overloadable to permit automatic semicolon insertion or Haskell-style layout.
highlight :: Highlight -> m a -> m a #
Tag a region of parsed text with a bit of semantic information. Most parsers won't use this, but it is indispensible for highlighters.
token p
first applies parser p
and then the whiteSpace
parser, returning the value of p
. Every lexical
token (token) is defined using token
, this way every parse
starts at a point without white space. Parsers that use token
are
called token parsers in this document.
The only point where the whiteSpace
parser should be
called explicitly is the start of the main parser in order to skip
any leading white space.
Alternatively, one might define token
as first parsing whiteSpace
and then parser p
. By parsing whiteSpace first, the parser is able
to return before parsing additional whiteSpace, improving laziness.
mainParser = sum <$ whiteSpace <*> many (token digit) <* eof
Instances
Other combinators
concatMany :: (Alternative p, Monoid a) => p a -> p a Source #
Zero or more argument occurrences like many
, with concatenated monoidal results.
concatSome :: (Alternative p, Semigroup a) => p a -> p a Source #
One or more argument occurrences like some
, with concatenated monoidal results.