[njs] Added support of oct literals.

Dmitry Volyntsev xeioex at nginx.com
Fri Nov 17 16:06:05 UTC 2017


details:   http://hg.nginx.org/njs/rev/17909969892f
branches:  
changeset: 425:17909969892f
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri Nov 17 18:55:07 2017 +0300
description:
Added support of oct literals.

diffstat:

 njs/njs_lexer.c          |  47 ++++++++++++++++++++++++++++++++++++++---------
 njs/njs_number.c         |  28 ++++++++++++++++++++++++++++
 njs/njs_number.h         |   1 +
 njs/test/njs_unit_test.c |  31 +++++++++++++++++++++++++++++++
 4 files changed, 98 insertions(+), 9 deletions(-)

diffs (151 lines):

diff -r 779156b4b930 -r 17909969892f njs/njs_lexer.c
--- a/njs/njs_lexer.c	Fri Nov 17 18:55:07 2017 +0300
+++ b/njs/njs_lexer.c	Fri Nov 17 18:55:07 2017 +0300
@@ -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) {
+
+        /* Hexadecimal literal values. */
+
+        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 (c == '0' && p != lexer->end && (*p == 'x' || *p == 'X')) {
-        p++;
+        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) {
+            if (p < lexer->end && (*p == '8' || *p == '9')) {
+                return NJS_TOKEN_ILLEGAL;
+            }
+
+            return NJS_TOKEN_NUMBER;
+        }
+
+        /* Legacy Octal literals are deprecated. */
+
+        if (*p >= '0' && *p <= '9') {
             return NJS_TOKEN_ILLEGAL;
         }
-
-        lexer->start = p;
-        lexer->number = njs_number_hex_parse(&lexer->start, lexer->end);
-
-        return NJS_TOKEN_NUMBER;
     }
 
     lexer->start = p - 1;
diff -r 779156b4b930 -r 17909969892f njs/njs_number.c
--- a/njs/njs_number.c	Fri Nov 17 18:55:07 2017 +0300
+++ b/njs/njs_number.c	Fri Nov 17 18:55:07 2017 +0300
@@ -169,6 +169,34 @@ njs_number_dec_parse(u_char **start, u_c
 
 
 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)
 {
     u_char    c, *p;
diff -r 779156b4b930 -r 17909969892f njs/njs_number.h
--- a/njs/njs_number.h	Fri Nov 17 18:55:07 2017 +0300
+++ b/njs/njs_number.h	Fri Nov 17 18:55:07 2017 +0300
@@ -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,
diff -r 779156b4b930 -r 17909969892f njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Fri Nov 17 18:55:07 2017 +0300
+++ b/njs/test/njs_unit_test.c	Fri Nov 17 18:55:07 2017 +0300
@@ -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"),


More information about the nginx-devel mailing list