From dd5c96bee3a02faf5a7c5032bd1356752cb427c2 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Wed, 30 Jul 2008 19:44:08 +0000 Subject: [PATCH] Implemented SD2-1431: C# <-> VB converter: hexadecimal literals should stay hexadecimal git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@3263 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Src/Ast/General/PrimitiveExpression.cs | 11 +-- .../Project/Src/Lexer/CSharp/Lexer.cs | 39 +++++---- .../NRefactory/Project/Src/Lexer/Token.cs | 22 ++++- .../Project/Src/Lexer/VBNet/Lexer.cs | 86 ++++++++++--------- .../Project/Src/Parser/CSharp/Parser.cs | 2 +- .../Project/Src/Parser/CSharp/cs.ATG | 2 +- .../Project/Src/Parser/VBNet/Parser.cs | 14 +-- .../Project/Src/Parser/VBNet/VBNET.ATG | 14 +-- .../CSharp/CSharpOutputVisitor.cs | 7 +- .../PrettyPrinter/VBNet/VBNetOutputVisitor.cs | 7 +- .../Test/Output/CSharp/CSharpOutputTest.cs | 8 +- .../Test/Output/VBNet/VBNetOutputTest.cs | 24 ++++-- 12 files changed, 140 insertions(+), 96 deletions(-) diff --git a/src/Libraries/NRefactory/Project/Src/Ast/General/PrimitiveExpression.cs b/src/Libraries/NRefactory/Project/Src/Ast/General/PrimitiveExpression.cs index 067b8614c1..3e7ea9e753 100644 --- a/src/Libraries/NRefactory/Project/Src/Ast/General/PrimitiveExpression.cs +++ b/src/Libraries/NRefactory/Project/Src/Ast/General/PrimitiveExpression.cs @@ -11,17 +11,10 @@ namespace ICSharpCode.NRefactory.Ast { public class PrimitiveExpression : Expression { - object val; string stringValue; - public object Value { - get { - return val; - } - set { - val = value; - } - } + public Parser.LiteralFormat LiteralFormat { get; set; } + public object Value { get; set; } public string StringValue { get { diff --git a/src/Libraries/NRefactory/Project/Src/Lexer/CSharp/Lexer.cs b/src/Libraries/NRefactory/Project/Src/Lexer/CSharp/Lexer.cs index bff50a7ede..fac9a21a65 100644 --- a/src/Libraries/NRefactory/Project/Src/Lexer/CSharp/Lexer.cs +++ b/src/Libraries/NRefactory/Project/Src/Lexer/CSharp/Lexer.cs @@ -287,28 +287,28 @@ namespace ICSharpCode.NRefactory.Parser.CSharp if (isfloat) { float num; if (float.TryParse(digit, NumberStyles.Any, CultureInfo.InvariantCulture, out num)) { - return new Token(Tokens.Literal, x, y, stringValue, num); + return new Token(Tokens.Literal, x, y, stringValue, num, LiteralFormat.DecimalNumber); } else { errors.Error(y, x, String.Format("Can't parse float {0}", digit)); - return new Token(Tokens.Literal, x, y, stringValue, 0f); + return new Token(Tokens.Literal, x, y, stringValue, 0f, LiteralFormat.DecimalNumber); } } if (isdecimal) { decimal num; if (decimal.TryParse(digit, NumberStyles.Any, CultureInfo.InvariantCulture, out num)) { - return new Token(Tokens.Literal, x, y, stringValue, num); + return new Token(Tokens.Literal, x, y, stringValue, num, LiteralFormat.DecimalNumber); } else { errors.Error(y, x, String.Format("Can't parse decimal {0}", digit)); - return new Token(Tokens.Literal, x, y, stringValue, 0m); + return new Token(Tokens.Literal, x, y, stringValue, 0m, LiteralFormat.DecimalNumber); } } if (isdouble) { double num; if (double.TryParse(digit, NumberStyles.Any, CultureInfo.InvariantCulture, out num)) { - return new Token(Tokens.Literal, x, y, stringValue, num); + return new Token(Tokens.Literal, x, y, stringValue, num, LiteralFormat.DecimalNumber); } else { errors.Error(y, x, String.Format("Can't parse double {0}", digit)); - return new Token(Tokens.Literal, x, y, stringValue, 0d); + return new Token(Tokens.Literal, x, y, stringValue, 0d, LiteralFormat.DecimalNumber); } } @@ -317,12 +317,12 @@ namespace ICSharpCode.NRefactory.Parser.CSharp if (ishex) { if (!ulong.TryParse(digit, NumberStyles.HexNumber, null, out result)) { errors.Error(y, x, String.Format("Can't parse hexadecimal constant {0}", digit)); - return new Token(Tokens.Literal, x, y, stringValue.ToString(), 0); + return new Token(Tokens.Literal, x, y, stringValue.ToString(), 0, LiteralFormat.HexadecimalNumber); } } else { if (!ulong.TryParse(digit, NumberStyles.Integer, null, out result)) { errors.Error(y, x, String.Format("Can't parse integral constant {0}", digit)); - return new Token(Tokens.Literal, x, y, stringValue.ToString(), 0); + return new Token(Tokens.Literal, x, y, stringValue.ToString(), 0, LiteralFormat.DecimalNumber); } } @@ -337,40 +337,41 @@ namespace ICSharpCode.NRefactory.Parser.CSharp Token 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 Token(Tokens.Literal, x, y, stringValue, num); + token = new Token(Tokens.Literal, x, y, stringValue, num, literalFormat); } else { errors.Error(y, x, String.Format("Can't parse unsigned long {0}", digit)); - token = new Token(Tokens.Literal, x, y, stringValue, 0UL); + token = new Token(Tokens.Literal, x, y, stringValue, 0UL, literalFormat); } } else { long num; if (long.TryParse(digit, ishex ? NumberStyles.HexNumber : NumberStyles.Number, CultureInfo.InvariantCulture, out num)) { - token = new Token(Tokens.Literal, x, y, stringValue, num); + token = new Token(Tokens.Literal, x, y, stringValue, num, literalFormat); } else { errors.Error(y, x, String.Format("Can't parse long {0}", digit)); - token = new Token(Tokens.Literal, x, y, stringValue, 0L); + token = new Token(Tokens.Literal, x, y, stringValue, 0L, literalFormat); } } } else { if (isunsigned) { uint num; if (uint.TryParse(digit, ishex ? NumberStyles.HexNumber : NumberStyles.Number, CultureInfo.InvariantCulture, out num)) { - token = new Token(Tokens.Literal, x, y, stringValue, num); + token = new Token(Tokens.Literal, x, y, stringValue, num, literalFormat); } else { errors.Error(y, x, String.Format("Can't parse unsigned int {0}", digit)); - token = new Token(Tokens.Literal, x, y, stringValue, (uint)0); + token = new Token(Tokens.Literal, x, y, stringValue, (uint)0, literalFormat); } } else { int num; if (int.TryParse(digit, ishex ? NumberStyles.HexNumber : NumberStyles.Number, CultureInfo.InvariantCulture, out num)) { - token = new Token(Tokens.Literal, x, y, stringValue, num); + token = new Token(Tokens.Literal, x, y, stringValue, num, literalFormat); } else { errors.Error(y, x, String.Format("Can't parse int {0}", digit)); - token = new Token(Tokens.Literal, x, y, stringValue, 0); + token = new Token(Tokens.Literal, x, y, stringValue, 0, literalFormat); } } } @@ -421,7 +422,7 @@ namespace ICSharpCode.NRefactory.Parser.CSharp errors.Error(y, x, String.Format("End of file reached inside string literal")); } - return new Token(Tokens.Literal, x, y, originalValue.ToString(), sb.ToString()); + return new Token(Tokens.Literal, x, y, originalValue.ToString(), sb.ToString(), LiteralFormat.StringLiteral); } Token ReadVerbatimString() @@ -456,7 +457,7 @@ namespace ICSharpCode.NRefactory.Parser.CSharp errors.Error(y, x, String.Format("End of file reached inside verbatim string literal")); } - return new Token(Tokens.Literal, x, y, originalValue.ToString(), sb.ToString()); + return new Token(Tokens.Literal, x, y, originalValue.ToString(), sb.ToString(), LiteralFormat.VerbatimStringLiteral); } char[] escapeSequenceBuffer = new char[12]; @@ -595,7 +596,7 @@ namespace ICSharpCode.NRefactory.Parser.CSharp errors.Error(y, x, String.Format("Char not terminated")); } } - return new Token(Tokens.Literal, x, y, "'" + ch + escapeSequence + "'", chValue); + return new Token(Tokens.Literal, x, y, "'" + ch + escapeSequence + "'", chValue, LiteralFormat.CharLiteral); } Token ReadOperator(char ch) diff --git a/src/Libraries/NRefactory/Project/Src/Lexer/Token.cs b/src/Libraries/NRefactory/Project/Src/Lexer/Token.cs index 632962eb6a..96b7898927 100644 --- a/src/Libraries/NRefactory/Project/Src/Lexer/Token.cs +++ b/src/Libraries/NRefactory/Project/Src/Lexer/Token.cs @@ -9,6 +9,18 @@ using System; namespace ICSharpCode.NRefactory.Parser { + public enum LiteralFormat : byte + { + None, + DecimalNumber, + HexadecimalNumber, + OctalNumber, + StringLiteral, + VerbatimStringLiteral, + CharLiteral, + DateTimeLiteral + } + public class Token { public int kind; @@ -16,6 +28,7 @@ namespace ICSharpCode.NRefactory.Parser public int col; public int line; + public LiteralFormat literalFormat; public object literalValue; public string val; public Token next; @@ -39,17 +52,22 @@ namespace ICSharpCode.NRefactory.Parser { } - public Token(int kind, int col, int line, string val) : this(kind, col, line, val, null) + public Token(int kind, int col, int line, string val) { + this.kind = kind; + this.col = col; + this.line = line; + this.val = val; } - public Token(int kind, int col, int line, string val, object literalValue) + public Token(int kind, int col, int line, string val, object literalValue, LiteralFormat literalFormat) { this.kind = kind; this.col = col; this.line = line; this.val = val; this.literalValue = literalValue; + this.literalFormat = literalFormat; } public override string ToString() diff --git a/src/Libraries/NRefactory/Project/Src/Lexer/VBNet/Lexer.cs b/src/Libraries/NRefactory/Project/Src/Lexer/VBNet/Lexer.cs index 76733a1b72..f7b991d2fb 100644 --- a/src/Libraries/NRefactory/Project/Src/Lexer/VBNet/Lexer.cs +++ b/src/Libraries/NRefactory/Project/Src/Lexer/VBNet/Lexer.cs @@ -124,7 +124,7 @@ namespace ICSharpCode.NRefactory.Parser.VB } catch (Exception e) { errors.Error(Line, Col, String.Format("Invalid date time {0}", e)); } - return new Token(Tokens.LiteralDate, x, y, s, time); + return new Token(Tokens.LiteralDate, x, y, s, time, LiteralFormat.DateTimeLiteral); } else { ReadPreprocessorDirective(); continue; @@ -217,9 +217,9 @@ namespace ICSharpCode.NRefactory.Parser.VB if (s.Length == 0) { s = "\0"; } - return new Token(Tokens.LiteralCharacter, x, y, '"' + s + "\"C", s[0]); + return new Token(Tokens.LiteralCharacter, x, y, '"' + s + "\"C", s[0], LiteralFormat.CharLiteral); } - return new Token(Tokens.LiteralString, x, y, '"' + s + '"', s); + return new Token(Tokens.LiteralString, x, y, '"' + s + '"', s, LiteralFormat.StringLiteral); } Token token = ReadOperator(ch); if (token != null) { @@ -294,7 +294,7 @@ namespace ICSharpCode.NRefactory.Parser.VB if (ch == '&') { errors.Error(Line, Col, String.Format("digit expected")); } - return new Token(Tokens.LiteralInteger, x, y, sb.ToString() ,ch - '0'); + return new Token(Tokens.LiteralInteger, x, y, sb.ToString() ,ch - '0', LiteralFormat.DecimalNumber); } if (ch == '.') { if (Char.IsDigit((char)ReaderPeek())) { @@ -334,21 +334,24 @@ namespace ICSharpCode.NRefactory.Parser.VB if (digit.Length == 0) { errors.Error(Line, Col, String.Format("digit expected")); - return new Token(Tokens.LiteralInteger, x, y, sb.ToString(), 0); + return new Token(Tokens.LiteralInteger, x, y, sb.ToString(), 0, LiteralFormat.DecimalNumber); } - if (ReaderPeek() != -1 && ("%&SILU".IndexOf(PeekUpperChar()) != -1 || ishex || isokt)) { - ch = (char)ReaderPeek(); - sb.Append(ch); - ch = Char.ToUpper(ch, CultureInfo.InvariantCulture); - bool unsigned = ch == 'U'; - if (unsigned) { - ReaderRead(); // read the U + 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); - if (ch != 'I' && ch != 'L' && ch != 'S') { - errors.Error(Line, Col, "Invalid type character: U" + ch); + 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 { @@ -360,56 +363,57 @@ namespace ICSharpCode.NRefactory.Parser.VB } if (ch == 'S') { if (unsigned) - return new Token(Tokens.LiteralInteger, x, y, sb.ToString(), (ushort)number); + 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); + 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); + 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); + 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); + 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); + 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)); + 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)); + 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)); + 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)); + 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)); + 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)); + 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)); + 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)); + 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)); + 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)); + 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); + return new Token(Tokens.LiteralInteger, x, y, sb.ToString(), 0, LiteralFormat.None); } } Token nextToken = null; // if we accedently read a 'dot' @@ -462,37 +466,37 @@ namespace ICSharpCode.NRefactory.Parser.VB try { if (issingle) { - return new Token(Tokens.LiteralSingle, x, y, sb.ToString(), Single.Parse(digit, CultureInfo.InvariantCulture)); + 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)); + 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)); + 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); + return new Token(Tokens.LiteralSingle, x, y, sb.ToString(), 0f, LiteralFormat.DecimalNumber); if (isdecimal) - return new Token(Tokens.LiteralDecimal, x, y, sb.ToString(), 0m); + 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); + 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)); + 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)); + 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); + 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); + token = new Token(Tokens.LiteralInteger, x, y, sb.ToString(), 0, LiteralFormat.DecimalNumber); } } token.next = nextToken; diff --git a/src/Libraries/NRefactory/Project/Src/Parser/CSharp/Parser.cs b/src/Libraries/NRefactory/Project/Src/Parser/CSharp/Parser.cs index b6bdb6ffcd..655ecb028b 100644 --- a/src/Libraries/NRefactory/Project/Src/Parser/CSharp/Parser.cs +++ b/src/Libraries/NRefactory/Project/Src/Parser/CSharp/Parser.cs @@ -4337,7 +4337,7 @@ out Expression pexpr) { lexer.NextToken(); #line 1816 "cs.ATG" - pexpr = new PrimitiveExpression(t.literalValue, t.val); + pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; } else if ( #line 1817 "cs.ATG" StartOfQueryExpression()) { diff --git a/src/Libraries/NRefactory/Project/Src/Parser/CSharp/cs.ATG b/src/Libraries/NRefactory/Project/Src/Parser/CSharp/cs.ATG index 5d83f73aa1..8949490295 100644 --- a/src/Libraries/NRefactory/Project/Src/Parser/CSharp/cs.ATG +++ b/src/Libraries/NRefactory/Project/Src/Parser/CSharp/cs.ATG @@ -1813,7 +1813,7 @@ PrimaryExpr "true" (.pexpr = new PrimitiveExpression(true, "true"); .) | "false" (.pexpr = new PrimitiveExpression(false, "false"); .) | "null" (.pexpr = new PrimitiveExpression(null, "null"); .) /* from literal token */ - | Literal (.pexpr = new PrimitiveExpression(t.literalValue, t.val); .) + | Literal (.pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; .) | IF (StartOfQueryExpression()) QueryExpression | IF (IdentAndDoubleColon()) diff --git a/src/Libraries/NRefactory/Project/Src/Parser/VBNet/Parser.cs b/src/Libraries/NRefactory/Project/Src/Parser/VBNet/Parser.cs index 7aeec3ac42..9abf4fa9ed 100644 --- a/src/Libraries/NRefactory/Project/Src/Parser/VBNet/Parser.cs +++ b/src/Libraries/NRefactory/Project/Src/Parser/VBNet/Parser.cs @@ -3597,49 +3597,49 @@ out Expression pexpr) { lexer.NextToken(); #line 1628 "VBNET.ATG" - pexpr = new PrimitiveExpression(t.literalValue, t.val); + pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; break; } case 4: { lexer.NextToken(); #line 1629 "VBNET.ATG" - pexpr = new PrimitiveExpression(t.literalValue, t.val); + pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; break; } case 7: { lexer.NextToken(); #line 1630 "VBNET.ATG" - pexpr = new PrimitiveExpression(t.literalValue, t.val); + pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; break; } case 6: { lexer.NextToken(); #line 1631 "VBNET.ATG" - pexpr = new PrimitiveExpression(t.literalValue, t.val); + pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; break; } case 5: { lexer.NextToken(); #line 1632 "VBNET.ATG" - pexpr = new PrimitiveExpression(t.literalValue, t.val); + pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; break; } case 9: { lexer.NextToken(); #line 1633 "VBNET.ATG" - pexpr = new PrimitiveExpression(t.literalValue, t.val); + pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; break; } case 8: { lexer.NextToken(); #line 1634 "VBNET.ATG" - pexpr = new PrimitiveExpression(t.literalValue, t.val); + pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; break; } case 174: { diff --git a/src/Libraries/NRefactory/Project/Src/Parser/VBNet/VBNET.ATG b/src/Libraries/NRefactory/Project/Src/Parser/VBNet/VBNET.ATG index d60b9539a4..7b6eb9a4eb 100644 --- a/src/Libraries/NRefactory/Project/Src/Parser/VBNet/VBNET.ATG +++ b/src/Libraries/NRefactory/Project/Src/Parser/VBNet/VBNET.ATG @@ -1625,13 +1625,13 @@ SimpleNonInvocationExpression ( ( /* 11.4.1 */ - LiteralString (.pexpr = new PrimitiveExpression(t.literalValue, t.val); .) - | LiteralCharacter (.pexpr = new PrimitiveExpression(t.literalValue, t.val); .) - | LiteralSingle (.pexpr = new PrimitiveExpression(t.literalValue, t.val); .) - | LiteralDouble (.pexpr = new PrimitiveExpression(t.literalValue, t.val); .) - | LiteralInteger (.pexpr = new PrimitiveExpression(t.literalValue, t.val); .) - | LiteralDate (.pexpr = new PrimitiveExpression(t.literalValue, t.val); .) - | LiteralDecimal (.pexpr = new PrimitiveExpression(t.literalValue, t.val); .) + LiteralString (.pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; .) + | LiteralCharacter (.pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; .) + | LiteralSingle (.pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; .) + | LiteralDouble (.pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; .) + | LiteralInteger (.pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; .) + | LiteralDate (.pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; .) + | LiteralDecimal (.pexpr = new PrimitiveExpression(t.literalValue, t.val) { LiteralFormat = t.literalFormat }; .) /* True, False and Nothing are handled as literals in the spec */ | "True" (.pexpr = new PrimitiveExpression(true, "true"); .) | "False" (.pexpr = new PrimitiveExpression(false, "false"); .) diff --git a/src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/CSharpOutputVisitor.cs b/src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/CSharpOutputVisitor.cs index 0a1d5cf427..88e1c3cd19 100644 --- a/src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/CSharpOutputVisitor.cs +++ b/src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/CSharpOutputVisitor.cs @@ -1780,7 +1780,12 @@ namespace ICSharpCode.NRefactory.PrettyPrinter } if (val is IFormattable) { - outputFormatter.PrintText(((IFormattable)val).ToString(null, NumberFormatInfo.InvariantInfo)); + if (primitiveExpression.LiteralFormat == LiteralFormat.HexadecimalNumber) { + outputFormatter.PrintText("0x"); + outputFormatter.PrintText(((IFormattable)val).ToString("x", NumberFormatInfo.InvariantInfo)); + } else { + outputFormatter.PrintText(((IFormattable)val).ToString(null, NumberFormatInfo.InvariantInfo)); + } if (val is uint || val is ulong) { outputFormatter.PrintText("u"); } diff --git a/src/Libraries/NRefactory/Project/Src/PrettyPrinter/VBNet/VBNetOutputVisitor.cs b/src/Libraries/NRefactory/Project/Src/PrettyPrinter/VBNet/VBNetOutputVisitor.cs index d3cdc07e1e..15c1324a67 100644 --- a/src/Libraries/NRefactory/Project/Src/PrettyPrinter/VBNet/VBNetOutputVisitor.cs +++ b/src/Libraries/NRefactory/Project/Src/PrettyPrinter/VBNet/VBNetOutputVisitor.cs @@ -2107,7 +2107,12 @@ namespace ICSharpCode.NRefactory.PrettyPrinter } if (val is IFormattable) { - outputFormatter.PrintText(((IFormattable)val).ToString(null, NumberFormatInfo.InvariantInfo)); + if (primitiveExpression.LiteralFormat == LiteralFormat.HexadecimalNumber) { + outputFormatter.PrintText("&H"); + outputFormatter.PrintText(((IFormattable)val).ToString("x", NumberFormatInfo.InvariantInfo)); + } else { + outputFormatter.PrintText(((IFormattable)val).ToString(null, NumberFormatInfo.InvariantInfo)); + } } else { outputFormatter.PrintText(val.ToString()); } diff --git a/src/Libraries/NRefactory/Test/Output/CSharp/CSharpOutputTest.cs b/src/Libraries/NRefactory/Test/Output/CSharp/CSharpOutputTest.cs index d0c279cc50..1f2ab35f60 100644 --- a/src/Libraries/NRefactory/Test/Output/CSharp/CSharpOutputTest.cs +++ b/src/Libraries/NRefactory/Test/Output/CSharp/CSharpOutputTest.cs @@ -239,7 +239,13 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter [Test] public void Integer() { - TestExpression("12"); + TestExpression("16"); + } + + [Test] + public void HexadecimalInteger() + { + TestExpression("0x10"); } [Test] diff --git a/src/Libraries/NRefactory/Test/Output/VBNet/VBNetOutputTest.cs b/src/Libraries/NRefactory/Test/Output/VBNet/VBNetOutputTest.cs index 9b3d560e04..b3ffc4b215 100644 --- a/src/Libraries/NRefactory/Test/Output/VBNet/VBNetOutputTest.cs +++ b/src/Libraries/NRefactory/Test/Output/VBNet/VBNetOutputTest.cs @@ -197,12 +197,6 @@ End Using"); TestExpression("Assembly = Ansi * [For] + Until - [Custom]"); } - [Test] - public void Integer() - { - TestExpression("12"); - } - [Test] public void DictionaryAccess() { @@ -416,5 +410,23 @@ End Using"); { TestProgram("Public Class Rational(Of T, O As {IRationalMath(Of T), Class})\nEnd Class"); } + + [Test] + public void Integer() + { + TestExpression("16"); + } + + [Test] + public void HexadecimalInteger() + { + TestExpression("&H10"); + } + + [Test] + public void HexadecimalMinusOne() + { + TestExpression("&Hffffffff"); + } } }