]> git.mxchange.org Git - simgear.git/blobdiff - simgear/nasal/lex.c
Olaf Flebbe:
[simgear.git] / simgear / nasal / lex.c
index 86f1442eb5c2ce79514f0b918845ff7ab9cd340a..73733e464c82604b3dc31097f26d15ba09bd0eb7 100644 (file)
@@ -40,7 +40,16 @@ struct Lexeme {
     {"return",   TOK_RETURN},
     {"break",    TOK_BREAK},
     {"continue", TOK_CONTINUE},
-    {"func", TOK_FUNC}
+    {"func", TOK_FUNC},
+    {"...", TOK_ELLIPSIS},
+    {"?", TOK_QUESTION},
+    {"var", TOK_VAR},
+    {"+=", TOK_PLUSEQ},
+    {"-=", TOK_MINUSEQ},
+    {"*=", TOK_MULEQ},
+    {"/=", TOK_DIVEQ},
+    {"~=", TOK_CATEQ},
+    {"forindex", TOK_FORINDEX},
 };
 
 // Build a table of where each line ending is
@@ -58,7 +67,6 @@ static int* findLines(struct Parser* p)
 
         // Skip over the \r of a \r\n pair.
         if(buf[i] == '\r' && (i+1)<p->len && buf[i+1] == '\n') {
-            i++;
             continue;
         }
         // Reallocate if necessary
@@ -128,14 +136,19 @@ static void newToken(struct Parser* p, int pos, int type,
     p->tree.lastChild = tok;
 }
 
-// Parse a hex nibble
-static int hexc(char c, struct Parser* p, int index)
+static int hex(char c)
 {
     if(c >= '0' && c <= '9') return c - '0';
-    if(c >= 'A' && c <= 'F') return c - 'a' + 10;
+    if(c >= 'A' && c <= 'F') return c - 'A' + 10;
     if(c >= 'a' && c <= 'f') return c - 'a' + 10;
-    error(p, "bad hex constant", index);
-    return 0;
+    return -1;
+}
+
+static int hexc(char c, struct Parser* p, int index)
+{
+    int n = hex(c);
+    if(n < 0) error(p, "bad hex constant", index);
+    return n;
 }
 
 // Escape and returns a single backslashed expression in a single
@@ -170,6 +183,7 @@ static void dqEscape(char* buf, int len, int index, struct Parser* p,
         if(len < 4) error(p, "unterminated string", index);
         *cOut = (char)((hexc(buf[2], p, index)<<4) | hexc(buf[3], p, index));
         *eatenOut = 4;
+        break;
     default:
         // Unhandled, put the backslash back
         *cOut = '\\';
@@ -177,13 +191,19 @@ static void dqEscape(char* buf, int len, int index, struct Parser* p,
     }
 }
 
+// FIXME: should handle UTF8 too
+static void charLiteral(struct Parser* p, int index, char* s, int len)
+{
+    if(len != 1) error(p, "character constant not single character", index);
+    newToken(p, index, TOK_LITERAL, 0, 0, *s);
+}
+
 // Read in a string literal
-static int lexStringLiteral(struct Parser* p, int index, int singleQuote)
+static int lexStringLiteral(struct Parser* p, int index, char q)
 {
     int i, j, len, iteration;
     char* out = 0;
     char* buf = p->buf;
-    char endMark = singleQuote ? '\'' : '"';
 
     for(iteration = 0; iteration<2; iteration++) {
         i = index+1;
@@ -191,11 +211,10 @@ static int lexStringLiteral(struct Parser* p, int index, int singleQuote)
         while(i < p->len) {
             char c = buf[i];
             int eaten = 1;
-            if(c == endMark)
-                break;
+            if(c == q) break;
             if(c == '\\') {
-                if(singleQuote) sqEscape(buf+i, p->len-i, i, p, &c, &eaten);
-                else            dqEscape(buf+i, p->len-i, i, p, &c, &eaten);
+                if(q == '\'') sqEscape(buf+i, p->len-i, i, p, &c, &eaten);
+                else          dqEscape(buf+i, p->len-i, i, p, &c, &eaten);
             }
             if(iteration == 1) out[j++] = c;
             i += eaten;
@@ -204,16 +223,31 @@ static int lexStringLiteral(struct Parser* p, int index, int singleQuote)
         // Finished stage one -- allocate the buffer for stage two
         if(iteration == 0) out = naParseAlloc(p, len);
     }
-    newToken(p, index, TOK_LITERAL, out, len, 0);
+    if(q == '`') charLiteral(p, index, out, len);
+    else         newToken(p, index, TOK_LITERAL, out, len, 0);
     return i+1;
 }
 
+static int lexHexLiteral(struct Parser* p, int index)
+{
+    int nib, i = index;
+    double d = 0;
+    while(i < p->len && (nib = hex(p->buf[i])) >= 0) {
+        d = d*16 + nib;
+        i++;
+    }
+    newToken(p, index, TOK_LITERAL, 0, 0, d);
+    return i;
+}
+
 static int lexNumLiteral(struct Parser* p, int index)
 {
     int len = p->len, i = index;
-    unsigned char* buf = p->buf;
+    unsigned char* buf = (unsigned char*)p->buf;
     double d;
 
+    if(i+1<len && buf[i+1] == 'x') return lexHexLiteral(p, index+2);
+
     while(i<len && buf[i] >= '0' && buf[i] <= '9') i++;
     if(i<len && buf[i] == '.') {
         i++;
@@ -235,7 +269,8 @@ static int trySymbol(struct Parser* p, int start)
 {
     int i = start;
     while((i < p->len) &&
-          ((p->buf[i] >= 'A' && p->buf[i] <= 'Z') ||
+          ((p->buf[i] == '_') ||
+           (p->buf[i] >= 'A' && p->buf[i] <= 'Z') ||
            (p->buf[i] >= 'a' && p->buf[i] <= 'z') ||
            (p->buf[i] >= '0' && p->buf[i] <= '9')))
     { i++; }
@@ -299,12 +334,12 @@ void naLex(struct Parser* p)
         case '#':
             i = lineEnd(p, getLine(p, i));
             break;
-        case '\'': case '"':
-            i = lexStringLiteral(p, i, (c=='"' ? 0 : 1));
+        case '\'': case '"': case '`':
+            i = lexStringLiteral(p, i, c);
             break;
         default:
             if(c >= '0' && c <= '9') i = lexNumLiteral(p, i);
-            else                     handled = 0;
+            else handled = 0;
         }
 
         // Lexemes and symbols are a little more complicated.  Pick
@@ -314,9 +349,9 @@ void naLex(struct Parser* p)
         // symbol (e.g. "orchid").  If neither match, we have a bad
         // character in the mix.
         if(!handled) {
-            int symlen=0, lexlen=0, lexeme;
+            int symlen=0, lexlen=0, lexeme=-1;
             lexlen = tryLexemes(p, i, &lexeme);
-            if((c>='A' && c<='Z') || (c>='a' && c<='z'))
+            if((c>='A' && c<='Z') || (c>='a' && c<='z') || (c=='_'))
                 symlen = trySymbol(p, i);
             if(lexlen && lexlen >= symlen) {
                 newToken(p, i, LEXEMES[lexeme].tok, 0, 0, 0);