unsynn (from german 'unsinn' for nonsense) is a minimalist rust parser library. It achieves this by leaving out the actual grammar implementations and compromise on simpler error reporting. In exchange it offers simple composeable Parsers and ergonomic Parser construction. Grammars will be implemented in their own crates (see unsynn-rust).
It is primarily intended use is when one wants to create proc macros for rust that define their own grammar or need only sparse rust parsers.
Examples
Custom Types
The [unsynn!{}] macro will generate the [Parser] and [ToTokens] impls (and more). This
is optional, the impls could be written by hand when necessary.
Notice that unsynn implements [Parser] and [ToTokens] for many standard rust types. Like
we use u32 in this example.
# use *;
let mut token_iter = "foo ( 1, 2, 3 )".to_token_iter;
unsynn!
// iter.parse() is from the IParse trait
let ast: IdentThenParenthesisedNumbers = token_iter.parse.unwrap;
assert_eq!
Using Composition
Composition can be used without defining new datatypes. This is useful for simple parsers or when one wants to parse things on the fly which are desconstructed immediately.
# use *;
// We parse this below
let mut token_iter = "foo ( 1, 2, 3 )".to_token_iter;
// Type::parse() is from the Parse trait
let ast =
parse.unwrap;
assert_eq!
Custom Operators and Keywords
To define keywords and operators we provide the keyword! and operator! macros:
# use *;
keyword!
operator!
// The above can be written within a unsynn!
// See next example about parsing recursive grammars
// looks like BNF, but can't do recursive types
type Expression = ;
type AdditiveOp = ;
type AdditiveExpr = ;
type MultiplicativeOp = ;
type MultiplicativeExpr = ;
let ast = "CALC 2*3+4/5 ;".to_token_iter
..expect;
Parsing Recursive Grammars
Recursive grammars can be parsed using structs and resolving the recursive parts in a Box or
Rc. This looks less BNF like but acts closer to it:
# use *;
# use Rc;
unsynn!
// now we can parse more complex expressions. Adding parenthesis is left as excercise to the reader
let ast = "CALC 10+1-2*3+4/5*100 ;".to_token_iter
..expect;
Feature Flags
By default unsynn is very lean and does not include extra features. The only thing that are
always present are the [Parser], [Parse], [ToTokens] and [Debug] traits.
The Display can't be implemented for all types (eg. [Option]). Further Display may
sometimes be surprising since we do not have good rules how to pretty-print tokens (eg. spaces
around Delimiters). Display then often inserts surplus spaces to ensure that tokens are
properly delimited.