parse a single byte
read a fixed number bytes, prefixed by a length
Keep reading bytes until the terminus is encounted.
Keep reading bytes until the terminus is encounted. This accounts for possible partial terminus in the data. The terminus is NOT included in the returned value
Read in an unknown number of bytes, ended only when endOfStream is called
Read in an unknown number of bytes, ended only when endOfStream is called
be aware this parser has no max size and will read in data forever if endOfStream is never called
Creates a parser that will always return the same value without consuming any data.
Creates a parser that will always return the same value without consuming any data. Useful when flatMapping parsers
Parse a series of ascii strings seperated by a single-byte delimiter and terminated by a byte
Parses the ASCII representation of an integer, keeps going until the terminus is encountered
Creates a parser that wraps another parser and will throw an exception if
more than size
data is required to parse a single object.
Creates a parser that wraps another parser and will throw an exception if
more than size
data is required to parse a single object. See the
ParserSizeTracker for more details.
Repeat a pattern a fixed number of times
Repeat a pattern a fixed number of times
the number of times to parse the pattern
the parser for the pattern
the parsed sequence
Parse a pattern multiple times based on a numeric prefix
Parse a pattern multiple times based on a numeric prefix
This is useful for any situation where the repeated pattern is prefixed by
the number of repetitions, for example num:[obj1][obj2][obj3]
. In
situations where the pattern doesn't immediately follow the number, you'll
have to do it yourself, something like
intUntil(':') ~ otherParser |> {case num ~ other => repeat(num, patternParser)
}
intUntil(':') ~ otherParser |> {case num ~ other => repeat(num, patternParser) }}}
parser for the number of times to repeat the pattern
the parser that will parse a single instance of the pattern
the parsed sequence
Repeatedly parse a pattern until a terminal byte is reached
Repeatedly parse a pattern until a terminal byte is reached
Before calling parser
this will examine the next byte. If the byte
matches the terminus, it will return the built sequence. Otherwise it
will pass control to parser
(including the examined byte) until the
parser returns a result.
Notice that the terminal byte is consumed, so if we have
val parser = repeatUntil(bytes(2), ':') parser.parse(DataBuffer(ByteString("aabbcc:ddee")))
the bytes remaining in the buffer after parsing are just ddee
.
the parser repeat
the byte to singal to stop repeating
the parsed sequence
Create a parser that will repeat the given parser forever until
endOfStream()
is called.
Create a parser that will repeat the given parser forever until
endOfStream()
is called. The results from each call to the given parser
are accumulated and returned at the end of the stream.
creates a parser that will skip over n bytes.
creates a parser that will skip over n bytes. You generally only want to do this inside a peek parser
Parse a string until a designated byte is encountered
Parse a string until a designated byte is encountered
Limited filtering is currently supported, all of which happens during the reading.
reading will stop when this byte is encountered
if true any characters in the range A-Z will be lowercased before insertion
specify a minimum size
throw a ParseException if any whitespace is encountered before the terminus. If the terminus is a whitespace character, it will not be counted
trim leading whitespace
Streaming Parser Combinators
Overview
A
Parser[T]
is an object that consumes a stream of bytes to produce a result of typeT
.A Combinator is a "higher-order" parser that takes one or more parsers to produce a new parser
The Stream parsers are very fast and efficient, but because of this they need to make some tradeoffs. They are mutable, not thread safe, and in general are designed for network protocols, which tend to have very deterministic grammars.
The Parser Rules:
1. A parser must greedily consume the data stream until it produces a result 2. When a parser consumes the last byte necessary to produce a result, it must stop consuming the stream and return the new result while resetting its state
Examples
Use any parser by itself:
Combine two parsers