/// <summary> /// Compiles an Ultraviolet Style Sheet (UVSS) document from the specified abstract syntax tree. /// </summary> /// <param name="uv">The Ultraviolet context.</param> /// <param name="tree">A <see cref="UvssDocumentSyntax"/> that represents the /// abstract syntax tree to compile.</param> /// <returns>A new instance of <see cref="UvssDocument"/> that represents the compiled data.</returns> public static UvssDocument Compile(UltravioletContext uv, UvssDocumentSyntax tree) { Contract.Require(uv, nameof(uv)); Contract.Require(tree, nameof(tree)); return(UvssCompiler.Compile(uv, tree)); }
/// <summary> /// Compiles an Ultraviolet Style Sheet (UVSS) document from the specified abstract syntax tree. /// </summary> /// <param name="uv">The Ultraviolet context.</param> /// <param name="tree">A <see cref="UvssDocumentSyntax"/> that represents the /// abstract syntax tree to compile.</param> /// <returns>A new instance of <see cref="UvssDocument"/> that represents the compiled data.</returns> public static UvssDocument Compile(UltravioletContext uv, UvssDocumentSyntax tree) { Contract.Require(uv, nameof(uv)); // Fail to compile if the tree reports any error diagnostics. var errors = tree.GetDiagnostics().Where(x => x.Severity == DiagnosticSeverity.Error); if (errors.Any()) { throw new UvssException(PresentationStrings.StyleSheetParserError, errors.Select(x => UvssError.FromDiagnosticInfo(x))); } // Determine the document's culture. var cultureDirective = GetLastDirective <UvssCultureDirectiveSyntax>(tree); var culture = (cultureDirective == null) ? UvssDocument.DefaultCulture : CultureInfo.GetCultureInfo(cultureDirective.CultureValue.Value); // Compile a list of rule sets and storyboards. var docRuleSets = new List <UvssRuleSet>(); var docStoryboards = new List <UvssStoryboard>(); for (int i = 0; i < tree.Content.Count; i++) { var contentNode = tree.Content[i]; switch (contentNode.Kind) { case SyntaxKind.RuleSet: var ruleSet = CompileRuleSet((UvssRuleSetSyntax)contentNode, culture); docRuleSets.Add(ruleSet); break; case SyntaxKind.Storyboard: var storyboard = CompileStoryboard((UvssStoryboardSyntax)contentNode, culture); docStoryboards.Add(storyboard); break; case SyntaxKind.CultureDirective: /* Skip directives */ break; default: throw new UvssException(PresentationStrings.StyleSheetParserError); } } return(new UvssDocument(uv, docRuleSets, docStoryboards)); }
/// <inheritdoc/> public override SyntaxNode VisitDocument(UvssDocumentSyntax node) { var unchanged = true; var newContent = VisitList(node.Content); if (newContent.Node != node.Content.Node) { unchanged = false; } var newEndOfFileToken = (SyntaxToken)Visit(node.EndOfFileToken); if (newEndOfFileToken != node.EndOfFileToken) { unchanged = false; } return(unchanged ? node : new UvssDocumentSyntax( newContent, newEndOfFileToken)); }
/// <summary> /// Visits the specified document node. /// </summary> /// <param name="node">The node to visit.</param> /// <returns>A node which should replace the visited node, or a reference to the visited node /// itself if no changes were made.</returns> public virtual SyntaxNode VisitDocument(UvssDocumentSyntax node) { return(VisitSyntaxNode(node)); }
/// <summary> /// Gets the last directive of the specified type within the specified document. /// </summary> private static TDirective GetLastDirective <TDirective>(UvssDocumentSyntax document) where TDirective : UvssDirectiveSyntax { return(document.Directives.OfType <TDirective>().LastOrDefault()); }
/// <summary> /// Initializes a new instance of the <see cref="UvssTextParserResult"/> structure. /// </summary> /// <param name="snapshot">The snapshot from which the document was generated.</param> /// <param name="document">The document that was generated by the parser.</param> public UvssTextParserResult(ITextSnapshot snapshot, UvssDocumentSyntax document) { this.Snapshot = snapshot; this.Document = document; }