private Expression ParseDirectiveExpression(DothtmlDirectiveNode directive, BindingParserNode expressionSyntax) { TypeRegistry registry; if (expressionSyntax is AssemblyQualifiedNameBindingParserNode) { var assemblyQualifiedName = expressionSyntax as AssemblyQualifiedNameBindingParserNode; expressionSyntax = assemblyQualifiedName.TypeName; registry = TypeRegistry.DirectivesDefault(compiledAssemblyCache, assemblyQualifiedName.AssemblyName.ToDisplayString()); } else { registry = TypeRegistry.DirectivesDefault(compiledAssemblyCache); } var visitor = new ExpressionBuildingVisitor(registry) { ResolveOnlyTypeName = true, Scope = null }; try { return(visitor.Visit(expressionSyntax)); } catch (Exception ex) { directive.AddError($"{expressionSyntax.ToDisplayString()} is not a valid type or namespace: {ex.Message}"); return(null); } }
public IAbstractImportDirective BuildImportDirective( DothtmlDirectiveNode node, BindingParserNode aliasSyntax, BindingParserNode nameSyntax) { foreach (var syntaxNode in nameSyntax.EnumerateNodes().Concat(aliasSyntax?.EnumerateNodes() ?? Enumerable.Empty <BindingParserNode>())) { syntaxNode.NodeErrors.ForEach(node.AddError); } var visitor = new ExpressionBuildingVisitor(TypeRegistry.DirectivesDefault) { ResolveOnlyTypeName = true, Scope = null }; Expression expression; try { expression = visitor.Visit(nameSyntax); } catch (Exception ex) { node.AddError($"{nameSyntax.ToDisplayString()} is not a valid type or namespace: {ex.Message}"); return(new ResolvedImportDirective(aliasSyntax, nameSyntax, null) { DothtmlNode = node }); } if (expression is UnknownStaticClassIdentifierExpression) { var namespaceValid = expression .CastTo <UnknownStaticClassIdentifierExpression>().Name .Apply(ReflectionUtils.IsAssemblyNamespace); if (!namespaceValid) { node.AddError($"{nameSyntax.ToDisplayString()} is unknown type or namespace."); } return(new ResolvedImportDirective(aliasSyntax, nameSyntax, null) { DothtmlNode = node }); } else if (expression is StaticClassIdentifierExpression) { return(new ResolvedImportDirective(aliasSyntax, nameSyntax, expression.Type) { DothtmlNode = node }); } node.AddError($"{nameSyntax.ToDisplayString()} is not a type or namespace."); return(new ResolvedImportDirective(aliasSyntax, nameSyntax, null)); }
public Expression Parse(string expression, DataContextStack dataContexts, BindingParserOptions options) { try { var tokenizer = new BindingTokenizer(); tokenizer.Tokenize(new StringReader(expression)); var parser = new BindingParser(); parser.Tokens = tokenizer.Tokens; var node = parser.ReadExpression(); if (!parser.OnEnd()) { throw new BindingCompilationException( $"Unexpected token '{expression.Substring(0, parser.Peek().StartPosition)} ---->{parser.Peek().Text}<---- {expression.Substring(parser.Peek().StartPosition + parser.Peek().Length)}'", null, new TokenBase[] { parser.Peek() }); } foreach (var n in node.EnumerateNodes()) { if (n.HasNodeErrors) throw new BindingCompilationException(string.Join(", ", n.NodeErrors), n); } var symbols = InitSymbols(dataContexts); symbols = options.AddTypes(symbols); var visitor = new ExpressionBuildingVisitor(symbols); visitor.Scope = symbols.Resolve(options.ScopeParameter); return visitor.Visit(node); } catch (Exception ex) { ex.ForInnerExceptions<BindingCompilationException>(bce => { if (bce.Expression == null) bce.Expression = expression; }); throw; } }