Esempio n. 1
0
        // Convert formatting and other options into a set of properties, just as if they had been
        // specified in persistence format.

        private ListDictionary GetPropertyDictionary(LiteralFormat format, bool breakAfter, String linkUrl)
        {
            ListDictionary dictionary = null;

            if (format != LiteralFormat.None || !breakAfter || linkUrl != null)
            {
                dictionary = new ListDictionary();
                if ((format & LiteralFormat.Bold) == LiteralFormat.Bold)
                {
                    dictionary.Add("Font-Bold", "True");
                }
                if ((format & LiteralFormat.Italic) == LiteralFormat.Italic)
                {
                    dictionary.Add("Font-Italic", "True");
                }
                if (!breakAfter)
                {
                    dictionary.Add("BreakAfter", "False");
                }
                if (linkUrl != null)
                {
                    dictionary.Add("NavigateUrl", linkUrl);
                }
            }

            return(dictionary);
        }
Esempio n. 2
0
 public IdentifierExpression(string Value, LiteralFormat LiteralFormat = LiteralFormat.None, LiteralSubformat Subformat = 0)
 {
     Strings.Add(Value);
     ValueStringHash = Value.GetHashCode();
     this.Format     = LiteralFormat;
     this.Subformat  = Subformat;
 }
Esempio n. 3
0
        public void Literal_WithEscapedCloseBraces_CollapsesDoubleBraces()
        {
            //arrange
            var literal = new LiteralFormat("hello}}world");
            //act
            string result = literal.Eval(null);

            //assert
            Assert.AreEqual("hello}world", result);
        }
Esempio n. 4
0
        public void Literal_WithEscapedOpenBraces_CollapsesDoubleBraces()
        {
            //arrange
            var literal = new LiteralFormat("hello{{world");
            //act
            var result = literal.Eval(null);

            //assert
            Assert.That(result, Is.EqualTo("hello{world"));
        }
Esempio n. 5
0
		public Token(int kind, Location startLocation, Location endLocation, string val, object literalValue, LiteralFormat literalFormat)
		{
			this.kind         = kind;
			this.col          = startLocation.Column;
			this.line         = startLocation.Line;
			this.endLocation = endLocation;
			this.val          = val;
			this.literalValue = literalValue;
			this.literalFormat = literalFormat;
		}
Esempio n. 6
0
 public Token(int kind, Location startLocation, Location endLocation, string val, object literalValue, LiteralFormat literalFormat)
 {
     this.kind          = kind;
     this.col           = startLocation.Column;
     this.line          = startLocation.Line;
     this.endLocation   = endLocation;
     this.val           = val;
     this.literalValue  = literalValue;
     this.literalFormat = literalFormat;
 }
Esempio n. 7
0
 public override void WritePrimitiveValue(object value, LiteralFormat format = LiteralFormat.None)
 {
     if (lastWritten == LastWritten.KeywordOrIdentifier)
     {
         Space();
     }
     base.WritePrimitiveValue(value, format);
     if (value == null || value is bool)
     {
         return;
     }
     if (value is string)
     {
         lastWritten = LastWritten.Other;
     }
     else if (value is char)
     {
         lastWritten = LastWritten.Other;
     }
     else if (value is decimal)
     {
         lastWritten = LastWritten.Other;
     }
     else if (value is float)
     {
         float f = (float)value;
         if (float.IsInfinity(f) || float.IsNaN(f))
         {
             return;
         }
         lastWritten = LastWritten.Other;
     }
     else if (value is double)
     {
         double f = (double)value;
         if (double.IsInfinity(f) || double.IsNaN(f))
         {
             return;
         }
         // needs space if identifier follows number;
         // this avoids mistaking the following identifier as type suffix
         lastWritten = LastWritten.KeywordOrIdentifier;
     }
     else if (value is IFormattable)
     {
         // needs space if identifier follows number;
         // this avoids mistaking the following identifier as type suffix
         lastWritten = LastWritten.KeywordOrIdentifier;
     }
     else
     {
         lastWritten = LastWritten.Other;
     }
 }
Esempio n. 8
0
 public IdentifierExpression(object Val, LiteralFormat LiteralFormat, LiteralSubformat Subformat = 0, string escapeString = null)
 {
     Value          = Val;
     this.Format    = LiteralFormat;
     this.Subformat = Subformat;
     if (escapeString != null)
     {
         Strings.Add(escapeString);
         EscapeStringHash = escapeString.GetHashCode();
     }
 }
Esempio n. 9
0
        public DToken(int kind, int startLocation_Col, int startLocation_Line, int endLocation_Col, int endLocation_Line,
                      object literalValue, string value, LiteralFormat literalFormat = 0, LiteralSubformat literalSubFormat = 0)
        {
            Line        = startLocation_Line;
            Location    = new CodeLocation(startLocation_Col, startLocation_Line);
            EndLocation = new CodeLocation(endLocation_Col, endLocation_Line);

            Kind          = kind;
            LiteralFormat = literalFormat;
            Subformat     = literalSubFormat;
            LiteralValue  = literalValue;
            Value         = value;
        }
Esempio n. 10
0
        public override void WritePrimitiveValue(object value, LiteralFormat format = LiteralFormat.None)
        {
            Expression node          = nodes.Peek().LastOrDefault() as Expression;
            var        startLocation = locationProvider.Location;

            base.WritePrimitiveValue(value, format);
            if (node is PrimitiveExpression)
            {
                ((PrimitiveExpression)node).SetLocation(startLocation, locationProvider.Location);
            }
            if (node is NullReferenceExpression)
            {
                ((NullReferenceExpression)node).SetStartLocation(startLocation);
            }
        }
Esempio n. 11
0
		DToken Token(byte kind, int startLocation_Col, int startLocation_Line, int tokenLength,
			object literalValue,/* string value,*/ LiteralFormat literalFormat = 0, LiteralSubformat literalSubFormat = 0)
		{
			var tk = tok();
			tk.Line = startLocation_Line;
			tk.Column = startLocation_Col;
			tk.EndLineDifference = 0;
			tk.EndColumn = unchecked(startLocation_Col + tokenLength);

			tk.Kind = kind;
			tk.LiteralFormat = literalFormat;
			tk.Subformat = literalSubFormat;
			tk.LiteralValue = literalValue;
			tk.RawCodeRepresentation = null;
			
			return tk;
		}
Esempio n. 12
0
        private void ProcessElementInternal(LiteralElement element)
        {
            // This method needs to fill in an element with formatting and
            // breaking information, and calls ProcessElement. However,
            // each element needs to know whether there will be a break
            // AFTER the element, so elements are processed lazily, keeping
            // the last one in a single-element queue.

            LiteralFormat currentFormat = _formatStack.CurrentFormat;

            if (_lastQueuedElement != null)
            {
                // If both the last and current element are text elements, and
                // the formatting hasn't changed, then just combine the two into
                // a single element.

                if (_lastQueuedElement.IsText && element.IsText &&
                    (_lastQueuedElement.Format == currentFormat) &&
                    !_beginNewParagraph)
                {
                    _lastQueuedElement.Text += element.Text;
                    return;
                }
                else if (_lastQueuedElement.IsEmptyText &&
                         !_beginNewParagraph &&
                         IgnoreWhiteSpaceElement(_lastQueuedElement))
                {
                    // Empty text element with no breaks - so just ignore.
                }
                else
                {
                    _lastQueuedElement.BreakAfter = _beginNewParagraph;
                    ProcessElement(_lastQueuedElement);
                    _elementsProcessed = true;
                }
            }

            _lastQueuedElement        = element;
            _lastQueuedElement.Format = currentFormat;
            _beginNewParagraph        = false;
        }
        public override void WritePrimitiveValue(object value, LiteralFormat literalValue = LiteralFormat.None)
        {
            HighlightingColor color = null;

            if (value is null)
            {
                color = valueKeywordColor;
            }
            if (value is true || value is false)
            {
                color = trueKeywordColor;
            }
            if (color != null)
            {
                textOutput.BeginSpan(color);
            }
            base.WritePrimitiveValue(value, literalValue);
            if (color != null)
            {
                textOutput.EndSpan();
            }
        }
Esempio n. 14
0
 public Literal(string val, object literalValue, LiteralFormat literalFormat)
 {
     this.val           = val;
     this.literalValue  = literalValue;
     this.literalFormat = literalFormat;
 }
Esempio n. 15
0
 internal Token(int kind, int x, int y, string val, object literalValue, LiteralFormat literalFormat)
     : this(kind, new Location(x, y), new Location(x + val.Length, y), val, literalValue, literalFormat)
 {
 }
Esempio n. 16
0
        public override void WritePrimitiveValue(object value, LiteralFormat format = LiteralFormat.None)
        {
            if (value == null)
            {
                // usually NullReferenceExpression should be used for this, but we'll handle it anyways
                textWriter.Write("null");
                column += 4;
                Length += 4;
                return;
            }

            if (value is bool)
            {
                if ((bool)value)
                {
                    textWriter.Write("true");
                    column += 4;
                    Length += 4;
                }
                else
                {
                    textWriter.Write("false");
                    column += 5;
                    Length += 5;
                }
                return;
            }

            if (value is string)
            {
                string tmp = ConvertString(value.ToString());
                column += tmp.Length + 2;
                Length += tmp.Length + 2;
                textWriter.Write('"');
                textWriter.Write(tmp);
                textWriter.Write('"');
            }
            else if (value is char)
            {
                string tmp = ConvertCharLiteral((char)value);
                column += tmp.Length + 2;
                Length += tmp.Length + 2;
                textWriter.Write('\'');
                textWriter.Write(tmp);
                textWriter.Write('\'');
            }
            else if (value is decimal)
            {
                string str = ((decimal)value).ToString(NumberFormatInfo.InvariantInfo) + "m";
                column += str.Length;
                Length += str.Length;
                textWriter.Write(str);
            }
            else if (value is float)
            {
                float f = (float)value;
                if (float.IsInfinity(f) || float.IsNaN(f))
                {
                    // Strictly speaking, these aren't PrimitiveExpressions;
                    // but we still support writing these to make life easier for code generators.
                    textWriter.Write("float");
                    column += 5;
                    Length += 5;
                    WriteToken(Roles.Dot, ".");
                    if (float.IsPositiveInfinity(f))
                    {
                        textWriter.Write("PositiveInfinity");
                        column += "PositiveInfinity".Length;
                        Length += "PositiveInfinity".Length;
                    }
                    else if (float.IsNegativeInfinity(f))
                    {
                        textWriter.Write("NegativeInfinity");
                        column += "NegativeInfinity".Length;
                        Length += "NegativeInfinity".Length;
                    }
                    else
                    {
                        textWriter.Write("NaN");
                        column += 3;
                        Length += 3;
                    }
                    return;
                }
                var str = f.ToString("R", NumberFormatInfo.InvariantInfo) + "f";
                if (f == 0 && 1 / f == float.NegativeInfinity && str[0] != '-')
                {
                    // negative zero is a special case
                    // (again, not a primitive expression, but it's better to handle
                    // the special case here than to do it in all code generators)
                    str = '-' + str;
                }
                column += str.Length;
                Length += str.Length;
                textWriter.Write(str);
            }
            else if (value is double)
            {
                double f = (double)value;
                if (double.IsInfinity(f) || double.IsNaN(f))
                {
                    // Strictly speaking, these aren't PrimitiveExpressions;
                    // but we still support writing these to make life easier for code generators.
                    textWriter.Write("double");
                    column += 6;
                    Length += 6;
                    WriteToken(Roles.Dot, ".");
                    if (double.IsPositiveInfinity(f))
                    {
                        textWriter.Write("PositiveInfinity");
                        column += "PositiveInfinity".Length;
                        Length += "PositiveInfinity".Length;
                    }
                    else if (double.IsNegativeInfinity(f))
                    {
                        textWriter.Write("NegativeInfinity");
                        column += "NegativeInfinity".Length;
                        Length += "NegativeInfinity".Length;
                    }
                    else
                    {
                        textWriter.Write("NaN");
                        column += 3;
                        Length += 3;
                    }
                    return;
                }
                string number = f.ToString("R", NumberFormatInfo.InvariantInfo);
                if (f == 0 && 1 / f == double.NegativeInfinity && number[0] != '-')
                {
                    // negative zero is a special case
                    // (again, not a primitive expression, but it's better to handle
                    // the special case here than to do it in all code generators)
                    number = '-' + number;
                }
                if (number.IndexOf('.') < 0 && number.IndexOf('E') < 0)
                {
                    number += ".0";
                }
                textWriter.Write(number);
                column += number.Length;
                Length += number.Length;
            }
            else if (value is IFormattable)
            {
                StringBuilder b = new StringBuilder();
                if (format == LiteralFormat.HexadecimalNumber)
                {
                    b.Append("0x");
                    b.Append(((IFormattable)value).ToString("X", NumberFormatInfo.InvariantInfo));
                }
                else
                {
                    b.Append(((IFormattable)value).ToString(null, NumberFormatInfo.InvariantInfo));
                }
                if (value is uint || value is ulong)
                {
                    b.Append("u");
                }
                if (value is long || value is ulong)
                {
                    b.Append("L");
                }
                textWriter.Write(b.ToString());
                column += b.Length;
                Length += b.Length;
            }
            else
            {
                textWriter.Write(value.ToString());
                int length = value.ToString().Length;
                column += length;
                Length += length;
            }
        }
Esempio n. 17
0
		internal Token(int kind, int x, int y, string val, object literalValue, LiteralFormat literalFormat)
			: this(kind, new Location(x, y), new Location(x + val.Length, y), val, literalValue, literalFormat)
		{
		}
Esempio n. 18
0
 public override void WritePrimitiveValue(object value, LiteralFormat format = LiteralFormat.None)
 {
     new TextWriterTokenWriter(new TextOutputWriter(output)).WritePrimitiveValue(value, format);
 }
Esempio n. 19
0
 //public IdentifierExpression() { }
 public IdentifierExpression(object Val)
 {
     Value  = Val;
     Format = LiteralFormat.None;
 }
Esempio n. 20
0
 public override void WritePrimitiveValue(object value, LiteralFormat format = LiteralFormat.None)
 {
     decoratedWriter.WritePrimitiveValue(value, format);
 }
Esempio n. 21
0
 /// <summary>
 /// Writes a primitive/literal value
 /// </summary>
 public abstract void WritePrimitiveValue(object value, LiteralFormat format = LiteralFormat.None);
Esempio n. 22
0
 public IdentifierExpression(object Val, LiteralFormat LiteralFormat, LiteralSubformat Subformat = 0)
 {
     Value          = Val;
     this.Format    = LiteralFormat;
     this.Subformat = Subformat;
 }
Esempio n. 23
0
 public override void WritePrimitiveValue(object value, LiteralFormat format = LiteralFormat.None)
 {
 }
Esempio n. 24
0
 public PrimitiveExpression(object value, LiteralFormat format)
 {
     this.Value  = value;
     this.format = format;
 }
Esempio n. 25
0
        public DToken(int kind, CodeLocation startLocation, CodeLocation endLocation, string val, object literalValue, 
			LiteralFormat literalFormat,
			LiteralSubformat Subformat=0)
        {
            this.Kind = kind;
            this.col = startLocation.Column;
            this.line = startLocation.Line;
            this.endLocation = endLocation;
            this.val = val;
            this.literalValue = literalValue;
            this.literalFormat = literalFormat;
            this.Subformat = Subformat;
        }
        Token ReadDigit(char ch, int x)
        {
            sb.Length = 0;
            sb.Append(ch);

            int    y     = Line;
            string digit = "";

            if (ch != '&')
            {
                digit += ch;
            }

            bool ishex     = false;
            bool isokt     = false;
            bool issingle  = false;
            bool isdouble  = false;
            bool isdecimal = false;

            if (ReaderPeek() == -1)
            {
                if (ch == '&')
                {
                    errors.Error(Line, Col, String.Format("digit expected"));
                }
                return(new Token(Tokens.LiteralInteger, x, y, sb.ToString(), ch - '0', LiteralFormat.DecimalNumber));
            }
            if (ch == '.')
            {
                if (Char.IsDigit((char)ReaderPeek()))
                {
                    isdouble = true; // double is default
                    if (ishex || isokt)
                    {
                        errors.Error(Line, Col, String.Format("No hexadecimal or oktadecimal floating point values allowed"));
                    }
                    while (ReaderPeek() != -1 && Char.IsDigit((char)ReaderPeek())) // read decimal digits beyond the dot
                    {
                        digit += (char)ReaderRead();
                    }
                }
            }
            else if (ch == '&' && PeekUpperChar() == 'H')
            {
                const string hex = "0123456789ABCDEF";
                sb.Append((char)ReaderRead()); // skip 'H'
                while (ReaderPeek() != -1 && hex.IndexOf(PeekUpperChar()) != -1)
                {
                    ch = (char)ReaderRead();
                    sb.Append(ch);
                    digit += Char.ToUpper(ch, CultureInfo.InvariantCulture);
                }
                ishex = true;
            }
            else if (ReaderPeek() != -1 && ch == '&' && PeekUpperChar() == 'O')
            {
                const string okt = "01234567";
                sb.Append((char)ReaderRead()); // skip 'O'
                while (ReaderPeek() != -1 && okt.IndexOf(PeekUpperChar()) != -1)
                {
                    ch = (char)ReaderRead();
                    sb.Append(ch);
                    digit += Char.ToUpper(ch, CultureInfo.InvariantCulture);
                }
                isokt = true;
            }
            else
            {
                while (ReaderPeek() != -1 && Char.IsDigit((char)ReaderPeek()))
                {
                    ch     = (char)ReaderRead();;
                    digit += ch;
                    sb.Append(ch);
                }
            }

            if (digit.Length == 0)
            {
                errors.Error(Line, Col, String.Format("digit expected"));
                return(new Token(Tokens.LiteralInteger, x, y, sb.ToString(), 0, LiteralFormat.DecimalNumber));
            }

            if (ReaderPeek() != -1 && "%&SILU".IndexOf(PeekUpperChar()) != -1 || ishex || isokt)
            {
                bool unsigned = false;
                if (ReaderPeek() != -1)
                {
                    ch = (char)ReaderPeek();
                    sb.Append(ch);
                    ch       = Char.ToUpper(ch, CultureInfo.InvariantCulture);
                    unsigned = ch == 'U';
                    if (unsigned)
                    {
                        ReaderRead(); // read the U
                        ch = (char)ReaderPeek();
                        sb.Append(ch);
                        ch = Char.ToUpper(ch, CultureInfo.InvariantCulture);
                        if (ch != 'I' && ch != 'L' && ch != 'S')
                        {
                            errors.Error(Line, Col, "Invalid type character: U" + ch);
                        }
                    }
                }
                try
                {
                    if (isokt)
                    {
                        ReaderRead();
                        ulong number = 0L;
                        for (int i = 0; i < digit.Length; ++i)
                        {
                            number = number * 8 + digit[i] - '0';
                        }
                        if (ch == 'S')
                        {
                            if (unsigned)
                            {
                                return(new Token(Tokens.LiteralInteger, x, y, sb.ToString(), (ushort)number, LiteralFormat.OctalNumber));
                            }
                            else
                            {
                                return(new Token(Tokens.LiteralInteger, x, y, sb.ToString(), (short)number, LiteralFormat.OctalNumber));
                            }
                        }
                        else if (ch == '%' || ch == 'I')
                        {
                            if (unsigned)
                            {
                                return(new Token(Tokens.LiteralInteger, x, y, sb.ToString(), (uint)number, LiteralFormat.OctalNumber));
                            }
                            else
                            {
                                return(new Token(Tokens.LiteralInteger, x, y, sb.ToString(), (int)number, LiteralFormat.OctalNumber));
                            }
                        }
                        else if (ch == '&' || ch == 'L')
                        {
                            if (unsigned)
                            {
                                return(new Token(Tokens.LiteralInteger, x, y, sb.ToString(), (ulong)number, LiteralFormat.OctalNumber));
                            }
                            else
                            {
                                return(new Token(Tokens.LiteralInteger, x, y, sb.ToString(), (long)number, LiteralFormat.OctalNumber));
                            }
                        }
                        else
                        {
                            if (number > uint.MaxValue)
                            {
                                return(new Token(Tokens.LiteralInteger, x, y, sb.ToString(), unchecked ((long)number), LiteralFormat.OctalNumber));
                            }
                            else
                            {
                                return(new Token(Tokens.LiteralInteger, x, y, sb.ToString(), unchecked ((int)number), LiteralFormat.OctalNumber));
                            }
                        }
                    }
                    LiteralFormat literalFormat = ishex ? LiteralFormat.HexadecimalNumber : LiteralFormat.DecimalNumber;
                    if (ch == 'S')
                    {
                        ReaderRead();
                        if (unsigned)
                        {
                            return(new Token(Tokens.LiteralInteger, x, y, sb.ToString(), UInt16.Parse(digit, ishex ? NumberStyles.HexNumber : NumberStyles.Number), literalFormat));
                        }
                        else
                        {
                            return(new Token(Tokens.LiteralInteger, x, y, sb.ToString(), Int16.Parse(digit, ishex ? NumberStyles.HexNumber : NumberStyles.Number), literalFormat));
                        }
                    }
                    else if (ch == '%' || ch == 'I')
                    {
                        ReaderRead();
                        if (unsigned)
                        {
                            return(new Token(Tokens.LiteralInteger, x, y, sb.ToString(), UInt32.Parse(digit, ishex ? NumberStyles.HexNumber : NumberStyles.Number), literalFormat));
                        }
                        else
                        {
                            return(new Token(Tokens.LiteralInteger, x, y, sb.ToString(), Int32.Parse(digit, ishex ? NumberStyles.HexNumber : NumberStyles.Number), literalFormat));
                        }
                    }
                    else if (ch == '&' || ch == 'L')
                    {
                        ReaderRead();
                        if (unsigned)
                        {
                            return(new Token(Tokens.LiteralInteger, x, y, sb.ToString(), UInt64.Parse(digit, ishex ? NumberStyles.HexNumber : NumberStyles.Number), literalFormat));
                        }
                        else
                        {
                            return(new Token(Tokens.LiteralInteger, x, y, sb.ToString(), Int64.Parse(digit, ishex ? NumberStyles.HexNumber : NumberStyles.Number), literalFormat));
                        }
                    }
                    else if (ishex)
                    {
                        ulong number = UInt64.Parse(digit, NumberStyles.HexNumber);
                        if (number > uint.MaxValue)
                        {
                            return(new Token(Tokens.LiteralInteger, x, y, sb.ToString(), unchecked ((long)number), literalFormat));
                        }
                        else
                        {
                            return(new Token(Tokens.LiteralInteger, x, y, sb.ToString(), unchecked ((int)number), literalFormat));
                        }
                    }
                }
                catch (OverflowException ex)
                {
                    errors.Error(Line, Col, ex.Message);
                    return(new Token(Tokens.LiteralInteger, x, y, sb.ToString(), 0, LiteralFormat.None));
                }
            }
            Token nextToken = null;               // if we accedently read a 'dot'

            if (!isdouble && ReaderPeek() == '.') // read floating point number
            {
                ReaderRead();
                if (ReaderPeek() != -1 && Char.IsDigit((char)ReaderPeek()))
                {
                    isdouble = true; // double is default
                    if (ishex || isokt)
                    {
                        errors.Error(Line, Col, String.Format("No hexadecimal or oktadecimal floating point values allowed"));
                    }
                    digit += '.';
                    while (ReaderPeek() != -1 && Char.IsDigit((char)ReaderPeek())) // read decimal digits beyond the dot
                    {
                        digit += (char)ReaderRead();
                    }
                }
                else
                {
                    nextToken = new Token(Tokens.Dot, Col - 1, Line);
                }
            }

            if (ReaderPeek() != -1 && PeekUpperChar() == 'E') // read exponent
            {
                isdouble = true;
                digit   += (char)ReaderRead();
                if (ReaderPeek() != -1 && (ReaderPeek() == '-' || ReaderPeek() == '+'))
                {
                    digit += (char)ReaderRead();
                }
                while (ReaderPeek() != -1 && Char.IsDigit((char)ReaderPeek())) // read exponent value
                {
                    digit += (char)ReaderRead();
                }
            }

            if (ReaderPeek() != -1)
            {
                switch (PeekUpperChar())
                {
                case 'R':
                case '#':
                    ReaderRead();
                    isdouble = true;
                    break;

                case 'D':
                case '@':
                    ReaderRead();
                    isdecimal = true;
                    break;

                case 'F':
                case '!':
                    ReaderRead();
                    issingle = true;
                    break;
                }
            }

            try
            {
                if (issingle)
                {
                    return(new Token(Tokens.LiteralSingle, x, y, sb.ToString(), Single.Parse(digit, CultureInfo.InvariantCulture), LiteralFormat.DecimalNumber));
                }
                if (isdecimal)
                {
                    return(new Token(Tokens.LiteralDecimal, x, y, sb.ToString(), Decimal.Parse(digit, NumberStyles.Currency | NumberStyles.AllowExponent, CultureInfo.InvariantCulture), LiteralFormat.DecimalNumber));
                }
                if (isdouble)
                {
                    return(new Token(Tokens.LiteralDouble, x, y, sb.ToString(), Double.Parse(digit, CultureInfo.InvariantCulture), LiteralFormat.DecimalNumber));
                }
            }
            catch (FormatException)
            {
                errors.Error(Line, Col, String.Format("{0} is not a parseable number", digit));
                if (issingle)
                {
                    return(new Token(Tokens.LiteralSingle, x, y, sb.ToString(), 0f, LiteralFormat.DecimalNumber));
                }
                if (isdecimal)
                {
                    return(new Token(Tokens.LiteralDecimal, x, y, sb.ToString(), 0m, LiteralFormat.DecimalNumber));
                }
                if (isdouble)
                {
                    return(new Token(Tokens.LiteralDouble, x, y, sb.ToString(), 0.0, LiteralFormat.DecimalNumber));
                }
            }
            Token token;

            try
            {
                token = new Token(Tokens.LiteralInteger, x, y, sb.ToString(), Int32.Parse(digit, ishex ? NumberStyles.HexNumber : NumberStyles.Number), ishex ? LiteralFormat.HexadecimalNumber : LiteralFormat.DecimalNumber);
            }
            catch (Exception)
            {
                try
                {
                    token = new Token(Tokens.LiteralInteger, x, y, sb.ToString(), Int64.Parse(digit, ishex ? NumberStyles.HexNumber : NumberStyles.Number), ishex ? LiteralFormat.HexadecimalNumber : LiteralFormat.DecimalNumber);
                }
                catch (FormatException)
                {
                    errors.Error(Line, Col, String.Format("{0} is not a parseable number", digit));
                    // fallback, when nothing helps :)
                    token = new Token(Tokens.LiteralInteger, x, y, sb.ToString(), 0, LiteralFormat.DecimalNumber);
                }
                catch (OverflowException)
                {
                    errors.Error(Line, Col, String.Format("{0} is too long for a integer literal", digit));
                    // fallback, when nothing helps :)
                    token = new Token(Tokens.LiteralInteger, x, y, sb.ToString(), 0, LiteralFormat.DecimalNumber);
                }
            }
            token.next = nextToken;
            return(token);
        }
Esempio n. 27
0
		DToken Token(byte kind, int startLocation_Col, int startLocation_Line, int endLocation_Col, int endLocation_Line,
			object literalValue,/* string value,*/ LiteralFormat literalFormat = 0, LiteralSubformat literalSubFormat = 0, string rawCode = null)
		{
			var tk = tok();
			tk.Line = startLocation_Line;
			tk.Column = startLocation_Col;
			
			tk.EndLineDifference = (ushort)unchecked(endLocation_Line-startLocation_Line);
			tk.EndColumn = endLocation_Col;
			
			tk.Kind = kind;
			tk.LiteralFormat = literalFormat;
			tk.Subformat = literalSubFormat;
			tk.LiteralValue = literalValue;
			tk.RawCodeRepresentation = rawCode;
			
			return tk;
		}
        // Convert formatting and other options into a set of properties, just as if they had been
        // specified in persistence format.

        private ListDictionary GetPropertyDictionary(LiteralFormat format, bool breakAfter, String linkUrl)
        {
            ListDictionary dictionary = null;
            if (format != LiteralFormat.None || !breakAfter || linkUrl != null)
            {
                dictionary = new ListDictionary();
                if ((format & LiteralFormat.Bold) == LiteralFormat.Bold)
                {
                    dictionary.Add("Font-Bold", "True");
                }
                if ((format & LiteralFormat.Italic) == LiteralFormat.Italic)
                {
                    dictionary.Add("Font-Italic", "True");
                }
                if(!breakAfter)
                {
                    dictionary.Add("BreakAfter", "False");
                }
                if (linkUrl != null)
                {
                    dictionary.Add("NavigateUrl", linkUrl);
                }
            }

            return dictionary;
        }
Esempio n. 29
0
        Literal ReadDigit(char ch, int x)
        {
            unchecked {             // prevent exception when ReaderPeek() = -1 is cast to char
                int y = Line;
                sb.Length = 0;
                sb.Append(ch);
                string prefix = null;
                string suffix = null;

                bool ishex      = false;
                bool isunsigned = false;
                bool islong     = false;
                bool isfloat    = false;
                bool isdouble   = false;
                bool isdecimal  = false;

                char peek = (char)ReaderPeek();

                if (ch == '.')
                {
                    isdouble = true;

                    while (Char.IsDigit((char)ReaderPeek()))                       // read decimal digits beyond the dot
                    {
                        sb.Append((char)ReaderRead());
                    }
                    peek = (char)ReaderPeek();
                }
                else if (ch == '0' && (peek == 'x' || peek == 'X'))
                {
                    ReaderRead();                     // skip 'x'
                    sb.Length = 0;                    // Remove '0' from 0x prefix from the stringvalue
                    while (IsHex((char)ReaderPeek()))
                    {
                        sb.Append((char)ReaderRead());
                    }
                    if (sb.Length == 0)
                    {
                        sb.Append('0');                         // dummy value to prevent exception
                        Error(y, x, "Invalid hexadecimal integer literal");
                    }
                    ishex  = true;
                    prefix = "0x";
                    peek   = (char)ReaderPeek();
                }
                else
                {
                    while (Char.IsDigit((char)ReaderPeek()))
                    {
                        sb.Append((char)ReaderRead());
                    }
                    peek = (char)ReaderPeek();
                }

                Literal nextToken = null;          // if we accidently read a 'dot'
                if (peek == '.')                   // read floating point number
                {
                    ReaderRead();
                    peek = (char)ReaderPeek();
                    if (!Char.IsDigit(peek))
                    {
                        nextToken = new Literal(null, null, LiteralFormat.None);
                        peek      = '.';
                    }
                    else
                    {
                        isdouble = true;                         // double is default
                        if (ishex)
                        {
                            Error(y, x, "No hexadecimal floating point values allowed");
                        }
                        sb.Append('.');

                        while (Char.IsDigit((char)ReaderPeek()))                           // read decimal digits beyond the dot
                        {
                            sb.Append((char)ReaderRead());
                        }
                        peek = (char)ReaderPeek();
                    }
                }

                if (peek == 'e' || peek == 'E')                   // read exponent
                {
                    isdouble = true;
                    sb.Append((char)ReaderRead());
                    peek = (char)ReaderPeek();
                    if (peek == '-' || peek == '+')
                    {
                        sb.Append((char)ReaderRead());
                    }
                    while (Char.IsDigit((char)ReaderPeek()))                       // read exponent value
                    {
                        sb.Append((char)ReaderRead());
                    }
                    isunsigned = true;
                    peek       = (char)ReaderPeek();
                }

                if (peek == 'f' || peek == 'F')                   // float value
                {
                    ReaderRead();
                    suffix  = "f";
                    isfloat = true;
                }
                else if (peek == 'd' || peek == 'D')                     // double type suffix (obsolete, double is default)
                {
                    ReaderRead();
                    suffix   = "d";
                    isdouble = true;
                }
                else if (peek == 'm' || peek == 'M')                     // decimal value
                {
                    ReaderRead();
                    suffix    = "m";
                    isdecimal = true;
                }
                else if (!isdouble)
                {
                    if (peek == 'u' || peek == 'U')
                    {
                        ReaderRead();
                        suffix     = "u";
                        isunsigned = true;
                        peek       = (char)ReaderPeek();
                    }

                    if (peek == 'l' || peek == 'L')
                    {
                        ReaderRead();
                        peek   = (char)ReaderPeek();
                        islong = true;
                        if (!isunsigned && (peek == 'u' || peek == 'U'))
                        {
                            ReaderRead();
                            suffix     = "Lu";
                            isunsigned = true;
                        }
                        else
                        {
                            suffix = isunsigned ? "uL" : "L";
                        }
                    }
                }

                string digit       = sb.ToString();
                string stringValue = prefix + digit + suffix;

                if (isfloat)
                {
                    float num;
                    if (float.TryParse(digit, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
                    {
                        return(new Literal(stringValue, num, LiteralFormat.DecimalNumber));
                    }
                    else
                    {
                        Error(y, x, String.Format("Can't parse float {0}", digit));
                        return(new Literal(stringValue, 0f, LiteralFormat.DecimalNumber));
                    }
                }
                if (isdecimal)
                {
                    decimal num;
                    if (decimal.TryParse(digit, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
                    {
                        return(new Literal(stringValue, num, LiteralFormat.DecimalNumber));
                    }
                    else
                    {
                        Error(y, x, String.Format("Can't parse decimal {0}", digit));
                        return(new Literal(stringValue, 0m, LiteralFormat.DecimalNumber));
                    }
                }
                if (isdouble)
                {
                    double num;
                    if (double.TryParse(digit, NumberStyles.Any, CultureInfo.InvariantCulture, out num))
                    {
                        return(new Literal(stringValue, num, LiteralFormat.DecimalNumber));
                    }
                    else
                    {
                        Error(y, x, String.Format("Can't parse double {0}", digit));
                        return(new Literal(stringValue, 0d, LiteralFormat.DecimalNumber));
                    }
                }

                // Try to determine a parsable value using ranges.
                ulong result;
                if (ishex)
                {
                    if (!ulong.TryParse(digit, NumberStyles.HexNumber, null, out result))
                    {
                        Error(y, x, String.Format("Can't parse hexadecimal constant {0}", digit));
                        return(new Literal(stringValue.ToString(), 0, LiteralFormat.HexadecimalNumber));
                    }
                }
                else
                {
                    if (!ulong.TryParse(digit, NumberStyles.Integer, null, out result))
                    {
                        Error(y, x, String.Format("Can't parse integral constant {0}", digit));
                        return(new Literal(stringValue.ToString(), 0, LiteralFormat.DecimalNumber));
                    }
                }

                if (result > long.MaxValue)
                {
                    islong     = true;
                    isunsigned = true;
                }
                else if (result > uint.MaxValue)
                {
                    islong = true;
                }
                else if (islong == false && result > int.MaxValue)
                {
                    isunsigned = true;
                }

                Literal token;

                LiteralFormat literalFormat = ishex ? LiteralFormat.HexadecimalNumber : LiteralFormat.DecimalNumber;
                if (islong)
                {
                    if (isunsigned)
                    {
                        ulong num;
                        if (ulong.TryParse(digit, ishex ? NumberStyles.HexNumber : NumberStyles.Number, CultureInfo.InvariantCulture, out num))
                        {
                            token = new Literal(stringValue, num, literalFormat);
                        }
                        else
                        {
                            Error(y, x, String.Format("Can't parse unsigned long {0}", digit));
                            token = new Literal(stringValue, 0UL, literalFormat);
                        }
                    }
                    else
                    {
                        long num;
                        if (long.TryParse(digit, ishex ? NumberStyles.HexNumber : NumberStyles.Number, CultureInfo.InvariantCulture, out num))
                        {
                            token = new Literal(stringValue, num, literalFormat);
                        }
                        else
                        {
                            Error(y, x, String.Format("Can't parse long {0}", digit));
                            token = new Literal(stringValue, 0L, literalFormat);
                        }
                    }
                }
                else
                {
                    if (isunsigned)
                    {
                        uint num;
                        if (uint.TryParse(digit, ishex ? NumberStyles.HexNumber : NumberStyles.Number, CultureInfo.InvariantCulture, out num))
                        {
                            token = new Literal(stringValue, num, literalFormat);
                        }
                        else
                        {
                            Error(y, x, String.Format("Can't parse unsigned int {0}", digit));
                            token = new Literal(stringValue, (uint)0, literalFormat);
                        }
                    }
                    else
                    {
                        int num;
                        if (int.TryParse(digit, ishex ? NumberStyles.HexNumber : NumberStyles.Number, CultureInfo.InvariantCulture, out num))
                        {
                            token = new Literal(stringValue, num, literalFormat);
                        }
                        else
                        {
                            Error(y, x, String.Format("Can't parse int {0}", digit));
                            token = new Literal(stringValue, 0, literalFormat);
                        }
                    }
                }
                token.next = nextToken;
                return(token);
            }
        }
Esempio n. 30
0
 public DToken(int kind, int x, int y, string val, object literalValue, LiteralFormat literalFormat, LiteralSubformat Subformat=0)
     : this(kind, new CodeLocation(x, y), new CodeLocation(x + val.Length, y), val, literalValue, literalFormat, Subformat)
 {
 }