Esempio n. 1
0
 /// <summary>
 /// Get a LiteralValueContext containing a BIN_CONST, INT_CONST, HEX_CONST, or a REAL_CONST
 /// as a String, and convert it to the "real" value, with the corresponding Type.
 /// </summary>
 /// <param name="context"></param>
 /// <returns>An Object of the needed Type, with the value</returns>
 private object GetNumericValue(XSharpParser.LiteralValueContext context)
 {
     Object ret = null;
     String value = context.GetText();
     //
     if (context.BIN_CONST() != null || context.INT_CONST() != null || context.HEX_CONST() != null)
     {
         bool isUnsigned = (value.Substring(0, 1).ToLower().CompareTo("u") == 0);
         // -1 for Unsigned; -2 for 0x or 0b
         int len = value.Length - (isUnsigned ? 1 : 0) - (context.BIN_CONST() != null || context.HEX_CONST() != null ? 2 : 0);
         //
         if (context.BIN_CONST() != null)
         {
             if (len > 64)
             {
                 ret = Double.NaN;
             }
             else
             {
                 // Don't forget to remove the prefix !!!
                 value = value.Substring(2);
                 // BIN are always unsigned (??)
                 UInt64 bin64;
                 try
                 {
                     bin64 = Convert.ToUInt64(value, 2);
                     // Maybe 32 bits is enough ?
                     if (bin64 <= UInt32.MaxValue)
                     {
                         UInt32 bin32 = Convert.ToUInt32(bin64);
                         ret = bin32;
                     }
                     else
                     {
                         ret = bin64;
                     }
                 }
                 catch
                 {
                     ret = Double.NaN;
                 }
             }
         }
         else if (context.HEX_CONST() != null)
         {
             if (len > 16)
             {
                 ret = Double.NaN;
             }
             else
             {
                 // Don't forget to remove the prefix !!!
                 value = value.Substring(2);
                 // HEX are always unsigned (??)
                 UInt64 hex64;
                 try
                 {
                     hex64 = Convert.ToUInt64(value, 16);
                     // Maybe 32 bits is enough ?
                     if (hex64 <= UInt32.MaxValue)
                     {
                         UInt32 hex32 = Convert.ToUInt32(hex64);
                         ret = hex32;
                     }
                     else
                     {
                         ret = hex64;
                     }
                 }
                 catch
                 {
                     ret = Double.NaN;
                 }
             }
         }
         else
         {
             // context.INT_CONST() != null
             if (len > 64)
             {
                 ret = Double.NaN;
             }
             else if (isUnsigned)
             {
                 UInt64 myUInt64;
                 try
                 {
                     myUInt64 = Convert.ToUInt64(value, 10);
                     // Maybe 32 bits is enough ?
                     if (myUInt64 <= UInt32.MaxValue)
                     {
                         UInt32 myUInt32 = Convert.ToUInt32(myUInt64);
                         ret = myUInt32;
                     }
                     else
                     {
                         ret = myUInt64;
                     }
                 }
                 catch
                 {
                     ret = Double.NaN;
                 }
             }
             else
             {
                 Int64 myInt64;
                 try
                 {
                     myInt64 = Convert.ToInt64(value, 10);
                     // Maybe 32 bits is enough ?
                     if ((myInt64 >= UInt32.MinValue) && (myInt64 <= UInt32.MaxValue))
                     {
                         Int32 myInt32 = Convert.ToInt32(myInt64);
                         ret = myInt32;
                     }
                     else
                     {
                         ret = myInt64;
                     }
                 }
                 catch
                 {
                     ret = Double.NaN;
                 }
             }
         }
     }
     else
     {
         double d;
         // Should be REAL_CONST
         if (!double.TryParse(value, out d))
         {
             d = double.NaN;
         }
         ret = d;
     }
     return ret;
 }
Esempio n. 2
0
 private CodeExpression BuildLiteralValue(XSharpParser.LiteralValueContext context)
 {
     CodeExpression expr = null;
     ITerminalNode node;
     //
     node = context.BIN_CONST();
     if (node != null)
     {
         expr = new CodePrimitiveExpression(GetNumericValue(context));
     }
     node = context.INT_CONST();
     if (node != null)
     {
         expr = new CodePrimitiveExpression(GetNumericValue(context));
     }
     //
     node = context.HEX_CONST();
     if (node != null)
     {
         expr = new CodePrimitiveExpression(GetNumericValue(context));
     }
     //
     node = context.REAL_CONST();
     if (node != null)
     {
         expr = new CodePrimitiveExpression(GetNumericValue(context));
     }
     //
     node = context.TRUE_CONST();
     if (node != null)
     {
         expr = new CodePrimitiveExpression(true);
     }
     //
     node = context.FALSE_CONST();
     if (node != null)
     {
         expr = new CodePrimitiveExpression(false);
     }
     //
     node = context.STRING_CONST();
     if (node != null)
     {
         // Remove the quotes
         String value = context.GetText();
         value = value.Substring(1, value.Length - 2);
         expr = new CodePrimitiveExpression(value);
     }
     //
     node = context.ESCAPED_STRING_CONST();
     if (node != null)
     {
         // Remove the e in front of quotes, AND the Quotes
         String value = context.GetText();
         value = value.Substring(1);
         value = value.Substring(1, value.Length - 2);
         expr = new CodePrimitiveExpression(BuildUnEscapedString(value));
     }
     //
     node = context.CHAR_CONST();
     if (node != null)
     {
         // Remove the quotes
         String value = context.GetText();
         value = value.Substring(1, value.Length - 2);
         if (value.Length >= 1)
             expr = new CodePrimitiveExpression(value[0]);
     }
     //
     node = context.NIL();
     if (node != null)
     {
         expr = new CodeSnippetExpression("NIL");
     }
     //
     node = context.NULL();
     if (node != null)
     {
         expr = new CodePrimitiveExpression(null);
     }
     //
     node = context.NULL_ARRAY();
     if (node != null)
     {
         expr = new CodeSnippetExpression("NULL_ARRAY");
     }
     //
     node = context.NULL_CODEBLOCK();
     if (node != null)
     {
         expr = new CodeSnippetExpression("NULL_CODEBLOCK");
     }
     //
     node = context.NULL_DATE();
     if (node != null)
     {
         expr = new CodeSnippetExpression("NULL_DATE");
     }
     //
     node = context.NULL_OBJECT();
     if (node != null)
     {
         expr = new CodeSnippetExpression("NULL_OBJECT");
     }
     //            
     node = context.NULL_PSZ();
     if (node != null)
     {
         expr = new CodeSnippetExpression("NULL_PSZ");
     }
     //            
     node = context.NULL_PTR();
     if (node != null)
     {
         expr = new CodeSnippetExpression("NULL_PTR");
     }
     //            
     node = context.NULL_STRING();
     if (node != null)
     {
         expr = new CodeSnippetExpression("NULL_STRING");
     }
     //            
     node = context.NULL_SYMBOL();
     if (node != null)
     {
         expr = new CodeSnippetExpression("NULL_SYMBOL");
     }
     //
     node = context.SYMBOL_CONST();
     if (node != null)
     {
         expr = new CodeSnippetExpression(context.GetText().ToUpper());
     }
     //
     node = context.DATE_CONST();
     if (node != null)
     {
         expr = new CodeSnippetExpression(context.GetText());
     }
     //
     if ( expr == null )
     {
         expr = new CodeSnippetExpression(context.GetText());
     }
     return expr;
 }