0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-03 12:28:52 +01:00
construct/include/ircd/rfc1459_parse.h

223 lines
5.5 KiB
C
Raw Normal View History

2016-09-17 04:48:25 +02:00
/**
* Copyright (C) 2016 Charybdis Development Team
* Copyright (C) 2016 Jason Volk <jason@zemos.net>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice is present in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#define HAVE_IRCD_RFC1459_PARSE_H
#include <boost/spirit/include/qi.hpp>
BOOST_FUSION_ADAPT_STRUCT
(
2016-11-29 16:23:38 +01:00
ircd::rfc1459::pfx
,( decltype(ircd::rfc1459::pfx::nick), nick )
,( decltype(ircd::rfc1459::pfx::user), user )
,( decltype(ircd::rfc1459::pfx::host), host )
2016-09-17 04:48:25 +02:00
)
BOOST_FUSION_ADAPT_STRUCT
(
2016-11-29 16:23:38 +01:00
ircd::rfc1459::line
,( decltype(ircd::rfc1459::line::pfx), pfx )
,( decltype(ircd::rfc1459::line::cmd), cmd )
,( decltype(ircd::rfc1459::line::parv), parv )
2016-09-17 04:48:25 +02:00
)
namespace ircd {
namespace rfc1459 {
namespace parse {
namespace qi = boost::spirit::qi;
using qi::lit;
using qi::char_;
using qi::repeat;
using qi::attr;
using qi::eps;
2016-11-29 16:23:38 +01:00
using qi::raw;
using qi::omit;
2016-09-17 04:48:25 +02:00
/* The grammar template class.
* This aggregates all the rules under one template to make composing them easier.
*
* The grammar template is instantiated by individual parsers depending on the goal
* for the specific parse, or the "top level." The first top-level was an IRC line, so
* a class was created `struct head` specifying grammar::line as the top rule, and
* rfc1459::line as the top output target to parse into.
*/
template<class it,
class top>
struct grammar
:qi::grammar<it, top>
{
qi::rule<it> space;
qi::rule<it> colon;
qi::rule<it> nulcrlf;
qi::rule<it> spnulcrlf;
qi::rule<it> terminator;
2016-11-29 16:23:38 +01:00
qi::rule<it, rfc1459::host> hostname;
qi::rule<it, rfc1459::host> server;
qi::rule<it, rfc1459::user> user;
qi::rule<it, rfc1459::nick> nick;
2016-09-17 04:48:25 +02:00
qi::rule<it, rfc1459::pfx> prefix;
2016-11-29 16:23:38 +01:00
qi::rule<it, string_view> trailing;
qi::rule<it, string_view> middle;
2016-09-17 04:48:25 +02:00
qi::rule<it, rfc1459::parv> params;
2016-11-29 16:23:38 +01:00
qi::rule<it, string_view> numeric;
2016-09-17 04:48:25 +02:00
qi::rule<it, rfc1459::cmd> command;
qi::rule<it, rfc1459::line> line;
2016-11-29 16:23:38 +01:00
qi::rule<it, std::deque<rfc1459::line>> tape;
2016-09-17 04:48:25 +02:00
grammar(qi::rule<it, top> &top_rule);
};
template<class it,
class top>
grammar<it, top>::grammar(qi::rule<it, top> &top_rule)
:grammar<it, top>::base_type
{
top_rule
}
,space // A single space character
{
/* TODO: RFC says:
* 1) <SPACE> is consists only of SPACE character(s) (0x20).
* Specially notice that TABULATION, and all other control
* characters are considered NON-WHITE-SPACE.
* But our table in this namespace has control characters labeled as SPACE.
* This needs to be fixed.
*/
//char_(charset(character::SPACE))
2016-09-17 04:48:25 +02:00
lit(' ')
,"space"
}
,colon // A single colon character
{
lit(':')
,"colon"
}
,nulcrlf // Match on NUL or CR or LF
{
lit('\0') | lit('\r') | lit('\n')
,"nulcrlf"
}
,spnulcrlf // Match on space or nulcrlf
{
space | nulcrlf
,"spnulcrlf"
}
,terminator // The message terminator
{
lit('\r') >> lit('\n')
,"terminator"
}
,hostname // A valid hostname
{
2016-11-29 16:23:38 +01:00
raw[+char_(charset(character::HOST))] // TODO: https://tools.ietf.org/html/rfc952
2016-09-17 04:48:25 +02:00
,"hostname"
}
,server // A valid servername
{
hostname
,"server"
}
2016-11-29 16:23:38 +01:00
,user // A valid username
{
raw[+char_(charset(character::USER))]
,"user"
}
2016-09-17 04:48:25 +02:00
,nick // A valid nickname, leading letter followed by any NICK chars
{
2016-11-29 16:23:38 +01:00
raw[char_(charset(character::ALPHA)) >> *char_(charset(character::NICK))]
2016-09-17 04:48:25 +02:00
,"nick"
}
,prefix // A valid prefix, required name, optional user and host (or empty placeholders)
{
colon >> (nick | server) >> -(lit('!') >> user) >> -(lit('@') >> hostname)
,"prefix"
}
,trailing // Trailing string pinch
{
2016-11-29 16:23:38 +01:00
colon >> raw[+(char_ - nulcrlf)]
2016-09-17 04:48:25 +02:00
,"trailing"
}
,middle // Spaced parameters
{
2016-11-29 16:23:38 +01:00
!colon >> raw[+(char_ - spnulcrlf)]
2016-09-17 04:48:25 +02:00
,"middle"
}
,params // Parameter vector
{
*(+space >> middle) >> -(+space >> trailing)
,"params"
}
,numeric // \d\d\d numeric
{
repeat(3)[char_(charset(character::DIGIT))]
2016-09-17 04:48:25 +02:00
,"numeric"
}
,command // A command or numeric
{
2016-11-29 16:23:38 +01:00
raw[+char_(charset(character::ALPHA)) | numeric]
2016-09-17 04:48:25 +02:00
,"command"
}
,line
{
-(prefix >> +space) >> command >> params
,"line"
}
,tape
{
+(-line >> +terminator)
,"tape"
}
{
}
2016-11-29 16:23:38 +01:00
// Instantiate the input grammar to parse a uint8_t* buffer into an rfc1459::line object.
// The top rule is inherited and then specified as grammar::line, which is compatible
// with an rfc1459::line object.
//
struct head
:parse::grammar<const char *, rfc1459::line>
{
head(): grammar{grammar::line} {}
}
extern const head;
// Instantiate the input grammar to parse a uint8_t* buffer into an rfc1459::tape object.
// The top rule is now grammar::tape and the target object is an rfc1459::tape deque.
//
struct capstan
:parse::grammar<const char *, std::deque<rfc1459::line>>
{
capstan(): grammar{grammar::tape} {}
}
extern const capstan;
} // namespace parse
} // namespace rfc1459
} // namespace ircd