ParseIt
Description
This library provides a very simple and lightweight parser (recursive descendant ll(1)) to combine and express a given grammar.
The library uses Logos as a lexical analyzer and tokenizer.
The premise
This library major incentives were:
- lightweight : very small and does not require a deep dive
- transparency : literally 3 structs with a handful of methods
- speed : not bad speed (with a gratitude to Logos)
The steps to implement
Create a set of tokens using Logos
use Logos;
Create a parser that will be able to parse the given set of tokens
The library provides ParseIt<'a,T> instance that encompasses a set of tokens and auxiliary methods
Implement a parsing functions using ParseIt instance and auxiliary methods from the Step
The helpers:
- the macros token! that alleviates comparing and matching single tokens
- methods
then,then_zipand others fromStep - methods
one_or_more,zero_or_morefromParseIt
Transform the result into Result<Structure, ParserError<'a>>
Complete example
use crateParseIt;
use cratetoken;
use crateStep;
use crateEmptyToken;
use crateParseError;
use Logos;
The base auxiliary methods
On parser
token- gives a possibility to pull out a curren tokenone_or_more- gives a one or more semanticzero_or_more- gives a zero or more semanticvalidate_eof- ensure the parser reaches end of the input
On step
To alternate
or- gives an alternative in a horizon of one tokenor_from- gives a backtracking option
To combine
then- gives a basic combination with the next rule ommiting the current onethen_zip- combines a current result and a next one into a pairthen_or_none-combines a next one in an option with the current one or return a none otherwise
To collect
take_left- drops a right value from the pairtake_right- drops a left value from the pairmerge- merge a value into a listto_map- transforms a list of pairs into a map
To transform
or_val- replaces a value with a default value if it is not presentedor_none- replaces a value with a none if it is not presented
To work with value
ok- transforms a value into optionerror- transforms an error into optionmap- transforms a valuecombine- combines a value with another value from a given stepvalidate- validates a given value and transforms into error if a validation is failed
To print
print- print a stepprint_with- print a step with a given prefixprint_as- print a step with a transformation of valueprint_with_as- print a step with a transformation of value with a given prefix