]> git.mxchange.org Git - simgear.git/blobdiff - simgear/nasal/lex.c
Alas. Fix #pragma magic for GCC <= 4.5.
[simgear.git] / simgear / nasal / lex.c
index 958e0fb4e9ec254022a57a52853f88987e377ebc..89c3c407b28c578181f07c4e460c684ea8fe591d 100644 (file)
@@ -1,7 +1,7 @@
 #include "parse.h"
 
 // Static table of recognized lexemes in the language
-struct Lexeme {
+static const struct Lexeme {
     char* str;
     int   tok;
 } LEXEMES[] = {
@@ -130,7 +130,6 @@ static void newToken(struct Parser* p, int pos, int type,
     tok->str = str;
     tok->strlen = slen;
     tok->num = num;
-    tok->parent = &p->tree;
     tok->next = 0;
     tok->prev = last;
     tok->children = 0;
@@ -182,6 +181,7 @@ static void sqEscape(char* buf, int len, int index, struct Parser* p,
 }
 
 // Ditto, but more complicated for double quotes.
+/* FIXME: need to handle \b (8), \f (12), and \uXXXX for JSON compliance */
 static void dqEscape(char* buf, int len, int index, struct Parser* p,
                      char* cOut, int* eatenOut)
 {
@@ -256,25 +256,27 @@ static int lexHexLiteral(struct Parser* p, int index)
     return i;
 }
 
+#define ISNUM(c) ((c) >= '0' && (c) <= '9')
+#define ISHEX(c) (ISNUM(c) || ((c)>='a' && (c)<='f') || ((c)>='A' && (c)<='F'))
+#define NUMSTART(c) (ISNUM(c) || (c) == '+' || (c) == '-')
 static int lexNumLiteral(struct Parser* p, int index)
 {
     int len = p->len, i = index;
     unsigned char* buf = (unsigned char*)p->buf;
     double d;
 
-    if(i+1<len && buf[i+1] == 'x') return lexHexLiteral(p, index+2);
+    if(buf[i] == '0' && i+2<len && buf[i+1] == 'x' && ISHEX(buf[i+2]))
+       return lexHexLiteral(p, index+2);
 
-    while(i<len && buf[i] >= '0' && buf[i] <= '9') i++;
+    while(i<len && ISNUM(buf[i])) i++;
     if(i<len && buf[i] == '.') {
         i++;
-        while(i<len && buf[i] >= '0' && buf[i] <= '9') i++;
+        while(i<len && ISNUM(buf[i])) i++;
     }
-    if(i<len && (buf[i] == 'e' || buf[i] == 'E')) {
+    if(i+1<len && (buf[i] == 'e' || buf[i] == 'E') && NUMSTART(buf[i+1])) {
         i++;
-        if(i<len
-           && (buf[i] == '-' || buf[i] == '+')
-           && (i+1<len && buf[i+1] >= '0' && buf[i+1] <= '9')) i++;
-        while(i<len && buf[i] >= '0' && buf[i] <= '9') i++;
+        if(buf[i] == '-' || buf[i] == '+') i++;
+        while(i<len && ISNUM(buf[i])) i++;
     }
     naStr_parsenum(p->buf + index, i - index, &d);
     newToken(p, index, TOK_LITERAL, 0, 0, d);
@@ -333,7 +335,6 @@ static int tryLexemes(struct Parser* p, int index, int* lexemeOut)
     return best;
 }
 
-#define ISNUM(c) ((c) >= '0' && (c) <= '9')
 void naLex(struct Parser* p)
 {
     int i = 0;