LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
SpiritCharChartParser.hpp
Go to the documentation of this file.
1// Copyright 2002-2013 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6#ifndef SPIRITCHARCHARTPARSER_HPP
7#define SPIRITCHARCHARTPARSER_HPP
8
9#include <boost/config/warning_disable.hpp>
10#include <boost/spirit/include/qi.hpp>
11#include <boost/spirit/include/phoenix_core.hpp>
12#include <boost/spirit/include/phoenix_operator.hpp>
13#include <boost/spirit/include/phoenix_object.hpp>
14#include <boost/fusion/include/adapt_struct.hpp>
15#include <boost/fusion/include/io.hpp>
16#include <boost/program_options.hpp>
17
18#include <boost/shared_ptr.hpp>
19
20#include <fstream>
21
22namespace po = boost::program_options;
23
24
25#include <iostream>
26#include <string>
27
28#define skipper qi::space | '#' >> *(ascii::char_ - qi::eol) >> qi::eol
29
35
37{
38 std::string name;
39 boost::optional<std::string> parent;
40 std::string comment;
41};
42
49{
50 int code;
51 std::string name;
52 std::string charclass;
53 std::vector<modifierdef> modifiers;
54};
55
57{
58 std::vector<charchart_class> classes;
59 std::vector<charchart_char> chars;
60};
61
64 (ModifierTypes, first)
65 (int, second)
66 )
67
70 (std::string, name)
71 (boost::optional<std::string>, parent)
72 (std::string, comment)
73 )
74
77 (int, code)
78 (std::string, name)
79 (std::string, charclass)
80 (std::vector<modifierdef>, modifiers)
81 )
82
85 (std::vector<charchart_class>, classes)
86 (std::vector<charchart_char>, chars)
87 )
88
89namespace qi = boost::spirit::qi;
90namespace ascii = boost::spirit::ascii;
91
92struct modifiersymbol_ : qi::symbols<char, unsigned>
93{
94 // > is +1 store
95 // / is +1 flush (forget the current token)
96 // = is +1 token
97 // ^ is exit
98 // & is token (without +1)
100 {
101 add
102 ("m", MIN)
103 ("M", MAJ)
104 ("u", UNMARK)
105 ;
106 }
107
108};
109// boost::shared_ptr<modifiersymbol_> modifiersymbol(new modifiersymbol_);
110
111typedef BOOST_TYPEOF(skipper) skipper_type_;
112
113
114template <typename Iterator>
115struct charchart_parser : qi::grammar<Iterator, charchart(), skipper_type_>
116{
118 charchart_parser() : charchart_parser::base_type(start, "start")
119 {
120 using qi::eps;
121 using qi::lit;
122 using qi::_val;
123 using qi::_1;
124 using ascii::char_;
125 using ascii::space_type;
126 using qi::lexeme;
127 using qi::alnum;
128 using qi::hex;
129 using qi::eol;
130
131 start %= classes >> chars ;
132 classes %= lit("classes") >> '{' >> -(classdef % ';') >> '}';
133 chars %= lit("chars") >> '{' >> -(chardef % ';') >> '}';
134 identifier %= lexeme[+(alnum | char_('_'))];
135 // c_M < C_Mm : Upper case letter
136 classdef %= identifier >> -('<' >> identifier) >> ':' >> lexeme[+(char_ - (';'|eol))];
137 // 0000, NULL, m_parag
138 chardef %= hex >> ',' >> lexeme[+(char_ - ',')] >> ',' >> identifier >> -(',' >> modifier % ',');
139 modifier %= mymodifiersymbol >> hex;
140 }
141
142 qi::rule<Iterator, charchart(), skipper_type_> start;
143 qi::rule<Iterator, std::vector<charchart_class>(), skipper_type_> classes;
144 qi::rule<Iterator, std::vector<charchart_char>(), skipper_type_> chars;
145 qi::rule<Iterator, charchart_class(), skipper_type_> classdef;
146 qi::rule<Iterator, charchart_char(), skipper_type_> chardef;
147 qi::rule<Iterator, std::string(), skipper_type_> identifier;
148 qi::rule<Iterator, modifierdef(), skipper_type_> modifier;
149};
150//]
151
152#endif // SPIRITCHARCHARTPARSER_HPP
typedef BOOST_TYPEOF(skipper) skipper_type_
#define skipper
BOOST_FUSION_ADAPT_STRUCT(modifierdef,(ModifierTypes, first)(int, second)) BOOST_FUSION_ADAPT_STRUCT(charchart_class
std::vector< modifierdef > modifiers
boost::optional< std::string > parent
qi::rule< Iterator, charchart(), skipper_type_ > start
qi::rule< Iterator, std::vector< charchart_class >(), skipper_type_ > classes
qi::rule< Iterator, std::string(), skipper_type_ > identifier
qi::rule< Iterator, modifierdef(), skipper_type_ > modifier
modifiersymbol_ mymodifiersymbol
qi::rule< Iterator, charchart_char(), skipper_type_ > chardef
qi::rule< Iterator, std::vector< charchart_char >(), skipper_type_ > chars
qi::rule< Iterator, charchart_class(), skipper_type_ > classdef
std::vector< charchart_char > chars
std::vector< charchart_class > classes