From b4e38d7ea765422bf301980634b7b948fe989ae1 Mon Sep 17 00:00:00 2001 From: Jan Breuer <jan.breuer@jaybee.cz> Date: 周三, 19 6月 2013 23:10:02 +0800 Subject: [PATCH] Convert parameter handling to new lexer --- libscpi/src/lexer.c | 630 ++++++++++++++++++++++++++------------------------------ 1 files changed, 295 insertions(+), 335 deletions(-) diff --git a/libscpi/src/lexer.c b/libscpi/src/lexer.c index 197acdc..0b3bf09 100644 --- a/libscpi/src/lexer.c +++ b/libscpi/src/lexer.c @@ -38,42 +38,8 @@ #include <stdio.h> #include <string.h> -enum _token_type_t { - TokComma, - TokSemicolon, - TokQuiestion, - TokNewLine, - TokHexnum, - TokOctnum, - TokBinnum, - TokProgramMnemonic, - TokDecimalNumericProgramData, - TokMantisa, - TokExponent, - TokSuffixProgramData, - TokSingleQuoteProgramData, - TokDoubleQuoteProgramData, - TokProgramExpression, - TokCompoundProgramHeader, - TokCommonProgramHeader, - TokWhiteSpace, - TokUnknown, -}; -typedef enum _token_type_t token_type_t; +#include "scpi/lexer.h" -struct _token_t { - token_type_t type; - const char * ptr; - int len; -}; -typedef struct _token_t token_t; - -struct _lex_state_t { - const char * buffer; - const char * pos; - int len; -}; -typedef struct _lex_state_t lex_state_t; /* identify character */ static int isws ( int c ) { @@ -130,6 +96,8 @@ } /* skip characters */ +/* 7.4.1 <PROGRAM MESSAGE UNIT SEPARATOR>*/ +// TODO: static int skipProgramMessageUnitSeparator(lex_state_t * state) static int skipWs(lex_state_t * state) { int someSpace = 0; while(!iseos(state) && isws(state->pos[0])) { @@ -139,6 +107,12 @@ return someSpace; } + +/* 7.4.2 <PROGRAM DATA SEPARATOR> */ +// static int skipProgramDataSeparator(lex_state_t * state) + +/* 7.5.2 <PROGRAM MESSAGE TERMINATOR> */ +// static int skipProgramMessageTerminator(lex_state_t * state) static int skipDigit(lex_state_t * state) { if(!iseos(state) && isdigit(state->pos[0])) { @@ -212,6 +186,7 @@ } } +/* 7.6.1.2 <COMMAND PROGRAM HEADER> */ static int skipProgramMnemonic(lex_state_t * state) { const char * startPos = state->pos; if(!iseos(state) && isalpha(state->pos[0])) { @@ -237,104 +212,71 @@ } else { token->type = TokUnknown; } - + return token->len; } -int SCPI_LexHexnum(lex_state_t * state, token_t * token) { - token->ptr = state->pos; - - if(skipChr(state, '#')) { - if(!iseos(state) && isH(state->pos[0])) { - state->pos++; - - while(!iseos(state) && isxdigit(state->pos[0])) { - state->pos++; - } - - } else { +/* 7.6.1 <COMMAND PROGRAM HEADER> */ +static int skipCommonProgramHeader(lex_state_t * state) { + if (skipStar(state)) { + if(!skipProgramMnemonic(state)) { state->pos--; + } else { + return 1; } } - - token->len = state->pos - token->ptr; - if(token->len > 0) { - token->type = TokHexnum; - } else { - token->type = TokUnknown; - } - - return token->len; -} - -int SCPI_LexBinnum(lex_state_t * state, token_t * token) { - token->ptr = state->pos; - - if(skipChr(state, '#')) { - if(!iseos(state) && isB(state->pos[0])) { - state->pos++; - - while(!iseos(state) && isbdigit(state->pos[0])) { - state->pos++; - } - - } else { - state->pos--; - } - } - - token->len = state->pos - token->ptr; - if(token->len > 0) { - token->type = TokBinnum; - } else { - token->type = TokUnknown; - } - - return token->len; -} - -int SCPI_LexOctnum(lex_state_t * state, token_t * token) { - token->ptr = state->pos; - - if(skipChr(state, '#')) { - if(!iseos(state) && isQ(state->pos[0])) { - state->pos++; - - while(!iseos(state) && isqdigit(state->pos[0])) { - state->pos++; - } - - } else { - state->pos--; - } - } - - token->len = state->pos - token->ptr; - if(token->len > 0) { - token->type = TokOctnum; - } else { - token->type = TokUnknown; - } - - return token->len; -} - -int SCPI_LexNondecimalNumericData(lex_state_t * state, token_t * token) { - int res; - - res = SCPI_LexHexnum(state, token); - if(res > 0) return res; - - res = SCPI_LexBinnum(state, token); - if(res > 0) return res; - - res = SCPI_LexOctnum(state, token); - if(res > 0) return res; - return 0; } -int SCPI_LexProgramMnemonic(lex_state_t * state, token_t * token) { +static int skipCompoundProgramHeader(lex_state_t * state) { + const char * rollback = state->pos; + + skipColon(state); + + if(skipProgramMnemonic(state)) { + while(skipColon(state)) { + if(!skipProgramMnemonic(state)) { + state->pos = rollback; + return 0; + } + } + return 1; + } else { + state->pos = rollback; + return 0; + } +} + +int SCPI_LexProgramHeader(lex_state_t * state, token_t * token) { + token->ptr = state->pos; + token->type = TokUnknown; + + if(skipCommonProgramHeader(state)) { + if (skipChr(state, '?')) { + token->type = TokCommonQueryProgramHeader; + } else { + token->type = TokCommonProgramHeader; + } + } else if(skipCompoundProgramHeader(state)) { + if (skipChr(state, '?')) { + token->type = TokCompoundQueryProgramHeader; + } else { + token->type = TokCompoundProgramHeader; + } + } + + if (token->type != TokUnknown) { + token->len = state->pos - token->ptr; + } else { + token->len = 0; + state->pos = token->ptr; + } + + return token->len; +} + +/* 7.7.1 <CHARACTER PROGRAM DATA> */ +int SCPI_LexCharacterProgramData(lex_state_t * state, token_t * token) { token->ptr = state->pos; if(!iseos(state) && isalpha(state->pos[0])) { @@ -343,7 +285,7 @@ state->pos++; } } - + token->len = state->pos - token->ptr; if(token->len > 0) { token->type = TokProgramMnemonic; @@ -354,9 +296,9 @@ return token->len; } -int SCPI_LexMantisa(lex_state_t * state, token_t * token) { +/* 7.7.2 <DECIMAL NUMERIC PROGRAM DATA> */ +static int skipMantisa(lex_state_t * state) { int someNumbers = 0; - token->ptr = state->pos; skipPlusmn(state); @@ -366,67 +308,50 @@ someNumbers += skipNumbers(state); } - token->len = state->pos - token->ptr; - if((token->len > 0) && (someNumbers > 0)) { - token->type = TokMantisa; - } else { - token->type = TokUnknown; - state->pos = token->ptr; - token->len = 0; - } - - return token->len; + return someNumbers; } -int SCPI_LexExponent(lex_state_t * state, token_t * token) { +static int skipExponent(lex_state_t * state) { int someNumbers = 0; - token->ptr = state->pos; if(!iseos(state) && isE(state->pos[0])) { state->pos++; - + skipWs(state); - + skipPlusmn(state); - someNumbers += skipNumbers(state); + someNumbers = skipNumbers(state); } - token->len = state->pos - token->ptr; - if((token->len > 0) && (someNumbers > 0)) { - token->type = TokExponent; - } else { - token->type = TokUnknown; - state->pos = token->ptr; - token->len = 0; - } - - return token->len; + return someNumbers; } int SCPI_LexDecimalNumericProgramData(lex_state_t * state, token_t * token) { - token_t exponent; + const char * rollback; + token->ptr = state->pos; - if (SCPI_LexMantisa(state, token)) { + if (skipMantisa(state)) { + rollback = state->pos; skipWs(state); - SCPI_LexExponent(state, &exponent); + if(!skipExponent(state)) { + state->pos = rollback; + } + } else { + state->pos = token->ptr; } - if((token->len > 0) && (exponent.len > 0)) { + token->len = state->pos - token->ptr; + if(token->len > 0) { token->type = TokDecimalNumericProgramData; - token->len = (exponent.ptr + exponent.len) - token->ptr; - } else if (token->len > 0) { - token->type = TokDecimalNumericProgramData; - state->pos = token->ptr + token->len; } else { token->type = TokUnknown; - state->pos = token->ptr; - token->len = 0; } return token->len; } +/* 7.7.3 <SUFFIX PROGRAM DATA> */ int SCPI_LexSuffixProgramData(lex_state_t * state, token_t * token) { token->ptr = state->pos; @@ -443,7 +368,7 @@ skipDigit(state); } } - + token->len = state->pos - token->ptr; if((token->len > 0)) { token->type = TokSuffixProgramData; @@ -456,38 +381,121 @@ return token->len; } -int SCPI_LexCommonProgramHeader(lex_state_t * state, token_t * token) { +/* 7.7.4 <NONDECIMAL NUMERIC PROGRAM DATA> */ +static int skipHexNum(lex_state_t * state) { + int someNumbers = 0; + while(!iseos(state) && isxdigit(state->pos[0])) { + state->pos++; + someNumbers++; + } + return someNumbers; +} + +static int skipOctNum(lex_state_t * state) { + int someNumbers = 0; + while(!iseos(state) && isqdigit(state->pos[0])) { + state->pos++; + someNumbers++; + } + return someNumbers; +} + +static int skipBinNum(lex_state_t * state) { + int someNumbers = 0; + while(!iseos(state) && isbdigit(state->pos[0])) { + state->pos++; + someNumbers++; + } + return someNumbers; +} + + +int SCPI_LexNondecimalNumericData(lex_state_t * state, token_t * token) { token->ptr = state->pos; - - if (skipStar(state)) { - if(!skipProgramMnemonic(state)) { - state->pos--; + int someNumbers = 0; + if(skipChr(state, '#')) { + if(!iseos(state)) { + if(isH(state->pos[0])) { + state->pos++; + someNumbers = skipHexNum(state); + token->type = TokHexnum; + } else if(isQ(state->pos[0])) { + state->pos++; + someNumbers = skipOctNum(state); + token->type = TokOctnum; + } else if(isB(state->pos[0])) { + state->pos++; + someNumbers = skipBinNum(state); + token->type = TokBinnum; + } } } - token->len = state->pos - token->ptr; - - if((token->len > 0)) { - token->type = TokCommonProgramHeader; + if (someNumbers) { + token->ptr += 2; // ignore number prefix + token->len = state->pos - token->ptr; } else { token->type = TokUnknown; state->pos = token->ptr; token->len = 0; } - - return token->len; + return token->len > 0 ? token->len + 2 : 0; } -int SCPI_LexCompoundProgramHeader(lex_state_t * state, token_t * token) { + +/* 7.7.5 <STRING PROGRAM DATA> */ +static int isascii7bit(int c) { + return (c >= 0) && (c <= 0x7f); +} + + +static int skipQuoteProgramData(lex_state_t * state, int quote) { + while(!iseos(state)) { + if (isascii7bit(state->pos[0]) && !ischr(state, quote)) { + state->pos++; + } else if (ischr(state, quote)) { + state->pos++; + if (!iseos(state) && ischr(state, quote)) { + state->pos++; + } else { + state->pos--; + break; + } + } + } +} + +static int skipDoubleQuoteProgramData(lex_state_t * state) { + skipQuoteProgramData(state, '"'); +} + +static int skipSingleQuoteProgramData(lex_state_t * state) { + skipQuoteProgramData(state, '\''); +} + +int SCPI_LexStringProgramData(lex_state_t * state, token_t * token) { token->ptr = state->pos; - skipColon(state); - - if(skipProgramMnemonic(state)) { - while(skipColon(state)) { - if(!skipProgramMnemonic(state)) { - // TODO: lexer error - break; + if (!iseos(state)) { + if (ischr(state, '"')) { + state->pos++; + token->type = TokDoubleQuoteProgramData; + skipDoubleQuoteProgramData(state); + if (!iseos(state) && ischr(state, '"')) { + state->pos++; + token->len = state->pos - token->ptr; + } else { + state->pos = token->ptr; + } + } else if (ischr(state, '\'')) { + state->pos++; + token->type = TokSingleQuoteProgramData; + skipSingleQuoteProgramData(state); + if (!iseos(state) && ischr(state, '\'')) { + state->pos++; + token->len = state->pos - token->ptr; + } else { + state->pos = token->ptr; } } } @@ -495,26 +503,118 @@ token->len = state->pos - token->ptr; if((token->len > 0)) { - token->type = TokCompoundProgramHeader; + token->ptr++; + token->len -= 2; } else { token->type = TokUnknown; state->pos = token->ptr; token->len = 0; } - - return token->len; + + return token->len > 0 ? token->len + 2 : 0; } -int SCPI_LexProgramHeader(lex_state_t * state, token_t * token) { - int res; - - res = SCPI_LexCommonProgramHeader(state, token); - if(res > 0) return res; - - res = SCPI_LexCompoundProgramHeader(state, token); - if(res > 0) return res; +/* 7.7.6 <ARBITRARY BLOCK PROGRAM DATA> */ +static int isNonzeroDigit(int c) { + return isdigit(c) && (c != '0'); +} +int SCPI_LexArbitraryBlockProgramData(lex_state_t * state, token_t * token) { + int i; + int j = 0; + const char * ptr = state->pos; + token->ptr = state->pos; + + if (skipChr(state, '#')) { + if (!iseos(state) && isNonzeroDigit(state->pos[0])) { + /* Get number of digits */ + i = state->pos[0] - '0'; + state->pos++; + + for(; i > 0; i--) { + if (!iseos(state) && isdigit(state->pos[0])) { + j *= 10; + j += (state->pos[0] - '0'); + state->pos++; + } else { + break; + } + } + + if(i == 0) { + state->pos += j; + if ((state->buffer + state->len) < (state->pos)) { + token->len = 0; + } else { + token->ptr = state->pos - j; + token->len = j; + } + } else { + token->len = 0; + } + } else { + token->len = 0; + } + } + + if((token->len > 0)) { + token->type = TokArbitraryBlockProgramData; + } else { + token->type = TokUnknown; + state->pos = token->ptr; + token->len = 0; + } + + return token->len + (token->ptr - ptr); +} + +/* 7.7.7 <EXPRESSION PROGRAM DATA> */ +static int isProgramExpression(int c) { + if ((c >= 0x20) && (c<=0x7e)) { + if ((c!= 0x22) + && (c!= 0x23) + && (c!= 0x27) + && (c!= 0x28) + && (c!= 0x29) + && (c!= 0x3B)) { + return 1; + } + } + return 0; +} + +static void skipProgramExpression(lex_state_t * state) { + while(!iseos(state) && isProgramExpression(state->pos[0])) { + state->pos++; + } +} + +// TODO: 7.7.7.2-2 recursive - any program data +int SCPI_LexProgramExpression(lex_state_t * state, token_t * token) { + token->ptr = state->pos; + + if (!iseos(state) && ischr(state, '(')) { + state->pos++; + skipProgramExpression(state); + + if (!iseos(state) && ischr(state, ')')) { + state->pos++; + token->len = state->pos - token->ptr; + } else { + token->len = 0; + } + } + + if((token->len > 0)) { + token->type = TokProgramExpression; + } else { + token->type = TokUnknown; + state->pos = token->ptr; + token->len = 0; + } + + return token->len; } int SCPI_LexComma(lex_state_t * state, token_t * token) { @@ -523,20 +623,6 @@ if (skipChr(state, ',')) { token->len = 1; token->type = TokComma; - } else { - token->len = 0; - token->type = TokUnknown; - } - - return token->len; -} - -int SCPI_LexQuestion(lex_state_t * state, token_t * token) { - token->ptr = state->pos; - - if (skipChr(state, '?')) { - token->len = 1; - token->type = TokQuiestion; } else { token->len = 0; token->type = TokUnknown; @@ -564,7 +650,7 @@ skipChr(state, '\r'); skipChr(state, '\n'); - + token->len = state->pos - token->ptr; if((token->len > 0)) { @@ -574,135 +660,9 @@ state->pos = token->ptr; token->len = 0; } - + return token->len; } -/* -int SCPI_LexProgramExpression(lex_state_t * state, token_t * token) { - return 0; -} - -void SCPI_LexSingleQuoteProgramData(lex_state_t * state) { -} - -void SCPI_LexDoubleQuoteProgramDatalex_state_t * state) { -} - -*/ - -const char * typeToStr(token_type_t type) { - switch(type) { - case TokComma: return "TokComma"; - case TokSemicolon: return "TokSemicolon"; - case TokQuiestion: return "TokQuiestion"; - case TokNewLine: return "TokNewLine"; - case TokHexnum: return "TokHexnum"; - case TokOctnum: return "TokOctnum"; - case TokBinnum: return "TokBinnum"; - case TokProgramMnemonic: return "TokProgramMnemonic"; - case TokDecimalNumericProgramData: return "TokDecimalNumericProgramData"; - case TokMantisa: return "TokMantisa"; - case TokExponent: return "TokExponent"; - case TokSuffixProgramData: return "TokSuffixProgramData"; - case TokSingleQuoteProgramData: return "TokSingleQuoteProgramData"; - case TokDoubleQuoteProgramData: return "TokDoubleQuoteProgramData"; - case TokProgramExpression: return "TokProgramExpression"; - case TokCompoundProgramHeader: return "TokCompoundProgramHeader"; - case TokCommonProgramHeader: return "TokCommonProgramHeader"; - case TokWhiteSpace: return "TokWhiteSpace"; - default: return "TokUnknown"; - } -} - -void printToken(token_t * token) { - printf("Token:\r\n"); - printf("\t->type = %s\r\n", typeToStr(token->type)); - printf("\t->ptr = %p (\"%.*s\")\r\n", token->ptr, token->len, token->ptr); - printf("\t->len = %d\r\n", token->len); -} - -#define INIT_STATE(str) do { \ - state.buffer = state.pos = (str); \ - state.len = strlen((str)); \ -} while(0) - - -int main(int argc, char ** argv) { - lex_state_t state; - token_t token; - - -// INIT_STATE("MEAS:VOLT:DC? 1, 5\r\n"); - INIT_STATE(" \t MEAS:VOLT:DC? 1.58, .125, 5V\r\n"); - SCPI_LexWhiteSpace(&state, &token); printToken(&token); - - INIT_STATE("#H123fe5A , "); - SCPI_LexNondecimalNumericData(&state, &token); printToken(&token); - - INIT_STATE("#B0111010101 , "); - SCPI_LexNondecimalNumericData(&state, &token); printToken(&token); - - INIT_STATE("#Q125725433 , "); - SCPI_LexNondecimalNumericData(&state, &token); printToken(&token); - - INIT_STATE("abc_213as564 , "); - SCPI_LexProgramMnemonic(&state, &token); printToken(&token); - - INIT_STATE("10 , "); - SCPI_LexDecimalNumericProgramData(&state, &token); printToken(&token); - - INIT_STATE("-10.5 , "); - SCPI_LexDecimalNumericProgramData(&state, &token); printToken(&token); - - INIT_STATE("+.5 , "); - SCPI_LexDecimalNumericProgramData(&state, &token); printToken(&token); - - INIT_STATE("-. , "); - SCPI_LexDecimalNumericProgramData(&state, &token); printToken(&token); - - INIT_STATE("-1 e , "); - SCPI_LexDecimalNumericProgramData(&state, &token); printToken(&token); - - INIT_STATE("-1 e 3, "); - SCPI_LexDecimalNumericProgramData(&state, &token); printToken(&token); - - INIT_STATE("1.5E12 , "); - SCPI_LexDecimalNumericProgramData(&state, &token); printToken(&token); - - INIT_STATE("A/V , "); - SCPI_LexSuffixProgramData(&state, &token); printToken(&token); - - INIT_STATE("mA.h , "); - SCPI_LexSuffixProgramData(&state, &token); printToken(&token); - - INIT_STATE("*IDN?, "); - SCPI_LexCommonProgramHeader(&state, &token); printToken(&token); - - INIT_STATE("*?, "); - SCPI_LexCommonProgramHeader(&state, &token); printToken(&token); - - INIT_STATE("MEAS:VOLT:DC?, "); - SCPI_LexCommonProgramHeader(&state, &token); printToken(&token); - - INIT_STATE("MEAS:VOLT:DC?, "); - SCPI_LexCompoundProgramHeader(&state, &token); printToken(&token); - - INIT_STATE(":MEAS:VOLT:DC?, "); - SCPI_LexCompoundProgramHeader(&state, &token); printToken(&token); - - INIT_STATE(":MEAS::VOLT:DC?, "); - SCPI_LexCompoundProgramHeader(&state, &token); printToken(&token); - - INIT_STATE(":MEAS::VOLT:DC?, "); - SCPI_LexProgramHeader(&state, &token); printToken(&token); - - INIT_STATE("MEAS:VOLT:DC?, "); - SCPI_LexProgramHeader(&state, &token); printToken(&token); - - INIT_STATE("*IDN?, "); - SCPI_LexProgramHeader(&state, &token); printToken(&token); - return 0; -} -- Gitblit v1.9.1