/// <inheritdoc/> protected override string Convert(Expression <TMethod> expression) { var visitor = new DelegateVisitor(this, 1); visitor.Visit(expression.Body); return(visitor.Resolution); }
public string TransformToCss(string source, string fileName) { try { Parser.StrictMath = StrictMath; var tree = Parser.Parse(source, fileName); var env = Env ?? new Env(Parser) { Compress = Compress, Debug = Debug, KeepFirstSpecialComment = KeepFirstSpecialComment, DisableVariableRedefines = DisableVariableRedefines, DisableColorCompression = DisableColorCompression }; if (Plugins != null) { foreach (IPluginConfigurator configurator in Plugins) { env.AddPlugin(configurator.CreatePlugin()); } } var css = tree.ToCSS(env); var stylizer = new PlainStylizer(); foreach (var unmatchedExtension in env.FindUnmatchedExtensions()) { Logger.Warn("Warning: extend '{0}' has no matches {1}\n", unmatchedExtension.BaseSelector.ToCSS(env).Trim(), stylizer.Stylize(new Zone(unmatchedExtension.Extend.Location)).Trim()); } tree.Accept(DelegateVisitor.For <Media>(m => { foreach (var unmatchedExtension in m.FindUnmatchedExtensions()) { Logger.Warn("Warning: extend '{0}' has no matches {1}\n", unmatchedExtension.BaseSelector.ToCSS(env).Trim(), stylizer.Stylize(new Zone(unmatchedExtension.Extend.Location)).Trim()); } })); LastTransformationSuccessful = true; return(css); } catch (ParserException e) { LastTransformationSuccessful = false; LastTransformationError = e; Logger.Error(e.Message); } return(""); }
public void OnWebApiCommand() { string input = "namespace Foo { webapi Bar { command void Baz(); } }"; var results = TypesLanguageCompiler.Compile(new[] { new TypesLanguageCompilerInput(input, "") }, Enumerable.Empty<Assembly>()); if (!results.WasSuccessful || !results.HasValue) Assert.Fail("Expected compilation success.\r\n" + results.Observations.Delimit("\r\n")); string output = ""; var visitor = new DelegateVisitor { OnWebApiCommand = (cmd, b) => output += cmd.Name }; visitor.Visit(results.Value); Assert.AreEqual("Baz", output); }
protected override Expression VisitUnary(UnaryExpression node) { if (node.NodeType != ExpressionType.Not) { throw new NotSupportedException($"Unary expression {node.NodeType}"); } var visitor = new DelegateVisitor(_converter, _methodDepth, _objectInstance); visitor.Visit(node.Operand); Resolution += $"not ({visitor.Resolution})"; return(node); }
protected override Expression VisitBinary(BinaryExpression node) { var op = node.NodeType switch { ExpressionType.Add => "+", ExpressionType.AndAlso => "and", ExpressionType.OrElse => "or", _ => throw new NotSupportedException($"Binary operator {node.NodeType}") }; var leftVisitor = new DelegateVisitor(_converter, _methodDepth, _objectInstance); leftVisitor.Visit(node.Left); var rightVisitor = new DelegateVisitor(_converter, _methodDepth, _objectInstance); rightVisitor.Visit(node.Right); Resolution += $"{leftVisitor.Resolution} {op} {rightVisitor.Resolution}"; return(node); }
protected override Expression VisitMethodCall(MethodCallExpression node) { var objectInstance = (node.Object is ConstantExpression constant) ? constant.Value : _objectInstance; var methodCallFormatter = new Func <string, ReadOnlyCollection <Expression>, string>((name, args) => { var arguments = args.Select(arg => { var argVisitor = new DelegateVisitor(_converter, _methodDepth + 1, objectInstance); argVisitor.Visit(arg); return(argVisitor.Resolution); }); return(string.Format(_converter._methodCallFormat, name, string.Join(", ", arguments))); }); var supportMethods = new Func <IEnumerable <MethodInfo> >(() => typeof(Installation).GetMethods(BindingFlags.Instance | BindingFlags.Public) .Where(m => m.GetCustomAttribute <BuiltInSymbolAttribute>() != null)); switch (_methodDepth) { case 1: var supportMethod = supportMethods().FirstOrDefault(m => m.Name == node.Method.Name); if (supportMethod == null) { var handler = BindingSearch.Flags .Select(f => objectInstance.GetType().GetMethod(node.Method.Name, f)) .FirstOrDefault(m => m != null); if (handler == null) { var msg = $"Only class instance methods may be invoked; {node.Method.Name} is not supported"; throw new NotSupportedException(msg); } _converter._visitMethodHandler(node.Method); } break; case 2: var expandConstantMethod = supportMethods().FirstOrDefault(m => m.Name == nameof(Installation.ExpandConstant)); if (expandConstantMethod == null || node.Method.MetadataToken != expandConstantMethod.MetadataToken) { var msg = $"Only the method call {nameof(Installation.ExpandConstant)} is allowed at this depth; {node.Method.Name} is not supported"; throw new NotSupportedException(msg); } break; default: methodCallFormatter = (name, args) => { var visitor = new InvokingVisitor(objectInstance); visitor.Visit(node); return(visitor.Results[0] is string?$"'{visitor.Results[0]}'" : visitor.Results[0].ToString()); }; break; } var methodName = node.Method.GetCustomAttribute <AliasAttribute>()?.Name ?? node.Method.Name; Resolution += methodCallFormatter(methodName, node.Arguments); return(node); }
public override Node Evaluate(Env env) { OriginalPath = OriginalPath.Evaluate(env); var quoted = OriginalPath as Quoted; if (quoted != null) { Path = quoted.Value; } ImportAction action = GetImportAction(env.Parser.Importer); if (action == Importers.ImportAction.ImportNothing) { return(new NodeList().ReducedFrom <NodeList>(this)); } Node features = null; if (Features) { features = Features.Evaluate(env); } if (action == ImportAction.LeaveImport) { return(new Import(OriginalPath, features)); } if (action == ImportAction.ImportCss) { var importCss = new Import(OriginalPath, null) { _importAction = ImportAction.ImportCss, InnerContent = InnerContent }; if (features) { return(new Media(features, new NodeList() { importCss })); } return(importCss); } using (env.Parser.Importer.BeginScope(this)) { if (IsReference || IsOptionSet(ImportOptions, ImportOptions.Reference)) { // Walk the parse tree and mark all nodes as references. IsReference = true; IVisitor referenceImporter = null; referenceImporter = DelegateVisitor.For <Node>(node => { var ruleset = node as Ruleset; if (ruleset != null) { if (ruleset.Selectors != null) { ruleset.Selectors.Accept(referenceImporter); ruleset.Selectors.IsReference = true; } if (ruleset.Rules != null) { ruleset.Rules.Accept(referenceImporter); ruleset.Rules.IsReference = true; } } var media = node as Media; if (media != null) { media.Ruleset.Accept(referenceImporter); } var nodeList = node as NodeList; if (nodeList != null) { nodeList.Accept(referenceImporter); } node.IsReference = true; return(node); }); Accept(referenceImporter); } NodeHelper.ExpandNodes <Import>(env, InnerRoot.Rules); } var rulesList = new NodeList(InnerRoot.Rules).ReducedFrom <NodeList>(this); if (features) { return(new Media(features, rulesList)); } return(rulesList); }