|
| 1 | +using Antlr4.Runtime; |
| 2 | +using Rubberduck.Parsing.Symbols; |
| 3 | + |
| 4 | +namespace Rubberduck.Parsing.Binding |
| 5 | +{ |
| 6 | + public sealed class DefaultBindingContext : IBindingContext |
| 7 | + { |
| 8 | + private readonly DeclarationFinder _declarationFinder; |
| 9 | + |
| 10 | + public DefaultBindingContext(DeclarationFinder declarationFinder) |
| 11 | + { |
| 12 | + _declarationFinder = declarationFinder; |
| 13 | + } |
| 14 | + |
| 15 | + public IBoundExpression Resolve(Declaration module, Declaration parent, ParserRuleContext expression) |
| 16 | + { |
| 17 | + dynamic dynamicExpression = expression; |
| 18 | + IExpressionBinding bindingTree = Visit(module, parent, dynamicExpression); |
| 19 | + if (bindingTree != null) |
| 20 | + { |
| 21 | + return bindingTree.Resolve(); |
| 22 | + } |
| 23 | + return null; |
| 24 | + } |
| 25 | + |
| 26 | + private IExpressionBinding Visit(Declaration module, Declaration parent, VBAExpressionParser.NewExprContext expression) |
| 27 | + { |
| 28 | + return Visit(module, parent, expression.newExpression()); |
| 29 | + } |
| 30 | + |
| 31 | + private IExpressionBinding Visit(Declaration module, Declaration parent, VBAExpressionParser.NewExpressionContext expression) |
| 32 | + { |
| 33 | + var typeExpressionBinding = Visit(module, parent, expression.typeExpression()); |
| 34 | + if (typeExpressionBinding == null) |
| 35 | + { |
| 36 | + return null; |
| 37 | + } |
| 38 | + return new NewTypeBinding(_declarationFinder, module, parent, expression, typeExpressionBinding); |
| 39 | + } |
| 40 | + |
| 41 | + private IExpressionBinding Visit(Declaration module, Declaration parent, VBAExpressionParser.TypeExpressionContext expression) |
| 42 | + { |
| 43 | + if (expression.builtInType() != null) |
| 44 | + { |
| 45 | + return null; |
| 46 | + } |
| 47 | + return Visit(module, parent, expression.definedTypeExpression()); |
| 48 | + } |
| 49 | + |
| 50 | + private IExpressionBinding Visit(Declaration module, Declaration parent, VBAExpressionParser.DefinedTypeExpressionContext expression) |
| 51 | + { |
| 52 | + if (expression.simpleNameExpression() != null) |
| 53 | + { |
| 54 | + return Visit(module, parent, expression.simpleNameExpression()); |
| 55 | + } |
| 56 | + return Visit(module, parent, expression.memberAccessExpression()); |
| 57 | + } |
| 58 | + |
| 59 | + private IExpressionBinding Visit(Declaration module, Declaration parent, VBAExpressionParser.SimpleNameExprContext expression) |
| 60 | + { |
| 61 | + return Visit(module, parent, expression.simpleNameExpression()); |
| 62 | + } |
| 63 | + |
| 64 | + private IExpressionBinding Visit(Declaration module, Declaration parent, VBAExpressionParser.SimpleNameExpressionContext expression) |
| 65 | + { |
| 66 | + return new SimpleNameTypeBinding(_declarationFinder, module, parent, expression); |
| 67 | + } |
| 68 | + |
| 69 | + private IExpressionBinding Visit(Declaration module, Declaration parent, VBAExpressionParser.MemberAccessExprContext expression) |
| 70 | + { |
| 71 | + dynamic lExpression = expression.lExpression(); |
| 72 | + var lExpressionBinding = Visit(module, parent, lExpression); |
| 73 | + return new MemberAccessTypeBinding(_declarationFinder, module, parent, expression, lExpressionBinding); |
| 74 | + } |
| 75 | + |
| 76 | + private IExpressionBinding Visit(Declaration module, Declaration parent, VBAExpressionParser.MemberAccessExpressionContext expression) |
| 77 | + { |
| 78 | + dynamic lExpression = expression.lExpression(); |
| 79 | + var lExpressionBinding = Visit(module, parent, lExpression); |
| 80 | + return new MemberAccessTypeBinding(_declarationFinder, module, parent, expression, lExpressionBinding); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments