Resolve #3: Chanel lists parsing
| | |
| | | scpi_expr_result_t SCPI_ExprNumericListEntry(scpi_t * context, scpi_parameter_t * param, int index, scpi_bool_t * isRange, scpi_parameter_t * valueFrom, scpi_parameter_t * valueTo); |
| | | scpi_expr_result_t SCPI_ExprNumericListEntryInt(scpi_t * context, scpi_parameter_t * param, int index, scpi_bool_t * isRange, int32_t * valueFrom, int32_t * valueTo); |
| | | scpi_expr_result_t SCPI_ExprNumericListEntryDouble(scpi_t * context, scpi_parameter_t * param, int index, scpi_bool_t * isRange, double * valueFrom, double * valueTo); |
| | | scpi_expr_result_t SCPI_ExprChannelListEntry(scpi_t * context, scpi_parameter_t * param, int index, scpi_bool_t * isRange, int32_t * valuesFrom, int32_t * valuesTo, size_t length, size_t * dimensions); |
| | | |
| | | #ifdef __cplusplus |
| | | } |
| | |
| | | SCPI_TOKEN_COMMA, |
| | | SCPI_TOKEN_SEMICOLON, |
| | | SCPI_TOKEN_COLON, |
| | | SCPI_TOKEN_SPECIFIC_CHARACTER, |
| | | SCPI_TOKEN_QUESTION, |
| | | SCPI_TOKEN_NL, |
| | | SCPI_TOKEN_HEXNUM, |
| | |
| | | |
| | | /** |
| | | * Parse one range or single value |
| | | * @param state lexical state |
| | | * @param state lexer state |
| | | * @param isRange return true if parsed expression is range |
| | | * @param valueFrom return parsed value from |
| | | * @param valueTo return parsed value to |
| | |
| | | |
| | | return res; |
| | | } |
| | | |
| | | /** |
| | | * Parse one channel_spec e.g. "1!5!8" |
| | | * @param context |
| | | * @param state lexer state |
| | | * @param values range values |
| | | * @param length length of values array |
| | | * @param dimensions real number of dimensions |
| | | */ |
| | | static scpi_expr_result_t channelSpec(scpi_t * context, lex_state_t * state, int32_t * values, size_t length, size_t * dimensions) |
| | | { |
| | | scpi_parameter_t param; |
| | | size_t i = 0; |
| | | while(scpiLex_DecimalNumericProgramData(state, ¶m)) { |
| | | if (i < length) { |
| | | SCPI_ParamToInt(context, ¶m, &values[i]); |
| | | } |
| | | |
| | | if (scpiLex_SpecificCharacter(state, ¶m, '!')) { |
| | | i++; |
| | | } else { |
| | | *dimensions = i + 1; |
| | | return SCPI_EXPR_OK; |
| | | } |
| | | } |
| | | |
| | | if (i == 0) { |
| | | return SCPI_EXPR_NO_MORE; |
| | | } else { |
| | | // there was at least one number followed by !, but after ! was not another number |
| | | return SCPI_EXPR_ERROR; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Parse channel_range e.g. "1!2:5!6" |
| | | * @param context |
| | | * @param state lexer state |
| | | * @param isRange return true if it is range |
| | | * @param valuesFrom return array of values from |
| | | * @param valuesTo return array of values to |
| | | * @param length length of values arrays |
| | | * @param dimensions real number of dimensions |
| | | */ |
| | | static scpi_expr_result_t channelRange(scpi_t * context, lex_state_t * state, scpi_bool_t * isRange, int32_t * valuesFrom, int32_t * valuesTo, size_t length, size_t * dimensions) |
| | | { |
| | | scpi_token_t token; |
| | | scpi_expr_result_t err; |
| | | size_t fromDimensions; |
| | | size_t toDimensions; |
| | | |
| | | err = channelSpec(context, state, valuesFrom, length, &fromDimensions); |
| | | if (err == SCPI_EXPR_OK) { |
| | | if (scpiLex_Colon(state, &token)) { |
| | | *isRange = TRUE; |
| | | err = channelSpec(context, state, valuesTo, length, &toDimensions); |
| | | if (err != SCPI_EXPR_OK) { |
| | | return SCPI_EXPR_ERROR; |
| | | } |
| | | if (fromDimensions != toDimensions) { |
| | | return SCPI_EXPR_ERROR; |
| | | } |
| | | *dimensions = fromDimensions; |
| | | } else { |
| | | *isRange = FALSE; |
| | | return SCPI_EXPR_OK; |
| | | } |
| | | } |
| | | |
| | | return err; |
| | | } |
| | | |
| | | /** |
| | | * Parse one list entry at specific position e.g. "1!2:5!6" |
| | | * @param context |
| | | * @param param |
| | | * @param index |
| | | * @param isRange return true if it is range |
| | | * @param valuesFrom return array of values from |
| | | * @param valuesTo return array of values to |
| | | * @param length length of values arrays |
| | | * @param dimensions real number of dimensions |
| | | */ |
| | | scpi_expr_result_t SCPI_ExprChannelListEntry(scpi_t * context, scpi_parameter_t * param, int index, scpi_bool_t * isRange, int32_t * valuesFrom, int32_t * valuesTo, size_t length, size_t * dimensions) |
| | | { |
| | | lex_state_t lex; |
| | | int i; |
| | | scpi_expr_result_t res = SCPI_EXPR_OK; |
| | | scpi_token_t token; |
| | | |
| | | if (!isRange || !param || !dimensions || (length && (!valuesFrom || !valuesTo))) { |
| | | SCPI_ErrorPush(context, SCPI_ERROR_SYSTEM_ERROR); |
| | | return SCPI_EXPR_ERROR; |
| | | } |
| | | |
| | | if (param->type != SCPI_TOKEN_PROGRAM_EXPRESSION) { |
| | | SCPI_ErrorPush(context, SCPI_ERROR_DATA_TYPE_ERROR); |
| | | return SCPI_EXPR_ERROR; |
| | | } |
| | | |
| | | lex.buffer = param->ptr + 1; |
| | | lex.pos = lex.buffer; |
| | | lex.len = param->len - 2; |
| | | |
| | | // detect channel list expression |
| | | if (!scpiLex_SpecificCharacter(&lex, &token, '@')) { |
| | | SCPI_ErrorPush(context, SCPI_ERROR_EXPRESSION_PARSING_ERROR); |
| | | return SCPI_EXPR_ERROR; |
| | | } |
| | | |
| | | for (i = 0; i <= index; i++) { |
| | | res = channelRange(context, &lex, isRange, valuesFrom, valuesTo, (i == index) ? length : 0, dimensions); |
| | | if (res != SCPI_EXPR_OK) { |
| | | break; |
| | | } |
| | | if (i != index) { |
| | | if (!scpiLex_Comma(&lex, &token)) { |
| | | res = scpiLex_IsEos(&lex) ? SCPI_EXPR_NO_MORE : SCPI_EXPR_ERROR; |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | |
| | | if (res == SCPI_EXPR_ERROR) { |
| | | SCPI_ErrorPush(context, SCPI_ERROR_EXPRESSION_PARSING_ERROR); |
| | | } |
| | | return res; |
| | | } |
| | |
| | | } |
| | | |
| | | /** |
| | | * Detect specified character |
| | | * @param state |
| | | * @param token |
| | | * @return |
| | | */ |
| | | int scpiLex_SpecificCharacter(lex_state_t * state, scpi_token_t * token, char chr) { |
| | | token->ptr = state->pos; |
| | | |
| | | if (skipChr(state, chr)) { |
| | | token->len = 1; |
| | | token->type = SCPI_TOKEN_SPECIFIC_CHARACTER; |
| | | } else { |
| | | token->len = 0; |
| | | token->type = SCPI_TOKEN_UNKNOWN; |
| | | } |
| | | |
| | | return token->len; |
| | | } |
| | | |
| | | /** |
| | | * Detect token New line |
| | | * @param state |
| | | * @param token |
| | |
| | | int scpiLex_Semicolon(lex_state_t * state, scpi_token_t * token) LOCAL; |
| | | int scpiLex_Colon(lex_state_t * state, scpi_token_t * token) LOCAL; |
| | | int scpiLex_NewLine(lex_state_t * state, scpi_token_t * token) LOCAL; |
| | | int scpiLex_SpecificCharacter(lex_state_t * state, scpi_token_t * token, char chr) LOCAL; |
| | | |
| | | #ifdef __cplusplus |
| | | } |
| | |
| | | TEST_NumericListDouble("(12,5:6:3)", 2, FALSE, 0, 0, SCPI_EXPR_ERROR, SCPI_ERROR_EXPRESSION_PARSING_ERROR); |
| | | } |
| | | |
| | | #define NOPAREN(...) __VA_ARGS__ |
| | | |
| | | #define TEST_ChannelList(data, index, val_len, expected_range, expected_dimensions, _expected_from, _expected_to, expected_result, expected_error_code) \ |
| | | { \ |
| | | scpi_bool_t result; \ |
| | | scpi_expr_result_t result2; \ |
| | | int16_t errCode; \ |
| | | scpi_parameter_t param; \ |
| | | int32_t val_from[val_len], val_to[val_len]; \ |
| | | scpi_bool_t val_range; \ |
| | | int32_t expected_from[] = {NOPAREN _expected_from}; \ |
| | | int32_t expected_to[] = {NOPAREN _expected_to}; \ |
| | | size_t val_dimensions; \ |
| | | \ |
| | | SCPI_CoreCls(&scpi_context); \ |
| | | scpi_context.input_count = 0; \ |
| | | scpi_context.param_list.lex_state.buffer = data; \ |
| | | scpi_context.param_list.lex_state.len = strlen(scpi_context.param_list.lex_state.buffer);\ |
| | | scpi_context.param_list.lex_state.pos = scpi_context.param_list.lex_state.buffer; \ |
| | | result = SCPI_Parameter(&scpi_context, ¶m, TRUE); \ |
| | | result2 = SCPI_ExprChannelListEntry(&scpi_context, ¶m, index, &val_range, val_from, val_to, val_len, &val_dimensions);\ |
| | | errCode = SCPI_ErrorPop(&scpi_context); \ |
| | | CU_ASSERT_EQUAL(result2, expected_result); \ |
| | | if (expected_result == SCPI_EXPR_OK) { \ |
| | | CU_ASSERT_EQUAL(val_range, expected_range); \ |
| | | { size_t i; for(i = 0; (i < val_len) && (i < val_dimensions); i++) { \ |
| | | CU_ASSERT_EQUAL(val_from[i], expected_from[i]); \ |
| | | }} \ |
| | | if (expected_range) { \ |
| | | { size_t i; for(i = 0; (i < val_len) && (i < val_dimensions); i++) { \ |
| | | CU_ASSERT_EQUAL(val_to[i], expected_to[i]); \ |
| | | }} \ |
| | | } \ |
| | | } \ |
| | | CU_ASSERT_EQUAL(errCode, expected_error_code); \ |
| | | } |
| | | |
| | | static void testChannelList(void) { |
| | | TEST_ChannelList("(1)", 0, 1, FALSE, 0, (0), (0), SCPI_EXPR_ERROR, SCPI_ERROR_EXPRESSION_PARSING_ERROR); |
| | | |
| | | TEST_ChannelList("(@1)", 0, 1, FALSE, 1, (1), (0), SCPI_EXPR_OK, 0); |
| | | TEST_ChannelList("(@1)", 1, 1, FALSE, 0, (0), (0), SCPI_EXPR_NO_MORE, 0); |
| | | |
| | | TEST_ChannelList("(@1,2)", 0, 1, FALSE, 1, (1), (0), SCPI_EXPR_OK, 0); |
| | | TEST_ChannelList("(@1,2)", 1, 1, FALSE, 1, (2), (0), SCPI_EXPR_OK, 0); |
| | | TEST_ChannelList("(@1,2)", 2, 1, FALSE, 0, (0), (0), SCPI_EXPR_NO_MORE, 0); |
| | | |
| | | TEST_ChannelList("(@1,2:3)", 0, 1, FALSE, 1, (1), (0), SCPI_EXPR_OK, 0); |
| | | TEST_ChannelList("(@1,2:3)", 1, 1, TRUE, 1, (2), (3), SCPI_EXPR_OK, 0); |
| | | TEST_ChannelList("(@1,2:3)", 2, 1, FALSE, 0, (0), (0), SCPI_EXPR_NO_MORE, 0); |
| | | |
| | | TEST_ChannelList("(@1,2!5:3!6)", 0, 2, FALSE, 1, (1), (0), SCPI_EXPR_OK, 0); |
| | | TEST_ChannelList("(@1,2!5:3!6)", 1, 2, TRUE, 2, (2,5), (3,6), SCPI_EXPR_OK, 0); |
| | | TEST_ChannelList("(@1,2!5:3!6)", 2, 2, FALSE, 0, (0), (0), SCPI_EXPR_NO_MORE, 0); |
| | | |
| | | TEST_ChannelList("(@1,2!5:3!6)", 0, 1, FALSE, 1, (1), (0), SCPI_EXPR_OK, 0); |
| | | TEST_ChannelList("(@1,2!5:3!6)", 1, 1, TRUE, 2, (2), (3), SCPI_EXPR_OK, 0); |
| | | TEST_ChannelList("(@1,2!5:3!6)", 2, 1, FALSE, 0, (0), (0), SCPI_EXPR_NO_MORE, 0); |
| | | |
| | | TEST_ChannelList("(@1,2!5:3!6!7)", 0, 2, FALSE, 1, (1), (0), SCPI_EXPR_OK, 0); |
| | | TEST_ChannelList("(@1,2!5:3!6!7)", 1, 2, FALSE, 0, (0), (0), SCPI_EXPR_ERROR, SCPI_ERROR_EXPRESSION_PARSING_ERROR); |
| | | TEST_ChannelList("(@1,2!5:3!6!7)", 2, 2, FALSE, 0, (0), (0), SCPI_EXPR_ERROR, SCPI_ERROR_EXPRESSION_PARSING_ERROR); |
| | | } |
| | | |
| | | int main() { |
| | | unsigned int result; |
| | |
| | | || (NULL == CU_add_test(pSuite, "Error handling", testErrorHandling)) |
| | | || (NULL == CU_add_test(pSuite, "IEEE 488.2 Mandatory commands", testIEEE4882)) |
| | | || (NULL == CU_add_test(pSuite, "Numeric list", testNumericList)) |
| | | || (NULL == CU_add_test(pSuite, "Channel list", testChannelList)) |
| | | ) { |
| | | CU_cleanup_registry(); |
| | | return CU_get_error(); |