Coder Social home page Coder Social logo

francoischabot / abulafia Goto Github PK

View Code? Open in Web Editor NEW
35.0 8.0 2.0 606 KB

A header-only C++ parsing framework

Home Page: https://francoischabot.github.io/abulafia/

License: Boost Software License 1.0

CMake 1.43% C++ 98.44% C 0.12%
parsing expression-template cpp17

abulafia's People

Contributors

francoischabot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

abulafia's Issues

Add the BindDst pattern/parser

Add a mechanism to "tag" the dst for the entire pattern hierarchy under it. This will allow semantic actions to read and assign them.

This will require adding a second parameter to the semantic action, which will be an opaque type with the bound dst reference as member.

Having this allows us to write operator precedence parsers in a very nice and manageable way:

 auto binop = [](std::tuple<char,int> v, auto p) -> void { 
    auto lhs = p.bound_dst;
    auto rhs = get<1>(v);

    switch(get<0>(v)) {
      case '+': 
        p.bound_dst = lhs + rhs;
        break;
      case '-': 
        p.bound_dst = lhs - rhs;
        break;
       // etc...
    }
  };

auto muldiv = abu::bind_dst(
    primary[assign] >> *(char_("+-*/") >> primary)[binop]
  );

Put a wrapper around the ctx reference

We need value semantics, and the reference is getting in the way. This will also hopefully help us moe to a more composition, rather than inheritance, model.

Investigate constexpr support.

The problematic parts are:

  1. Recur<>
  2. The rollback buffers.
  3. std::variant<>'s implementation is not consistently constexpr.

Both could probably be addressed with a statically sized allocator with a pre-defined maximal depth,

Composing a value with a vector fails to compile

TEST(test_sequence, compose_vector) {
  auto pattern = Int<10, 2, 2>() >> *Int<10, 1, 1>();

  std::vector<int> dst;

  auto status = parse(pattern, std::string("11222333"), dst);
  EXPECT_EQ(result::SUCCESS, status);
  EXPECT_EQ(std::vector<int>({11,222,333}), dst);
}

abu::result type and wrong case in code

Is there a particular reason why abu::result is properly spelled in the example and CamelCase in result.h?

Why not use std::error_code? It makes testing for errors and handling them easier.

  if (const auto ec = abu::parse(data.begin(), data.end(), ignoring_whitespace, dst)) {
    for (auto v : dst) {  // it has to be a value, and not a const reference for this?
      std::cout << v << std::endl;
    }
  } else {
    throw std::system_error(ec);
  }

Sequence parser should be able to contruct an arbitrary struct without going through a semantic action

Currently, invoking a constructor must be done through semantic action.

This should work:

TEST(test_sequence, sequence_into_user_struct) {
  struct xyz {
    xyz(int x, int y, int z)
     : x_(x), y_(y), z_(z) {}
    
    int x_;
    int y_;
    int z_;
  };
  auto pattern = int_ >> lit('-') >> int_ >> lit('-') >> int_;

  xyz  dst{ 0, 0, 0 };
  xyz  expected{1, 2, 3};
  auto status = parse(pattern, std::string("1-2-3"), dst);
  EXPECT_EQ(result::SUCCESS, status);
  EXPECT_EQ(expected, dst);
}

Review struct construction

as<>() is a bad name for the interface. It's leaking the abstraction. Since its only usage is for constructor selection, its name should reflect that.

Add a recur wrapper to the test_utils harness.

There is really bad test coverage of the various transform() functions, and some of them are probably wrong.

Adding another test to the standard harness that exercises this code path will help a lot.

weakenRecur cannot be used from functions templated on the pattern

template<typename T>
auto as_recur(T const& pat) {
  abu::Recur<struct as_recur_t> as_recur;
  ABU_Recur_define(as_recur, as_recur_t, pat);
  return as_recur;
}

This causes the following error (in GCC):

use of 'auto abu::weaken_recur(const PAT_T&) [...]' before deduction of 'auto'

Get rid of recur's ATTR_T

ATTR_T is gone entirely from abulafia as a concept. It's still in recur to identify wether it's a nil or value parser, but that's it.

At best, we might be able to infer that information. At worst, we need to change the parameter to an enum, or a tag type.

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.