From 8a0ad09e70d555974cd319e6157b09f857960867 Mon Sep 17 00:00:00 2001
From: nancy.liao <huihui.liao@greentest.com.cn>
Date: 周三, 23 4月 2025 11:31:35 +0800
Subject: [PATCH] 通用的命令解析完成 如<[:MEASure]|[SOURCE]><[:ARM]|[TRigger]>[:DC] 以及[:MEASure][:DC],当前还没有区分必填项和可选性,输入参数将严格按照:的参数个数进行解析

---
 libscpi/src/lexer.c |  267 +++++++++++++++++++++++++++++++++-------------------
 1 files changed, 168 insertions(+), 99 deletions(-)

diff --git a/libscpi/src/lexer.c b/libscpi/src/lexer.c
index a1be5e4..3294559 100644
--- a/libscpi/src/lexer.c
+++ b/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.
  */
 
 /**
@@ -160,7 +161,7 @@
 
 /* skip characters */
 /* 7.4.1 <PROGRAM MESSAGE UNIT SEPARATOR>*/
-// TODO: static int skipProgramMessageUnitSeparator(lex_state_t * state)
+/* TODO: static int skipProgramMessageUnitSeparator(lex_state_t * state) */
 
 /**
  * Skip all whitespaces
@@ -178,10 +179,10 @@
 }
 
 /* 7.4.2 <PROGRAM DATA SEPARATOR> */
-// static int skipProgramDataSeparator(lex_state_t * state)
+/* static int skipProgramDataSeparator(lex_state_t * state) */
 
 /* 7.5.2 <PROGRAM MESSAGE TERMINATOR> */
-// static int skipProgramMessageTerminator(lex_state_t * state)
+/* static int skipProgramMessageTerminator(lex_state_t * state) */
 
 /**
  * Skip decimal digit
@@ -189,7 +190,7 @@
  * @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 SKIP_OK;
     } else {
@@ -204,7 +205,7 @@
  */
 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++;
     }
@@ -232,7 +233,7 @@
  */
 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++;
     }
@@ -245,7 +246,7 @@
  * @param chr
  * @return 
  */
-static int skipChr(lex_state_t * state, int chr) {
+static int skipChr(lex_state_t * state, char chr) {
     if (!iseos(state) && ischr(state, chr)) {
         state->pos++;
         return SKIP_OK;
@@ -297,6 +298,7 @@
 }
 
 /* 7.6.1.2 <COMMAND PROGRAM HEADER> */
+
 /**
  * Skip program mnemonic [a-z][a-z0-9_]*
  * @param state
@@ -304,9 +306,9 @@
  */
 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++;
         }
     }
@@ -319,6 +321,7 @@
 }
 
 /* tokens */
+
 /**
  * Detect token white space
  * @param state
@@ -333,15 +336,18 @@
     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;
+   return token->len;
+
+
 }
 
 /* 7.6.1 <COMMAND PROGRAM HEADER> */
+
 /**
  * Skip command program header \*<PROGRAM MNEMONIC>
  * @param state
@@ -399,35 +405,48 @@
  * @param token
  * @return 
  */
+
+ /*璇嗗埆鍜屽垎绫籗CPI鍛戒护澶�     
+    鍏叡鍛戒护澶� 浠�*璇嗗埆  濡�  *IDN?
+    澶嶅悎鍛戒护澶� 浠�:璇嗗埆 濡�  SYSTem:ERRor?
+    鍛戒护鏌ヨ  浠ワ紵璇嗗埆 
+        鏅�氬懡浠わ細MEAS:VOLT
+        鏌ヨ鍛戒护锛歁EAS:VOLT?
+ */
 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);
+    // 瑙f瀽鍒板叕鍏卞懡浠ゅご鍚庢鏌ユ煡璇㈢
     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) {
+
+         // 瑙f瀽鍒板鍚堝懡浠ゅ悗妫�鏌ユ煡璇㈢
         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;
@@ -438,6 +457,7 @@
 }
 
 /* 7.7.1 <CHARACTER PROGRAM DATA> */
+
 /**
  * Detect token "Character program data"
  * @param state
@@ -447,18 +467,18 @@
 int scpiLex_CharacterProgramData(lex_state_t * state, scpi_token_t * token) {
     token->ptr = 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++;
         }
     }
 
     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;
@@ -502,7 +522,7 @@
  * @return 
  */
 int scpiLex_DecimalNumericProgramData(lex_state_t * state, scpi_token_t * token) {
-    const char * rollback;
+    char * rollback;
     token->ptr = state->pos;
 
     if (skipMantisa(state)) {
@@ -517,9 +537,9 @@
 
     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;
@@ -531,7 +551,7 @@
 
     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);
@@ -545,9 +565,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;
     }
@@ -558,7 +578,7 @@
 /* 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((uint8_t)(state->pos[0]))) {
         state->pos++;
         someNumbers++;
     }
@@ -590,31 +610,31 @@
  * @return 
  */
 int scpiLex_NondecimalNumericData(lex_state_t * state, scpi_token_t * token) {
-    token->ptr = state->pos;
     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;
             }
         }
     }
 
     if (someNumbers) {
-        token->ptr += 2; // ignore number prefix
+        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;
     }
@@ -626,7 +646,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++;
@@ -638,15 +658,17 @@
                 state->pos--;
                 break;
             }
+        } else {
+            break;
         }
     }
 }
 
-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, '\'');
 }
 
@@ -662,7 +684,7 @@
     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++;
@@ -672,7 +694,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++;
@@ -686,15 +708,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> */
@@ -712,6 +734,7 @@
     int i;
     int arbitraryBlockLength = 0;
     const char * ptr = state->pos;
+    int validData = -1;
     token->ptr = state->pos;
 
     if (skipChr(state, '#')) {
@@ -721,7 +744,7 @@
             state->pos++;
 
             for (; i > 0; i--) {
-                if (!iseos(state) && isdigit(state->pos[0])) {
+                if (!iseos(state) && isdigit((uint8_t)(state->pos[0]))) {
                     arbitraryBlockLength *= 10;
                     arbitraryBlockLength += (state->pos[0] - '0');
                     state->pos++;
@@ -732,24 +755,32 @@
 
             if (i == 0) {
                 state->pos += arbitraryBlockLength;
-                if ((state->buffer + state->len) < (state->pos)) {
-                    token->len = 0;
-                } else {
+                if ((state->buffer + state->len) >= (state->pos)) {
                     token->ptr = state->pos - arbitraryBlockLength;
                     token->len = arbitraryBlockLength;
+                    validData = 1;
+                } else {
+                    validData = 0;
                 }
-            } 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;
     }
@@ -760,12 +791,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;
         }
     }
@@ -779,7 +810,8 @@
     }
 }
 
-// TODO: 7.7.7.2-2 recursive - any program data
+/* TODO: 7.7.7.2-2 recursive - any program data */
+
 /**
  * Detect token Expression
  * @param state
@@ -802,9 +834,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;
     }
@@ -823,10 +855,10 @@
 
     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;
@@ -843,10 +875,50 @@
 
     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;
+}
+
+/**
+ * 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;
@@ -867,15 +939,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