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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
// libs/cpp.h :
#ifndef CPP_H_INCLUDED
#define CPP_H_INCLUDED
/*
Nom du fichier :
cpp.h
Description du fichier :
Fichier des prototypes du parseur du langage C++
Nom du programme :
KolorEngine
Description du programme :
KolorEngine est un programme réalisé en C++ qui parse plusieurs centaines de langages
Date de debut :
02/09/2008 - 12:05
Date de fin :
03/09/2008 - 12:15
Version :
0-09a
Programmeur :
Katagoto
*/
#include <string>
#include "..\tete.h"
#include "..\afficheur.h"
class CPP : public boost::spirit::grammar<CPP>{
public:
CPP();
~CPP(void);
// Le parseur
template <typename ScannerT>
struct definition
{
definition(CPP const& self)
{
program
= *boost::spirit::space_p >>
*( preprocessor [self.preprocessor]
| comment [self.comment]
| keyword [self.keyword]
| identifier [self.identifier]
| special [self.special]
| string [self.string]
| literal [self.literal]
| number [self.number]
| boost::spirit::anychar_p [self.unexpected]
| type [self.type]
| condition [self.condition]
| boucle [self.boucle]
| type [self.type]
| objet [self.objet]
)
;
preprocessor
= '#' >> *boost::spirit::space_p >> identifier
;
comment
= +((boost::spirit::comment_p("//") | boost::spirit::comment_p("/*", "*/"))
>> *boost::spirit::space_p)
;
keyword
= keywords >> (boost::spirit::eps_p - (boost::spirit::alnum_p | '_')) >> *boost::spirit::space_p
; // make sure we recognize whole words only
keywords
= "and_eq", "and", "asm", "auto", "bitand", "bitor",
"catch", "compl", "default", "enum", "export", "false",
"inline", "mutable", "namespace", "not_eq", "not",
"or_eq", "or", "register", "reinterpret_cast", "return",
"sizeof", "template", "true", "try", "typedef", "typeid",
"typename", "union", "unsigned", "using", "volatile", "xor_eq", "xor"
;
condition
= conditions >> (boost::spirit::eps_p - (boost::spirit::alnum_p | '_')) >> *boost::spirit::space_p
; // make sure we recognize whole words only
conditions
= "if", "else", "elseif", "case", "switch"
;
boucle
= boucles >> (boost::spirit::eps_p - (boost::spirit::alnum_p | '_')) >> *boost::spirit::space_p
; // make sure we recognize whole words only
boucles
= "while", "for", "break", "continue", "goto", "do"
;
objet
= objets >> (boost::spirit::eps_p - (boost::spirit::alnum_p | '_')) >> *boost::spirit::space_p
; // make sure we recognize whole words only
objets
= "class", "struct", "private", "protected", "public", "throw", "this",
"new", "delete", "friend", "operator", "explicit"
;
type
= types >> (boost::spirit::eps_p - (boost::spirit::alnum_p | '_')) >> *boost::spirit::space_p
; // make sure we recognize whole words only
types
= "bool", "int", "signed", "unsigned", "char", "double", "long", "short",
"static", "virtual", "void", "wchart", "float", "static_cast", "dynamic_cast",
"const_cast", "const", "extern"
;
special
= +boost::spirit::chset_p("~!%^&*()+={[}]:;,<.>?/|\\-") >> *boost::spirit::space_p
;
string
= !boost::spirit::as_lower_d['l'] >> boost::spirit::confix_p('"', *boost::spirit::c_escape_ch_p, '"')
>> *boost::spirit::space_p
;
literal
= !boost::spirit::as_lower_d['l'] >> boost::spirit::confix_p('\'', *boost::spirit::c_escape_ch_p, '\'')
>> *boost::spirit::space_p
;
number
= ( boost::spirit::real_p
| boost::spirit::as_lower_d["0x"] >> boost::spirit::hex_p
| '0' >> boost::spirit::oct_p
)
>> *boost::spirit::as_lower_d[boost::spirit::chset_p("ldfu")]
>> *boost::spirit::space_p
;
identifier
= ((boost::spirit::alpha_p | '_') >> *(boost::spirit::alnum_p | '_'))
>> *boost::spirit::space_p
;
}
boost::spirit::symbols<>
keywords, types, conditions, boucles, objets;
boost::spirit::rule<ScannerT>
program, preprocessor, comment, special, string, literal,
number, identifier, keyword, type, condition, boucle, objet;
boost::spirit::rule<ScannerT> const&
start() const
{
return program;
}
};
afficheur preprocessor, comment, keyword, identifier,
special, string, literal, number, type, condition, boucle, objet;
unexpected_char unexpected;
private:
std::string dernier;
};
#endif // CPP_H_INCLUDED
// lib/cpp.cpp :
/*
Nom du fichier :
libs/cpp.cpp
Description du fichier :
Fichier de définition du parseur du langage C++
Nom du programme :
KolorEngine
Description du programme :
KolorEngine est un programme réalisé en C++ qui parse plusieurs centaines de langages
Date de debut :
02/09/2008 - 12:05
Date de fin :
03/09/2008 - 12:15
Version :
0-09a
Programmeur :
Katagoto
*/
#include <string>
#include "..\tete.h"
#include "..\afficheur.h"
#include "cpp.h"
CPP::CPP() : preprocessor("preprocesseur", dernier)
, comment("commentaires", dernier)
, keyword("keywords", dernier)
, identifier("identifier", dernier)
, special("special", dernier)
, string("chaine", dernier)
, literal("literal", dernier)
, number("number", dernier)
, type("type", dernier)
, condition("condition", dernier)
, boucle("boucle", dernier)
, objet("objet", dernier)
{
}
CPP::~CPP(void)
{
}
// tete.h :
#ifndef TETE_H_INCLUDED
#define TETE_H_INCLUDED
/*
Nom du fichier :
tete.h
Description du fichier :
Fichier d'inclusion des headers vitaux aux parseurs
Nom du programme :
KolorEngine
Description du programme :
KolorEngine est un programme réalisé en C++ qui parse plusieurs centaines de langages
Date de debut :
04/10/2008 - 11:40
Date de fin :
04/10/2008 - 11:45
Version :
0-09a
Programmeur :
Katagoto
*/
#include <boost/spirit.hpp>
#include <boost/spirit/core.hpp>
#include <boost/spirit/symbols/symbols.hpp>
#include <boost/spirit/utility/chset.hpp>
#include <boost/spirit/utility/escape_char.hpp>
#include <boost/spirit/utility/confix.hpp>
#endif // TETE_H_INCLUDED
// afficheur.h :
#ifndef AFFICHEUR_H_INCLUDED
#define AFFICHEUR_H_INCLUDED
/*
Nom du fichier :
afficheur.h
Description du fichier :
Fichier de déclaration des classes vitales au parseur
Nom du programme :
KolorEngine
Description du programme :
KolorEngine est un programme réalisé en C++ qui parse plusieurs centaines de langages
Date de debut :
02/09/2008 - 12:05
Date de fin :
03/09/2008 - 12:15
Version :
0-09a
Programmeur :
Katagoto
*/
#include <string>
template <typename CharT>
void print_char(CharT ch);
struct afficheur
{
afficheur(std::string name, std::string& dernier);
template <typename IteratorT>
void operator()(IteratorT first, IteratorT last) const;
std::string name;
std::string dernier;
};
struct unexpected_char
{
template <typename CharT>
void operator()(CharT) const;
};
#endif // AFFICHEUR_H_INCLUDED
// afficheur.cpp :
/*
Nom du fichier :
afficheur.cpp
Description du fichier :
Fichier de définition des classes vitales au parseur
Nom du programme :
KolorEngine
Description du programme :
KolorEngine est un programme réalisé en C++ qui parse plusieurs centaines de langages
Date de debut :
02/09/2008 - 12:05
Date de fin :
03/09/2008 - 12:15
Version :
0-09a
Programmeur :
Katagoto
*/
#include <string>
#include <iostream>
#include "afficheur.h"
template <typename CharT>
void print_char(CharT ch)
{
switch (ch)
{
case '<': std::cout << "<"; break;
case '>': std::cout << ">"; break;
case '&': std::cout << "&"; break;
case '"': std::cout << """; break;
default: std::cout << ch; break;
}
}
afficheur::afficheur(std::string name, std::string& dernier)
: name(name){}
template <typename IteratorT>
void afficheur::operator()(IteratorT first, IteratorT last) const
{
if(name!=dernier)
{
std::cout << "</span>";
std::cout << "<span class=" << name << ">";
}
while (first != last)
print_char(*first++);
}
template <typename CharT>
void unexpected_char::operator()(CharT) const
{
std::cout << '#';
} |
Partager