Jan Breuer
2013-10-24 43d97ad8631dd8a1ab9966f38e345627188baf47
libscpi/src/lexer.c
@@ -38,25 +38,24 @@
#include <stdio.h>
#include <string.h>
#include "scpi/lexer.h"
#include "lexer_private.h"
/* identify character */
static int isws ( int c ) {
static int isws(int c) {
    if ((c == ' ') || (c == '\t')) {
        return 1;
    }
    return 0;
}
static int isbdigit ( int c ) {
static int isbdigit(int c) {
    if ((c == '0') || (c == '1')) {
        return 1;
    }
    return 0;
}
static int isqdigit ( int c ) {
static int isqdigit(int c) {
    if ((c == '0') || (c == '1') || (c == '2') || (c == '3') || (c == '4') || (c == '5') || (c == '6') || (c == '7')) {
        return 1;
    }
@@ -71,6 +70,10 @@
    }
}
int lexIsEos(lex_state_t * state) {
    return iseos(state);
}
static int ischr(lex_state_t * state, char chr) {
    return (state->pos[0] == chr);
}
@@ -80,31 +83,36 @@
}
static int isH(int c) {
    return c == 'h' || c == 'H';
    return c == 'h' || c == 'H';
}
static int isB(int c) {
    return c == 'b' || c == 'B';
    return c == 'b' || c == 'B';
}
static int isQ(int c) {
    return c == 'q' || c == 'Q';
    return c == 'q' || c == 'Q';
}
static int isE(int c) {
    return c == 'e' || c == 'E';
    return c == 'e' || c == 'E';
}
#define SKIP_NONE       0
#define SKIP_OK         1
#define SKIP_INCOMPLETE -1
/* 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])) {
    while (!iseos(state) && isws(state->pos[0])) {
        state->pos++;
        someSpace++;
    }
    return someSpace;
}
@@ -115,17 +123,17 @@
// static int skipProgramMessageTerminator(lex_state_t * state)
static int skipDigit(lex_state_t * state) {
    if(!iseos(state) && isdigit(state->pos[0])) {
    if (!iseos(state) && isdigit(state->pos[0])) {
        state->pos++;
        return 1;
        return SKIP_OK;
    } else {
        return 0;
        return SKIP_NONE;
    }
}
static int skipNumbers(lex_state_t * state) {
    int someNumbers = 0;
    while(!iseos(state) && isdigit(state->pos[0])) {
    while (!iseos(state) && isdigit(state->pos[0])) {
        state->pos++;
        someNumbers++;
    }
@@ -133,275 +141,283 @@
}
static int skipPlusmn(lex_state_t * state) {
    if(!iseos(state) && isplusmn(state->pos[0])) {
    if (!iseos(state) && isplusmn(state->pos[0])) {
        state->pos++;
        return 1;
        return SKIP_OK;
    } else {
        return 0;
        return SKIP_NONE;
    }
}
static int skipAlpha(lex_state_t * state) {
    int someLetters = 0;
    while(!iseos(state) && isalpha(state->pos[0])) {
    while (!iseos(state) && isalpha(state->pos[0])) {
        state->pos++;
        someLetters++;
    }
    return someLetters;
    return someLetters;
}
static int skipChr(lex_state_t * state, int chr) {
    if(!iseos(state) && ischr(state, chr)) {
    if (!iseos(state) && ischr(state, chr)) {
        state->pos++;
        return 1;
        return SKIP_OK;
    } else {
        return 0;
        return SKIP_NONE;
    }
}
static int skipSlashDot(lex_state_t * state) {
    if(!iseos(state) && (ischr(state, '/') | ischr(state, '.'))) {
    if (!iseos(state) && (ischr(state, '/') | ischr(state, '.'))) {
        state->pos++;
        return 1;
        return SKIP_OK;
    } else {
        return 0;
    }
        return SKIP_NONE;
    }
}
static int skipStar(lex_state_t * state) {
    if(!iseos(state) && ischr(state, '*')) {
    if (!iseos(state) && ischr(state, '*')) {
        state->pos++;
        return 1;
        return SKIP_OK;
    } else {
        return 0;
    }
        return SKIP_NONE;
    }
}
static int skipColon(lex_state_t * state) {
    if(!iseos(state) && ischr(state, ':')) {
    if (!iseos(state) && ischr(state, ':')) {
        state->pos++;
        return 1;
        return SKIP_OK;
    } else {
        return 0;
    }
        return SKIP_NONE;
    }
}
/* 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])) {
    if (!iseos(state) && isalpha(state->pos[0])) {
        state->pos++;
        while(!iseos(state) && (isalnum(state->pos[0]) || ischr(state, '_'))) {
        while (!iseos(state) && (isalnum(state->pos[0]) || ischr(state, '_'))) {
            state->pos++;
        }
    }
    return state->pos - startPos;
    if (iseos(state)) {
        return (state->pos - startPos) * SKIP_INCOMPLETE;
    } else {
        return (state->pos - startPos) * SKIP_OK;
    }
}
/* tokens */
int SCPI_LexWhiteSpace(lex_state_t * state, token_t * token) {
int lexWhiteSpace(lex_state_t * state, token_t * token) {
    token->ptr = state->pos;
    skipWs(state);
    token->len = state->pos - token->ptr;
    token->len = state->pos - token->ptr;
    if (token->len > 0) {
        token->type = TokWhiteSpace;
    } else {
        token->type = TokUnknown;
    }
    }
    return token->len;
}
/* 7.6.1 <COMMAND PROGRAM HEADER> */
int SCPI_LexCommonProgramHeader(lex_state_t * state, token_t * token) {
    token->ptr = state->pos;
static int skipCommonProgramHeader(lex_state_t * state) {
    int res;
    if (skipStar(state)) {
        if(!skipProgramMnemonic(state)) {
            state->pos--;
        res = skipProgramMnemonic(state);
        if (res == SKIP_NONE && iseos(state)) {
            return SKIP_INCOMPLETE;
        } else if (res <= SKIP_INCOMPLETE) {
            return SKIP_OK;
        } else if (res >= SKIP_OK) {
            return SKIP_OK;
        } else {
            return SKIP_INCOMPLETE;
        }
    }
    token->len = state->pos - token->ptr;
    if((token->len > 0)) {
        token->type = TokCommonProgramHeader;
    } else {
        token->type = TokUnknown;
        state->pos = token->ptr;
        token->len = 0;
    }
    return token->len;
    return SKIP_NONE;
}
int SCPI_LexCompoundProgramHeader(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;
static int skipCompoundProgramHeader(lex_state_t * state) {
    int res;
    int firstColon = skipColon(state);
    res = skipProgramMnemonic(state);
    if (res >= SKIP_OK) {
        while (skipColon(state)) {
            res = skipProgramMnemonic(state);
            if (res <= SKIP_INCOMPLETE) {
                return SKIP_OK;
            } else if (res == SKIP_NONE) {
                return SKIP_INCOMPLETE;
            }
        }
    }
    token->len = state->pos - token->ptr;
    if((token->len > 0)) {
        token->type = TokCompoundProgramHeader;
        return SKIP_OK;
    } else if (res <= SKIP_INCOMPLETE) {
        return SKIP_OK;
    } else if (firstColon) {
        return SKIP_INCOMPLETE;
    } else {
        token->type = TokUnknown;
        state->pos = token->ptr;
        token->len = 0;
        return SKIP_NONE;
    }
    return token->len;
}
int SCPI_LexProgramHeader(lex_state_t * state,  token_t * token) {
int 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;
    return 0;
}
int SCPI_LexQuestion(lex_state_t * state, token_t * token) {
    token->ptr = state->pos;
    if (skipChr(state, '?')) {
        token->len = 1;
        token->type = TokQuiestion;
    token->type = TokUnknown;
    res = skipCommonProgramHeader(state);
    if (res >= SKIP_OK) {
        if (skipChr(state, '?') >= SKIP_OK) {
            token->type = TokCommonQueryProgramHeader;
        } else {
            token->type = TokCommonProgramHeader;
        }
    } else if (res <= SKIP_INCOMPLETE) {
        token->type = TokIncompleteCommonProgramHeader;
    } else if (res == SKIP_NONE) {
        res = skipCompoundProgramHeader(state);
        if (res >= SKIP_OK) {
            if (skipChr(state, '?') >= SKIP_OK) {
                token->type = TokCompoundQueryProgramHeader;
            } else {
                token->type = TokCompoundProgramHeader;
            }
        } else if (res <= SKIP_INCOMPLETE) {
            token->type = TokIncompleteCompoundProgramHeader;
        }
    }
    if (token->type != TokUnknown) {
        token->len = state->pos - token->ptr;
    } else {
        token->len = 0;
        token->type = TokUnknown;
        state->pos = token->ptr;
    }
    return token->len;
}
/* 7.7.1 <CHARACTER PROGRAM DATA> */
int SCPI_LexCharacterProgramData(lex_state_t * state, token_t * token) {
int lexCharacterProgramData(lex_state_t * state, token_t * token) {
    token->ptr = state->pos;
    if(!iseos(state) && isalpha(state->pos[0])) {
    if (!iseos(state) && isalpha(state->pos[0])) {
        state->pos++;
        while(!iseos(state) && (isalnum(state->pos[0]) || ischr(state, '_'))) {
        while (!iseos(state) && (isalnum(state->pos[0]) || ischr(state, '_'))) {
            state->pos++;
        }
    }
    token->len = state->pos - token->ptr;
    if(token->len > 0) {
    if (token->len > 0) {
        token->type = TokProgramMnemonic;
    } else {
        token->type = TokUnknown;
    }
    return token->len;
}
/* 7.7.2 <DECIMAL NUMERIC PROGRAM DATA> */
static int skipMantisa(lex_state_t * state) {
    int someNumbers = 0;
    skipPlusmn(state);
    someNumbers += skipNumbers(state);
    if(skipChr(state, '.')) {
    if (skipChr(state, '.')) {
        someNumbers += skipNumbers(state);
    }
    return someNumbers;
}
static int skipExponent(lex_state_t * state) {
    int someNumbers = 0;
    if(!iseos(state) && isE(state->pos[0])) {
    if (!iseos(state) && isE(state->pos[0])) {
        state->pos++;
        skipWs(state);
        skipPlusmn(state);
        someNumbers = skipNumbers(state);
    }
    return someNumbers;
}
int SCPI_LexDecimalNumericProgramData(lex_state_t * state, token_t * token) {
int lexDecimalNumericProgramData(lex_state_t * state, token_t * token) {
    const char * rollback;
    token->ptr = state->pos;
    if (skipMantisa(state)) {
        rollback = state->pos;
        skipWs(state);
        if(!skipExponent(state)) {
        if (!skipExponent(state)) {
            state->pos = rollback;
        }
    } else {
        state->pos = token->ptr;
    }
    token->len = state->pos - token->ptr;
    if(token->len > 0) {
    if (token->len > 0) {
        token->type = TokDecimalNumericProgramData;
    } else {
        token->type = TokUnknown;
    }
    return token->len;
}
/* 7.7.3 <SUFFIX PROGRAM DATA> */
int SCPI_LexSuffixProgramData(lex_state_t * state, token_t * token) {
int lexSuffixProgramData(lex_state_t * state, token_t * token) {
    token->ptr = state->pos;
    skipChr(state, '/');
    // TODO: strict parsing  : SLASH? (ALPHA+ (MINUS? DIGIT)?) ((SLASH | DOT) (ALPHA+ (MINUS? DIGIT)?))*
    if (skipAlpha(state)) {
        skipChr(state, '-');
        skipDigit(state);
        while (skipSlashDot(state)) {
            skipAlpha(state);
            skipChr(state, '-');
            skipDigit(state);
            skipDigit(state);
        }
    }
    token->len = state->pos - token->ptr;
    if((token->len > 0)) {
    if ((token->len > 0)) {
        token->type = TokSuffixProgramData;
    } else {
        token->type = TokUnknown;
        state->pos = token->ptr;
        token->len = 0;
    }
    return token->len;
}
/* 7.7.4 <NONDECIMAL NUMERIC PROGRAM DATA> */
static int skipHexNum(lex_state_t * state) {
    int someNumbers = 0;
    while(!iseos(state) && isxdigit(state->pos[0])) {
    while (!iseos(state) && isxdigit(state->pos[0])) {
        state->pos++;
        someNumbers++;
    }
@@ -410,7 +426,7 @@
static int skipOctNum(lex_state_t * state) {
    int someNumbers = 0;
    while(!iseos(state) && isqdigit(state->pos[0])) {
    while (!iseos(state) && isqdigit(state->pos[0])) {
        state->pos++;
        someNumbers++;
    }
@@ -419,54 +435,52 @@
static int skipBinNum(lex_state_t * state) {
    int someNumbers = 0;
    while(!iseos(state) && isbdigit(state->pos[0])) {
    while (!iseos(state) && isbdigit(state->pos[0])) {
        state->pos++;
        someNumbers++;
    }
    return someNumbers;
}
int SCPI_LexNondecimalNumericData(lex_state_t * state, token_t * token) {
int lexNondecimalNumericData(lex_state_t * state, token_t * token) {
    token->ptr = state->pos;
    int someNumbers = 0;
    if(skipChr(state, '#')) {
        if(!iseos(state)) {
            if(isH(state->pos[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])) {
            } else if (isQ(state->pos[0])) {
                state->pos++;
                someNumbers = skipOctNum(state);
                token->type = TokOctnum;
            } else if(isB(state->pos[0])) {
            } else if (isB(state->pos[0])) {
                state->pos++;
                someNumbers = skipBinNum(state);
                token->type = TokBinnum;
            }
        }
    }
    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;
}
/* 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)) {
    while (!iseos(state)) {
        if (isascii7bit(state->pos[0]) && !ischr(state, quote)) {
            state->pos++;
        } else if (ischr(state, quote)) {
@@ -480,7 +494,7 @@
        }
    }
}
static int skipDoubleQuoteProgramData(lex_state_t * state) {
    skipQuoteProgramData(state, '"');
}
@@ -489,9 +503,9 @@
    skipQuoteProgramData(state, '\'');
}
int SCPI_LexStringProgramData(lex_state_t * state,  token_t * token) {
int lexStringProgramData(lex_state_t * state, token_t * token) {
    token->ptr = state->pos;
    if (!iseos(state)) {
        if (ischr(state, '"')) {
            state->pos++;
@@ -515,10 +529,10 @@
            }
        }
    }
    token->len = state->pos - token->ptr;
    if((token->len > 0)) {
    if ((token->len > 0)) {
        token->ptr++;
        token->len -= 2;
    } else {
@@ -526,8 +540,8 @@
        state->pos = token->ptr;
        token->len = 0;
    }
    return token->len;
    return token->len > 0 ? token->len + 2 : 0;
}
/* 7.7.6 <ARBITRARY BLOCK PROGRAM DATA> */
@@ -535,10 +549,10 @@
    return isdigit(c) && (c != '0');
}
int SCPI_LexArbitraryBlockProgramData(lex_state_t * state, token_t * token) {
int 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, '#')) {
@@ -546,8 +560,8 @@
            /* Get number of digits */
            i = state->pos[0] - '0';
            state->pos++;
            for(; i > 0; i--) {
            for (; i > 0; i--) {
                if (!iseos(state) && isdigit(state->pos[0])) {
                    j *= 10;
                    j += (state->pos[0] - '0');
@@ -556,14 +570,14 @@
                    break;
                }
            }
            if(i == 0) {
            if (i == 0) {
                state->pos += j;
                if (!iseos(state)) {
                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;
@@ -573,47 +587,48 @@
        }
    }
    if((token->len > 0)) {
    if ((token->len > 0)) {
        token->type = TokArbitraryBlockProgramData;
    } else {
        token->type = TokUnknown;
        state->pos = token->ptr;
        token->len = 0;
    }
    return token->len;
    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)) {
    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])) {
    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) {
int 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;
@@ -621,21 +636,21 @@
            token->len = 0;
        }
    }
    if((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) {
int lexComma(lex_state_t * state, token_t * token) {
    token->ptr = state->pos;
    if (skipChr(state, ',')) {
        token->len = 1;
        token->type = TokComma;
@@ -643,13 +658,13 @@
        token->len = 0;
        token->type = TokUnknown;
    }
    return token->len;
}
int SCPI_LexSemicolon(lex_state_t * state, token_t * token) {
int lexSemicolon(lex_state_t * state, token_t * token) {
    token->ptr = state->pos;
    if (skipChr(state, ';')) {
        token->len = 1;
        token->type = TokSemicolon;
@@ -657,26 +672,26 @@
        token->len = 0;
        token->type = TokUnknown;
    }
    return token->len;
}
int SCPI_LexNewLine(lex_state_t * state,  token_t * token) {
int lexNewLine(lex_state_t * state, token_t * token) {
    token->ptr = state->pos;
    skipChr(state, '\r');
    skipChr(state, '\n');
    token->len = state->pos - token->ptr;
    if((token->len > 0)) {
    if ((token->len > 0)) {
        token->type = TokNewLine;
    } else {
        token->type = TokUnknown;
        state->pos = token->ptr;
        token->len = 0;
    }
    return token->len;
}