7 unstable releases (3 breaking)

0.4.3 Sep 27, 2023
0.4.2 Sep 27, 2023
0.3.0 Sep 26, 2023
0.2.0 Sep 26, 2023
0.1.0 Sep 26, 2023

#392 in Procedural macros

31 downloads per month

MIT license

40KB
798 lines

Pest Tree

An alternative to pest_ast for converting dynamically typed pest trees into statically typed ones, using macros

This documentation is currently unfinished and some may be outdated. Feel free to directly ask in github issues if you have any problems!

Goals

  • Intuitive
  • Explicit
  • Easy to learn for beginners

Non-goals

  • Fast
  • Works for every case
  • customization support

This crate revolves around the trait PestTree which can be derived for Structs and Enums.

Options

Basic

Name Parameters Purpose
strategy(...) A strategy, such as Direct or Sequential Specify a matcher strategy for the struct/enum.
require(...) A Requirement, such as Rule(...) Define requirements not covered by the struct's rules/strategy.
convert(...) A Converter, such as auto Convert the Pairs into something like an i32 in a struct

Strategy

Name Parameters Purpose
Direct None Match tokens like in, let, etc.
Sequential None Match a series of tokens, such as let a = 3;

Requirement

Name Parameters Purpose
rule(...) A Rule enum variant generated by pest_derive Confirm that the Pairs matches a rule. This requirement should be used in most if not all structs/enums
validate(...) A closure or a function that accepts a Pair and returns true if the input is accepted Match based on custom conditions.
any(...) Two or more Requirements Only 1 requirement in or(...) have to match for it to be accepted. For functionality like and(...), just use multiple require attributes

Convert

Name Parameters Purpose
custom_p(...) A closure or a function that takes a Pair and outputs the type it should convert to. Convert tokens into custom types.
custom_s(...) A closure or a function that accepts a &str and returns true if the input is accepted Convert tokens into custom types.
auto None Automatically convert the pair into one of the basic types (pest_tree will determine the type based on your struct member declaration)
chain(...) Chain many functions together in sequence Chain functions that convert from a pair into a custom type




PestTree needs helper attributes to determine how the tree can be parsed. For examples on how they can be used, check the examples folder.


lib.rs:

An easy and simple way to convert your dynamic [pest] trees into an AST built from structs and enums.

This crate is centered around the pest_tree_derive::PestTree derive macro. The macro uses the pest_tree attribute macro to help derive the trait pest_tree_derive::PestTree for the relevant struct/enum.

Dependencies

~5MB
~94K SLoC