[njs] Non-integer fractions.

Valentin Bartenev vbart at nginx.com
Mon Oct 29 18:27:39 UTC 2018


details:   http://hg.nginx.org/njs/rev/0677cbeee41a
branches:  
changeset: 632:0677cbeee41a
user:      Valentin Bartenev <vbart at nginx.com>
date:      Mon Oct 29 21:06:24 2018 +0300
description:
Non-integer fractions.

diffstat:

 njs/njs_lexer.c          |  17 ++++++++++++++---
 njs/test/njs_unit_test.c |  20 +++++++++++++++++++-
 2 files changed, 33 insertions(+), 4 deletions(-)

diffs (123 lines):

diff -r 647de74fd2c7 -r 0677cbeee41a njs/njs_lexer.c
--- a/njs/njs_lexer.c	Mon Oct 29 17:36:43 2018 +0300
+++ b/njs/njs_lexer.c	Mon Oct 29 21:06:24 2018 +0300
@@ -290,7 +290,7 @@ njs_lexer_token(njs_lexer_t *lexer)
 static njs_token_t
 njs_lexer_next_token(njs_lexer_t *lexer)
 {
-    u_char                   c;
+    u_char                   c, *p;
     nxt_uint_t               n;
     njs_token_t              token;
     const njs_lexer_multi_t  *multi;
@@ -315,6 +315,16 @@ njs_lexer_next_token(njs_lexer_t *lexer)
         case NJS_TOKEN_SINGLE_QUOTE:
             return njs_lexer_string(lexer, c);
 
+        case NJS_TOKEN_DOT:
+            p = lexer->start;
+
+            if (p == lexer->end || njs_tokens[*p] != NJS_TOKEN_DIGIT) {
+                lexer->text.length = p - lexer->text.start;
+                return NJS_TOKEN_DOT;
+            }
+
+            /* Fall through. */
+
         case NJS_TOKEN_DIGIT:
             return njs_lexer_number(lexer, c);
 
@@ -405,7 +415,6 @@ njs_lexer_next_token(njs_lexer_t *lexer)
         case NJS_TOKEN_CLOSE_BRACKET:
         case NJS_TOKEN_OPEN_BRACE:
         case NJS_TOKEN_CLOSE_BRACE:
-        case NJS_TOKEN_DOT:
         case NJS_TOKEN_COMMA:
         case NJS_TOKEN_COLON:
         case NJS_TOKEN_SEMICOLON:
@@ -527,6 +536,8 @@ njs_lexer_number(njs_lexer_t *lexer, u_c
 {
     const u_char  *p;
 
+    lexer->text.start = lexer->start - 1;
+
     p = lexer->start;
 
     if (c == '0' && p != lexer->end) {
@@ -594,6 +605,7 @@ njs_lexer_number(njs_lexer_t *lexer, u_c
 done:
 
     lexer->start = (u_char *) p;
+    lexer->text.length = p - lexer->text.start;
 
     return NJS_TOKEN_NUMBER;
 
@@ -603,7 +615,6 @@ illegal_trailer:
 
 illegal_token:
 
-    lexer->text.start = lexer->start - 1;
     lexer->text.length = p - lexer->text.start;
 
     return NJS_TOKEN_ILLEGAL;
diff -r 647de74fd2c7 -r 0677cbeee41a njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Mon Oct 29 17:36:43 2018 +0300
+++ b/njs/test/njs_unit_test.c	Mon Oct 29 21:06:24 2018 +0300
@@ -101,9 +101,18 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("-0"),
       nxt_string("-0") },
 
+    { nxt_string(".0"),
+      nxt_string("0") },
+
     { nxt_string("0.1"),
       nxt_string("0.1") },
 
+    { nxt_string(".9"),
+      nxt_string("0.9") },
+
+    { nxt_string("-.01"),
+      nxt_string("-0.01") },
+
     { nxt_string("0.000001"),
       nxt_string("0.000001") },
 
@@ -140,6 +149,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("+1\n"),
       nxt_string("1") },
 
+    { nxt_string("."),
+      nxt_string("SyntaxError: Unexpected token \".\" in 1") },
+
     /* Octal Numbers. */
 
     { nxt_string("0o0"),
@@ -264,6 +276,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("1.1e+01"),
       nxt_string("11") },
 
+    { nxt_string("-.01e-01"),
+      nxt_string("-0.001") },
+
     { nxt_string("1e9"),
       nxt_string("1000000000") },
 
@@ -297,6 +312,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("1eZ"),
       nxt_string("SyntaxError: Unexpected token \"eZ\" in 1") },
 
+    { nxt_string(".e1"),
+      nxt_string("SyntaxError: Unexpected token \".\" in 1") },
+
     /* Indexes. */
 
     { nxt_string("var a = []; a[-1] = 2; a[-1] == a['-1']"),
@@ -4039,7 +4057,7 @@ static njs_unit_test_t  njs_test[] =
       nxt_string("NaN") },
 
     { nxt_string("var a = 'abcdef'; a.3"),
-      nxt_string("SyntaxError: Unexpected token \"3\" in 1") },
+      nxt_string("SyntaxError: Unexpected token \".3\" in 1") },
 
     { nxt_string("'abcdef'[3]"),
       nxt_string("d") },


More information about the nginx-devel mailing list