https://github.com/j123b567/scpi-parser

Dependents:   scpi_sx127x scpi_sx127x_firstTest MLX90418_I2C_master

Revision:
1:b497f235115a
Parent:
0:aad43948c45c
--- a/src/units.c	Thu Apr 09 22:42:15 2015 +0000
+++ b/src/units.c	Fri Aug 07 21:54:11 2015 +0000
@@ -37,8 +37,10 @@
 #include <string.h>
 #include "scpi/parser.h"
 #include "scpi/units.h"
-#include "scpi/utils_private.h"
+#include "utils_private.h"
+#include "scpi/utils.h"
 #include "scpi/error.h"
+#include "lexer_private.h"
 
 
 /*
@@ -62,39 +64,39 @@
  */
 const scpi_unit_def_t scpi_units_def[] = {
     /* voltage */
-    {/* name */ "UV",   /* unit */ SCPI_UNIT_VOLT,      /* mult */ 1e-6},
-    {/* name */ "MV",   /* unit */ SCPI_UNIT_VOLT,      /* mult */ 1e-3},
-    {/* name */ "V",    /* unit */ SCPI_UNIT_VOLT,      /* mult */ 1},
-    {/* name */ "KV",   /* unit */ SCPI_UNIT_VOLT,      /* mult */ 1e3},
+    {/* name */ "UV", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1e-6},
+    {/* name */ "MV", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1e-3},
+    {/* name */ "V", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1},
+    {/* name */ "KV", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1e3},
 
     /* current */
-    {/* name */ "UA",   /* unit */ SCPI_UNIT_AMPER,     /* mult */ 1e-6},
-    {/* name */ "MA",   /* unit */ SCPI_UNIT_AMPER,     /* mult */ 1e-3},
-    {/* name */ "A",    /* unit */ SCPI_UNIT_AMPER,     /* mult */ 1},
-    {/* name */ "KA",   /* unit */ SCPI_UNIT_AMPER,     /* mult */ 1e3},
+    {/* name */ "UA", /* unit */ SCPI_UNIT_AMPER, /* mult */ 1e-6},
+    {/* name */ "MA", /* unit */ SCPI_UNIT_AMPER, /* mult */ 1e-3},
+    {/* name */ "A", /* unit */ SCPI_UNIT_AMPER, /* mult */ 1},
+    {/* name */ "KA", /* unit */ SCPI_UNIT_AMPER, /* mult */ 1e3},
 
     /* resistance */
-    {/* name */ "OHM",  /* unit */ SCPI_UNIT_OHM,       /* mult */ 1},
-    {/* name */ "KOHM", /* unit */ SCPI_UNIT_OHM,       /* mult */ 1e3},
-    {/* name */ "MOHM", /* unit */ SCPI_UNIT_OHM,       /* mult */ 1e6},
+    {/* name */ "OHM", /* unit */ SCPI_UNIT_OHM, /* mult */ 1},
+    {/* name */ "KOHM", /* unit */ SCPI_UNIT_OHM, /* mult */ 1e3},
+    {/* name */ "MOHM", /* unit */ SCPI_UNIT_OHM, /* mult */ 1e6},
 
     /* frequency */
-    {/* name */ "HZ",   /* unit */ SCPI_UNIT_HERTZ,     /* mult */ 1},
-    {/* name */ "KHZ",  /* unit */ SCPI_UNIT_HERTZ,     /* mult */ 1e3},
-    {/* name */ "MHZ",  /* unit */ SCPI_UNIT_HERTZ,     /* mult */ 1e6},
-    {/* name */ "GHZ",  /* unit */ SCPI_UNIT_HERTZ,     /* mult */ 1e9},
+    {/* name */ "HZ", /* unit */ SCPI_UNIT_HERTZ, /* mult */ 1},
+    {/* name */ "KHZ", /* unit */ SCPI_UNIT_HERTZ, /* mult */ 1e3},
+    {/* name */ "MHZ", /* unit */ SCPI_UNIT_HERTZ, /* mult */ 1e6},
+    {/* name */ "GHZ", /* unit */ SCPI_UNIT_HERTZ, /* mult */ 1e9},
 
     /* temperature */
-    {/* name */ "CEL",  /* unit */ SCPI_UNIT_CELSIUS,   /* mult */ 1},
+    {/* name */ "CEL", /* unit */ SCPI_UNIT_CELSIUS, /* mult */ 1},
 
     /* time */
-    {/* name */ "PS",   /* unit */ SCPI_UNIT_SECONDS,   /* mult */ 1e-12},
-    {/* name */ "NS",   /* unit */ SCPI_UNIT_SECONDS,   /* mult */ 1e-9},
-    {/* name */ "US",   /* unit */ SCPI_UNIT_SECONDS,   /* mult */ 1e-6},
-    {/* name */ "MS",   /* unit */ SCPI_UNIT_SECONDS,   /* mult */ 1e-3},
-    {/* name */ "S",    /* unit */ SCPI_UNIT_SECONDS,   /* mult */ 1},
-    {/* name */ "MIN",  /* unit */ SCPI_UNIT_SECONDS,   /* mult */ 60},
-    {/* name */ "HR",   /* unit */ SCPI_UNIT_SECONDS,   /* mult */ 3600},
+    {/* name */ "PS", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 1e-12},
+    {/* name */ "NS", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 1e-9},
+    {/* name */ "US", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 1e-6},
+    {/* name */ "MS", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 1e-3},
+    {/* name */ "S", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 1},
+    {/* name */ "MIN", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 60},
+    {/* name */ "HR", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 3600},
 
     SCPI_UNITS_LIST_END,
 };
@@ -102,71 +104,20 @@
 /*
  * Special number values definition
  */
-const scpi_special_number_def_t scpi_special_numbers_def[] = {
-    {/* name */ "MINimum",      /* type */ SCPI_NUM_MIN},
-    {/* name */ "MAXimum",      /* type */ SCPI_NUM_MAX},
-    {/* name */ "DEFault",      /* type */ SCPI_NUM_DEF},
-    {/* name */ "UP",           /* type */ SCPI_NUM_UP},
-    {/* name */ "DOWN",         /* type */ SCPI_NUM_DOWN},
-    {/* name */ "NAN",          /* type */ SCPI_NUM_NAN},
-    {/* name */ "INFinity",     /* type */ SCPI_NUM_INF},
-    {/* name */ "NINF",         /* type */ SCPI_NUM_NINF},
-    {/* name */ "AUTO",         /* type */ SCPI_NUM_AUTO},
-    SCPI_SPECIAL_NUMBERS_LIST_END,
+const scpi_choice_def_t scpi_special_numbers_def[] = {
+    {/* name */ "MINimum", /* type */ SCPI_NUM_MIN},
+    {/* name */ "MAXimum", /* type */ SCPI_NUM_MAX},
+    {/* name */ "DEFault", /* type */ SCPI_NUM_DEF},
+    {/* name */ "UP", /* type */ SCPI_NUM_UP},
+    {/* name */ "DOWN", /* type */ SCPI_NUM_DOWN},
+    {/* name */ "NAN", /* type */ SCPI_NUM_NAN},
+    {/* name */ "INFinity", /* type */ SCPI_NUM_INF},
+    {/* name */ "NINF", /* type */ SCPI_NUM_NINF},
+    {/* name */ "AUTO", /* type */ SCPI_NUM_AUTO},
+    SCPI_CHOICE_LIST_END,
 };
 
 /**
- * Match string constant to one of special number values
- * @param specs specifications of special numbers (patterns)
- * @param str string to be recognised
- * @param len length of string
- * @param value resultin value
- * @return TRUE if str matches one of specs patterns
- */
-static scpi_bool_t translateSpecialNumber(const scpi_special_number_def_t * specs, const char * str, size_t len, scpi_number_t * value) {
-    int i;
-
-    value->value = 0.0;
-    value->unit = SCPI_UNIT_NONE;
-    value->type = SCPI_NUM_NUMBER;
-
-    if (specs == NULL) {
-        return FALSE;
-    }
-
-    for (i = 0; specs[i].name != NULL; i++) {
-        if (matchPattern(specs[i].name, strlen(specs[i].name), str, len)) {
-            value->type = specs[i].type;
-            return TRUE;
-        }
-    }
-
-    return FALSE;
-}
-
-/**
- * Convert special number type to its string representation
- * @param specs specifications of special numbers (patterns)
- * @param type type of special number
- * @return String representing special number or NULL
- */
-static const char * translateSpecialNumberInverse(const scpi_special_number_def_t * specs, scpi_special_number_t type) {
-    int i;
-
-    if (specs == NULL) {
-        return NULL;
-    }
-    
-    for (i = 0; specs[i].name != NULL; i++) {
-        if (specs[i].type == type) {
-            return specs[i].name;
-        }
-    }
-
-    return NULL;
-}
-
-/**
  * Convert string describing unit to its representation
  * @param units units patterns
  * @param unit text representation of unknown unit
@@ -175,11 +126,11 @@
  */
 static const scpi_unit_def_t * translateUnit(const scpi_unit_def_t * units, const char * unit, size_t len) {
     int i;
-    
+
     if (units == NULL) {
         return NULL;
     }
-    
+
     for (i = 0; units[i].name != NULL; i++) {
         if (compareStr(unit, len, units[i].name, strlen(units[i].name))) {
             return &units[i];
@@ -197,11 +148,11 @@
  */
 static const char * translateUnitInverse(const scpi_unit_def_t * units, const scpi_unit_t unit) {
     int i;
-    
+
     if (units == NULL) {
         return NULL;
     }
-    
+
     for (i = 0; units[i].name != NULL; i++) {
         if ((units[i].unit == unit) && (units[i].mult == 1)) {
             return units[i].name;
@@ -249,43 +200,97 @@
  * @param mandatory if the parameter is mandatory
  * @return 
  */
-scpi_bool_t SCPI_ParamNumber(scpi_t * context, scpi_number_t * value, scpi_bool_t mandatory) {
+scpi_bool_t SCPI_ParamNumber(scpi_t * context, const scpi_choice_def_t * special, scpi_number_t * value, scpi_bool_t mandatory)
+{
+    scpi_token_t token;
+    lex_state_t state;
+    scpi_parameter_t param;
     scpi_bool_t result;
-    const char * param;
-    size_t len;
-    size_t numlen;
+    int32_t tag;
 
-    /* read parameter and shift to the next one */
-    result = SCPI_ParamString(context, &param, &len, mandatory);
-
-    /* value not initializes */
     if (!value) {
+        SCPI_ErrorPush(context, SCPI_ERROR_SYSTEM_ERROR);
         return FALSE;
     }
 
-    value->type = SCPI_NUM_DEF;
+    result = SCPI_Parameter(context, &param, mandatory);
+
+    if (!result) {
+        return result;
+    }
+
+    state.buffer = param.ptr;
+    state.pos = state.buffer;
+    state.len = param.len;
 
-    /* if parameter was not found, return TRUE or FALSE according
-     * to fact that parameter was mandatory or not */
-    if (!result) {
-        return mandatory ? FALSE : TRUE;
+    switch(param.type) {
+        case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA:
+        case SCPI_TOKEN_HEXNUM:
+        case SCPI_TOKEN_OCTNUM:
+        case SCPI_TOKEN_BINNUM:
+        case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA_WITH_SUFFIX:
+        case SCPI_TOKEN_PROGRAM_MNEMONIC:
+            value->unit = SCPI_UNIT_NONE;
+            value->special = FALSE;
+            result = TRUE;
+            break;
     }
 
-    /* convert string to special number type */
-    if (translateSpecialNumber(context->special_numbers, param, len, value)) {
-        /* found special type */
-        return TRUE;
+    switch(param.type) {
+        case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA:
+        case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA_WITH_SUFFIX:
+        case SCPI_TOKEN_PROGRAM_MNEMONIC:
+            value->base = 10;
+            break;
+        case SCPI_TOKEN_BINNUM:
+            value->base = 2;
+            break;
+        case SCPI_TOKEN_HEXNUM:
+            value->base = 16;
+            break;
+        case SCPI_TOKEN_OCTNUM:
+            value->base = 8;
+            break;
     }
 
-    /* convert text from double - no special type */
-    numlen = strToDouble(param, &value->value);
+    switch(param.type) {
+        case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA:
+            SCPI_ParamToDouble(context, &param, &(value->value));
+            break;
+        case SCPI_TOKEN_HEXNUM:
+            SCPI_ParamToDouble(context, &param, &(value->value));
+            break;
+        case SCPI_TOKEN_OCTNUM:
+            SCPI_ParamToDouble(context, &param, &(value->value));
+            break;
+        case SCPI_TOKEN_BINNUM:
+            SCPI_ParamToDouble(context, &param, &(value->value));
+            break;
+        case SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA_WITH_SUFFIX:
+            scpiLex_DecimalNumericProgramData(&state, &token);
+            scpiLex_WhiteSpace(&state, &token);
+            scpiLex_SuffixProgramData(&state, &token);
 
-    /* transform units of value */
-    if (numlen <= len) {
-        return transformNumber(context, param + numlen, len - numlen, value);
+            SCPI_ParamToDouble(context, &param, &(value->value));
+
+            result = transformNumber(context, token.ptr, token.len, value);
+            break;
+        case SCPI_TOKEN_PROGRAM_MNEMONIC:
+            scpiLex_WhiteSpace(&state, &token);
+            scpiLex_CharacterProgramData(&state, &token);
+
+            /* convert string to special number type */
+            SCPI_ParamToChoice(context, &token, special, &tag);
+
+            value->special = TRUE;
+            value->tag = tag;
+
+            break;
+        default:
+            result = FALSE;
     }
-    return FALSE;
 
+    return result;
 }
 
 /**
@@ -296,7 +301,7 @@
  * @param len max length of string
  * @return number of chars written to string
  */
-size_t SCPI_NumberToStr(scpi_t * context, scpi_number_t * value, char * str, size_t len) {
+size_t SCPI_NumberToStr(scpi_t * context, const scpi_choice_def_t * special, scpi_number_t * value, char * str, size_t len) {
     const char * type;
     const char * unit;
     size_t result;
@@ -305,14 +310,17 @@
         return 0;
     }
 
-    type = translateSpecialNumberInverse(context->special_numbers, value->type);
-
-    if (type) {
-        strncpy(str, type, len);
-        return min(strlen(type), len);
+    if (value->special) {
+        if (SCPI_ChoiceToName(special, value->tag, &type)) {
+            strncpy(str, type, len);
+            return min(strlen(type), len);
+        } else {
+            str[0] = 0;
+            return 0;
+        }
     }
 
-    result = doubleToStr(value->value, str, len);
+    result = SCPI_DoubleToStr(value->value, str, len);
 
     unit = translateUnitInverse(context->units, value->unit);