Code Monkey home page Code Monkey logo

whirl's Introduction

whirl
header only library for implementing LL(1) parsers.

Its purpose is to simplify parser implementations providing a set of higher level functions on top of the stl's streaming capabilities or compatible APIs. It can be used as a more verbose but better readable alternative to regular expressions.

This library is in an early development stage. There is no unicode support, yet. Only utf-32 is fully supported. For utf-16 only the characters of the BMP are recognized correctly. Supports all single byte encodings.

Goals for Version 1

  • full unicode support for utf-8 and utf-16
  • functional composition of primitives.
  • transformators for reading functions
  • some simple examples
  • unit tests for all components

Long term goals

  • a rich set of predefined lookahead and reading functions for recurring token sequences
  • some complex examples
  • a doxypress generated api documentation
  • an introducing tutorial
  • a constexpr implementation of regular expressions on top of this library

Simple Example

Reading sequential measurement data from an input stream provided by an input device. The full example can be found in the examples directory.

The EBNF we want to represent is:

end                    = ? virtual end token (not part of the character set) ? ;
non-zero-decimal-digit = "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
decimal-digit          = "0" | non-zero-decimal-digit ;
whitespace             = " " | "\t" | "\n" ;
decimal-whole-number   =  "0" | [ "-" ], non-zero-decimal-digit, {decimal-digit} ;
separator              = { whitespace }- ;
data-entry             = ( decimal-whole-number, ( ( separator, data-entry ) | end ) ) ) | end;
data                   = { whitespace }, data-entry ;
constexpr auto number              = whirl::is(whirl::digit) || whirl::is(whirl::negative_sign);

constexpr auto read_sign           = whirl::next_is(whirl::negative_sign, whirl::as(-1)) || 1;
constexpr auto read_digit          = whirl::next(whirl::as_digit<int>);
constexpr auto read_digit_sequence = whirl::next_while(whirl::digit, whirl::as_digits<int>);

auto read_data_entries(std::istream& ins, whirl::code_position& pos)
{
    std::vector<int> temperatures;

    whirl::next_while(ins, pos, whirl::space);

    while(whirl::is(ins, number))
    {
        const auto number = whirl::is(ins, whirl::zero) ?
            read_digit(ins, pos) :
            read_digit_sequence(ins, pos, read_sign(ins, pos) * read_digit(ins, pos));

        temperatures.push_back(number);
        whirl::next_while(ins, pos, whirl::space);
    }

    whirl::next_is(ins, pos, whirl::end);

    return temperatures;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.