From a28ccd18f24b50dc62e37f1a5d8be641578343d9 Mon Sep 17 00:00:00 2001
From: Jan Breuer <jan.breuer@jaybee.cz>
Date: 周五, 16 10月 2015 21:04:54 +0800
Subject: [PATCH] Resolve issue with strBaseToInt64 on 32bit platforms

---
 libscpi/src/lexer.c |  391 ++++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 311 insertions(+), 80 deletions(-)

diff --git a/libscpi/src/lexer.c b/libscpi/src/lexer.c
index ed7f49c..ee4696c 100644
--- a/libscpi/src/lexer.c
+++ b/libscpi/src/lexer.c
@@ -39,8 +39,13 @@
 #include <string.h>
 
 #include "lexer_private.h"
+#include "scpi/error.h"
 
-/* identify character */
+/**
+ * Is white space
+ * @param c
+ * @return 
+ */
 static int isws(int c) {
     if ((c == ' ') || (c == '\t')) {
         return 1;
@@ -48,6 +53,11 @@
     return 0;
 }
 
+/**
+ * Is binary digit
+ * @param c
+ * @return 
+ */
 static int isbdigit(int c) {
     if ((c == '0') || (c == '1')) {
         return 1;
@@ -55,6 +65,11 @@
     return 0;
 }
 
+/**
+ * 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;
@@ -62,6 +77,11 @@
     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;
@@ -70,30 +90,66 @@
     }
 }
 
-int lexIsEos(lex_state_t * state) {
+/**
+ * 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';
 }
 
+/**
+ * Is letter B
+ * @param c
+ * @return 
+ */
 static int isB(int c) {
     return c == 'b' || c == 'B';
 }
 
+/**
+ * Is letter Q
+ * @param c
+ * @return 
+ */
 static int isQ(int c) {
     return c == 'q' || c == 'Q';
 }
 
+/**
+ * Is letter E
+ * @param c
+ * @return 
+ */
 static int isE(int c) {
     return c == 'e' || c == 'E';
 }
@@ -106,6 +162,11 @@
 /* 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])) {
@@ -122,6 +183,11 @@
 /* 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])) {
         state->pos++;
@@ -131,6 +197,11 @@
     }
 }
 
+/**
+ * Skip multiple decimal digits
+ * @param state
+ * @return 
+ */
 static int skipNumbers(lex_state_t * state) {
     int someNumbers = 0;
     while (!iseos(state) && isdigit(state->pos[0])) {
@@ -140,6 +211,11 @@
     return someNumbers;
 }
 
+/**
+ * Skip plus or minus
+ * @param state
+ * @return 
+ */
 static int skipPlusmn(lex_state_t * state) {
     if (!iseos(state) && isplusmn(state->pos[0])) {
         state->pos++;
@@ -149,6 +225,11 @@
     }
 }
 
+/**
+ * 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])) {
@@ -158,7 +239,13 @@
     return someLetters;
 }
 
-static int skipChr(lex_state_t * state, int 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 SKIP_OK;
@@ -167,6 +254,11 @@
     }
 }
 
+/**
+ * Skip slash or dot
+ * @param state
+ * @return 
+ */
 static int skipSlashDot(lex_state_t * state) {
     if (!iseos(state) && (ischr(state, '/') | ischr(state, '.'))) {
         state->pos++;
@@ -176,6 +268,11 @@
     }
 }
 
+/**
+ * Skip star
+ * @param state
+ * @return 
+ */
 static int skipStar(lex_state_t * state) {
     if (!iseos(state) && ischr(state, '*')) {
         state->pos++;
@@ -185,6 +282,11 @@
     }
 }
 
+/**
+ * Skip colon
+ * @param state
+ * @return 
+ */
 static int skipColon(lex_state_t * state) {
     if (!iseos(state) && ischr(state, ':')) {
         state->pos++;
@@ -195,6 +297,12 @@
 }
 
 /* 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])) {
@@ -212,7 +320,14 @@
 }
 
 /* tokens */
-int lexWhiteSpace(lex_state_t * state, scpi_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);
@@ -220,15 +335,21 @@
     token->len = state->pos - token->ptr;
 
     if (token->len > 0) {
-        token->type = TokWhiteSpace;
+        token->type = SCPI_TOKEN_WS;
     } else {
-        token->type = TokUnknown;
+        token->type = SCPI_TOKEN_UNKNOWN;
     }
 
     return token->len;
 }
 
 /* 7.6.1 <COMMAND PROGRAM HEADER> */
+
+/**
+ * Skip command program header \*<PROGRAM MNEMONIC>
+ * @param state
+ * @return 
+ */
 static int skipCommonProgramHeader(lex_state_t * state) {
     int res;
     if (skipStar(state)) {
@@ -246,6 +367,11 @@
     return SKIP_NONE;
 }
 
+/**
+ * Skip compound program header :<PROGRAM MNEMONIC>:<PROGRAM MNEMONIC>...
+ * @param state
+ * @return 
+ */
 static int skipCompoundProgramHeader(lex_state_t * state) {
     int res;
     int firstColon = skipColon(state);
@@ -270,35 +396,41 @@
     }
 }
 
-int lexProgramHeader(lex_state_t * state, scpi_token_t * token) {
+/**
+ * Detect token command or compound program header
+ * @param state
+ * @param token
+ * @return 
+ */
+int scpiLex_ProgramHeader(lex_state_t * state, scpi_token_t * token) {
     int res;
     token->ptr = state->pos;
-    token->type = TokUnknown;
+    token->type = SCPI_TOKEN_UNKNOWN;
 
     res = skipCommonProgramHeader(state);
     if (res >= SKIP_OK) {
         if (skipChr(state, '?') >= SKIP_OK) {
-            token->type = TokCommonQueryProgramHeader;
+            token->type = SCPI_TOKEN_COMMON_QUERY_PROGRAM_HEADER;
         } else {
-            token->type = TokCommonProgramHeader;
+            token->type = SCPI_TOKEN_COMMON_PROGRAM_HEADER;
         }
     } else if (res <= SKIP_INCOMPLETE) {
-        token->type = TokIncompleteCommonProgramHeader;
+        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 = TokCompoundQueryProgramHeader;
+                token->type = SCPI_TOKEN_COMPOUND_QUERY_PROGRAM_HEADER;
             } else {
-                token->type = TokCompoundProgramHeader;
+                token->type = SCPI_TOKEN_COMPOUND_PROGRAM_HEADER;
             }
         } else if (res <= SKIP_INCOMPLETE) {
-            token->type = TokIncompleteCompoundProgramHeader;
-        } 
+            token->type = SCPI_TOKEN_INCOMPLETE_COMPOUND_PROGRAM_HEADER;
+        }
     }
 
-    if (token->type != TokUnknown) {
+    if (token->type != SCPI_TOKEN_UNKNOWN) {
         token->len = state->pos - token->ptr;
     } else {
         token->len = 0;
@@ -309,7 +441,14 @@
 }
 
 /* 7.7.1 <CHARACTER PROGRAM DATA> */
-int lexCharacterProgramData(lex_state_t * state, scpi_token_t * token) {
+
+/**
+ * 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(state->pos[0])) {
@@ -321,9 +460,9 @@
 
     token->len = state->pos - token->ptr;
     if (token->len > 0) {
-        token->type = TokProgramMnemonic;
+        token->type = SCPI_TOKEN_PROGRAM_MNEMONIC;
     } else {
-        token->type = TokUnknown;
+        token->type = SCPI_TOKEN_UNKNOWN;
     }
 
     return token->len;
@@ -360,8 +499,14 @@
     return someNumbers;
 }
 
-int lexDecimalNumericProgramData(lex_state_t * state, scpi_token_t * token) {
-    const char * rollback;
+/**
+ * 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)) {
@@ -376,16 +521,16 @@
 
     token->len = state->pos - token->ptr;
     if (token->len > 0) {
-        token->type = TokDecimalNumericProgramData;
+        token->type = SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA;
     } else {
-        token->type = TokUnknown;
+        token->type = SCPI_TOKEN_UNKNOWN;
     }
 
     return token->len;
 }
 
 /* 7.7.3 <SUFFIX PROGRAM DATA> */
-int lexSuffixProgramData(lex_state_t * state, scpi_token_t * token) {
+int scpiLex_SuffixProgramData(lex_state_t * state, scpi_token_t * token) {
     token->ptr = state->pos;
 
     skipChr(state, '/');
@@ -404,9 +549,9 @@
 
     token->len = state->pos - token->ptr;
     if ((token->len > 0)) {
-        token->type = TokSuffixProgramData;
+        token->type = SCPI_TOKEN_SUFFIX_PROGRAM_DATA;
     } else {
-        token->type = TokUnknown;
+        token->type = SCPI_TOKEN_UNKNOWN;
         state->pos = token->ptr;
         token->len = 0;
     }
@@ -442,23 +587,29 @@
     return someNumbers;
 }
 
-int lexNondecimalNumericData(lex_state_t * state, scpi_token_t * token) {
-    token->ptr = state->pos;
+/**
+ * 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;
     if (skipChr(state, '#')) {
         if (!iseos(state)) {
             if (isH(state->pos[0])) {
                 state->pos++;
                 someNumbers = skipHexNum(state);
-                token->type = TokHexnum;
+                token->type = SCPI_TOKEN_HEXNUM;
             } else if (isQ(state->pos[0])) {
                 state->pos++;
                 someNumbers = skipOctNum(state);
-                token->type = TokOctnum;
+                token->type = SCPI_TOKEN_OCTNUM;
             } else if (isB(state->pos[0])) {
                 state->pos++;
                 someNumbers = skipBinNum(state);
-                token->type = TokBinnum;
+                token->type = SCPI_TOKEN_BINNUM;
             }
         }
     }
@@ -467,7 +618,7 @@
         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;
     }
@@ -479,7 +630,7 @@
     return (c >= 0) && (c <= 0x7f);
 }
 
-static int skipQuoteProgramData(lex_state_t * state, int quote) {
+static void skipQuoteProgramData(lex_state_t * state, char quote) {
     while (!iseos(state)) {
         if (isascii7bit(state->pos[0]) && !ischr(state, quote)) {
             state->pos++;
@@ -495,21 +646,27 @@
     }
 }
 
-static int skipDoubleQuoteProgramData(lex_state_t * state) {
+static void skipDoubleQuoteProgramData(lex_state_t * state) {
     skipQuoteProgramData(state, '"');
 }
 
-static int skipSingleQuoteProgramData(lex_state_t * state) {
+static void skipSingleQuoteProgramData(lex_state_t * state) {
     skipQuoteProgramData(state, '\'');
 }
 
-int lexStringProgramData(lex_state_t * state, scpi_token_t * token) {
+/**
+ * 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 = TokDoubleQuoteProgramData;
+            token->type = SCPI_TOKEN_DOUBLE_QUOTE_PROGRAM_DATA;
             skipDoubleQuoteProgramData(state);
             if (!iseos(state) && ischr(state, '"')) {
                 state->pos++;
@@ -519,7 +676,7 @@
             }
         } else if (ischr(state, '\'')) {
             state->pos++;
-            token->type = TokSingleQuoteProgramData;
+            token->type = SCPI_TOKEN_SINGLE_QUOTE_PROGRAM_DATA;
             skipSingleQuoteProgramData(state);
             if (!iseos(state) && ischr(state, '\'')) {
                 state->pos++;
@@ -533,15 +690,15 @@
     token->len = state->pos - token->ptr;
 
     if ((token->len > 0)) {
-        token->ptr++;
-        token->len -= 2;
+        //token->ptr++;
+        //token->len -= 2;
     } else {
-        token->type = TokUnknown;
+        token->type = SCPI_TOKEN_UNKNOWN;
         state->pos = token->ptr;
         token->len = 0;
     }
 
-    return token->len > 0 ? token->len + 2 : 0;
+    return token->len > 0 ? token->len : 0;
 }
 
 /* 7.7.6 <ARBITRARY BLOCK PROGRAM DATA> */
@@ -549,10 +706,17 @@
     return isdigit(c) && (c != '0');
 }
 
-int lexArbitraryBlockProgramData(lex_state_t * state, scpi_token_t * token) {
+/**
+ * Detect token Block Data
+ * @param state
+ * @param token
+ * @return 
+ */
+int scpiLex_ArbitraryBlockProgramData(lex_state_t * state, scpi_token_t * token) {
     int i;
-    int j = 0;
+    int arbitraryBlockLength = 0;
     const char * ptr = state->pos;
+    int validData = -1;
     token->ptr = state->pos;
 
     if (skipChr(state, '#')) {
@@ -563,8 +727,8 @@
 
             for (; i > 0; i--) {
                 if (!iseos(state) && isdigit(state->pos[0])) {
-                    j *= 10;
-                    j += (state->pos[0] - '0');
+                    arbitraryBlockLength *= 10;
+                    arbitraryBlockLength += (state->pos[0] - '0');
                     state->pos++;
                 } else {
                     break;
@@ -572,25 +736,31 @@
             }
 
             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;
+                state->pos += arbitraryBlockLength;
+                if ((state->buffer + state->len) >= (state->pos)) {
+                    token->ptr = state->pos - arbitraryBlockLength;
+                    token->len = arbitraryBlockLength;
+                    validData = 1;
                 }
-            } else {
-                token->len = 0;
+            } else if (iseos(state)) {
+                validData = 0;
             }
-        } else {
-            token->len = 0;
+        } else if (iseos(state)) {
+            validData = 0;
         }
     }
 
-    if ((token->len > 0)) {
-        token->type = TokArbitraryBlockProgramData;
+    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 {
-        token->type = TokUnknown;
+        // invalid
+        token->type = SCPI_TOKEN_UNKNOWN;
         state->pos = token->ptr;
         token->len = 0;
     }
@@ -601,12 +771,12 @@
 /* 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 != '"')
+                && (c != '#')
+                && (c != '\'')
+                && (c != '(')
+                && (c != ')')
+                && (c != ';')) {
             return 1;
         }
     }
@@ -622,7 +792,13 @@
 
 // TODO: 7.7.7.2-2 recursive - any program data
 
-int lexProgramExpression(lex_state_t * state, scpi_token_t * token) {
+/**
+ * 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, '(')) {
@@ -638,9 +814,9 @@
     }
 
     if ((token->len > 0)) {
-        token->type = TokProgramExpression;
+        token->type = SCPI_TOKEN_PROGRAM_EXPRESSION;
     } else {
-        token->type = TokUnknown;
+        token->type = SCPI_TOKEN_UNKNOWN;
         state->pos = token->ptr;
         token->len = 0;
     }
@@ -648,35 +824,93 @@
     return token->len;
 }
 
-int lexComma(lex_state_t * state, scpi_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 lexSemicolon(lex_state_t * state, scpi_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 = TokSemicolon;
+        token->type = SCPI_TOKEN_SEMICOLON;
     } else {
         token->len = 0;
-        token->type = TokUnknown;
+        token->type = SCPI_TOKEN_UNKNOWN;
     }
 
     return token->len;
 }
 
-int lexNewLine(lex_state_t * state, scpi_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');
@@ -685,15 +919,12 @@
     token->len = state->pos - token->ptr;
 
     if ((token->len > 0)) {
-        token->type = TokNewLine;
+        token->type = SCPI_TOKEN_NL;
     } else {
-        token->type = TokUnknown;
+        token->type = SCPI_TOKEN_UNKNOWN;
         state->pos = token->ptr;
         token->len = 0;
     }
 
     return token->len;
 }
-
-
-

--
Gitblit v1.9.1