]> git.ipfire.org Git - thirdparty/plymouth.git/commitdiff
[scan] Add error reporting to the scanner
authorCharlie Brej <cbrej@cs.man.ac.uk>
Tue, 23 Jun 2009 13:06:30 +0000 (14:06 +0100)
committerCharlie Brej <cbrej@cs.man.ac.uk>
Tue, 23 Jun 2009 13:06:30 +0000 (14:06 +0100)
The canner now returs error tokens when strings or comments are not correctly
terminated.

src/plugins/splash/script/ply-scan.c
src/plugins/splash/script/ply-scan.h

index d8b967724142762baa3b7bd81646e841ef9564d4..61b480c91cd21d1ad7bfce71a33c595f33f3e8e4 100644 (file)
@@ -54,6 +54,7 @@ void ply_scan_token_clean(ply_scan_token_t* token)
     case PLY_SCAN_TOKEN_TYPE_INTEGER:
     case PLY_SCAN_TOKEN_TYPE_FLOAT:
     case PLY_SCAN_TOKEN_TYPE_SYMBOL:
+    case PLY_SCAN_TOKEN_TYPE_ERROR:
         break;
     case PLY_SCAN_TOKEN_TYPE_IDENTIFIER:
     case PLY_SCAN_TOKEN_TYPE_STRING:
@@ -106,33 +107,40 @@ void ply_scan_read_next_token(ply_scan_t* scan, ply_scan_token_t* token)
  
  token->whitespace = 0;
  while(true){
-    if (curchar == ' ')  {curchar = ply_scan_get_next_char(scan); token->whitespace++; continue;}
+    if (curchar == ' ')  {curchar = ply_scan_get_next_char(scan); token->whitespace++; continue;}   //FIXME restrcuture
     if (curchar == '\n') {curchar = ply_scan_get_next_char(scan); token->whitespace++; continue;}
     if (curchar == '\t') {curchar = ply_scan_get_next_char(scan); token->whitespace++; continue;}
     break;
     }
+ nextchar = ply_scan_get_next_char(scan);
  if (ply_bitarray_lookup(scan->identifier_1st_char, curchar)){
     token->type = PLY_SCAN_TOKEN_TYPE_IDENTIFIER;
-    int index = 0;
-    token->data.string = NULL;
-    do {
+    int index = 1;
+    token->data.string =  malloc(2*sizeof(char));
+    token->data.string[0] = curchar;
+    token->data.string[1] = '\0';
+    curchar = nextchar;
+    while (ply_bitarray_lookup(scan->identifier_nth_char, curchar)){
         token->data.string = realloc(token->data.string, (index+2)*sizeof(char));
         token->data.string[index] = curchar;
         token->data.string[index+1] = '\0';
         index++;
         curchar = ply_scan_get_next_char(scan);
         }
-    while (ply_bitarray_lookup(scan->identifier_nth_char, curchar));
     return;
     }
  if (curchar >= '0' && curchar <= '9'){
-    long long int int_value = 0;
-    do {
+    long long int int_value = curchar - '0';
+    curchar = nextchar;
+    while (curchar >= '0' && curchar <= '9') {
         int_value *= 10;
         int_value += curchar - '0';
         curchar = ply_scan_get_next_char(scan);
         }
-    while (curchar >= '0' && curchar <= '9');
     
     if (curchar == '.'){
         double floatpoint = int_value;
@@ -159,16 +167,24 @@ void ply_scan_read_next_token(ply_scan_t* scan, ply_scan_token_t* token)
     return;
     }
  
- if (curchar == '"') {
+ if (curchar == '\"') {
     token->type = PLY_SCAN_TOKEN_TYPE_STRING;
     int index = 0;
     token->data.string = malloc(sizeof(char));
     token->data.string[0] = '\0';
+    curchar = nextchar;
 
-    while (1){
-        curchar = ply_scan_get_next_char(scan);
-        if (curchar == '"') break;
-        assert(curchar != '\0');
+    while (curchar != '\"'){
+        if (curchar == '\0') {
+            token->data.string = "End of file before end of string";
+            token->type = PLY_SCAN_TOKEN_TYPE_ERROR;
+            return;
+            }
+        if (curchar == '\n') {
+            token->data.string = "Line terminator before end of string";
+            token->type = PLY_SCAN_TOKEN_TYPE_ERROR;
+            return;
+            }
         if (curchar == '\\') {
             curchar = ply_scan_get_next_char(scan);
             switch (curchar){
@@ -189,13 +205,12 @@ void ply_scan_read_next_token(ply_scan_t* scan, ply_scan_token_t* token)
         token->data.string[index] = curchar;
         token->data.string[index+1] = '\0';
         index++;
+        curchar = ply_scan_get_next_char(scan);
         }
     ply_scan_get_next_char(scan);
     return;
     }
  
- nextchar = ply_scan_get_next_char(scan);
     {
     bool linecomment = false;
     if (curchar == '#') linecomment = true;
@@ -228,6 +243,13 @@ void ply_scan_read_next_token(ply_scan_t* scan, ply_scan_token_t* token)
     nextchar = ply_scan_get_next_char(scan);
     
     while (true){
+        if (nextchar == '\0') {
+            free(token->data.string);
+            token->data.string = "End of file before end of comment";
+            token->type = PLY_SCAN_TOKEN_TYPE_ERROR;
+            return;
+            }
+
         if (curchar == '/' && nextchar == '*') {
             depth++;
             }
index 01bb449b2adf6a218862c91d8bd8f6bebb6b23df..72a5d3110143e2ca116cb8dcf89810237bf28e23 100644 (file)
@@ -14,6 +14,7 @@ typedef enum
  PLY_SCAN_TOKEN_TYPE_STRING,
  PLY_SCAN_TOKEN_TYPE_SYMBOL,
  PLY_SCAN_TOKEN_TYPE_COMMENT,
+ PLY_SCAN_TOKEN_TYPE_ERROR,
 } ply_scan_token_type_t;
 
 typedef struct