public override void ExitLiteralValue([NotNull] XSharpParser.LiteralValueContext context)
 {
     if (context.Token.Type == XSharpLexer.INCOMPLETE_STRING_CONST)
     {
         var    err    = ErrorCode.ERR_UnterminatedStringLit;
         IToken anchor = context.Stop;
         if (anchor == null)
         {
             anchor = context.Start;
         }
         var errdata = new ParseErrorData(anchor, err);
         _parseErrors.Add(errdata);
     }
     else if (context.Token.Type == XSharpLexer.INVALID_NUMBER)
     {
         var    err    = ErrorCode.ERR_InvalidNumber;
         IToken anchor = context.Stop;
         if (anchor == null)
         {
             anchor = context.Start;
         }
         var errdata = new ParseErrorData(anchor, err);
         _parseErrors.Add(errdata);
     }
 }
        protected String buildLiteralValue(XSharpParser.LiteralValueContext context)
        {
            string value = "";

            //
            switch (context.Token.Type)
            {
            case XSharpParser.BIN_CONST:
            case XSharpParser.HEX_CONST:
            case XSharpParser.INT_CONST:
                value = "Int";
                break;

            case XSharpParser.REAL_CONST:
                value = "Double";     // or Real8 ??
                break;

            case XSharpParser.TRUE_CONST:
            case XSharpParser.FALSE_CONST:
                value = "Logic";
                break;

            case XSharpParser.STRING_CONST:
            case XSharpParser.ESCAPED_STRING_CONST:
                value = "String";
                break;

            case XSharpParser.CHAR_CONST:
                break;

            case XSharpParser.NULL:
                break;

            case XSharpParser.NIL:
            case XSharpParser.NULL_ARRAY:
            case XSharpParser.NULL_CODEBLOCK:
            case XSharpParser.NULL_DATE:
            case XSharpParser.NULL_OBJECT:
            case XSharpParser.NULL_PSZ:
            case XSharpParser.NULL_PTR:
            case XSharpParser.NULL_STRING:
            case XSharpParser.NULL_SYMBOL:
            case XSharpParser.SYMBOL_CONST:
            case XSharpParser.DATE_CONST:
            default:
                break;
            }
            return(value);
        }
        /// <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>
        protected object GetNumericValue(XSharpParser.LiteralValueContext context)
        {
            Object ret   = null;
            string value = context.Token.Text;
            var    type  = context.Token.Type;

            // try to check if this literal has a Prefix
            try
            {
                if (context.Parent.Parent is XSharpParser.PrimaryExpressionContext)
                {
                    var prim = context.Parent.Parent as XSharpParser.PrimaryExpressionContext;
                    if (prim.Parent is XSharpParser.PrefixExpressionContext)
                    {
                        var pref = prim.Parent as XSharpParser.PrefixExpressionContext;
                        if (pref.MINUS() != null)
                        {
                            value = "-" + value;
                        }
                    }
                }
            }
            catch
            {
                // eat the troubles and ignore please...
            }
            //
            if (type == XSharpParser.BIN_CONST || type == XSharpParser.INT_CONST || type == XSharpParser.HEX_CONST)
            {
                bool isUnsigned = value.EndsWith("u", StringComparison.OrdinalIgnoreCase);
                bool isSigned   = value.EndsWith("l", StringComparison.OrdinalIgnoreCase);
                if (isSigned || isUnsigned)
                {
                    value = value.Substring(0, value.Length - 1);
                }
                // -1 for Unsigned; -2 for 0x or 0b
                int len = value.Length - (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 (type == XSharpParser.HEX_CONST)
                {
                    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;
                if (value.EndsWith("m", StringComparison.OrdinalIgnoreCase) ||
                    value.EndsWith("r", StringComparison.OrdinalIgnoreCase) ||
                    value.EndsWith("d", StringComparison.OrdinalIgnoreCase))
                {
                    value = value.Substring(0, value.Length - 1);
                }
                try
                {
                    d = double.Parse(value, System.Globalization.CultureInfo.InvariantCulture);
                }
                catch (Exception)
                {
                    d = Double.NaN;
                }
                ret = d;
            }
            return(ret);
        }
        protected CodeExpression BuildLiteralValue(XSharpParser.LiteralValueContext context)
        {
            CodeExpression expr = null;
            string         value;

            //
            switch (context.Token.Type)
            {
            case XSharpParser.BIN_CONST:
            case XSharpParser.INT_CONST:
            case XSharpParser.HEX_CONST:
            case XSharpParser.REAL_CONST:
                expr = new CodePrimitiveExpression(GetNumericValue(context));
                break;

            case XSharpParser.TRUE_CONST:
                expr = new CodePrimitiveExpression(true);
                break;

            case XSharpParser.FALSE_CONST:
                expr = new CodePrimitiveExpression(false);
                break;

            case XSharpParser.STRING_CONST:
            case XSharpParser.ESCAPED_STRING_CONST:
                value = context.GetText();
                value = value.Substring(1, value.Length - 2);
                if (context.Token.Type == XSharpParser.ESCAPED_STRING_CONST)
                {
                    value = BuildUnEscapedString(value);
                }
                expr = new CodePrimitiveExpression(value);
                break;

            case XSharpParser.CHAR_CONST:
                value = context.GetText();
                value = value.Substring(1, value.Length - 2);
                if (value.Length >= 1)
                {
                    expr = new CodePrimitiveExpression(value[0]);
                }
                break;

            case XSharpParser.NULL:
                expr = new CodePrimitiveExpression(null);
                break;

            case XSharpParser.NIL:
            case XSharpParser.NULL_ARRAY:
            case XSharpParser.NULL_CODEBLOCK:
            case XSharpParser.NULL_DATE:
            case XSharpParser.NULL_OBJECT:
            case XSharpParser.NULL_PSZ:
            case XSharpParser.NULL_PTR:
            case XSharpParser.NULL_STRING:
            case XSharpParser.NULL_SYMBOL:
            case XSharpParser.SYMBOL_CONST:
            case XSharpParser.DATE_CONST:
            default:
                expr = BuildSnippetExpression(context.Token.Text);
                break;
            }
            return(expr);
        }