parser icon indicating copy to clipboard operation
parser copied to clipboard

allow delimiter attributes in delimiter parser

Open cppljevans opened this issue 7 months ago • 0 comments

Changing parser.hpp to contain:

  #ifdef DELIMITED_SEQ_PARSER_USE_DELMITER_ATTR
    template<typename... Parsers>
    using seq_parser_default=
      seq_parser
      < tuple
        < Parsers...
        >
      , tuple
        < std::true_type
        , std::true_type
        >
      , tuple
        < llong<0>
        , llong<0>
        >
      >
      ;
  #endif//DELIMITED_SEQ_PARSER_USE_DELMITER_ATTR
    template<typename Parser, typename DelimiterParser>
    struct delimited_seq_parser
  #ifdef DELIMITED_SEQ_PARSER_USE_DELMITER_ATTR
    : seq_parser_default
      < Parser
      , zero_plus_parser
        < seq_parser_default
          < DelimiterParser
          , Parser
          >
        >
      >
  #else
    : repeat_parser<Parser, DelimiterParser>
  #endif//DELIMITED_SEQ_PARSER_USE_DELMITER_ATTR
    {
      #ifdef DELIMITED_SEQ_PARSER_USE_DELMITER_ATTR
        using  repeater_alias=
          zero_plus_parser
          < seq_parser_default
            < DelimiterParser
            , Parser
            >
          >
          ;
        using  super=
          seq_parser_default
          < Parser
          , repeater_alias
          >
          ;
        constexpr delimited_seq_parser
          ( Parser parser
          , DelimiterParser delimiter_parser
          )
          : super
            ( std::make_tuple
              ( parser
              , repeater_alias
                ( seq_parser_default
                  ( std::make_tuple
                    ( delimiter_parser
                    , parser
                    )//make_tuple
                  )//seq_parser_default
                )//repeater_alias
              )//make_tuple
            )//super
          {}
      #else
        constexpr delimited_seq_parser(
            Parser parser, DelimiterParser delimiter_parser) :
            repeat_parser<Parser, DelimiterParser>(
                parser, 1, Inf, delimiter_parser)
        {}
      #endif//DELIMITED_SEQ_PARSER_USE_DELMITER_ATTR
    };//delimited_seq_parser struct

would allow the delimited_seq_parser attributes to contain the attribute of the DelimiterParser. If that attribute is nope, then attribute remains the same. For example, if DelimiterParser attribute is some binary operator, such as std::multiplies<int,int>, then the attribute would contain an easy means for interpretation of the attribute to return an int result.

The std::multiplies<int,int> could easily be put into the attributes by:

    template
    < char C
    , typename AttributeType=detail::nope
    >
    struct char_const_parser
    : char_parser
      < char
      , void
      >
    {...};      

     template
      < char C
      , typename AttributeType=detail::nope
      >
      const 
      parser_interface
      < char_const_parser
        < C
        , AttributeType
        >
      > 
    char_const;

where AttributeType == std::multiplies<int,int>.

This could then be put into a calculator grammar containing, for example:

      auto const
    op_mult
      = bp::char_const
        < '*'
        , std::multiplies<atom_attr>
        >
      ;
      auto const 
    term_gram
      = factor % op_mult
      ;

In addition, the documentation of the operator% here actually claims the equivalence of:

p1 % p2

to:

p1 >> *(p2 >> p1)

cppljevans avatar Jul 13 '24 17:07 cppljevans