From 7d6b42c51111d450097af396af16a052e52a4442 Mon Sep 17 00:00:00 2001
From: Jan Breuer <jan.breuer@jaybee.cz>
Date: 周日, 24 4月 2016 21:15:39 +0800
Subject: [PATCH] Fix order of deallocations, add heap unit test

---
 libscpi/src/lexer.c |   87 ++++++++++++++++++++++++++++++++-----------
 1 files changed, 64 insertions(+), 23 deletions(-)

diff --git a/libscpi/src/lexer.c b/libscpi/src/lexer.c
index 194205b..6a646f0 100644
--- a/libscpi/src/lexer.c
+++ b/libscpi/src/lexer.c
@@ -189,7 +189,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 +204,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 +232,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 +245,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;
@@ -305,9 +305,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++;
         }
     }
@@ -451,9 +451,9 @@
 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++;
         }
     }
@@ -562,7 +562,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++;
     }
@@ -630,7 +630,7 @@
     return (c >= 0) && (c <= 0x7f);
 }
 
-static void 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++;
@@ -642,6 +642,8 @@
                 state->pos--;
                 break;
             }
+        } else {
+            break;
         }
     }
 }
@@ -690,15 +692,15 @@
     token->len = state->pos - token->ptr;
 
     if ((token->len > 0)) {
-        token->ptr++;
-        token->len -= 2;
+        //token->ptr++;
+        //token->len -= 2;
     } else {
         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> */
@@ -726,7 +728,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++;
@@ -741,6 +743,8 @@
                     token->ptr = state->pos - arbitraryBlockLength;
                     token->len = arbitraryBlockLength;
                     validData = 1;
+                } else {
+                    validData = 0;
                 }
             } else if (iseos(state)) {
                 validData = 0;
@@ -771,12 +775,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;
         }
     }
@@ -865,6 +869,46 @@
 }
 
 /**
+ * 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
@@ -888,6 +932,3 @@
 
     return token->len;
 }
-
-
-

--
Gitblit v1.9.1