]> git.kaiwu.me - njs.git/commitdiff
Added support of oct literals.
authorDmitry Volyntsev <xeioex@nginx.com>
Fri, 17 Nov 2017 15:55:07 +0000 (18:55 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Fri, 17 Nov 2017 15:55:07 +0000 (18:55 +0300)
njs/njs_lexer.c
njs/njs_number.c
njs/njs_number.h
njs/test/njs_unit_test.c

index 71d899139b94add0a37103507b53cb1399f8d53d..1b327ff8129dbe09096ea2d222750bfc24e47d1f 100644 (file)
@@ -543,19 +543,48 @@ njs_lexer_number(njs_lexer_t *lexer)
     p = lexer->start;
     c = p[-1];
 
-    /* Hexadecimal literal values. */
+    if (c == '0' && p != lexer->end) {
 
-    if (c == '0' && p != lexer->end && (*p == 'x' || *p == 'X')) {
-        p++;
+        /* Hexadecimal literal values. */
 
-        if (p == lexer->end) {
-            return NJS_TOKEN_ILLEGAL;
+        if (*p == 'x' || *p == 'X') {
+            p++;
+
+            if (p == lexer->end) {
+                return NJS_TOKEN_ILLEGAL;
+            }
+
+            lexer->start = p;
+            lexer->number = njs_number_hex_parse(&lexer->start, lexer->end);
+
+            return NJS_TOKEN_NUMBER;
+        }
+
+        /* Octal literal values. */
+
+        if (*p == 'o') {
+            p++;
+
+            if (p == lexer->end) {
+                return NJS_TOKEN_ILLEGAL;
+            }
+
+            lexer->start = p;
+            lexer->number = njs_number_oct_parse(&lexer->start, lexer->end);
+            p = lexer->start;
+
+            if (p < lexer->end && (*p == '8' || *p == '9')) {
+                return NJS_TOKEN_ILLEGAL;
+            }
+
+            return NJS_TOKEN_NUMBER;
         }
 
-        lexer->start = p;
-        lexer->number = njs_number_hex_parse(&lexer->start, lexer->end);
+        /* Legacy Octal literals are deprecated. */
 
-        return NJS_TOKEN_NUMBER;
+        if (*p >= '0' && *p <= '9') {
+            return NJS_TOKEN_ILLEGAL;
+        }
     }
 
     lexer->start = p - 1;
index 15059a9e0f2abb2ad9fbfc01396c071c4fc4d340..affc5461195aae55c96d2516a354127c7f05d345 100644 (file)
@@ -168,6 +168,34 @@ njs_number_dec_parse(u_char **start, u_char *end)
 }
 
 
+uint64_t
+njs_number_oct_parse(u_char **start, u_char *end)
+{
+    u_char    c, *p;
+    uint64_t  num;
+
+    p = *start;
+
+    num = 0;
+
+    while (p < end) {
+        /* Values less than '0' become >= 208. */
+        c = *p - '0';
+
+        if (nxt_slow_path(c > 7)) {
+            break;
+        }
+
+        num = num * 8 + c;
+        p++;
+    }
+
+    *start = p;
+
+    return num;
+}
+
+
 uint64_t
 njs_number_hex_parse(u_char **start, u_char *end)
 {
index 3fabdcab0f32c6d18346a2f0278d91829727edfc..2c60fd9992d33d756a7a477e7bcff31fb0817ed6 100644 (file)
@@ -13,6 +13,7 @@
 
 uint32_t njs_value_to_index(njs_value_t *value);
 double njs_number_dec_parse(u_char **start, u_char *end);
+uint64_t njs_number_oct_parse(u_char **start, u_char *end);
 uint64_t njs_number_hex_parse(u_char **start, u_char *end);
 int64_t njs_number_radix_parse(u_char **start, u_char *end, uint8_t radix);
 njs_ret_t njs_number_to_string(njs_vm_t *vm, njs_value_t *string,
index f38d7dfa10fcb604bf256f7a060a0579a12f20d6..da64050954d177ae73184e9816abb1e32f799f09 100644 (file)
@@ -121,6 +121,37 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("+1\n"),
       nxt_string("1") },
 
+    /* Octal Numbers. */
+
+    { nxt_string("0o0"),
+      nxt_string("0") },
+
+    { nxt_string("0o011"),
+      nxt_string("9") },
+
+    { nxt_string("-0o777"),
+      nxt_string("-511") },
+
+    /* Legacy Octal Numbers are deprecated. */
+
+    { nxt_string("00"),
+      nxt_string("SyntaxError: Unexpected token \"\" in 1") },
+
+    { nxt_string("08"),
+      nxt_string("SyntaxError: Unexpected token \"\" in 1") },
+
+    { nxt_string("09"),
+      nxt_string("SyntaxError: Unexpected token \"\" in 1") },
+
+    { nxt_string("0011"),
+      nxt_string("SyntaxError: Unexpected token \"\" in 1") },
+
+    { nxt_string("0o"),
+      nxt_string("SyntaxError: Unexpected token \"\" in 1") },
+
+    { nxt_string("0o778"),
+      nxt_string("SyntaxError: Unexpected token \"\" in 1") },
+
     /* Hex Numbers. */
 
     { nxt_string("0x0"),