1+ #include " TokenBuilding.h"
2+
3+ namespace FPL {
4+ std::vector<Token> TokenBuilding::parseToken (const std::string &content) {
5+ std::vector<Token> tokens;
6+ Token currentToken;
7+
8+ for (auto &currT : content) // char
9+ {
10+ if (currentToken.mType == CHAINE_ESPACE_SEQUENCE) {
11+ switch (currT) {
12+ case ' n' :
13+ currentToken.mText .append (1 , ' \n ' );
14+ break ;
15+ case ' r' :
16+ currentToken.mText .append (1 , ' \r ' );
17+ break ;
18+ case ' t' :
19+ currentToken.mText .append (1 , ' \t ' );
20+ break ;
21+ case ' \\ ' :
22+ currentToken.mText .append (1 , ' \\ ' );
23+ break ;
24+ default :
25+ throw std::runtime_error (std::string (" Inconnu : \\ " ) + std::string (1 , currT) +
26+ " dans la ligne " + std::to_string (currentToken.mLineNumber ) + " ." );
27+ }
28+
29+ currentToken.mType = CHAINE_LITERAL;
30+ continue ;
31+ }
32+ else if (currentToken.mType == PEUTETRE_COMMENTAIRE && currT != ' /' ) {
33+ currentToken.mType = OPERATEUR;
34+ endToken (currentToken, tokens);
35+ continue ;
36+ }
37+
38+ switch (currT)
39+ {
40+ case ' 0' :
41+ case ' 1' :
42+ case ' 2' :
43+ case ' 3' :
44+ case ' 4' :
45+ case ' 5' :
46+ case ' 6' :
47+ case ' 7' :
48+ case ' 8' :
49+ case ' 9' :
50+ if (currentToken.mType == ESPACEVIDE) {
51+ currentToken.mType = ENTIER_LITERAL;
52+ currentToken.mText .append (1 , currT);
53+ } else if (currentToken.mType == PEUTETRE_DOUBLE) {
54+ currentToken.mType = DECIMAL_LITERAL;
55+ currentToken.mText .append (1 , currT);
56+ } else {
57+ currentToken.mText .append (1 , currT);
58+ }
59+ break ;
60+
61+
62+ case ' .' :
63+ if (currentToken.mType == ESPACEVIDE) {
64+ currentToken.mType = PEUTETRE_DOUBLE;
65+ currentToken.mText .append (1 , currT);
66+ } else if (currentToken.mType == ENTIER_LITERAL) {
67+ currentToken.mType = DECIMAL_LITERAL;
68+ currentToken.mText .append (1 , currT);
69+ } else if (currentToken.mType == CHAINE_LITERAL) {
70+ currentToken.mText .append (1 , currT);
71+ } else {
72+ endToken ( currentToken, tokens);
73+ currentToken.mType = OPERATEUR;
74+ currentToken.mText .append (1 , currT);
75+ endToken (currentToken, tokens);
76+ }
77+ break ;
78+
79+
80+ case ' {' :
81+ case ' }' :
82+ case ' (' :
83+ case ' )' :
84+ case ' =' :
85+ case ' +' :
86+ case ' -' :
87+ case ' *' :
88+ case ' >' :
89+ case ' <' :
90+ case ' ;' :
91+ case ' ,' :
92+ if (currentToken.mType != CHAINE_LITERAL) {
93+ endToken (currentToken, tokens);
94+ currentToken.mType = OPERATEUR;
95+ currentToken.mText .append (1 , currT);
96+ endToken (currentToken, tokens);
97+ } else {
98+ currentToken.mText .append (1 , currT);
99+ }
100+ break ;
101+
102+
103+ case ' \t ' :
104+ case ' ' :
105+ if (currentToken.mType == CHAINE_LITERAL || currentToken.mType == COMMENTAIRE)
106+ {
107+ currentToken.mText .append (1 , currT);
108+ }
109+ else {
110+ endToken (currentToken, tokens);
111+ }
112+ break ;
113+
114+
115+ case ' \r ' :
116+ case ' \n ' :
117+ endToken (currentToken, tokens);
118+ ++currentToken.mLineNumber ;
119+ break ;
120+
121+
122+ case ' "' :
123+ if (currentToken.mType != CHAINE_LITERAL) {
124+ endToken (currentToken, tokens);
125+ currentToken.mType = CHAINE_LITERAL;
126+ currentToken.mText .append (1 , currT);
127+ } else if (currentToken.mType == CHAINE_LITERAL) {
128+ endToken (currentToken, tokens);
129+ }
130+ break ;
131+
132+
133+ case ' \\ ' :
134+ if (currentToken.mType == CHAINE_LITERAL) {
135+ endToken (currentToken, tokens);
136+ currentToken.mType = CHAINE_LITERAL;
137+ currentToken.mText .append (1 , currT);
138+ } else {
139+ endToken (currentToken, tokens);
140+ currentToken.mType = OPERATEUR;
141+ currentToken.mText .append (1 , currT);
142+ endToken (currentToken, tokens);
143+ }
144+ break ;
145+
146+ case ' /' :
147+ if (currentToken.mType == CHAINE_LITERAL) {
148+ currentToken.mText .append (1 , currT);
149+ } else if (currentToken.mType == PEUTETRE_COMMENTAIRE) {
150+ currentToken.mType = COMMENTAIRE;
151+ currentToken.mText .erase ();
152+ } else {
153+ endToken (currentToken, tokens);
154+ currentToken.mType = PEUTETRE_COMMENTAIRE;
155+ currentToken.mText .append (1 , currT);
156+ }
157+ break ;
158+
159+ default :
160+ if (currentToken.mType == ESPACEVIDE || currentToken.mType == ENTIER_LITERAL
161+ || currentToken.mType == DECIMAL_LITERAL)
162+ {
163+ endToken (currentToken, tokens);
164+ currentToken.mType = IDENTIFIANT;
165+ currentToken.mText .append (1 , currT);
166+ }
167+ else {
168+ currentToken.mText .append (1 , currT);
169+ }
170+ break ;
171+ }
172+ }
173+
174+ return tokens;
175+ }
176+
177+ void TokenBuilding::endToken (Token &token, std::vector<Token> &tokens) {
178+ if (token.mType == COMMENTAIRE) {
179+ // std::cout << "Commentaire ignoree : '" << token.mText << "'." << std::endl;
180+ } else if (token.mType != ESPACEVIDE) {
181+ tokens.push_back (token);
182+ }
183+ if (token.mType == PEUTETRE_DOUBLE)
184+ {
185+ if (token.mText == " ." ) {
186+ token.mType = OPERATEUR;
187+ }
188+ else {
189+ token.mType = DECIMAL_LITERAL;
190+ }
191+ }
192+ token.mType = ESPACEVIDE;
193+ token.mText .erase ();
194+ }
195+
196+ [[maybe_unused]] void Token::DebugPrint () const {
197+ std::cout << " Token(" << sTokenTypes [mType ] << " , '" << mText << " '." << std::endl;
198+ }
199+ }
0 commit comments