Properly read characters from the source file.
[TP_AL_C.git] / lexer / main.c
index c8b897978c6c27e9945b1e5de0a2565132718bce..fd6a5ccc021f95a3e4d4be840b7b0d59dbded866 100644 (file)
@@ -25,7 +25,6 @@ enum TokenType {
     FIN
 } tokenType;
 const char* tokenTypestr[] = { "MOTCLE", "SECTION", "SSECTION", "NPARA", "MOT", "FIN" };
-unsigned int i = 0;
 
 /* It looks silly to check for each characters but for debugging, it's just the way to go */
 bool istAlpha() {
@@ -58,85 +57,65 @@ bool isSeparator() {
 }
 
 int scanner() {
-    const wchar_t* Titre = L"Titre";
-    const wchar_t* Auteur = L"Auteur";
-    unsigned int j = 0;
+    unsigned int i = 0;
+    wchar_t m[6];
 
-// The main loop get the next character
 init:
     if (c == L' ' || c == L'\t') {
         c = fgetwc(source);
-        token[tokenFound].value[i] = c;
-        i++;
         goto init;
     }
     if (c == L'\n') {
         c = fgetwc(source);
-        token[tokenFound].value[i] = c;
-        i++;
         goto initLV1;
     }
     if (c == L'>') {
         c = fgetwc(source);
-        token[tokenFound].value[i] = c;
-        i++;
         goto MC1;
     }
     if (c == L'=') {
         c = fgetwc(source);
-        token[tokenFound].value[i] = c;
-        i++;
         goto S1SS1;
     }
     if (istAlpha()) {
-        c = fgetwc(source);
         token[tokenFound].value[i] = c;
         i++;
+        c = fgetwc(source);
         goto M1;
     }
     if (c == WEOF) {
         goto FIN;
-    } else {
-        goto error;
     }
+    goto error;
 
 MC1:
-    // FIXME: Partial match need a rewind in the characters extraction from the file
-    if (c == (wint_t)Titre[j] && j < wcslen(Titre) - 1) {
+    if (c == L'A' && !wcscmp(fgetws(m, 6, source), L"uteur")) {
+        wcscpy((wchar_t*)token[tokenFound].value, L">Auteur");
         c = fgetwc(source);
-        token[tokenFound].value[i] = c;
-        i++;
-        j++;
-        goto MC1;
+        goto MC2;
     }
-    if (c == (wint_t)Auteur[j] && j < wcslen(Auteur) - 1) {
+    if (c == L'T' && !wcscmp(fgetws(m, 5, source), L"itre")) {
+        wcscpy((wchar_t*)token[tokenFound].value, L">Titre");
         c = fgetwc(source);
-        token[tokenFound].value[i] = c;
-        i++;
-        j++;
-        goto MC1;
-    } else {
-        c = fgetwc(source);
-        token[tokenFound].value[i] = c;
-        i++;
         goto MC2;
     }
+    goto error;
 
 S1SS1:
     if (c == L'=') {
         c = fgetwc(source);
-        token[tokenFound].value[i] = c;
-        i++;
         goto SS2;
     }
     if (isSeparator() || c == WEOF) {
         goto SECTION;
     }
+    goto error;
 
 SS2:
     if (isSeparator() || c == WEOF) {
         goto SSECTION;
     }
+    goto error;
 
 SECTION:
     tokenType = SECTION;
@@ -148,55 +127,47 @@ SSECTION:
 
 M1:
     if (istAlpha()) {
-        c = fgetwc(source);
         token[tokenFound].value[i] = c;
         i++;
+        c = fgetwc(source);
         goto M1;
     }
     if (isSeparator() || c == WEOF) {
         goto MOT;
     }
+    goto error;
 
 initLV1:
     if (c == L' ' || c == L'\t') {
         c = fgetwc(source);
-        token[tokenFound].value[i] = c;
-        i++;
         goto initLV1;
     }
     if (c == L'\n') {
         c = fgetwc(source);
-        token[tokenFound].value[i] = c;
-        i++;
         goto initLV1LV2;
     }
     if (istAlpha()) {
-        c = fgetwc(source);
         token[tokenFound].value[i] = c;
         i++;
+        c = fgetwc(source);
         goto M1;
     }
     if (c == L'=') {
         c = fgetwc(source);
-        token[tokenFound].value[i] = c;
-        i++;
         goto S1SS1;
     }
     if (c == L'>') {
         c = fgetwc(source);
-        token[tokenFound].value[i] = c;
-        i++;
         goto MC1;
     }
     if (c == WEOF) {
         goto FIN;
     }
+    goto error;
 
 initLV1LV2:
     if (isSeparator()) {
         c = fgetwc(source);
-        token[tokenFound].value[i] = c;
-        i++;
         goto initLV1LV2;
     }
     if (istAlpha()) {
@@ -204,19 +175,16 @@ initLV1LV2:
     }
     if (c == L'>') {
         c = fgetwc(source);
-        token[tokenFound].value[i] = c;
-        i++;
         goto MC1;
     }
     if (c == L'=') {
         c = fgetwc(source);
-        token[tokenFound].value[i] = c;
-        i++;
         goto S1SS1;
     }
     if (c == WEOF) {
         goto FIN;
     }
+    goto error;
 
 NPARA:
     tokenType = NPARA;
@@ -230,6 +198,7 @@ MC2:
     if (isSeparator() || c == WEOF) {
         goto MOTCLE;
     }
+    goto error;
 
 MOTCLE:
     tokenType = MOTCLE;
@@ -245,9 +214,8 @@ error:
 }
 
 int main() {
-
     // Ouvre le fichier test.txt en lecture seulement (le fichier doit exister) :
-    source = fopen("test.txt", "r");
+    source = fopen("test.txt", "r+");
     // Cree et ouvre un fichier target.html en lecture/ecriture
     // avec suppression du contenu au prealable :
     target = fopen("target.html", "w+");
@@ -262,26 +230,21 @@ int main() {
         return -1;
     }
 
+    c = fgetwc(source); // lecture du premier caractere
     do {
-        c = fgetwc(source); // lecture du caractere suivant du fichier source
-        token[tokenFound].value[i] = c;
-        i++;
         int scanrt = scanner();
         if (scanrt == -1) {
             wprintf(L"Scanner error with token value: %ls\n", token[tokenFound].value);
             exit(EXIT_FAILURE);
         }
         if (c != WEOF) {
-            wprintf(L"Token type found: %s with value: %ls\n", tokenTypestr[tokenType], token[tokenFound].value);
+            wprintf(L"%20s: %ls\n", tokenTypestr[tokenType], token[tokenFound].value);
         } else {
-            wprintf(L"Token type found: %s\n", tokenTypestr[tokenType]);
+            wprintf(L"%20s\n", tokenTypestr[tokenType]);
         }
         token[tokenFound].type = tokenTypestr[tokenType];
         tokenFound++;
-        // reinit token.value array counter
-        i = 0;
-    //} while (c != WEOF); // tant que la fin du fichier n'est pas atteinte
-    } while (!feof(source)); // tant que la fin du fichier n'est pas atteinte
+    } while (tokenType != FIN); // tant que la fin du fichier n'est pas atteinte
 
     if (source != NULL) fclose(source); // fermeture du fichier source
     if (target != NULL) fclose(target); // fermeture du fichier target