Quantcast

Parsing an empty member into a fusion adapted struct

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Parsing an empty member into a fusion adapted struct

Thomas Ellis
I am currently working on developing a parse grammar which would allow me to handle the two input lines identified below with a single parse grammar.  The first input parses fine.  It is when there is nothing for that "field" where the difficulty lies.  Does anyone have any words of wisdom on how I might accomplish this?

//***********************************
#define BOOST_SPIRIT_DEBUG
#define FUSION_MAX_VECTOR_SIZE      20
#define SPIRIT_ARGUMENTS_LIMIT      20

#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>

struct LineStruct {
    std::uint16_t year;
    std::uint16_t month;
    std::uint16_t day;
    std::uint16_t hour;
    std::uint16_t minute;
    std::uint16_t second;
    std::string node;
    std::string pcc;
    std::uint16_t request;
};


BOOST_FUSION_ADAPT_STRUCT( LineStruct,
(std::uint16_t,year)
(std::uint16_t,month)
(std::uint16_t,day)
(std::uint16_t,hour)
(std::uint16_t,minute)
(std::uint16_t,second)
(std::string,node)
(std::string,pcc)
(std::uint16_t,request)
);

template <typename Iterator, typename Skipper >
struct Parser : boost::spirit::qi::grammar<Iterator, LineStruct(), Skipper >
{
Parser() : Parser::base_type(linestruct)
{
linestruct = boost::spirit::qi::ushort_ >> boost::spirit::qi::lit("-") >> boost::spirit::qi::ushort_ >> boost::spirit::qi::lit("-") >> boost::spirit::qi::ushort_ >>
boost::spirit::qi::ushort_ >> boost::spirit::qi::lit(":") >> boost::spirit::qi::ushort_ >> boost::spirit::qi::lit(":") >> boost::spirit::qi::ushort_ >>
-boost::spirit::qi::lexeme[ +boost::spirit::qi::char_("a-zA-Z0-9") ] >>
-boost::spirit::qi::lexeme[ +boost::spirit::qi::char_("a-zA-Z0-9") ] >>
boost::spirit::qi::ushort_;

BOOST_SPIRIT_DEBUG_NODES( (linestruct) )
}
private:
boost::spirit::qi::rule<Iterator, LineStruct(), Skipper> linestruct;
};


int main()
{

try{
std::string input1("2017-04-18 23:59:55 hpchlp200  6ZLA      9");
std::string input2("2017-04-18 23:59:55 hpchlp200            9");

std::string::const_iterator f(input2.begin()), l(input2.end());
Parser<std::string::const_iterator, boost::spirit::qi::space_type> parser;
LineStruct line;

bool ok = boost::spirit::qi::phrase_parse(f, l, parser, boost::spirit::qi::space, line);
if (ok && f==l ) {
std::cout << "Parse success\n";
} else {
std::cout << "Parse failed\n";
}

if (f!=l) {
std::cout << "Remaining unparsed input: '" << std::string(f,l) << "'\n";
}
} catch( const boost::spirit::qi::expectation_failure< std::string::const_iterator >& e ) {
std::cerr << "FIXME: expected " << e.what_ << ", got '" << std::string(e.first, e.last) << "'" << std::endl;
        return false;
}
}

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Parsing an empty member into a fusion adapted struct

sehe
On 05-05-17 19:54, Thomas Ellis wrote:
I am currently working on developing a parse grammar which would allow me to handle the two input lines identified below with a single parse grammar.  The first input parses fine.  It is when there is nothing for that "field" where the difficulty lies.  Does anyone have any words of wisdom on how I might accomplish this?

The inputs strongly suggest that the format is fixed-width (and space separated). So you would probably do best to parse it as such.

If you don't want to, and the trailing `request` value is mandatory, you could look-ahead to assert that you're not able to parse the trailing ushort_:

        linestruct = 
            ushort_ >> '-' >> ushort_ >> '-' >> ushort_ >>
            ushort_ >> ':' >> ushort_ >> ':' >> ushort_ >>
            -(~(ushort_ >> eoi) >> lexeme[ *alnum ]) >>
            -(~(ushort_ >> eoi) >> lexeme[ *alnum ]) >>
            ushort_;

Which _does_ work: http://coliru.stacked-crooked.com/a/53479b03385ca51d, printing

<pre class="f0" onclick="window.jumpToError(this)" onmouseover="window.highlightError(this, true)" onmouseout="window.highlightError(this, false)" style="display: inline-block; margin: 0px; padding: 0px;"><linestruct>
<pre class="f0" onclick="window.jumpToError(this)" onmouseover="window.highlightError(this, true)" onmouseout="window.highlightError(this, false)" style="display: inline-block; margin: 0px; padding: 0px;"> <try>2017-04-18 23:59:55 </try>
<pre class="f0" onclick="window.jumpToError(this)" onmouseover="window.highlightError(this, true)" onmouseout="window.highlightError(this, false)" style="display: inline-block; margin: 0px; padding: 0px;"> <success></success>
<pre class="f0" onclick="window.jumpToError(this)" onmouseover="window.highlightError(this, true)" onmouseout="window.highlightError(this, false)" style="display: inline-block; margin: 0px; padding: 0px;"> <attributes>[[2017, 4, 18, 23, 59, 55, [h, p, c, h, l, p, 2, 0, 0], [6, Z, L, A], 9]]</attributes>
<pre class="f0" onclick="window.jumpToError(this)" onmouseover="window.highlightError(this, true)" onmouseout="window.highlightError(this, false)" style="display: inline-block; margin: 0px; padding: 0px;"></linestruct>
<pre class="f0" onclick="window.jumpToError(this)" onmouseover="window.highlightError(this, true)" onmouseout="window.highlightError(this, false)" style="display: inline-block; margin: 0px; padding: 0px;">Parse success
<pre class="f0" onclick="window.jumpToError(this)" onmouseover="window.highlightError(this, true)" onmouseout="window.highlightError(this, false)" style="display: inline-block; margin: 0px; padding: 0px;"><linestruct>
<pre class="f0" onclick="window.jumpToError(this)" onmouseover="window.highlightError(this, true)" onmouseout="window.highlightError(this, false)" style="display: inline-block; margin: 0px; padding: 0px;"> <try>2017-04-18 23:59:55 </try>
<pre class="f0" onclick="window.jumpToError(this)" onmouseover="window.highlightError(this, true)" onmouseout="window.highlightError(this, false)" style="display: inline-block; margin: 0px; padding: 0px;"> <success></success>
<pre class="f0" onclick="window.jumpToError(this)" onmouseover="window.highlightError(this, true)" onmouseout="window.highlightError(this, false)" style="display: inline-block; margin: 0px; padding: 0px;"> <attributes>[[2017, 4, 18, 23, 59, 55, [h, p, c, h, l, p, 2, 0, 0], [], 9]]</attributes>
<pre class="f0" onclick="window.jumpToError(this)" onmouseover="window.highlightError(this, true)" onmouseout="window.highlightError(this, false)" style="display: inline-block; margin: 0px; padding: 0px;"></linestruct>
<pre class="f0" onclick="window.jumpToError(this)" onmouseover="window.highlightError(this, true)" onmouseout="window.highlightError(this, false)" style="display: inline-block; margin: 0px; padding: 0px;">Parse success


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Parsing an empty member into a fusion adapted struct

sehe
On 05-05-17 23:06, Seth wrote:
>             ushort_ >> ':' >> ushort_ >> ':' >> ushort_ >>
>             -(~(ushort_ >> eoi) >> lexeme[ *alnum ]) >>
>             -(~(ushort_ >> eoi) >> lexeme[ *alnum ]) >>
>             ushort_;

Well. It works without the typo. ~ needed to be !


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Loading...