class Pars::Parser(T)

Defined in:

pars/parser.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(&block : ParseContext -> ParseResult(T)) #

[View source]

Class Method Detail

def self.byte #

Creates a Parser that consumes the parse head, or fails if the end of input has been reached.


[View source]
def self.char #

Creates a Parser that consumes the parse head, or fails if the end of input has been reached.


[View source]
def self.const(value : T) #

Creates a Parser that always succeeds with value.


[View source]
def self.fail(message : String) #

Creates a Parser that always fails with message.


[View source]
def self.head #

Creates a Parser that consumes the parse head, or fails if the end of input has been reached.


[View source]

Instance Method Detail

def &(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.


[View source]
def &+(other : Parser(B)) forall B #

Sequences self with other, providing a new Parser that returns the results as a Tuple.

If multiple parsers are chained, the results are flattened.


[View source]
def *(count : Int) : Parser(Array(T)) #

Creates a new parser that repeats self exactly count times.


[View source]
def *(range : Range(Int, Int) | Range(Int, Nil)) : Parser(Array(T)) #

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.


[View source]
def +(other : Parser(B)) forall B #

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.


[View source]
def <<(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.


[View source]
def >>(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.


[View source]
def ^(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.

If both succeed, the parser will fail.


[View source]
def |(other : Parser(B)) : Parser(T | B) forall B #

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.


[View source]
def |(message : String) : Parser(T) #

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.


[View source]
def bind(&block : T -> Parser(B)) : Parser(B) forall B #

Sequences self with another parser.

Expects a block that receives the result of the current parser and returns a new parser of any type.


[View source]
def 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.

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.


[View source]
def parse(input) : T | ParseError #

Parses the input string input given the parser's logic provided by its block at definition.


[View source]
def run(context : ParseContext) : ParseResult(T) #

Runs self for a given context.


[View source]