+/* Parse the next token as an unsigned integer. */
+static
+bt_value *ini_parse_uint(struct ini_parsing_state *state)
+{
+ bt_value *value = NULL;
+ GTokenType token_type = g_scanner_get_next_token(state->scanner);
+
+ if (token_type != G_TOKEN_INT) {
+ ini_append_error_expecting(state, state->scanner,
+ "integer value");
+ goto end;
+ }
+
+ value = bt_value_unsigned_integer_create_init(
+ state->scanner->value.v_int64);
+
+end:
+ return value;
+}
+
+/* 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 > (((uint64_t) INT64_MAX) + 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_signed_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 bt_value *ini_parse_value(struct ini_parsing_state *state);
+
+/*
+ * Parse the current and following tokens as an array. Arrays are formatted as
+ * an opening `[`, a list of comma-separated values and a closing `]`. For
+ * convenience we support an optional trailing comma, after the last value.
+ *
+ * The current token of the parser must be the opening square bracket of the
+ * array.
+ */
+static
+bt_value *ini_parse_array(struct ini_parsing_state *state)
+{
+ /* The [ character must have already been ingested. */
+ BT_ASSERT(g_scanner_cur_token(state->scanner) == G_TOKEN_CHAR);
+ BT_ASSERT(g_scanner_cur_value(state->scanner).v_char == '[');
+
+ bt_value *array_value;
+ GTokenType token_type;
+
+ array_value = bt_value_array_create ();
+ token_type = g_scanner_get_next_token(state->scanner);
+
+ /* While the current token is not a ]... */
+ while (!(token_type == G_TOKEN_CHAR && g_scanner_cur_value(state->scanner).v_char == ']')) {
+ /* Parse the item... */
+ bt_value *item_value;
+ bt_value_status status;
+
+ item_value = ini_parse_value(state);
+ if (!item_value) {
+ goto error;
+ }
+
+ /* ... and add it to the result array. */
+ status = bt_value_array_append_element(array_value, item_value);
+ BT_VALUE_PUT_REF_AND_RESET(item_value);
+
+ if (status != BT_VALUE_STATUS_OK) {
+ goto error;
+ }
+
+ /*
+ * Ingest the token following the value, it should be either a
+ * comma or closing square brace.
+ */
+ token_type = g_scanner_get_next_token(state->scanner);
+
+ if (token_type == G_TOKEN_CHAR && g_scanner_cur_value(state->scanner).v_char == ',') {
+ /*
+ * Ingest the token following the comma. If it happens
+ * to be a closing square bracket, we'll exit the loop
+ * and we are done (we allow trailing commas).
+ * Otherwise, we are ready for the next ini_parse_value call.
+ */
+ token_type = g_scanner_get_next_token(state->scanner);
+ } else if (token_type != G_TOKEN_CHAR || g_scanner_cur_value(state->scanner).v_char != ']') {
+ ini_append_error_expecting(state, state->scanner, ", or ]");
+ goto error;
+ }
+ }
+
+ goto end;
+
+error:
+ BT_VALUE_PUT_REF_AND_RESET(array_value);
+
+end:
+ return array_value;
+}
+
+/*
+ * Parse the current token (and the following ones if needed) as a value, return
+ * it as a bt_value.
+ */
+static
+bt_value *ini_parse_value(struct ini_parsing_state *state)
+{
+ bt_value *value = NULL;
+ GTokenType token_type = state->scanner->token;
+
+ switch (token_type) {
+ case G_TOKEN_CHAR:
+ if (state->scanner->value.v_char == '-') {
+ /* Negative number */
+ value = ini_parse_neg_number(state);
+ } else if (state->scanner->value.v_char == '+') {
+ /* Unsigned integer */
+ value = ini_parse_uint(state);
+ } else if (state->scanner->value.v_char == '[') {
+ /* Array */
+ value = ini_parse_array(state);
+ } else {
+ ini_append_error_expecting(state, state->scanner, "value");
+ }
+ break;
+ case G_TOKEN_INT:
+ {
+ /* Positive, signed integer */
+ uint64_t int_val = state->scanner->value.v_int64;
+
+ if (int_val > INT64_MAX) {
+ 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_signed_integer_create_init(
+ (int64_t) int_val);
+ }
+ break;
+ }
+ case G_TOKEN_FLOAT:
+ /* Positive floating point number */
+ value = bt_value_real_create_init(state->scanner->value.v_float);
+ break;
+ case G_TOKEN_STRING:
+ /* Quoted string */
+ value = bt_value_string_create_init(state->scanner->value.v_string);
+ break;
+ case G_TOKEN_IDENTIFIER:
+ {
+ /*
+ * Using symbols would be appropriate here,
+ * but said symbols are allowed as map key,
+ * so it's easier to consider everything an
+ * identifier.
+ *
+ * If one of the known symbols is not
+ * recognized here, then fall back to creating
+ * a string value.
+ */
+ const char *id = state->scanner->value.v_identifier;
+
+ if (!strcmp(id, "null") || !strcmp(id, "NULL") ||
+ !strcmp(id, "nul")) {
+ value = bt_value_null;
+ } else if (!strcmp(id, "true") || !strcmp(id, "TRUE") ||
+ !strcmp(id, "yes") ||
+ !strcmp(id, "YES")) {
+ value = bt_value_bool_create_init(true);
+ } else if (!strcmp(id, "false") ||
+ !strcmp(id, "FALSE") ||
+ !strcmp(id, "no") ||
+ !strcmp(id, "NO")) {
+ value = bt_value_bool_create_init(false);
+ } else {
+ value = bt_value_string_create_init(id);
+ }
+ break;
+ }
+ default:
+ /* Unset value variable will trigger the error */
+ ini_append_error_expecting(state, state->scanner, "value");
+ break;
+ }
+
+ return value;
+}
+