| | |
| | | |
| | | #include "scpi/config.h" |
| | | #include "scpi/parser.h" |
| | | #include "scpi/lexer.h" |
| | | #include "utils.h" |
| | | #include "parser_private.h" |
| | | #include "lexer_private.h" |
| | | #include "scpi/error.h" |
| | | #include "scpi/constants.h" |
| | | |
| | |
| | | * @param context |
| | | * @result TRUE if context->paramlist is filled with correct values |
| | | */ |
| | | static bool_t findCommandHeader(scpi_t * context, const char * header, int len) { |
| | | static scpi_bool_t findCommandHeader(scpi_t * context, const char * header, int len) { |
| | | int32_t i; |
| | | const scpi_command_t * cmd; |
| | | |
| | |
| | | while (1) { |
| | | result = 0; |
| | | |
| | | r = SCPI_DetectProgramMessageUnit(state, data, len); |
| | | r = detectProgramMessageUnit(state, data, len); |
| | | |
| | | if (state->programHeader.type == TokInvalid) { |
| | | SCPI_ErrorPush(context, SCPI_ERROR_INVALID_CHARACTER); |
| | |
| | | context->param_list.lex_state.buffer = state->programData.ptr; |
| | | context->param_list.lex_state.pos = context->param_list.lex_state.buffer; |
| | | context->param_list.lex_state.len = state->programData.len; |
| | | context->param_list.cmd_raw.data = state->programHeader.ptr; |
| | | context->param_list.cmd_raw.position = 0; |
| | | context->param_list.cmd_raw.length = state->programHeader.len; |
| | | |
| | | processCommand(context); |
| | | |
| | |
| | | |
| | | |
| | | while (1) { |
| | | cmdlen = SCPI_DetectProgramMessageUnit(&context->parser_state, context->buffer.data + totcmdlen, context->buffer.position - totcmdlen); |
| | | cmdlen = detectProgramMessageUnit(&context->parser_state, context->buffer.data + totcmdlen, context->buffer.position - totcmdlen); |
| | | totcmdlen += cmdlen; |
| | | if (context->parser_state.termination == PmutNewLine) break; |
| | | if (context->parser_state.programHeader.type == TokUnknown) break; |
| | |
| | | * @param val |
| | | * @return |
| | | */ |
| | | size_t SCPI_ResultBool(scpi_t * context, bool_t val) { |
| | | size_t SCPI_ResultBool(scpi_t * context, scpi_bool_t val) { |
| | | return SCPI_ResultIntBase(context, val ? 1 : 0, 10); |
| | | } |
| | | |
| | | |
| | | /* parsing parameters */ |
| | | |
| | | bool_t SCPI_Parameter(scpi_t * context, scpi_parameter_t * parameter, bool_t mandatory) { |
| | | scpi_bool_t SCPI_Parameter(scpi_t * context, scpi_parameter_t * parameter, scpi_bool_t mandatory) { |
| | | token_t token; |
| | | lex_state_t * state; |
| | | int32_t value; |
| | |
| | | return FALSE; |
| | | } |
| | | if (context->input_count != 0) { |
| | | SCPI_LexComma(state, &token); |
| | | lexComma(state, &token); |
| | | if (token.type != TokComma) { |
| | | SCPI_ErrorPush(context, SCPI_ERROR_INVALID_SEPARATOR); |
| | | return FALSE; |
| | |
| | | |
| | | context->input_count++; |
| | | |
| | | SCPI_ParseProgramData(&context->param_list.lex_state, &token); |
| | | parseProgramData(&context->param_list.lex_state, &token); |
| | | |
| | | parameter->type = token.type; |
| | | parameter->data.ptr = token.ptr; |
| | |
| | | } |
| | | |
| | | /* SCPI-99 7.3 Boolean Program Data */ |
| | | bool_t SCPI_ParamGetBoolVal(scpi_t * context, scpi_parameter_t * parameter) { |
| | | scpi_bool_t SCPI_ParamGetBoolVal(scpi_t * context, scpi_parameter_t * parameter) { |
| | | switch (parameter->type) { |
| | | case TokDecimalNumericProgramData: |
| | | return parameter->number.value ? 1 : 0; |
| | |
| | | } |
| | | } |
| | | |
| | | int SCPI_ParseProgramData(lex_state_t * state, token_t * token) { |
| | | int parseProgramData(lex_state_t * state, token_t * token) { |
| | | token_t tmp; |
| | | int result = 0; |
| | | int wsLen; |
| | | int suffixLen; |
| | | int realLen = 0; |
| | | realLen += SCPI_LexWhiteSpace(state, &tmp); |
| | | realLen += lexWhiteSpace(state, &tmp); |
| | | |
| | | if (result == 0) result = SCPI_LexNondecimalNumericData(state, token); |
| | | if (result == 0) result = SCPI_LexCharacterProgramData(state, token); |
| | | if (result == 0) result = lexNondecimalNumericData(state, token); |
| | | if (result == 0) result = lexCharacterProgramData(state, token); |
| | | if (result == 0) { |
| | | result = SCPI_LexDecimalNumericProgramData(state, token); |
| | | result = lexDecimalNumericProgramData(state, token); |
| | | if (result != 0) { |
| | | wsLen = SCPI_LexWhiteSpace(state, &tmp); |
| | | suffixLen = SCPI_LexSuffixProgramData(state, &tmp); |
| | | wsLen = lexWhiteSpace(state, &tmp); |
| | | suffixLen = lexSuffixProgramData(state, &tmp); |
| | | if (suffixLen > 0) { |
| | | token->len += wsLen + suffixLen; |
| | | token->type = TokDecimalNumericProgramDataWithSuffix; |
| | |
| | | } |
| | | } |
| | | |
| | | if (result == 0) result = SCPI_LexStringProgramData(state, token); |
| | | if (result == 0) result = SCPI_LexArbitraryBlockProgramData(state, token); |
| | | if (result == 0) result = SCPI_LexProgramExpression(state, token); |
| | | if (result == 0) result = lexStringProgramData(state, token); |
| | | if (result == 0) result = lexArbitraryBlockProgramData(state, token); |
| | | if (result == 0) result = lexProgramExpression(state, token); |
| | | |
| | | realLen += SCPI_LexWhiteSpace(state, &tmp); |
| | | realLen += lexWhiteSpace(state, &tmp); |
| | | |
| | | return result + realLen; |
| | | } |
| | | |
| | | int SCPI_ParseAllProgramData(lex_state_t * state, token_t * token, int * numberOfParameters) { |
| | | int parseAllProgramData(lex_state_t * state, token_t * token, int * numberOfParameters) { |
| | | |
| | | int result; |
| | | token_t tmp; |
| | |
| | | token->ptr = state->pos; |
| | | |
| | | |
| | | for (result = 1; result != 0; result = SCPI_LexComma(state, &tmp)) { |
| | | for (result = 1; result != 0; result = lexComma(state, &tmp)) { |
| | | token->len += result; |
| | | |
| | | if (result == 0) { |
| | |
| | | break; |
| | | } |
| | | |
| | | result = SCPI_ParseProgramData(state, &tmp); |
| | | result = parseProgramData(state, &tmp); |
| | | if (tmp.type != TokUnknown) { |
| | | token->len += result; |
| | | } else { |
| | |
| | | token->type = TokUnknown; |
| | | } |
| | | |
| | | int SCPI_DetectProgramMessageUnit(scpi_parser_state_t * state, const char * buffer, int len) { |
| | | int detectProgramMessageUnit(scpi_parser_state_t * state, const char * buffer, int len) { |
| | | lex_state_t lex_state; |
| | | token_t tmp; |
| | | int result = 0; |
| | |
| | | state->numberOfParameters = 0; |
| | | |
| | | /* ignore whitespace at the begginig */ |
| | | SCPI_LexWhiteSpace(&lex_state, &tmp); |
| | | lexWhiteSpace(&lex_state, &tmp); |
| | | |
| | | if (SCPI_LexProgramHeader(&lex_state, &state->programHeader) >= 0) { |
| | | if (SCPI_LexWhiteSpace(&lex_state, &tmp) > 0) { |
| | | SCPI_ParseAllProgramData(&lex_state, &state->programData, &state->numberOfParameters); |
| | | if (lexProgramHeader(&lex_state, &state->programHeader) >= 0) { |
| | | if (lexWhiteSpace(&lex_state, &tmp) > 0) { |
| | | parseAllProgramData(&lex_state, &state->programData, &state->numberOfParameters); |
| | | } else { |
| | | invalidateToken(&state->programData, lex_state.pos); |
| | | } |
| | |
| | | invalidateToken(&state->programData, lex_state.buffer); |
| | | } |
| | | |
| | | if (result == 0) result = SCPI_LexNewLine(&lex_state, &tmp); |
| | | if (result == 0) result = SCPI_LexSemicolon(&lex_state, &tmp); |
| | | if (result == 0) result = lexNewLine(&lex_state, &tmp); |
| | | if (result == 0) result = lexSemicolon(&lex_state, &tmp); |
| | | |
| | | if (!SCPI_LexIsEos(&lex_state) && (result == 0)) { |
| | | if (!lexIsEos(&lex_state) && (result == 0)) { |
| | | lex_state.pos++; |
| | | |
| | | state->programHeader.len = 1; |