Skip to content

Bloodb0ne/ConstFuse

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ConstFuse

Single header library for parser combinators written in C++ 17. Its not really meant for production, but a exploration of templates and combinator parsing and constexpr.

Medium Article: https://link.medium.com/W0oAN8D3z5

Alternative Blog Article: https://bloodb0ne.github.io/constfuse_parser_combinator.html

ConstFuse.h Contains all the combinators and helper components StringParsers.h Contains simple string and character related parsers used in conjunction with ConstFuse.h

Combinators

namespace: constfuse

Seq matches a sequence of parsers in the defined order

Any matches any of the passed parses following the same order they are passed in

Repeat matches a parser multiple times but has to match it at leas once

RepeatN matches a parser a fixed amount of times

Many matches a parser multiple times, can be a matched 0 times

Optional matches a parser, but succeeds every time even if it failed

LIgnore ( operator << ) matches a parses but ignores the result from the left side

RIgnore ( operator >> ) matches a parser but ignores the result from the right side (ex. comma)

Not when you want a certain parser to not match

Map applies a function to the parsed result

Precond executes the parsing if a condition is satisfied in the current accumulated result

Postcond modifies the result after succesful parsing

Reference create a parser reference for handling recursive cases

SepBy matches a parser separated by another parser

FullSepBy matches a parser separated by another parser, the result includes the parsed separator result

Monadic

namespace: constfuse::monadic

Many similar to the normal Many combinator, but works over a single result

Repeat similar to the normal Repeat combinator, but works over a single result

Combine ( operator && ) chains parsers together and combines the result if they all succeded

OrCombine ( operator || ) chains parsers together and combines the result if at least one succeded

Compound

namespace: constfuse::compound

LeftBinOper handles left associative binary operations

RightBinOper handles right associative binary operations

Postfix handles postfix operators

Prefix handles prefix operators

Wrapper handles a parses wrapper between 2 different parses (ex. Handling brackets)

The compound operators can be made and used to handle complex relations of parsers like associativity and order that cant really be done because of left recursive grammars.

String Combiners

namespace: constfuse::string

ParseChar parses a single character

ParseNumber parses a single digit

CharRange parses a range of characters specified with a start and end one

AcceptString parses a string and returns it as a result

ParseASCII parses a ascii symbol between 0-255

ParseLit parses a string literal

Literals

""_symb a shorter way to use ParseChar

""_asymb same as symb but accepts the result returning a string (using AcceptString)

""_range parses any of the symbols in the range

Things that didnt make the first version

  • Simplified Iterator usage, currently it uses a start and an end iterator but it would be better to have a single parameter similar to span.
  • Basic Parsers and Iterator for handling binary files ( parsing binary structure sounds like something usefull for validating file structure)
  • Polishing String/Char handling parsers
  • Add References to some related papers on the subject
  • Add more interesting and usefull examples

Error handling

At the moment errors might get quite cryptic because of the templating, static_asserts might be usefull but its hard to mention class names ( including template params ) in the error message. Another thing that must be considered is pushing a error state inside the ContextIterator to track error/s history, another benefit from that might be attaching error recovery procedures. Part of the Context handling is layed out but only basic features (at the end of the file EmptyContext/GenericContext and the template parameter of the Iterator).