I want to create a grammar and lexer to parse the below string:
100 reason phrase
regular expression will be: "\d{3} [^\r\n]*"
token definition:
template <typename Lexer>
struct custom_tokens : lex::lexer<Lexer>
{
custom_tokens()
{
this->self.add_pattern
("STATUSCODE", "\\d{3}")
("SP", " ")
("REASONPHRASE", "[^\r\n]*")
;
this->self.add
("{STATUSCODE}", T_STATUSCODE)
("{SP}", T_SP)
("{REASONPHRASE}", T_REASONPHRASE)
;
}
};
grammar:
template <typename Iterator>
struct custom_grammar : qi::grammar<Iterator >
{
template <typename TokenDef>
custom_grammar(TokenDef const& tok)
: custom_grammar::base_type(start)
{
start = (qi::token(T_STATUSCODE) >> qi::token(T_SP) >> qi::token(T_REASONPHRASE));
}
qi::rule<Iterator> start;
};
however, I realized that I couldn't define token "T_REASONPHRASE" because it will match everything including "T_STATUSCODE". what I can do is
undefine T_REASONPHRASE and use qi::lexeme to write a rule inside custom_grammar?
can I use lex state to do that? e.g. define "T_REASONPHRASE" in second state, if it sees T_STATUSCODE in first state then parse the rest to second state? please give an example?