ParseIt
Description
This library provides a very simple and lightweight parser (recursive descendant ll(1)) to combine and express a 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 : good speed (with a gratitude to Logos)
The steps to implement
Create a set of tokens using Logos
Add logos to dependency
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
Macros
token!- parses the current token. In general, it is used the followingtoken!(p.token(pos) => T::Bang => "!")wrap!- implements a simple pattern in grammar likeleft value right, for instance[1,2,3]or(a,b)- can handle the default value like
wrap!(0 => left; value or default; right) - can handle the option value like
wrap!(0 => left; value ?; right)
- can handle the default value like
seq!- implements a simple pattern of sequence likeel sep el ..., for instance1,2,3- can have a
,at the end signaling the separator can be at the ned of the seq like1,2,3 (,)?
- can have a
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 a next rule omitting the current onethen_zip- combines a current result and a next one into a pairthen_skip- parses the next one but drops the result keeping only current onethen_or_none-combines a next one in an option with a current one or return a none otherwise
To collect
take_left- drops a right value from a pairtake_right- drops a left value from a 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 an optionerror- transforms an error into an optionmap- transforms a valuecombine- combines a value with another value from a given stepvalidate- validates a given value and transforms into an error if a validation 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
Testing
Lexer
To test a lexer there are methods from crate::parsit::test::lexer_test::* for service
use Logos;
use crate*;
Parser
To test a parser there are methods from crate::parsit::test::parser_test::* for service
use Logos;
use cratefail;
use crateparsit;
use cratetoken;
use crateParseIt;
use crateStep;