class Pars::Parser(T)
- Pars::Parser(T)
- Reference
- Object
Defined in:
pars/parser.crConstructors
Class Method Summary
-
.byte
Creates a
Parser
that consumes the parse head, or fails if the end of input has been reached. -
.char
Creates a
Parser
that consumes the parse head, or fails if the end of input has been reached. -
.const(value : T)
Creates a
Parser
that always succeeds with value. -
.fail(message : String)
Creates a
Parser
that always fails with message. -
.head
Creates a
Parser
that consumes the parse head, or fails if the end of input has been reached.
Instance Method Summary
-
#&(other : Parser(B)) : Parser(Tuple(T, B)) forall B
Given
A & B
, creates a parser that succeeds when both A and B succeed for the same input, returning the results as a Tuple. -
#&+(other : Parser(B)) forall B
Sequences
self
with other, providing a new Parser that returns the results as a Tuple. -
#*(count : Int) : Parser(Array(T))
Creates a new parser that repeats
self
exactly count times. -
#*(range : Range(Int, Int) | Range(Int, Nil)) : Parser(Array(T))
Creates a new parser that repeats
self
continuously up to range.end times. -
#+(other : Parser(B)) forall B
Sequences
self
with other, providing a new Parser that returns the results as an Array. -
#<<(other : Parser(B)) : Parser(T) forall B
Sequences the current parser with another parser, and disregards the other parser's result, but ensures the two succeed.
-
#>>(other : Parser(B)) : Parser(B) forall B
Sequences the current parser with another parser, and disregards the original parser's result, but ensures the two succeed.
-
#^(other : Parser(B)) : Parser(T | B) forall B
Given
A ^ B
, creates a parser that succeeds if A or B succeed exclusively for the same input. -
#|(other : Parser(B)) : Parser(T | B) forall B
Given
A | B
, creates a new parser that succeeds when A succeeds or B succeeds. -
#|(message : String) : Parser(T)
Creates a new
Parser(T)
that fails with message ifself
is unsuccessful. -
#bind(&block : T -> Parser(B)) : Parser(B) forall B
Sequences
self
with another parser. -
#map(&block : T -> B) : Parser(B) forall B
Transforms the result of the parser such that, when the parser runs, the output value becomes a different value.
-
#parse(input) : T | ParseError
Parses the input string
input
given the parser's logic provided by its block at definition. -
#run(context : ParseContext) : ParseResult(T)
Runs
self
for a given context.
Constructor Detail
Class Method Detail
Creates a Parser
that consumes the parse head, or fails if the end of
input has been reached.
Creates a Parser
that consumes the parse head, or fails if the end of
input has been reached.
Creates a Parser
that consumes the parse head, or fails if the end of
input has been reached.
Instance Method Detail
Given A & B
, creates a parser that succeeds when both A and B succeed
for the same input, returning the results as a Tuple.
Sequences self
with other, providing a new Parser that returns the
results as a Tuple.
If multiple parsers are chained, the results are flattened.
Creates a new parser that repeats self
exactly count times.
Creates a new parser that repeats self
continuously up to range.end
times. If range is not bounded it will continue to repeat until failing.
Sequences self
with other, providing a new Parser that returns the
results as an Array.
This may be preferred in place of Parser(T)#.&+
when building parsers
that enumerate or reduce over a structure of unknown size, such as when
working within an Iterator.
If multiple parsers are chained, the results are flattened.
Sequences the current parser with another parser, and disregards the other parser's result, but ensures the two succeed.
Sequences the current parser with another parser, and disregards the original parser's result, but ensures the two succeed.
Given A ^ B
, creates a parser that succeeds if A or B succeed
exclusively for the same input.
If both succeed, the parser will fail.
Given A | B
, creates a new parser that succeeds when A succeeds or B
succeeds. Checks A first, doesn't check B if A succeeds. Ignores type
differences, gives union type.
Creates a new Parser(T)
that fails with message if self
is
unsuccessful.
This can be used to provide a custom error message when chaining parsers.
Sequences self
with another parser.
Expects a block that receives the result of the current parser and returns a new parser of any type.
Transforms the result of the parser such that, when the parser runs, the output value becomes a different value.
For example, if you took a Parser(Char)
and wanted to transform it to a
Parser(String)
by Char#to_s
, then you could use
char_parser.transform &.to_s
.
Parses the input string input
given the parser's logic provided by its
block at definition.