nancy.liao
5 天以前 31c1e390d2f7a5278e73491aee3568a6352482d1
libscpi/src/lexer.c
@@ -1,28 +1,29 @@
/*-
 * Copyright (c) 2012-2013 Jan Breuer,
 * BSD 2-Clause License
 *
 * All Rights Reserved
 *
 * Copyright (c) 2012-2018, Jan Breuer
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/**
@@ -38,65 +39,50 @@
#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 "lexer_private.h"
#include "scpi/error.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 ) {
/**
 * Is white space
 * @param c
 * @return
 */
static int isws(int c) {
    if ((c == ' ') || (c == '\t')) {
        return 1;
    }
    return 0;
}
static int isbdigit ( int c ) {
/**
 * Is binary digit
 * @param c
 * @return
 */
static int isbdigit(int c) {
    if ((c == '0') || (c == '1')) {
        return 1;
    }
    return 0;
}
static int isqdigit ( int c ) {
/**
 * Is hexadecimal digit
 * @param c
 * @return
 */
static int isqdigit(int c) {
    if ((c == '0') || (c == '1') || (c == '2') || (c == '3') || (c == '4') || (c == '5') || (c == '6') || (c == '7')) {
        return 1;
    }
    return 0;
}
/**
 * Is end of string
 * @param state
 * @return
 */
static int iseos(lex_state_t * state) {
    if ((state->buffer + state->len) <= (state->pos)) {
        return 1;
@@ -105,372 +91,483 @@
    }
}
/**
 * Private export of iseos
 * @param state
 * @return
 */
int scpiLex_IsEos(lex_state_t * state) {
    return iseos(state);
}
/**
 * Test current character
 * @param state
 * @param chr
 * @return
 */
static int ischr(lex_state_t * state, char chr) {
    return (state->pos[0] == chr);
}
/**
 * Is plus or minus
 * @param c
 * @return
 */
static int isplusmn(int c) {
    return c == '+' || c == '-';
}
/**
 * Is letter H
 * @param c
 * @return
 */
static int isH(int c) {
    return c == 'h' || c == 'H';
    return c == 'h' || c == 'H';
}
/**
 * Is letter B
 * @param c
 * @return
 */
static int isB(int c) {
    return c == 'b' || c == 'B';
    return c == 'b' || c == 'B';
}
/**
 * Is letter Q
 * @param c
 * @return
 */
static int isQ(int c) {
    return c == 'q' || c == 'Q';
    return c == 'q' || c == 'Q';
}
/**
 * Is letter E
 * @param c
 * @return
 */
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) */
/**
 * Skip all whitespaces
 * @param state
 * @return
 */
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;
}
/* 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) */
/**
 * Skip decimal digit
 * @param state
 * @return
 */
static int skipDigit(lex_state_t * state) {
    if(!iseos(state) && isdigit(state->pos[0])) {
    if (!iseos(state) && isdigit((uint8_t)(state->pos[0]))) {
        state->pos++;
        return 1;
        return SKIP_OK;
    } else {
        return 0;
        return SKIP_NONE;
    }
}
/**
 * Skip multiple decimal digits
 * @param state
 * @return
 */
static int skipNumbers(lex_state_t * state) {
    int someNumbers = 0;
    while(!iseos(state) && isdigit(state->pos[0])) {
    while (!iseos(state) && isdigit((uint8_t)(state->pos[0]))) {
        state->pos++;
        someNumbers++;
    }
    return someNumbers;
}
/**
 * Skip plus or minus
 * @param state
 * @return
 */
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;
    }
}
/**
 * Skip any character from 'a'-'Z'
 * @param state
 * @return
 */
static int skipAlpha(lex_state_t * state) {
    int someLetters = 0;
    while(!iseos(state) && isalpha(state->pos[0])) {
    while (!iseos(state) && isalpha((uint8_t)(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)) {
/**
 * Skip exact character chr or nothing
 * @param state
 * @param chr
 * @return
 */
static int skipChr(lex_state_t * state, char chr) {
    if (!iseos(state) && ischr(state, chr)) {
        state->pos++;
        return 1;
        return SKIP_OK;
    } else {
        return 0;
        return SKIP_NONE;
    }
}
/**
 * Skip slash or dot
 * @param state
 * @return
 */
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;
    }
}
/**
 * Skip star
 * @param state
 * @return
 */
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;
    }
}
/**
 * Skip colon
 * @param state
 * @return
 */
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> */
/**
 * Skip program mnemonic [a-z][a-z0-9_]*
 * @param state
 * @return
 */
static int skipProgramMnemonic(lex_state_t * state) {
    const char * startPos = state->pos;
    if(!iseos(state) && isalpha(state->pos[0])) {
    if (!iseos(state) && isalpha((uint8_t)(state->pos[0]))) {
        state->pos++;
        while(!iseos(state) && (isalnum(state->pos[0]) || ischr(state, '_'))) {
        while (!iseos(state) && (isalnum((uint8_t)(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) {
/**
 * Detect token white space
 * @param state
 * @param token
 * @return
 */
int scpiLex_WhiteSpace(lex_state_t * state, scpi_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;
        token->type = SCPI_TOKEN_WS;
    } else {
        token->type = TokUnknown;
    }
    return token->len;
        token->type = SCPI_TOKEN_UNKNOWN;
    }
   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 {
            state->pos--;
        }
    }
    token->len = state->pos - token->ptr;
    if(token->len > 0) {
        token->type = TokHexnum;
    } else {
        token->type = TokUnknown;
    }
    return token->len;
}
/* 7.6.1 <COMMAND PROGRAM HEADER> */
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) {
/**
 * Skip command program header \*<PROGRAM MNEMONIC>
 * @param state
 * @return
 */
static int skipCommonProgramHeader(lex_state_t * state) {
    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;
    if (skipStar(state)) {
        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;
        }
    }
    return SKIP_NONE;
}
int SCPI_LexProgramMnemonic(lex_state_t * state, token_t * token) {
    token->ptr = state->pos;
/**
 * Skip compound program header :<PROGRAM MNEMONIC>:<PROGRAM MNEMONIC>...
 * @param state
 * @return
 */
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;
            }
        }
        return SKIP_OK;
    } else if (res <= SKIP_INCOMPLETE) {
        return SKIP_OK;
    } else if (firstColon) {
        return SKIP_INCOMPLETE;
    } else {
        return SKIP_NONE;
    }
}
/**
 * Detect token command or compound program header
 * @param state
 * @param token
 * @return
 */
 /*识别和分类SCPI命令头
    公共命令头 以*识别  如  *IDN?
    复合命令头 以:识别 如  SYSTem:ERRor?
    命令查询  以?识别
        普通命令:MEAS:VOLT
        查询命令:MEAS:VOLT?
 */
int scpiLex_ProgramHeader(lex_state_t * state, scpi_token_t * token) {
    
    if(!iseos(state) && isalpha(state->pos[0])) {
     // 记录起始位置 并且初始化类型
    int res;
    token->ptr = state->pos;
    token->type = SCPI_TOKEN_UNKNOWN;
    res = skipCommonProgramHeader(state);
    // 解析到公共命令头后检查查询符
    if (res >= SKIP_OK) {
        if (skipChr(state, '?') >= SKIP_OK) {
            token->type = SCPI_TOKEN_COMMON_QUERY_PROGRAM_HEADER;
        } else {
            token->type = SCPI_TOKEN_COMMON_PROGRAM_HEADER;
        }
    } else if (res <= SKIP_INCOMPLETE) {
        token->type = SCPI_TOKEN_INCOMPLETE_COMMON_PROGRAM_HEADER;
    } else if (res == SKIP_NONE) {
         // 解析到复合命令后检查查询符
        res = skipCompoundProgramHeader(state);
        if (res >= SKIP_OK) {
            if (skipChr(state, '?') >= SKIP_OK) {
                token->type = SCPI_TOKEN_COMPOUND_QUERY_PROGRAM_HEADER;
            } else {
                token->type = SCPI_TOKEN_COMPOUND_PROGRAM_HEADER;
            }
        } else if (res <= SKIP_INCOMPLETE) {
            token->type = SCPI_TOKEN_INCOMPLETE_COMPOUND_PROGRAM_HEADER;
        }
    }
    // 计算长度
    if (token->type != SCPI_TOKEN_UNKNOWN) {
        token->len = state->pos - token->ptr;
    } else {
        token->len = 0;
        state->pos = token->ptr;
    }
    return token->len;
}
/* 7.7.1 <CHARACTER PROGRAM DATA> */
/**
 * Detect token "Character program data"
 * @param state
 * @param token
 * @return
 */
int scpiLex_CharacterProgramData(lex_state_t * state, scpi_token_t * token) {
    token->ptr = state->pos;
    if (!iseos(state) && isalpha((uint8_t)(state->pos[0]))) {
        state->pos++;
        while(!iseos(state) && (isalnum(state->pos[0]) || ischr(state, '_'))) {
        while (!iseos(state) && (isalnum((uint8_t)(state->pos[0])) || ischr(state, '_'))) {
            state->pos++;
        }
    }
    token->len = state->pos - token->ptr;
    if(token->len > 0) {
        token->type = TokProgramMnemonic;
    if (token->len > 0) {
        token->type = SCPI_TOKEN_PROGRAM_MNEMONIC;
    } else {
        token->type = TokUnknown;
        token->type = SCPI_TOKEN_UNKNOWN;
    }
    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);
    someNumbers += skipNumbers(state);
    if(skipChr(state, '.')) {
    if (skipChr(state, '.')) {
        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])) {
    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;
    if (SCPI_LexMantisa(state, token)) {
        skipWs(state);
        SCPI_LexExponent(state, &exponent);
    }
    if((token->len > 0) && (exponent.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;
}
int SCPI_LexSuffixProgramData(lex_state_t * state, token_t * token) {
/**
 * Detect token Decimal number
 * @param state
 * @param token
 * @return
 */
int scpiLex_DecimalNumericProgramData(lex_state_t * state, scpi_token_t * token) {
    char * rollback;
    token->ptr = state->pos;
    if (skipMantisa(state)) {
        rollback = state->pos;
        skipWs(state);
        if (!skipExponent(state)) {
            state->pos = rollback;
        }
    } else {
        state->pos = token->ptr;
    }
    token->len = state->pos - token->ptr;
    if (token->len > 0) {
        token->type = SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA;
    } else {
        token->type = SCPI_TOKEN_UNKNOWN;
    }
    return token->len;
}
/* 7.7.3 <SUFFIX PROGRAM DATA> */
int scpiLex_SuffixProgramData(lex_state_t * state, scpi_token_t * token) {
    token->ptr = state->pos;
    skipChr(state, '/');
    // TODO: strict parsing  : SLASH? (ALPHA+ (MINUS? DIGIT)?) ((SLASH | DOT) (ALPHA+ (MINUS? DIGIT)?))*
    /* 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)) {
        token->type = TokSuffixProgramData;
    if ((token->len > 0)) {
        token->type = SCPI_TOKEN_SUFFIX_PROGRAM_DATA;
    } else {
        token->type = TokUnknown;
        state->pos = token->ptr;
        token->len = 0;
    }
    return token->len;
}
int SCPI_LexCommonProgramHeader(lex_state_t * state, token_t * token) {
    token->ptr = state->pos;
    if (skipStar(state)) {
        if(!skipProgramMnemonic(state)) {
            state->pos--;
        }
    }
    token->len = state->pos - token->ptr;
    if((token->len > 0)) {
        token->type = TokCommonProgramHeader;
    } else {
        token->type = TokUnknown;
        token->type = SCPI_TOKEN_UNKNOWN;
        state->pos = token->ptr;
        token->len = 0;
    }
@@ -478,26 +575,268 @@
    return token->len;
}
int SCPI_LexCompoundProgramHeader(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((uint8_t)(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;
}
/**
 * Detect token nondecimal number
 * @param state
 * @param token
 * @return
 */
int scpiLex_NondecimalNumericData(lex_state_t * state, scpi_token_t * token) {
    int someNumbers = 0;
    token->ptr = state->pos;
    skipColon(state);
    if(skipProgramMnemonic(state)) {
        while(skipColon(state)) {
            if(!skipProgramMnemonic(state)) {
                // TODO: lexer error
                break;
    if (skipChr(state, '#')) {
        if (!iseos(state)) {
            if (isH(state->pos[0])) {
                state->pos++;
                someNumbers = skipHexNum(state);
                token->type = SCPI_TOKEN_HEXNUM;
            } else if (isQ(state->pos[0])) {
                state->pos++;
                someNumbers = skipOctNum(state);
                token->type = SCPI_TOKEN_OCTNUM;
            } else if (isB(state->pos[0])) {
                state->pos++;
                someNumbers = skipBinNum(state);
                token->type = SCPI_TOKEN_BINNUM;
            }
        }
    }
    token->len = state->pos - token->ptr;
    if((token->len > 0)) {
        token->type = TokCompoundProgramHeader;
    if (someNumbers) {
        token->ptr += 2; /* ignore number prefix */
        token->len = state->pos - token->ptr;
    } else {
        token->type = TokUnknown;
        token->type = SCPI_TOKEN_UNKNOWN;
        state->pos = token->ptr;
        token->len = 0;
    }
    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 void skipQuoteProgramData(lex_state_t * state, char 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;
            }
        } else {
            break;
        }
    }
}
static void skipDoubleQuoteProgramData(lex_state_t * state) {
    skipQuoteProgramData(state, '"');
}
static void skipSingleQuoteProgramData(lex_state_t * state) {
    skipQuoteProgramData(state, '\'');
}
/**
 * Detect token String data
 * @param state
 * @param token
 * @return
 */
int scpiLex_StringProgramData(lex_state_t * state, scpi_token_t * token) {
    token->ptr = state->pos;
    if (!iseos(state)) {
        if (ischr(state, '"')) {
            state->pos++;
            token->type = SCPI_TOKEN_DOUBLE_QUOTE_PROGRAM_DATA;
            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 = SCPI_TOKEN_SINGLE_QUOTE_PROGRAM_DATA;
            skipSingleQuoteProgramData(state);
            if (!iseos(state) && ischr(state, '\'')) {
                state->pos++;
                token->len = state->pos - token->ptr;
            } else {
                state->pos = token->ptr;
            }
        }
    }
    token->len = state->pos - token->ptr;
    if ((token->len > 0)) {
        /* token->ptr++;
         * token->len -= 2; */
    } else {
        token->type = SCPI_TOKEN_UNKNOWN;
        state->pos = token->ptr;
        token->len = 0;
    }
    return token->len > 0 ? token->len : 0;
}
/* 7.7.6 <ARBITRARY BLOCK PROGRAM DATA> */
static int isNonzeroDigit(int c) {
    return isdigit(c) && (c != '0');
}
/**
 * Detect token Block Data
 * @param state
 * @param token
 * @return
 */
int scpiLex_ArbitraryBlockProgramData(lex_state_t * state, scpi_token_t * token) {
    int i;
    int arbitraryBlockLength = 0;
    const char * ptr = state->pos;
    int validData = -1;
    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((uint8_t)(state->pos[0]))) {
                    arbitraryBlockLength *= 10;
                    arbitraryBlockLength += (state->pos[0] - '0');
                    state->pos++;
                } else {
                    break;
                }
            }
            if (i == 0) {
                state->pos += arbitraryBlockLength;
                if ((state->buffer + state->len) >= (state->pos)) {
                    token->ptr = state->pos - arbitraryBlockLength;
                    token->len = arbitraryBlockLength;
                    validData = 1;
                } else {
                    validData = 0;
                }
            } else if (iseos(state)) {
                validData = 0;
            }
        } else if (iseos(state)) {
            validData = 0;
        }
    }
    if (validData == 1) {
        /* valid */
        token->type = SCPI_TOKEN_ARBITRARY_BLOCK_PROGRAM_DATA;
    } else if (validData == 0) {
        /* incomplete */
        token->type = SCPI_TOKEN_UNKNOWN;
        token->len = 0;
        state->pos = state->buffer + state->len;
    } else {
        /* invalid */
        token->type = SCPI_TOKEN_UNKNOWN;
        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 != '"')
                && (c != '#')
                && (c != '\'')
                && (c != '(')
                && (c != ')')
                && (c != ';')) {
            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 */
/**
 * Detect token Expression
 * @param state
 * @param token
 * @return
 */
int scpiLex_ProgramExpression(lex_state_t * state, scpi_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 = SCPI_TOKEN_PROGRAM_EXPRESSION;
    } else {
        token->type = SCPI_TOKEN_UNKNOWN;
        state->pos = token->ptr;
        token->len = 0;
    }
@@ -505,204 +844,107 @@
    return token->len;
}
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;
    return 0;
}
int SCPI_LexComma(lex_state_t * state, token_t * token) {
/**
 * Detect token comma
 * @param state
 * @param token
 * @return
 */
int scpiLex_Comma(lex_state_t * state, scpi_token_t * token) {
    token->ptr = state->pos;
    if (skipChr(state, ',')) {
        token->len = 1;
        token->type = TokComma;
        token->type = SCPI_TOKEN_COMMA;
    } else {
        token->len = 0;
        token->type = TokUnknown;
        token->type = SCPI_TOKEN_UNKNOWN;
    }
    return token->len;
}
int SCPI_LexQuestion(lex_state_t * state, token_t * token) {
/**
 * Detect token semicolon
 * @param state
 * @param token
 * @return
 */
int scpiLex_Semicolon(lex_state_t * state, scpi_token_t * token) {
    token->ptr = state->pos;
    if (skipChr(state, '?')) {
        token->len = 1;
        token->type = TokQuiestion;
    } else {
        token->len = 0;
        token->type = TokUnknown;
    }
    return token->len;
}
int SCPI_LexSemicolon(lex_state_t * state, token_t * token) {
    token->ptr = state->pos;
    if (skipChr(state, ';')) {
        token->len = 1;
        token->type = TokSemicolon;
        token->type = SCPI_TOKEN_SEMICOLON;
    } else {
        token->len = 0;
        token->type = TokUnknown;
        token->type = SCPI_TOKEN_UNKNOWN;
    }
    return token->len;
}
int SCPI_LexNewLine(lex_state_t * state,  token_t * token) {
/**
 * Detect token colon
 * @param state
 * @param token
 * @return
 */
int scpiLex_Colon(lex_state_t * state, scpi_token_t * token) {
    token->ptr = state->pos;
    if (skipChr(state, ':')) {
        token->len = 1;
        token->type = SCPI_TOKEN_COLON;
    } else {
        token->len = 0;
        token->type = SCPI_TOKEN_UNKNOWN;
    }
    return token->len;
}
/**
 * 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
 * @return
 */
int scpiLex_NewLine(lex_state_t * state, scpi_token_t * token) {
    token->ptr = state->pos;
    skipChr(state, '\r');
    skipChr(state, '\n');
    token->len = state->pos - token->ptr;
    if((token->len > 0)) {
        token->type = TokNewLine;
    if ((token->len > 0)) {
        token->type = SCPI_TOKEN_NL;
    } else {
        token->type = TokUnknown;
        token->type = SCPI_TOKEN_UNKNOWN;
        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;
}