cli: Refactor handling of negative numbers
authorSimon Marchi <simon.marchi@efficios.com>
Mon, 22 Apr 2019 14:41:39 +0000 (10:41 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 2 May 2019 04:12:55 +0000 (00:12 -0400)
This patch breaks out the code that reads a number just after having
read the minus sign introducing a negative number.

Instead of keeping a state in the parser, and going through a new
iteration, we call the new function which returns a bt_value directly.

This enables the following patch to do a similar change, but for the
parsing of all values (which includes the parsing of negative numbers).
In turn, this will allow parsing values recursively, which is needed to
support array values.  Orthogonally, I think the code is easier to
follow this way.

No functional changes intended.

Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
cli/babeltrace-cfg-cli-args.c

index 27f9a981208b75d4a8a3092bc51cd4d26974073e..af2274610c66273b334fc120090936d56e9b1c1f 100644 (file)
@@ -70,9 +70,6 @@ enum ini_parsing_fsm_state {
        /* Expect a value */
        INI_EXPECT_VALUE,
 
-       /* Expect a negative number value */
-       INI_EXPECT_VALUE_NUMBER_NEG,
-
        /* Expect a comma character (',') */
        INI_EXPECT_COMMA,
 };
@@ -188,6 +185,41 @@ void ini_append_error_expecting(struct ini_parsing_state *state,
        g_string_append_printf(state->ini_error, "^\n\n");
 }
 
+/* Parse the next token as a number and return its negation. */
+static
+bt_value *ini_parse_neg_number(struct ini_parsing_state *state)
+{
+       bt_value *value = NULL;
+       GTokenType token_type = g_scanner_get_next_token(state->scanner);
+
+       switch (token_type) {
+       case G_TOKEN_INT:
+       {
+               /* Negative integer */
+               uint64_t int_val = state->scanner->value.v_int64;
+
+               if (int_val > (1ULL << 63) - 1) {
+                       g_string_append_printf(state->ini_error,
+                               "Integer value -%" PRIu64 " is outside the range of a 64-bit signed integer\n",
+                               int_val);
+               } else {
+                       value = bt_value_integer_create_init(-((int64_t) int_val));
+               }
+
+               break;
+       }
+       case G_TOKEN_FLOAT:
+               /* Negative floating point number */
+               value = bt_value_real_create_init(-state->scanner->value.v_float);
+               break;
+       default:
+               ini_append_error_expecting(state, state->scanner, "value");
+               break;
+       }
+
+       return value;
+}
+
 static
 int ini_handle_state(struct ini_parsing_state *state)
 {
@@ -204,7 +236,6 @@ int ini_handle_state(struct ini_parsing_state *state)
                                        state->scanner, "'='");
                                break;
                        case INI_EXPECT_VALUE:
-                       case INI_EXPECT_VALUE_NUMBER_NEG:
                                ini_append_error_expecting(state,
                                        state->scanner, "value");
                                break;
@@ -271,8 +302,12 @@ int ini_handle_state(struct ini_parsing_state *state)
                case G_TOKEN_CHAR:
                        if (state->scanner->value.v_char == '-') {
                                /* Negative number */
-                               state->expecting =
-                                       INI_EXPECT_VALUE_NUMBER_NEG;
+                               value = ini_parse_neg_number(state);
+                               if (!value) {
+                                       goto error;
+                               }
+
+                               state->expecting = INI_EXPECT_COMMA;
                                goto success;
                        } else {
                                ini_append_error_expecting(state,
@@ -348,42 +383,6 @@ int ini_handle_state(struct ini_parsing_state *state)
                state->expecting = INI_EXPECT_COMMA;
                goto success;
        }
-       case INI_EXPECT_VALUE_NUMBER_NEG:
-       {
-               switch (token_type) {
-               case G_TOKEN_INT:
-               {
-                       /* Negative integer */
-                       uint64_t int_val = state->scanner->value.v_int64;
-
-                       if (int_val > (1ULL << 63) - 1) {
-                               g_string_append_printf(state->ini_error,
-                                       "Integer value -%" PRIu64 " is outside the range of a 64-bit signed integer\n",
-                                       int_val);
-                               goto error;
-                       }
-
-                       value = bt_value_integer_create_init(-((int64_t)int_val));
-                       break;
-               }
-               case G_TOKEN_FLOAT:
-                       /* Negative floating point number */
-                       value = bt_value_real_create_init(-state->scanner->value.v_float);
-                       break;
-               default:
-                       /* Unset value variable will trigger the error */
-                       break;
-               }
-
-               if (!value) {
-                       ini_append_error_expecting(state,
-                               state->scanner, "value");
-                       goto error;
-               }
-
-               state->expecting = INI_EXPECT_COMMA;
-               goto success;
-       }
        case INI_EXPECT_COMMA:
                if (token_type != G_TOKEN_CHAR) {
                        ini_append_error_expecting(state,
This page took 0.026906 seconds and 5 git commands to generate.