-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.hpp
127 lines (104 loc) · 3.03 KB
/
grammar.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <string>
#include <vector>
#include <boost/spirit/home/qi.hpp>
#include <boost/spirit/include/qi_and_predicate.hpp>
#include <boost/spirit/include/qi_hold.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/qi_repeat.hpp>
#include <boost/fusion/tuple.hpp>
#include "tlds.hpp"
using namespace boost::spirit;
namespace phx = boost::phoenix;
template <typename Iterator>
struct myGrammar : qi::grammar<Iterator, std::vector<std::string>()>
{
myGrammar() : myGrammar::base_type(start)
{
using qi::eps;
using qi::lit;
using qi::_val;
using qi::_1;
using ascii::char_;
notURL = char_;//(!URL >> char_);
username =
(
+((qi::alnum | qi::punct) - char_(":@"))
);
password =
(
+((qi::alnum | qi::punct) - char_("@"))
);
auth_string =
(
username >> -(char_(":") >> password) >> char_("@")
);
tld =
(
tlds_
);
domain =
(
+( +(char_("a-zA-Z_\\-")) >> char_(".") ) >>
tld >> &~(char_("a-zA-Z_\\-"))
);
port %=
(
lit(":") >> uint_
);
key = +( qi::alnum | qi::punct );
value = *( qi::alnum | qi::punct );
pair =
(
key >> -(char_("=") >> value)
);
params =
(
char_("?") >>
pair >>
*(char_("&") >> pair)
);
path =
(
char_("/") >>
*(+( char_("a-zA-Z_0-9,.") | char_("-") ) >>
-char_("/")) >>
-params
);
proto =
(
( qi::string("http") >> -qi::string("s") ) | qi::string("ftp")
);
URL =
(
-( proto >> qi::string("://") ) >>
-hold[auth_string] >>
domain >>
-port >>
-path
);
start =
(
+(URL | notURL)
// *(*notURL >> URL [phx::push_back(_val,_1)] >> *notURL)
)
;
}
qi::rule<Iterator, std::vector<std::string>()> start;
qi::rule<Iterator, std::string()> URL;
qi::rule<Iterator, void()> notURL;
qi::rule<Iterator, std::string()> separator;
qi::rule<Iterator, std::string()> username;
qi::rule<Iterator, std::string()> password;
qi::rule<Iterator, std::string()> auth_string;
qi::rule<Iterator, std::string()> tld;
qi::rule<Iterator, std::string()> domain;
qi::rule<Iterator, unsigned()> port;
qi::rule<Iterator, std::string()> proto;
qi::rule<Iterator, std::string()> path;
qi::rule<Iterator, std::string()> params;
qi::rule<Iterator, std::string()> pair;
qi::rule<Iterator, std::string()> key;
qi::rule<Iterator, std::string()> value;
tlds tlds_;
};