Browse Source

Introduced namedexpression/namedexpressionlist to handle the

collection initializers better.
newNRvisualizers
Mike Krüger 14 years ago
parent
commit
3763397da8
  1. 10
      ICSharpCode.NRefactory/CSharp/Ast/DepthFirstAstVisitor.cs
  2. 86
      ICSharpCode.NRefactory/CSharp/Ast/Expressions/NamedExpression.cs
  3. 86
      ICSharpCode.NRefactory/CSharp/Ast/Expressions/NamedExpressionList.cs
  4. 2
      ICSharpCode.NRefactory/CSharp/Ast/IAstVisitor.cs
  5. 20
      ICSharpCode.NRefactory/CSharp/Ast/ObservableAstVisitor.cs
  6. 23
      ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs
  7. 81
      ICSharpCode.NRefactory/CSharp/Parser/CSharpParser.cs
  8. 4
      ICSharpCode.NRefactory/CSharp/Parser/mcs/cs-parser.cs
  9. 4
      ICSharpCode.NRefactory/CSharp/Parser/mcs/cs-parser.jay
  10. 13
      ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj

10
ICSharpCode.NRefactory/CSharp/Ast/DepthFirstAstVisitor.cs

@ -585,6 +585,16 @@ namespace ICSharpCode.NRefactory.CSharp @@ -585,6 +585,16 @@ namespace ICSharpCode.NRefactory.CSharp
return VisitChildren (namedArgumentExpression, data);
}
public virtual S VisitNamedExpression (NamedExpression namedExpression, T data)
{
return VisitChildren (namedExpression, data);
}
public virtual S VisitNamedExpressionList (NamedExpressionList namedExpressionList, T data)
{
return VisitChildren (namedExpressionList, data);
}
public virtual S VisitEmptyExpression (EmptyExpression emptyExpression, T data)
{
return VisitChildren (emptyExpression, data);

86
ICSharpCode.NRefactory/CSharp/Ast/Expressions/NamedExpression.cs

@ -0,0 +1,86 @@ @@ -0,0 +1,86 @@
//
// NamedExpression.cs
//
// Author:
// Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2011 Xamarin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System.Collections.Generic;
using System.Linq;
namespace ICSharpCode.NRefactory.CSharp
{
/// <summary>
/// name = expression
/// This isn't the same as 'assign' even if it has the same syntax. This expression is used in object initializers.
/// </summary>
public class NamedExpression : Expression
{
public NamedExpression()
{
}
public NamedExpression (string identifier, Expression expression)
{
this.Identifier = identifier;
this.Expression = expression;
}
public string Identifier {
get {
return GetChildByRole (Roles.Identifier).Name;
}
set {
SetChildByRole(Roles.Identifier, CSharp.Identifier.Create (value, AstLocation.Empty));
}
}
public Identifier IdentifierToken {
get {
return GetChildByRole (Roles.Identifier);
}
set {
SetChildByRole(Roles.Identifier, value);
}
}
public CSharpTokenNode AssignToken {
get { return GetChildByRole (Roles.Assign); }
}
public Expression Expression {
get { return GetChildByRole (Roles.Expression); }
set { SetChildByRole (Roles.Expression, value); }
}
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
{
return visitor.VisitNamedExpression(this, data);
}
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
{
var o = other as NamedExpression;
return o != null && MatchString(this.Identifier, o.Identifier) && this.Expression.DoMatch(o.Expression, match);
}
}
}

86
ICSharpCode.NRefactory/CSharp/Ast/Expressions/NamedExpressionList.cs

@ -0,0 +1,86 @@ @@ -0,0 +1,86 @@
//
// NamedExpressionList.cs
//
// Author:
// Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2011 Xamarin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System.Collections.Generic;
using System.Linq;
namespace ICSharpCode.NRefactory.CSharp
{
/// <summary>
/// name = { expression1, ... , expressionN }
/// </summary>
public class NamedExpressionList : Expression
{
public NamedExpressionList()
{
}
public string Identifier {
get {
return GetChildByRole (Roles.Identifier).Name;
}
set {
SetChildByRole(Roles.Identifier, CSharp.Identifier.Create (value, AstLocation.Empty));
}
}
public Identifier IdentifierToken {
get {
return GetChildByRole (Roles.Identifier);
}
set {
SetChildByRole(Roles.Identifier, value);
}
}
public CSharpTokenNode AssignToken {
get { return GetChildByRole (Roles.Assign); }
}
public CSharpTokenNode LBraceToken {
get { return GetChildByRole (Roles.LBrace); }
}
public AstNodeCollection<Expression> Expressions {
get { return GetChildrenByRole (Roles.Expression); }
}
public CSharpTokenNode RBraceToken {
get { return GetChildByRole (Roles.RBrace); }
}
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
{
return visitor.VisitNamedExpressionList(this, data);
}
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
{
var o = other as NamedExpressionList;
return o != null && MatchString(this.Identifier, o.Identifier) && this.Expressions.DoMatch(o.Expressions, match);
}
}
}

2
ICSharpCode.NRefactory/CSharp/Ast/IAstVisitor.cs

@ -45,6 +45,8 @@ namespace ICSharpCode.NRefactory.CSharp @@ -45,6 +45,8 @@ namespace ICSharpCode.NRefactory.CSharp
S VisitLambdaExpression(LambdaExpression lambdaExpression, T data);
S VisitMemberReferenceExpression(MemberReferenceExpression memberReferenceExpression, T data);
S VisitNamedArgumentExpression(NamedArgumentExpression namedArgumentExpression, T data);
S VisitNamedExpression(NamedExpression namedExpression, T data);
S VisitNamedExpressionList(NamedExpressionList namedExpressionList, T data);
S VisitNullReferenceExpression(NullReferenceExpression nullReferenceExpression, T data);
S VisitObjectCreateExpression(ObjectCreateExpression objectCreateExpression, T data);
S VisitAnonymousTypeCreateExpression(AnonymousTypeCreateExpression anonymousTypeCreateExpression, T data);

20
ICSharpCode.NRefactory/CSharp/Ast/ObservableAstVisitor.cs

@ -1121,6 +1121,26 @@ namespace ICSharpCode.NRefactory.CSharp @@ -1121,6 +1121,26 @@ namespace ICSharpCode.NRefactory.CSharp
return VisitChildren (namedArgumentExpression, data);
}
public event Action<NamedExpression, T> NamedExpressionVisited;
S IAstVisitor<T, S>.VisitNamedExpression (NamedExpression namedExpression, T data)
{
var handler = NamedExpressionVisited;
if (handler != null)
handler (namedExpression, data);
return VisitChildren (namedExpression, data);
}
public event Action<NamedExpressionList, T> NamedExpressionListVisited;
S IAstVisitor<T, S>.VisitNamedExpressionList (NamedExpressionList namedExpressionList, T data)
{
var handler = NamedExpressionListVisited;
if (handler != null)
handler (namedExpressionList, data);
return VisitChildren (namedExpressionList, data);
}
public event Action<EmptyExpression, T> EmptyExpressionVisited;
S IAstVisitor<T, S>.VisitEmptyExpression (EmptyExpression emptyExpression, T data)

23
ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs

@ -575,6 +575,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -575,6 +575,7 @@ namespace ICSharpCode.NRefactory.CSharp
OpenBrace(style);
bool isFirst = true;
foreach (AstNode node in elements) {
Console.WriteLine ("expr:"+node.GetType ());
if (isFirst) {
isFirst = false;
} else {
@ -830,6 +831,28 @@ namespace ICSharpCode.NRefactory.CSharp @@ -830,6 +831,28 @@ namespace ICSharpCode.NRefactory.CSharp
return EndNode (namedArgumentExpression);
}
public object VisitNamedExpression (NamedExpression namedExpression, object data)
{
StartNode (namedExpression);
WriteIdentifier (namedExpression.Identifier);
Space();
WriteToken("=", NamedArgumentExpression.Roles.Assign);
Space ();
namedExpression.Expression.AcceptVisitor (this, data);
return EndNode (namedExpression);
}
public object VisitNamedExpressionList (NamedExpressionList namedExpressionList, object data)
{
StartNode (namedExpressionList);
WriteIdentifier (namedExpressionList.Identifier);
Space();
WriteToken("=", NamedArgumentExpression.Roles.Assign);
Space ();
PrintInitializerElements(namedExpressionList.Expressions);
return EndNode (namedExpressionList);
}
public object VisitNullReferenceExpression (NullReferenceExpression nullReferenceExpression, object data)
{
StartNode (nullReferenceExpression);

81
ICSharpCode.NRefactory/CSharp/Parser/CSharpParser.cs

@ -2328,6 +2328,60 @@ namespace ICSharpCode.NRefactory.CSharp @@ -2328,6 +2328,60 @@ namespace ICSharpCode.NRefactory.CSharp
return result;
}
ArrayInitializerExpression ConvertCollectionOrObjectInitializers (CollectionOrObjectInitializers minit)
{
if (minit == null)
return null;
var init = new ArrayInitializerExpression ();
AddConvertCollectionOrObjectInitializers (init, minit);
return init;
}
void AddConvertCollectionOrObjectInitializers (AstNode init, CollectionOrObjectInitializers minit)
{
var initLoc = LocationsBag.GetLocations (minit);
var commaLoc = LocationsBag.GetLocations (minit.Initializers);
int curComma = commaLoc != null ? commaLoc.Count - 1 : -1;
foreach (var expr in minit.Initializers) {
var collectionInit = expr as CollectionElementInitializer;
if (collectionInit != null) {
for (int i = 0; i < collectionInit.Arguments.Count; i++) {
var arg = collectionInit.Arguments[i] as CollectionElementInitializer.ElementInitializerArgument;
if (arg == null)
continue;
init.AddChild ((ICSharpCode.NRefactory.CSharp.Expression)arg.Expr.Accept (this), ArrayInitializerExpression.Roles.Expression);
if (curComma >= 0)
init.AddChild (new CSharpTokenNode (Convert (commaLoc[curComma--]), 1), ArrayInitializerExpression.Roles.Comma);
}
continue;
}
var eleInit = expr as ElementInitializer;
if (eleInit != null) {
var nexpr = eleInit.Source is CollectionOrObjectInitializers ? (ICSharpCode.NRefactory.CSharp.Expression)new NamedExpressionList () : new NamedExpression ();
nexpr.AddChild (Identifier.Create (eleInit.Name, Convert(eleInit.Location)), NamedArgumentExpression.Roles.Identifier);
var assignLoc = LocationsBag.GetLocations (eleInit);
if (assignLoc != null)
nexpr.AddChild (new CSharpTokenNode (Convert (assignLoc[0]), 1), NamedArgumentExpression.Roles.Assign);
if (eleInit.Source != null) {
if (eleInit.Source is CollectionOrObjectInitializers) {
AddConvertCollectionOrObjectInitializers (nexpr, eleInit.Source as CollectionOrObjectInitializers);
} else {
nexpr.AddChild ((Expression)eleInit.Source.Accept (this), NamedArgumentExpression.Roles.Expression);
}
}
init.AddChild (nexpr, ArrayInitializerExpression.Roles.Expression);
}
}
if (initLoc != null) {
if (initLoc.Count == 3) // optional comma
init.AddChild (new CSharpTokenNode (Convert (initLoc[1]), 1), ArrayInitializerExpression.Roles.Comma);
init.AddChild (new CSharpTokenNode (Convert (initLoc[initLoc.Count - 1]), 1), ArrayInitializerExpression.Roles.RBrace);
}
}
public override object Visit (NewInitialize newInitializeExpression)
{
@ -2344,32 +2398,9 @@ namespace ICSharpCode.NRefactory.CSharp @@ -2344,32 +2398,9 @@ namespace ICSharpCode.NRefactory.CSharp
if (location != null)
result.AddChild (new CSharpTokenNode (Convert (location[1]), 1), ObjectCreateExpression.Roles.RPar);
var minit = newInitializeExpression.Initializers;
if (minit != null){
var init = new ArrayInitializerExpression ();
var initLoc = LocationsBag.GetLocations (newInitializeExpression);
if (initLoc != null)
result.AddChild (new CSharpTokenNode (Convert (location[0]), 1), ArrayInitializerExpression.Roles.LBrace);
var commaLoc = LocationsBag.GetLocations (minit.Initializers);
int curComma = commaLoc != null ? commaLoc.Count - 1 : -1;
foreach (var expr in minit.Initializers) {
var eleInit = expr as CollectionElementInitializer;
if (eleInit == null)
continue;
for (int i = 0; i < eleInit.Arguments.Count; i++) {
var arg = eleInit.Arguments[i] as CollectionElementInitializer.ElementInitializerArgument;
if (arg == null)
continue;
init.AddChild ((ICSharpCode.NRefactory.CSharp.Expression)arg.Expr.Accept (this), ArrayInitializerExpression.Roles.Expression);
if (curComma >= 0)
init.AddChild (new CSharpTokenNode (Convert (commaLoc[curComma--]), 1), ArrayInitializerExpression.Roles.Comma);
}
}
if (initLoc != null)
result.AddChild (new CSharpTokenNode (Convert (location[1]), 1), ArrayInitializerExpression.Roles.RBrace);
var init = ConvertCollectionOrObjectInitializers (newInitializeExpression.Initializers);
if (init != null)
result.AddChild (init, ObjectCreateExpression.InitializerRole);
}
return result;
}

4
ICSharpCode.NRefactory/CSharp/Parser/mcs/cs-parser.cs

@ -6126,7 +6126,7 @@ void case_448() @@ -6126,7 +6126,7 @@ void case_448()
/* TODO: lbag*/
} else {
yyVal = new CollectionOrObjectInitializers ((List<Expression>) yyVals[-1+yyTop], GetLocation (yyVals[-2+yyTop]));
lbag.AddLocation (yyVal, GetLocation (yyVals[0+yyTop]));
lbag.AddLocation (yyVal, GetLocation (yyVals[-2+yyTop]), GetLocation (yyVals[0+yyTop]));
}
}
@ -6134,7 +6134,7 @@ void case_449() @@ -6134,7 +6134,7 @@ void case_449()
#line 3189 "cs-parser.jay"
{
yyVal = new CollectionOrObjectInitializers ((List<Expression>) yyVals[-2+yyTop], GetLocation (yyVals[-3+yyTop]));
lbag.AddLocation (yyVal, GetLocation (yyVals[-1+yyTop]), GetLocation (yyVals[0+yyTop]));
lbag.AddLocation (yyVal, GetLocation (yyVals[-3+yyTop]), GetLocation (yyVals[-1+yyTop]), GetLocation (yyVals[0+yyTop]));
}
void case_452()

4
ICSharpCode.NRefactory/CSharp/Parser/mcs/cs-parser.jay

@ -3182,13 +3182,13 @@ object_or_collection_initializer @@ -3182,13 +3182,13 @@ object_or_collection_initializer
// TODO: lbag
} else {
$$ = new CollectionOrObjectInitializers ((List<Expression>) $2, GetLocation ($1));
lbag.AddLocation ($$, GetLocation ($3));
lbag.AddLocation ($$, GetLocation ($1), GetLocation ($3));
}
}
| OPEN_BRACE member_initializer_list COMMA CLOSE_BRACE
{
$$ = new CollectionOrObjectInitializers ((List<Expression>) $2, GetLocation ($1));
lbag.AddLocation ($$, GetLocation ($3), GetLocation ($4));
lbag.AddLocation ($$, GetLocation ($1), GetLocation ($3), GetLocation ($4));
}
;

13
ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<PropertyGroup>
<ProjectGuid>{3B2A5653-EC97-4001-BB9B-D90F1AF2C371}</ProjectGuid>
@ -7,7 +7,6 @@ @@ -7,7 +7,6 @@
<OutputType>Library</OutputType>
<RootNamespace>ICSharpCode.NRefactory</RootNamespace>
<AssemblyName>ICSharpCode.NRefactory</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<AppDesignerFolder>Properties</AppDesignerFolder>
<ProductVersion>10.0.0</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
@ -17,7 +16,7 @@ @@ -17,7 +16,7 @@
<NoWarn>1591,0618</NoWarn>
<RunCodeAnalysis>False</RunCodeAnalysis>
<CodeAnalysisRules>-Microsoft.Design#CA1026;-Microsoft.Security#CA2104</CodeAnalysisRules>
<SignAssembly>True</SignAssembly>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\ICSharpCode.NRefactory.snk</AssemblyOriginatorKeyFile>
<DelaySign>False</DelaySign>
<AssemblyOriginatorKeyMode>File</AssemblyOriginatorKeyMode>
@ -52,11 +51,9 @@ @@ -52,11 +51,9 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core">
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq">
</Reference>
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<Compile Include="CSharp\Analysis\ControlFlow.cs" />
@ -412,6 +409,8 @@ @@ -412,6 +409,8 @@
<Compile Include="TypeSystem\Error.cs" />
<Compile Include="TypeSystem\IAnnotatable.cs" />
<Compile Include="CSharp\Ast\ErrorNode.cs" />
<Compile Include="CSharp\Ast\Expressions\NamedExpression.cs" />
<Compile Include="CSharp\Ast\Expressions\NamedExpressionList.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="CSharp\" />

Loading…
Cancel
Save