Crates.io | ratatat |
lib.rs | ratatat |
version | 0.1.1 |
source | src |
created_at | 2021-05-28 15:48:47.500379 |
updated_at | 2021-06-06 05:46:47.484574 |
description | expressive parser combinators with caching |
homepage | https://github.com/hclarke/ratatat |
repository | https://github.com/hclarke/ratatat |
max_upload_size | |
id | 403190 |
size | 91,364 |
a parser combinator library for rust
[u8]
only)steps for parsing a file:
Input
Context
from the inputan input is a [u8]
backed by a reference counted vec. this allows parsers to hold on to spans of the input, rather than copy them.
a context has a reference to the input, and a cache of parsers. this allows parsers to precompute things for the entire input, memoize results, etc.
once parsing is complete, the context can be thrown away
a parser has a parse method that takes a context, a mutable position, and returns an optional result.
there are many built-in parsers, and parser combinators. for example:
str
is a parser, which returns itself if the input contains it at the positionchar
behaves similarlyMap(parser,func)
runs a parser, and on success, applies a function to its resultFilter(parser, func)
runs a parser, then filters results by a functiona parser generator generates a parser for a type. this is the main way to make use of caching parsers in a Context
you can get a cached parser with context.parser::<Generator>()
or parse immediately with context.parse::<Generator>(limit, &mut position)
there are also many built-in generators:
char
generates a parser that parses any char[Generator;N]
generates a parser that parses a fixed-size array of valuesMemo<Generator>
generates a memoized parser from another generatorTake a look at the examples to see how it works. the json parser is a good place to start. usually, you'll want to implement Generator<YourType>
for the root of your AST.
the core traits and types are in src/lib.rs
and the built-in parsers and combinators are in src/parsers/