Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactor IronPython resolver.

  • Loading branch information...
commit 69e65acf48bb83579b835c17d78bfd65e335e400 1 parent 1123bf8
@mrward mrward authored
Showing with 117 additions and 88 deletions.
  1. +1 −1  src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/IPythonResolver.cs
  2. +8 −6 src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonClassResolver.cs
  3. +2 −2 src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDotNetMethodResolver.cs
  4. +4 −8 src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonImportResolver.cs
  5. +12 −10 src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonLocalVariableResolver.cs
  6. +7 −2 src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonMemberResolver.cs
  7. +3 −3 src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonMethodResolver.cs
  8. +2 −1  src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonNamespaceResolver.cs
  9. +5 −5 src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonResolver.cs
  10. +50 −28 src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonResolverContext.cs
  11. +8 −8 src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonSelfResolver.cs
  12. +2 −2 src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonStandardModuleMethodResolver.cs
  13. +8 −6 src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonStandardModuleResolver.cs
  14. +3 −3 src/AddIns/BackendBindings/Python/PythonBinding/Test/Resolver/PythonSelfResolverTests.cs
  15. +2 −3 src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/PythonResolverTestsHelper.cs
View
2  src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/IPythonResolver.cs
@@ -8,6 +8,6 @@ namespace ICSharpCode.PythonBinding
{
public interface IPythonResolver
{
- ResolveResult Resolve(PythonResolverContext resolverContext, ExpressionResult expressionResult);
+ ResolveResult Resolve(PythonResolverContext resolverContext);
}
}
View
14 src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonClassResolver.cs
@@ -10,19 +10,21 @@ public class PythonClassResolver : IPythonResolver
{
PythonResolverContext resolverContext;
- public PythonClassResolver()
+ public ResolveResult Resolve(PythonResolverContext resolverContext)
{
- }
-
- public ResolveResult Resolve(PythonResolverContext resolverContext, ExpressionResult expressionResult)
- {
- IClass matchingClass = GetClass(resolverContext, expressionResult.Expression);
+ IClass matchingClass = GetClass(resolverContext);
if (matchingClass != null) {
return CreateTypeResolveResult(matchingClass);
}
return null;
}
+ public IClass GetClass(PythonResolverContext resolverContext)
+ {
+ string name = resolverContext.Expression;
+ return GetClass(resolverContext, name);
+ }
+
public IClass GetClass(PythonResolverContext resolverContext, string name)
{
this.resolverContext = resolverContext;
View
4 src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDotNetMethodResolver.cs
@@ -15,9 +15,9 @@ public PythonDotNetMethodResolver(PythonClassResolver classResolver)
this.classResolver = classResolver;
}
- public ResolveResult Resolve(PythonResolverContext resolverContext, ExpressionResult expressionResult)
+ public ResolveResult Resolve(PythonResolverContext resolverContext)
{
- MemberName memberName = new MemberName(expressionResult.Expression);
+ MemberName memberName = resolverContext.CreateExpressionMemberName();
IClass matchingClass = classResolver.GetClass(resolverContext, memberName.Type);
if (matchingClass != null) {
return new PythonMethodGroupResolveResult(matchingClass, memberName.Name);
View
12 src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonImportResolver.cs
@@ -8,15 +8,11 @@ namespace ICSharpCode.PythonBinding
{
public class PythonImportResolver : IPythonResolver
{
- public PythonImportResolver()
+ public ResolveResult Resolve(PythonResolverContext resolverContext)
{
- }
-
- public ResolveResult Resolve(PythonResolverContext resolverContext, ExpressionResult expressionResult)
- {
- if (IsNamespace(expressionResult)) {
- PythonImportExpression importExpression = new PythonImportExpression(expressionResult.Expression);
- PythonImportExpressionContext context = expressionResult.Context as PythonImportExpressionContext;
+ if (IsNamespace(resolverContext.ExpressionResult)) {
+ PythonImportExpression importExpression = new PythonImportExpression(resolverContext.Expression);
+ PythonImportExpressionContext context = resolverContext.ExpressionContext as PythonImportExpressionContext;
context.HasFromAndImport = importExpression.HasFromAndImport;
return new PythonImportModuleResolveResult(importExpression);
View
22 src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonLocalVariableResolver.cs
@@ -123,34 +123,36 @@ public static string GetTypeName(Expression node)
return PythonControlFieldExpression.GetMemberName(node as MemberExpression);
}
- public ResolveResult Resolve(PythonResolverContext resolverContext, ExpressionResult expressionResult)
+ public ResolveResult Resolve(PythonResolverContext resolverContext)
{
- return GetLocalVariable(resolverContext, expressionResult);
+ return GetLocalVariable(resolverContext);
}
/// <summary>
/// Tries to find the type that matches the local variable name.
/// </summary>
- LocalResolveResult GetLocalVariable(PythonResolverContext resolverContext, ExpressionResult expressionResult)
+ LocalResolveResult GetLocalVariable(PythonResolverContext resolverContext)
{
- string code = GetLocalMethodCode(resolverContext.FileContent, expressionResult);
- string typeName = Resolve(expressionResult.Expression, code);
+ string code = GetLocalMethodCode(resolverContext);
+ string typeName = Resolve(resolverContext.Expression, code);
if (typeName != null) {
- return CreateLocalResolveResult(typeName, expressionResult.Expression, resolverContext);
+ return CreateLocalResolveResult(typeName, resolverContext);
}
return null;
}
- string GetLocalMethodCode(string fullCode, ExpressionResult expressionResult)
+ string GetLocalMethodCode(PythonResolverContext resolverContext)
{
- ScriptingLocalMethod localMethod = new ScriptingLocalMethod(fullCode);
- return localMethod.GetCode(expressionResult.Region.BeginLine);
+ ScriptingLocalMethod localMethod = new ScriptingLocalMethod(resolverContext.FileContent);
+ int beginLine = resolverContext.ExpressionRegion.BeginLine;
+ return localMethod.GetCode(beginLine);
}
- LocalResolveResult CreateLocalResolveResult(string typeName, string identifier, PythonResolverContext resolverContext)
+ LocalResolveResult CreateLocalResolveResult(string typeName, PythonResolverContext resolverContext)
{
IClass resolvedClass = classResolver.GetClass(resolverContext, typeName);
if (resolvedClass != null) {
+ string identifier = resolverContext.Expression;
return CreateLocalResolveResult(identifier, resolvedClass);
}
return null;
View
9 src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonMemberResolver.cs
@@ -19,15 +19,20 @@ public PythonMemberResolver(PythonClassResolver classResolver)
this.classResolver = classResolver;
}
- public ResolveResult Resolve(PythonResolverContext resolverContext, ExpressionResult expressionResult)
+ public ResolveResult Resolve(PythonResolverContext resolverContext)
{
- IMember member = FindMember(resolverContext, expressionResult.Expression);
+ IMember member = FindMember(resolverContext);
if (member != null) {
return CreateMemberResolveResult(member);
}
return null;
}
+ IMember FindMember(PythonResolverContext resolverContext)
+ {
+ return FindMember(resolverContext, resolverContext.Expression);
+ }
+
IMember FindMember(PythonResolverContext resolverContext, string expression)
{
MemberName memberName = new MemberName(expression);
View
6 src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonMethodResolver.cs
@@ -17,13 +17,13 @@ public PythonMethodResolver(PythonClassResolver classResolver, PythonStandardMod
standardModuleMethodResolver = new PythonStandardModuleMethodResolver(standardModuleResolver);
}
- public ResolveResult Resolve(PythonResolverContext resolverContext, ExpressionResult expressionResult)
+ public ResolveResult Resolve(PythonResolverContext resolverContext)
{
- ResolveResult resolveResult = dotNetMethodResolver.Resolve(resolverContext, expressionResult);
+ ResolveResult resolveResult = dotNetMethodResolver.Resolve(resolverContext);
if (resolveResult != null) {
return resolveResult;
}
- return standardModuleMethodResolver.Resolve(resolverContext, expressionResult);
+ return standardModuleMethodResolver.Resolve(resolverContext);
}
}
}
View
3  src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonNamespaceResolver.cs
@@ -11,8 +11,9 @@ public class PythonNamespaceResolver : IPythonResolver
PythonResolverContext resolverContext;
ExpressionResult expressionResult;
- public PythonNamespaceResolver()
+ public ResolveResult Resolve(PythonResolverContext resolverContext)
{
+ return Resolve(resolverContext, resolverContext.ExpressionResult);
}
public ResolveResult Resolve(PythonResolverContext resolverContext, ExpressionResult expressionResult)
View
10 src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonResolver.cs
@@ -46,18 +46,18 @@ public ResolveResult Resolve(ExpressionResult expressionResult, ParseInformation
return null;
}
- resolverContext = new PythonResolverContext(parseInfo, fileContent);
- if (!resolverContext.GetCallingMember(expressionResult.Region)) {
+ resolverContext = new PythonResolverContext(parseInfo, expressionResult, fileContent);
+ if (!resolverContext.HasProjectContent) {
return null;
}
- return Resolve(resolverContext, expressionResult);
+ return Resolve(resolverContext);
}
- public ResolveResult Resolve(PythonResolverContext resolverContext, ExpressionResult expressionResult)
+ public ResolveResult Resolve(PythonResolverContext resolverContext)
{
foreach (IPythonResolver resolver in resolvers) {
- ResolveResult resolveResult = resolver.Resolve(resolverContext, expressionResult);
+ ResolveResult resolveResult = resolver.Resolve(resolverContext);
if (resolveResult != null) {
return resolveResult;
}
View
78 src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonResolverContext.cs
@@ -14,6 +14,7 @@ public class PythonResolverContext
IProjectContent projectContent;
IClass callingClass;
string fileContent;
+ ExpressionResult expressionResult;
public PythonResolverContext(ParseInformation parseInfo)
: this(parseInfo, String.Empty)
@@ -21,10 +22,17 @@ public PythonResolverContext(ParseInformation parseInfo)
}
public PythonResolverContext(ParseInformation parseInfo, string fileContent)
+ : this(parseInfo, new ExpressionResult(), fileContent)
+ {
+ }
+
+ public PythonResolverContext(ParseInformation parseInfo, ExpressionResult expressionResult, string fileContent)
{
this.fileContent = fileContent;
+ this.expressionResult = expressionResult;
GetCompilationUnit(parseInfo);
GetProjectContent();
+ GetCallingMember();
}
void GetCompilationUnit(ParseInformation parseInfo)
@@ -41,6 +49,27 @@ void GetProjectContent()
}
}
+ /// <summary>
+ /// Determines the class and member at the specified
+ /// line and column in the specified file.
+ /// </summary>
+ void GetCallingMember()
+ {
+ if (projectContent != null) {
+ GetCallingClass();
+ }
+ }
+
+ /// <summary>
+ /// Gets the calling class at the specified line and column.
+ /// </summary>
+ void GetCallingClass()
+ {
+ if (compilationUnit.Classes.Count > 0) {
+ callingClass = compilationUnit.Classes[0];
+ }
+ }
+
public string FileContent {
get { return fileContent; }
}
@@ -49,6 +78,27 @@ void GetProjectContent()
get { return projectContent; }
}
+ public ExpressionResult ExpressionResult {
+ get { return expressionResult; }
+ }
+
+ public MemberName CreateExpressionMemberName()
+ {
+ return new MemberName(Expression);
+ }
+
+ public string Expression {
+ get { return expressionResult.Expression; }
+ }
+
+ public ExpressionContext ExpressionContext {
+ get { return expressionResult.Context; }
+ }
+
+ public DomRegion ExpressionRegion {
+ get { return expressionResult.Region; }
+ }
+
public bool HasProjectContent {
get { return projectContent != null; }
}
@@ -86,34 +136,6 @@ bool PartialNamespaceExists(IProjectContent projectContent, string name)
return false;
}
- /// <summary>
- /// Determines the class and member at the specified
- /// line and column in the specified file.
- /// </summary>
- public bool GetCallingMember(DomRegion region)
- {
- if (compilationUnit == null) {
- return false;
- }
-
- if (projectContent != null) {
- callingClass = GetCallingClass(region);
- return true;
- }
- return false;
- }
-
- /// <summary>
- /// Gets the calling class at the specified line and column.
- /// </summary>
- IClass GetCallingClass(DomRegion region)
- {
- if (compilationUnit.Classes.Count > 0) {
- return compilationUnit.Classes[0];
- }
- return null;
- }
-
public IClass GetClass(string fullyQualifiedName)
{
return projectContent.GetClass(fullyQualifiedName, 0);
View
16 src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonSelfResolver.cs
@@ -8,22 +8,22 @@ namespace ICSharpCode.PythonBinding
{
public class PythonSelfResolver : IPythonResolver
{
- public ResolveResult Resolve(PythonResolverContext resolverContext, ExpressionResult expressionResult)
+ public ResolveResult Resolve(PythonResolverContext resolverContext)
{
if (resolverContext.HasCallingClass) {
- if (IsSelfExpression(expressionResult)) {
+ if (IsSelfExpression(resolverContext)) {
return CreateResolveResult(resolverContext);
- } else if (IsSelfExpressionAtStart(expressionResult)) {
- MemberName memberName = new MemberName(expressionResult.Expression);
+ } else if (IsSelfExpressionAtStart(resolverContext)) {
+ MemberName memberName = resolverContext.CreateExpressionMemberName();
return new PythonMethodGroupResolveResult(resolverContext.CallingClass, memberName.Name);
}
}
return null;
}
- bool IsSelfExpression(ExpressionResult expressionResult)
+ bool IsSelfExpression(PythonResolverContext resolverContext)
{
- return expressionResult.Expression == "self";
+ return resolverContext.Expression == "self";
}
ResolveResult CreateResolveResult(PythonResolverContext resolverContext)
@@ -33,9 +33,9 @@ ResolveResult CreateResolveResult(PythonResolverContext resolverContext)
return new ResolveResult(callingClass, null, returnType);
}
- bool IsSelfExpressionAtStart(ExpressionResult expressionResult)
+ bool IsSelfExpressionAtStart(PythonResolverContext resolverContext)
{
- return expressionResult.Expression.StartsWith("self.");
+ return resolverContext.Expression.StartsWith("self.");
}
}
}
View
4 ...ns/BackendBindings/Python/PythonBinding/Project/Src/PythonStandardModuleMethodResolver.cs
@@ -15,9 +15,9 @@ public PythonStandardModuleMethodResolver(PythonStandardModuleResolver standardM
this.standardModuleResolver = standardModuleResolver;
}
- public ResolveResult Resolve(PythonResolverContext resolverContext, ExpressionResult expressionResult)
+ public ResolveResult Resolve(PythonResolverContext resolverContext)
{
- MemberName memberName = new MemberName(expressionResult.Expression);
+ MemberName memberName = resolverContext.CreateExpressionMemberName();
MethodGroupResolveResult result = ResolveMethodFromImportedNames(resolverContext, memberName);
if (result != null) {
return result;
View
14 src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonStandardModuleResolver.cs
@@ -10,19 +10,21 @@ public class PythonStandardModuleResolver : IPythonResolver
{
PythonStandardModules standardPythonModules = new PythonStandardModules();
- public PythonStandardModuleResolver()
+ public ResolveResult Resolve(PythonResolverContext resolverContext)
{
- }
-
- public ResolveResult Resolve(PythonResolverContext resolverContext, ExpressionResult expressionResult)
- {
- PythonStandardModuleType type = GetStandardModuleTypeIfImported(resolverContext, expressionResult.Expression);
+ PythonStandardModuleType type = GetStandardModuleTypeIfImported(resolverContext);
if (type != null) {
return new PythonStandardModuleResolveResult(type);
}
return null;
}
+ PythonStandardModuleType GetStandardModuleTypeIfImported(PythonResolverContext resolverContext)
+ {
+ string moduleName = resolverContext.Expression;
+ return GetStandardModuleTypeIfImported(resolverContext, moduleName);
+ }
+
public PythonStandardModuleType GetStandardModuleTypeIfImported(PythonResolverContext resolverContext, string moduleName)
{
if (resolverContext.HasImport(moduleName) || PythonBuiltInModuleMemberName.IsBuiltInModule(moduleName)) {
View
6 src/AddIns/BackendBindings/Python/PythonBinding/Test/Resolver/PythonSelfResolverTests.cs
@@ -24,14 +24,13 @@ public void Resolve_NoCallingClass_ReturnsNull()
CreateParseInfo();
CreatePythonResolverContext();
- ResolveResult result = resolver.Resolve(context, expression);
+ ResolveResult result = resolver.Resolve(context);
Assert.IsNull(result);
}
void CreatePythonSelfResolver()
{
resolver = new PythonSelfResolver();
- expression = new ExpressionResult("self");
}
void CreateParseInfo()
@@ -43,7 +42,8 @@ void CreateParseInfo()
void CreatePythonResolverContext()
{
- context = new PythonResolverContext(parseInfo);
+ expression = new ExpressionResult("self");
+ context = new PythonResolverContext(parseInfo, expression, String.Empty);
}
}
}
View
5 src/AddIns/BackendBindings/Python/PythonBinding/Test/Utils/PythonResolverTestsHelper.cs
@@ -36,9 +36,8 @@ public PythonResolverTestsHelper()
public ResolveResult Resolve(string expression)
{
ExpressionResult expressionResult = new ExpressionResult(expression);
- PythonResolverContext context = new PythonResolverContext(ParseInfo);
- context.GetCallingMember(expressionResult.Region);
- ResolveResult = Resolver.Resolve(context, expressionResult);
+ PythonResolverContext context = new PythonResolverContext(ParseInfo, expressionResult, String.Empty);
+ ResolveResult = Resolver.Resolve(context);
return ResolveResult;
}
Please sign in to comment.
Something went wrong with that request. Please try again.