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
| #include <string>
#include <boost/spirit.hpp>
#include "..\parser.h"
#include "..\afficheur.h"
class CPP : public Parser{
public:
CPP();
std::string parse(const std::string& code);
void reset();
Parser* Clone();
~CPP(void);
bool existe;
// Le parseur
template <typename ScannerT>
struct definition
{
definition(CPP const& self)
{
declarations actions = self.evenements;
program
= *space_p >>
*( preprocessor [actions.preprocessor]
| comment [actions.comment]
| keyword [actions.keyword]
| identifier [actions.identifier]
| special [actions.special]
| string [actions.string]
| literal [actions.literal]
| number [actions.number]
| anychar_p [actions.unexpected]
| type [actions.type]
| condition [actions.condition]
| boucle [actions.boucle]
| type [actions.type]
| objet [actions.objet]
)
;
preprocessor
= '#' >> *space_p >> identifier
;
comment
= +((comment_p("//") | comment_p("/*", "*/"))
>> *space_p)
;
keyword
= keywords >> (eps_p - (alnum_p | '_')) >> *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 >> (eps_p - (alnum_p | '_')) >> *space_p
; // make sure we recognize whole words only
conditions
= "if", "else", "elseif", "case", "switch"
;
boucle
= boucles >> (eps_p - (alnum_p | '_')) >> *space_p
; // make sure we recognize whole words only
boucles
= "while", "for", "break", "continue", "goto", "do"
;
objet
= objets >> (eps_p - (alnum_p | '_')) >> *space_p
; // make sure we recognize whole words only
objets
= "class", "struct", "private", "protected", "public", "throw", "this",
"new", "delete", "friend", "operator", "explicit"
;
type
= types >> (eps_p - (alnum_p | '_')) >> *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
= +chset_p("~!%^&*()+={[}]:;,<.>?/|\\-") >> *space_p
;
string
= !as_lower_d['l'] >> confix_p('"', *c_escape_ch_p, '"')
>> *space_p
;
literal
= !as_lower_d['l'] >> confix_p('\'', *c_escape_ch_p, '\'')
>> *space_p
;
number
= ( real_p
| as_lower_d["0x"] >> hex_p
| '0' >> oct_p
)
>> *as_lower_d[chset_p("ldfu")]
>> *space_p
;
identifier
= ((alpha_p | '_') >> *(alnum_p | '_'))
>> *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;
}
};
class declarations {
private:
std::string dernier;
public:
declarations();
afficheur preprocessor, comment, keyword, identifier, special, string, literal, number, condition, boucle, type, objet;
unexpected_char unexpected;
};
declarations evenements;
private:
std::string code_dormant;
}; |
Partager