Browse Source

finished VBNetOutputVisitor for XML Literals

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/vbnet@6021 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
pull/1/head
Siegfried Pammer 16 years ago
parent
commit
bb74407cfc
  1. 2
      src/Libraries/NRefactory/NRefactoryASTGenerator/AST/Expressions.cs
  2. 21
      src/Libraries/NRefactory/Project/Src/Ast/Generated.cs
  3. 6
      src/Libraries/NRefactory/Project/Src/Parser/VBNet/Parser.cs
  4. 6
      src/Libraries/NRefactory/Project/Src/Parser/VBNet/VBNET.ATG
  5. 132
      src/Libraries/NRefactory/Project/Src/PrettyPrinter/VBNet/VBNetOutputVisitor.cs
  6. 2
      src/Libraries/NRefactory/Project/Src/Visitors/AbstractASTVisitor.cs
  7. 11
      src/Libraries/NRefactory/Project/Src/Visitors/AbstractAstTransformer.cs
  8. 79
      src/Libraries/NRefactory/Test/Output/VBNet/VBNetOutputTest.cs

2
src/Libraries/NRefactory/NRefactoryASTGenerator/AST/Expressions.cs

@ -422,6 +422,7 @@ namespace NRefactoryASTGenerator.Ast @@ -422,6 +422,7 @@ namespace NRefactoryASTGenerator.Ast
[IncludeBoolProperty("IsExpression", "return !content.IsNull;")]
[IncludeBoolProperty("NameIsExpression", "return !nameExpression.IsNull;")]
[HasChildren]
class XmlElementExpression : XmlExpression {
Expression content;
Expression nameExpression;
@ -433,6 +434,7 @@ namespace NRefactoryASTGenerator.Ast @@ -433,6 +434,7 @@ namespace NRefactoryASTGenerator.Ast
class XmlAttribute : XmlExpression {
string name;
string literalValue;
bool useDoubleQuotes;
Expression expressionValue;
}
}

21
src/Libraries/NRefactory/Project/Src/Ast/Generated.cs

@ -5464,6 +5464,8 @@ public UsingDeclaration(string @namespace, TypeReference alias) { usings = new L @@ -5464,6 +5464,8 @@ public UsingDeclaration(string @namespace, TypeReference alias) { usings = new L
string literalValue;
bool useDoubleQuotes;
Expression expressionValue;
public string Name {
@ -5484,6 +5486,15 @@ public UsingDeclaration(string @namespace, TypeReference alias) { usings = new L @@ -5484,6 +5486,15 @@ public UsingDeclaration(string @namespace, TypeReference alias) { usings = new L
}
}
public bool UseDoubleQuotes {
get {
return useDoubleQuotes;
}
set {
useDoubleQuotes = value;
}
}
public Expression ExpressionValue {
get {
return expressionValue;
@ -5511,7 +5522,7 @@ public UsingDeclaration(string @namespace, TypeReference alias) { usings = new L @@ -5511,7 +5522,7 @@ public UsingDeclaration(string @namespace, TypeReference alias) { usings = new L
}
public override string ToString() {
return string.Format("[XmlAttribute Name={0} LiteralValue={1} ExpressionValue={2}]", Name, LiteralValue, ExpressionValue);
return string.Format("[XmlAttribute Name={0} LiteralValue={1} UseDoubleQuotes={2} ExpressionValue={3}]", Name, LiteralValue, UseDoubleQuotes, ExpressionValue);
}
}
@ -5608,15 +5619,15 @@ public UsingDeclaration(string @namespace, TypeReference alias) { usings = new L @@ -5608,15 +5619,15 @@ public UsingDeclaration(string @namespace, TypeReference alias) { usings = new L
attributes = new List<XmlExpression>();
}
public bool IsExpression {
public bool NameIsExpression {
get {
return !content.IsNull;
return !nameExpression.IsNull;
}
}
public bool NameIsExpression {
public bool IsExpression {
get {
return !nameExpression.IsNull;
return !content.IsNull;
}
}

6
src/Libraries/NRefactory/Project/Src/Parser/VBNet/Parser.cs

@ -4757,12 +4757,12 @@ List<XmlExpression> attrs) { @@ -4757,12 +4757,12 @@ List<XmlExpression> attrs) {
Expect(20);
#line 1839 "VBNET.ATG"
string literalValue = null; Expression expressionValue = null;
string literalValue = null; Expression expressionValue = null; bool useDoubleQuotes = false;
if (la.kind == 3) {
lexer.NextToken();
#line 1840 "VBNET.ATG"
literalValue = t.literalValue.ToString();
literalValue = t.literalValue.ToString(); useDoubleQuotes = t.val[0] == '"';
} else if (la.kind == 12) {
lexer.NextToken();
Expr(
@ -4772,7 +4772,7 @@ out expressionValue); @@ -4772,7 +4772,7 @@ out expressionValue);
} else SynErr(290);
#line 1841 "VBNET.ATG"
attrs.Add(new XmlAttribute() { Name = name, ExpressionValue = expressionValue, LiteralValue = literalValue, StartLocation = start, EndLocation = t.EndLocation });
attrs.Add(new XmlAttribute() { Name = name, ExpressionValue = expressionValue, LiteralValue = literalValue, UseDoubleQuotes = useDoubleQuotes, StartLocation = start, EndLocation = t.EndLocation });
} else if (la.kind == 12) {
lexer.NextToken();

6
src/Libraries/NRefactory/Project/Src/Parser/VBNet/VBNET.ATG

@ -1836,9 +1836,9 @@ XmlAttribute<List<XmlExpression> attrs> @@ -1836,9 +1836,9 @@ XmlAttribute<List<XmlExpression> attrs>
(. Location start = la.Location; .)
=
Identifier (. string name = t.val; .) "="
(. string literalValue = null; Expression expressionValue = null; .)
( LiteralString (. literalValue = t.literalValue.ToString(); .) | XmlStartInlineVB Expr<out expressionValue> XmlEndInlineVB )
(. attrs.Add(new XmlAttribute() { Name = name, ExpressionValue = expressionValue, LiteralValue = literalValue, StartLocation = start, EndLocation = t.EndLocation }); .)
(. string literalValue = null; Expression expressionValue = null; bool useDoubleQuotes = false; .)
( LiteralString (. literalValue = t.literalValue.ToString(); useDoubleQuotes = t.val[0] == '"'; .) | XmlStartInlineVB Expr<out expressionValue> XmlEndInlineVB )
(. attrs.Add(new XmlAttribute() { Name = name, ExpressionValue = expressionValue, LiteralValue = literalValue, UseDoubleQuotes = useDoubleQuotes, StartLocation = start, EndLocation = t.EndLocation }); .)
|
XmlStartInlineVB (. Expression innerExpression; .) Expr<out innerExpression> XmlEndInlineVB
(. attrs.Add(new XmlEmbeddedExpression() { InlineVBExpression = innerExpression, StartLocation = start, EndLocation = t.EndLocation }); .)

132
src/Libraries/NRefactory/Project/Src/PrettyPrinter/VBNet/VBNetOutputVisitor.cs

@ -2998,21 +2998,26 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -2998,21 +2998,26 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
outputFormatter.PrintToken(Tokens.Function);
}
public override object TrackedVisitQueryExpression(QueryExpression queryExpression, object data)
public override object TrackedVisitQueryExpressionVB(QueryExpressionVB queryExpression, object data)
{
outputFormatter.IndentationLevel++;
queryExpression.MiddleClauses.ForEach(PrintClause);
for (int i = 0; i < queryExpression.Clauses.Count; i++) {
QueryExpressionClause clause = queryExpression.Clauses[i];
if (!clause.IsNull) {
if (i != 0) {
outputFormatter.PrintLineContinuation();
outputFormatter.Indent();
}
clause.AcceptVisitor(this, null);
}
}
outputFormatter.IndentationLevel--;
return null;
}
void PrintClause(QueryExpressionClause clause)
{
if (!clause.IsNull) {
outputFormatter.PrintLineContinuation();
outputFormatter.Indent();
clause.AcceptVisitor(this, null);
}
}
public override object TrackedVisitQueryExpressionFromClause(QueryExpressionFromClause fromClause, object data)
@ -3111,15 +3116,19 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -3111,15 +3116,19 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
return null;
}
public override object TrackedVisitQueryExpressionSelectClause(QueryExpressionSelectClause selectClause, object data)
public override object TrackedVisitQueryExpressionSelectVBClause(QueryExpressionSelectVBClause selectClause, object data)
{
outputFormatter.PrintToken(Tokens.Select);
outputFormatter.Space();
return selectClause.Projection.AcceptVisitor(this, data);
foreach (ExpressionRangeVariable var in selectClause.Variables) {
var.AcceptVisitor(this, data);
}
return null;
}
public override object TrackedVisitQueryExpressionWhereClause(QueryExpressionWhereClause whereClause, object data)
{
outputFormatter.Space();
outputFormatter.PrintText("Where");
outputFormatter.Space();
return whereClause.Condition.AcceptVisitor(this, data);
@ -3130,5 +3139,110 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -3130,5 +3139,110 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
UnsupportedNode(externAliasDirective);
return null;
}
public override object TrackedVisitXmlContentExpression(XmlContentExpression xmlContentExpression, object data)
{
switch (xmlContentExpression.Type) {
case XmlContentType.Comment:
outputFormatter.PrintText("<!--" + xmlContentExpression.Content + "-->");
break;
case XmlContentType.Text:
outputFormatter.PrintText(xmlContentExpression.Content);
break;
case XmlContentType.CData:
outputFormatter.PrintText("<![CDATA[" + xmlContentExpression.Content + "]]>");
break;
case XmlContentType.ProcessingInstruction:
outputFormatter.PrintText("<?" + xmlContentExpression.Content + "?>");
break;
default:
throw new Exception("Invalid value for XmlContentType");
}
return null;
}
public override object TrackedVisitXmlEmbeddedExpression(XmlEmbeddedExpression xmlEmbeddedExpression, object data)
{
outputFormatter.PrintText("<%=");
outputFormatter.Space();
xmlEmbeddedExpression.InlineVBExpression.AcceptVisitor(this, data);
outputFormatter.Space();
outputFormatter.PrintText("%>");
return null;
}
public override object TrackedVisitXmlAttribute(XmlAttribute xmlAttribute, object data)
{
outputFormatter.PrintText(xmlAttribute.Name);
outputFormatter.PrintToken(Tokens.Assign);
if (xmlAttribute.IsLiteralValue) {
if (xmlAttribute.UseDoubleQuotes)
outputFormatter.PrintText("\"");
else
outputFormatter.PrintText("'");
outputFormatter.PrintText(xmlAttribute.LiteralValue);
if (xmlAttribute.UseDoubleQuotes)
outputFormatter.PrintText("\"");
else
outputFormatter.PrintText("'");
} else
xmlAttribute.ExpressionValue.AcceptVisitor(this, data);
return null;
}
public override object TrackedVisitXmlElementExpression(XmlElementExpression xmlElementExpression, object data)
{
outputFormatter.PrintText("<");
if (xmlElementExpression.NameIsExpression) {
outputFormatter.PrintToken(Tokens.XmlStartInlineVB);
outputFormatter.Space();
xmlElementExpression.NameExpression.AcceptVisitor(this, data);
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.XmlEndInlineVB);
} else {
outputFormatter.PrintText(xmlElementExpression.XmlName);
}
foreach (XmlExpression attribute in xmlElementExpression.Attributes) {
outputFormatter.Space();
attribute.AcceptVisitor(this, data);
}
if (xmlElementExpression.Children.Any()) {
outputFormatter.PrintText(">");
foreach (INode node in xmlElementExpression.Children) {
node.AcceptVisitor(this, data);
}
outputFormatter.PrintText("</");
if (!xmlElementExpression.NameIsExpression)
outputFormatter.PrintText(xmlElementExpression.XmlName);
outputFormatter.PrintText(">");
} else {
outputFormatter.Space();
outputFormatter.PrintText("/>");
}
return null;
}
public override object TrackedVisitXmlMemberAccessExpression(XmlMemberAccessExpression xmlMemberAccessExpression, object data)
{
xmlMemberAccessExpression.TargetObject.AcceptVisitor(this, data);
switch (xmlMemberAccessExpression.AxisType) {
case XmlAxisType.Element:
outputFormatter.PrintToken(Tokens.Dot);
break;
case XmlAxisType.Attribute:
outputFormatter.PrintToken(Tokens.DotAt);
break;
case XmlAxisType.Descendents:
outputFormatter.PrintToken(Tokens.TripleDot);
break;
default:
throw new Exception("Invalid value for XmlAxisType");
}
if (xmlMemberAccessExpression.IsXmlIdentifier)
outputFormatter.PrintText("<" + xmlMemberAccessExpression.Identifier + ">");
else
outputFormatter.PrintIdentifier(xmlMemberAccessExpression.Identifier);
return null;
}
}
}

2
src/Libraries/NRefactory/Project/Src/Visitors/AbstractASTVisitor.cs

@ -1273,7 +1273,7 @@ namespace ICSharpCode.NRefactory.Visitors { @@ -1273,7 +1273,7 @@ namespace ICSharpCode.NRefactory.Visitors {
Debug.Assert(o != null);
o.AcceptVisitor(this, data);
}
return null;
return xmlElementExpression.AcceptChildren(this, data);
}
public virtual object VisitXmlEmbeddedExpression(XmlEmbeddedExpression xmlEmbeddedExpression, object data) {

11
src/Libraries/NRefactory/Project/Src/Visitors/AbstractAstTransformer.cs

@ -2280,6 +2280,17 @@ namespace ICSharpCode.NRefactory.Visitors { @@ -2280,6 +2280,17 @@ namespace ICSharpCode.NRefactory.Visitors {
else
xmlElementExpression.Attributes[i] = o;
}
for (int i = 0; i < xmlElementExpression.Children.Count; i++) {
INode o = xmlElementExpression.Children[i];
Debug.Assert(o != null);
nodeStack.Push(o);
o.AcceptVisitor(this, data);
o = nodeStack.Pop();
if (o == null)
xmlElementExpression.Children.RemoveAt(i--);
else
xmlElementExpression.Children[i] = o;
}
return null;
}

79
src/Libraries/NRefactory/Test/Output/VBNet/VBNetOutputTest.cs

@ -31,7 +31,11 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter @@ -31,7 +31,11 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
string StripWhitespace(string text)
{
return text.Trim().Replace("\t", "").Replace("\r", "").Replace("\n", " ").Replace(" ", " ");
text = text.Trim().Replace("\t", "").Replace("\r", "").Replace("\n", " ").Replace(" ", " ");
while (text.Contains(" ")) {
text = text.Replace(" ", " ");
}
return text;
}
void TestTypeMember(string program)
@ -590,5 +594,78 @@ End Using"); @@ -590,5 +594,78 @@ End Using");
" Return x * x\n" +
"End Function");
}
[Test]
public void XmlSimple()
{
TestExpression("<!-- test -->\n" +
"<Test>\n" +
" <A />\n" +
" <B test='a' <%= test %> />\n" +
"</Test>");
}
[Test]
public void XmlNested()
{
TestExpression(@"<menu>
<course name=""appetizer"">
<dish>Shrimp Cocktail</dish>
<dish>Escargot</dish>
</course>
<course name=""main"">
<dish>Filet Mignon</dish>
<dish>Garlic Potatoes</dish>
<dish>Broccoli</dish>
</course>
<course name=""dessert"">
<dish>Chocolate Cheesecake</dish>
</course>
</menu>");
}
[Test]
public void XmlDocument()
{
TestExpression(@"<?xml version=""1.0""?>
<menu>
<course name=""appetizer"">
<dish>Shrimp Cocktail</dish>
<dish>Escargot</dish>
</course>
</menu>");
}
[Test]
public void XmlNestedWithExpressions()
{
TestExpression(@"<?xml version=""1.0""?>
<menu>
<course name=""appetizer"">
<%= From m In menu _
Where m.Course = ""appetizer"" _
Select <dish><%= m.Food %></dish> %>
</course>
<course name=""main"">
<%= From m In menu _
Where m.Course = ""main"" _
Select <dish><%= m.Food %></dish> %>
</course>
<course name=""dessert"">
<%= From m In menu _
Where m.Course = ""dessert"" _
Select <dish><%= m.Food %></dish> %>
</course>
</menu>");
}
[Test]
public void XmlAccessExpressions()
{
TestExpression("xml.<menu>.<course>");
TestExpression("xml...<course>");
TestExpression("xml...<course>(2)");
TestExpression("item.@name");
}
}
}

Loading…
Cancel
Save