private void VisitDefault(SpecialNode specialNode, SpecialNodeInspector inspector) { Frame frame = null; if (!specialNode.Element.IsEmptyElement) { ScopeChunk item = new ScopeChunk { Position = this.Locate(specialNode.Element) }; this.Chunks.Add(item); frame = new Frame(this, item.Body); } AttributeNode attr = inspector.TakeAttribute("type"); Snippets snippets = (attr != null) ? this.AsCode(attr) : "var"; foreach (AttributeNode node2 in inspector.Attributes) { DefaultVariableChunk chunk3 = new DefaultVariableChunk { Type = snippets, Name = node2.Name, Value = this.AsTextOrientedCode(node2), Position = this.Locate(node2) }; this.Chunks.Add(chunk3); } base.Accept(specialNode.Body); if (frame != null) { frame.Dispose(); } }
public void ScopeTest() { var scope1 = new ScopeChunk(); scope1.Body.Add(new LocalVariableChunk { Name = "x", Value = "4" }); scope1.Body.Add(new SendExpressionChunk { Code = "x" }); var scope2 = new ScopeChunk(); scope2.Body.Add(new LocalVariableChunk { Name = "x", Value = "2" }); scope2.Body.Add(new SendExpressionChunk { Code = "x" }); var chunks = Chunks(scope1, scope2); _compiler.CompileView(chunks, chunks); var contents = ExecuteView(); Assert.AreEqual(contents, "42"); }
protected override void Visit(ScopeChunk chunk) { AppendOpenBrace(); CodeDefault(); Accept(chunk.Body); AppendCloseBrace(); }
private void VisitSection(SpecialNode node, SpecialNodeInspector inspector) { if (SectionChunks == null) { throw new CompilerException("Section cannot be used at this location", Locate(node.Element)); } var name = inspector.TakeAttribute("name"); if (name == null) { throw new CompilerException("Section element must have a name attribute", Locate(node.Element)); } IList <Chunk> sectionChunks; if (!SectionChunks.TryGetValue(name.Value, out sectionChunks)) { sectionChunks = new List <Chunk>(); SectionChunks.Add(name.Value, sectionChunks); } var scope = new ScopeChunk { Position = Locate(inspector.OriginalNode) }; sectionChunks.Add(scope); using (new Frame(this, scope.Body)) { foreach (var attr in inspector.Attributes) { Chunks.Add(new LocalVariableChunk { Name = attr.Name, Value = AsCode(attr), Position = Locate(attr) }); } Accept(inspector.Body); } }
private void VisitSection(SpecialNode node, SpecialNodeInspector inspector) { IList <Chunk> list; if (this.SectionChunks == null) { throw new CompilerException("Section cannot be used at this location", this.Locate(node.Element)); } AttributeNode node2 = inspector.TakeAttribute("name"); if (node2 == null) { throw new CompilerException("Section element must have a name attribute", this.Locate(node.Element)); } if (!this.SectionChunks.TryGetValue(node2.Value, out list)) { list = new List <Chunk>(); this.SectionChunks.Add(node2.Value, list); } ScopeChunk item = new ScopeChunk { Position = this.Locate(inspector.OriginalNode) }; list.Add(item); using (new Frame(this, item.Body)) { foreach (AttributeNode node3 in inspector.Attributes) { LocalVariableChunk chunk2 = new LocalVariableChunk { Name = node3.Name, Value = this.AsCode(node3), Position = this.Locate(node3) }; this.Chunks.Add(chunk2); } base.Accept(inspector.Body); } }
private void VisitVar(SpecialNode specialNode, SpecialNodeInspector inspector) { Frame frame = null; if (!specialNode.Element.IsEmptyElement) { var scope = new ScopeChunk { Position = Locate(specialNode.Element) }; Chunks.Add(scope); frame = new Frame(this, scope.Body); } var typeAttr = inspector.TakeAttribute("type"); var type = typeAttr != null ? AsCode(typeAttr) : (Snippets)"var"; foreach (var attr in inspector.Attributes) { Chunks.Add(new LocalVariableChunk { Type = type, Name = attr.Name, Value = AsCode(attr), Position = Locate(attr) }); } Accept(specialNode.Body); if (frame != null) frame.Dispose(); }
private void VisitSection(SpecialNode node, SpecialNodeInspector inspector) { if (SectionChunks == null) throw new CompilerException("Section cannot be used at this location", Locate(node.Element)); var name = inspector.TakeAttribute("name"); if (name == null) throw new CompilerException("Section element must have a name attribute", Locate(node.Element)); IList<Chunk> sectionChunks; if (!SectionChunks.TryGetValue(name.Value, out sectionChunks)) { sectionChunks = new List<Chunk>(); SectionChunks.Add(name.Value, sectionChunks); } var scope = new ScopeChunk { Position = Locate(inspector.OriginalNode) }; sectionChunks.Add(scope); using (new Frame(this, scope.Body)) { foreach (var attr in inspector.Attributes) { Chunks.Add(new LocalVariableChunk { Name = attr.Name, Value = AsCode(attr), Position = Locate(attr) }); } Accept(inspector.Body); } }
protected override void Visit(AttributeNode attributeNode) { var accumulatedNodes = new List<Node>(); var processedNodes = new List<Node>(); foreach (var node in attributeNode.Nodes) { if (node is ConditionNode) { // condition nodes take the prior unconditional nodes as content var conditionNode = (ConditionNode)node; MovePriorNodesUnderCondition(conditionNode, accumulatedNodes); // prior nodes and condition are set for output processedNodes.AddRange(accumulatedNodes); processedNodes.Add(conditionNode); accumulatedNodes.Clear(); } else { // other types add to the unconditional list accumulatedNodes.Add(node); } } processedNodes.AddRange(accumulatedNodes); var allNodesAreConditional = processedNodes.All(node => node is ConditionNode); if (allNodesAreConditional == false || processedNodes.Any() == false) { // This attribute may not disapper - send it literally AddLiteral(" " + attributeNode.Name + "=\""); foreach (var node in processedNodes) Accept(node); AddLiteral("\""); } else { var scope = new ScopeChunk(); scope.Body.Add(new LocalVariableChunk { Name = "__just__once__", Value = new Snippets("0") }); _sendAttributeOnce = new ConditionalChunk { Type = ConditionalType.If, Condition = new Snippets("__just__once__ < 1") }; _sendAttributeOnce.Body.Add(new SendLiteralChunk { Text = " " + attributeNode.Name + "=\"" }); _sendAttributeIncrement = new AssignVariableChunk { Name = "__just__once__", Value = "1" }; Chunks.Add(scope); using (new Frame(this, scope.Body)) { foreach (var node in processedNodes) Accept(node); } _sendAttributeOnce = null; _sendAttributeIncrement = null; var ifWasSent = new ConditionalChunk { Type = ConditionalType.If, Condition = new Snippets("__just__once__ > 0") }; scope.Body.Add(ifWasSent); ifWasSent.Body.Add(new SendLiteralChunk { Text = "\"" }); } }
protected override void Visit(ScopeChunk chunk) { this.AppendOpenScope(); base.Accept(chunk.Body); this.AppendCloseScope(); }
protected override void Visit(ScopeChunk chunk) { Accept(chunk.Body); }
protected override void Visit(ScopeChunk chunk) { AppendOpenScope(); Accept(chunk.Body); AppendCloseScope(); }
private void VisitUse(SpecialNode specialNode, SpecialNodeInspector inspector) { AttributeNode node = inspector.TakeAttribute("file"); if (node != null) { ScopeChunk item = new ScopeChunk { Position = this.Locate(inspector.OriginalNode) }; this.Chunks.Add(item); using (new Frame(this, item.Body)) { foreach (AttributeNode node2 in inspector.Attributes) { LocalVariableChunk chunk2 = new LocalVariableChunk { Name = node2.Name, Value = this.AsTextOrientedCode(node2), Position = this.Locate(node2) }; this.Chunks.Add(chunk2); } RenderPartialChunk chunk3 = new RenderPartialChunk { Name = node.Value, Position = this.Locate(inspector.OriginalNode) }; this.Chunks.Add(chunk3); using (new Frame(this, chunk3.Body, chunk3.Sections)) { base.Accept(inspector.Body); } return; } } AttributeNode node3 = inspector.TakeAttribute("content"); AttributeNode attr = inspector.TakeAttribute("namespace"); AttributeNode node5 = inspector.TakeAttribute("assembly"); AttributeNode node6 = inspector.TakeAttribute("import"); AttributeNode node7 = inspector.TakeAttribute("master"); AttributeNode node8 = inspector.TakeAttribute("pageBaseType"); if (node3 != null) { UseContentChunk chunk6 = new UseContentChunk { Name = node3.Value, Position = this.Locate(inspector.OriginalNode) }; this.Chunks.Add(chunk6); using (new Frame(this, chunk6.Default)) { base.Accept(specialNode.Body); return; } } if ((attr != null) || (node5 != null)) { if (attr != null) { UseNamespaceChunk chunk = new UseNamespaceChunk { Namespace = this.AsCode(attr) }; this.AddUnordered(chunk); } if (node5 != null) { UseAssemblyChunk chunk10 = new UseAssemblyChunk { Assembly = node5.Value }; this.AddUnordered(chunk10); } } else if (node6 != null) { UseImportChunk chunk12 = new UseImportChunk { Name = node6.Value }; this.AddUnordered(chunk12); } else if (node7 != null) { UseMasterChunk chunk14 = new UseMasterChunk { Name = node7.Value }; this.AddUnordered(chunk14); } else { if (node8 == null) { throw new CompilerException("Special node use had no understandable attributes"); } PageBaseTypeChunk chunk16 = new PageBaseTypeChunk { BaseClass = this.AsCode(node8) }; this.AddUnordered(chunk16); } }
private void VisitRender(SpecialNode node, SpecialNodeInspector inspector) { AttributeNode node2 = inspector.TakeAttribute("partial"); if (node2 != null) { ScopeChunk chunk = new ScopeChunk { Position = this.Locate(inspector.OriginalNode) }; this.Chunks.Add(chunk); using (new Frame(this, chunk.Body)) { foreach (AttributeNode node3 in inspector.Attributes) { LocalVariableChunk chunk2 = new LocalVariableChunk { Name = node3.Name, Value = this.AsTextOrientedCode(node3), Position = this.Locate(node3) }; this.Chunks.Add(chunk2); } RenderPartialChunk chunk3 = new RenderPartialChunk { Name = node2.Value, Position = this.Locate(inspector.OriginalNode) }; this.Chunks.Add(chunk3); using (new Frame(this, chunk3.Body, chunk3.Sections)) { base.Accept(inspector.Body); } return; } } AttributeNode node4 = inspector.TakeAttribute("segment"); if (base.Context.ParseSectionTagAsSegment && (node4 == null)) { node4 = inspector.TakeAttribute("section"); } string str = null; if (node4 != null) { str = node4.Value; } ScopeChunk item = new ScopeChunk { Position = this.Locate(inspector.OriginalNode) }; this.Chunks.Add(item); using (new Frame(this, item.Body)) { foreach (AttributeNode node5 in inspector.Attributes) { LocalVariableChunk chunk7 = new LocalVariableChunk { Name = node5.Name, Value = this.AsTextOrientedCode(node5), Position = this.Locate(node5) }; this.Chunks.Add(chunk7); } RenderSectionChunk chunk8 = new RenderSectionChunk { Name = str }; this.Chunks.Add(chunk8); using (new Frame(this, chunk8.Default)) { base.Accept(inspector.Body); } } }
protected override void Visit(AttributeNode attributeNode) { List <Node> priorNodes = new List <Node>(); List <Node> source = new List <Node>(); foreach (Node node in attributeNode.Nodes) { if (node is ConditionNode) { ConditionNode condition = (ConditionNode)node; MovePriorNodesUnderCondition(condition, priorNodes); source.AddRange(priorNodes); source.Add(condition); priorNodes.Clear(); } else { priorNodes.Add(node); } } source.AddRange(priorNodes); if (!source.All <Node>(node => (node is ConditionNode)) || !source.Any <Node>()) { this.AddLiteral(string.Format(" {0}={1}", attributeNode.Name, attributeNode.QuotChar)); foreach (Node node3 in source) { base.Accept(node3); } this.AddLiteral(attributeNode.QuotChar.ToString()); } else { ScopeChunk item = new ScopeChunk(); LocalVariableChunk chunk3 = new LocalVariableChunk { Name = "__just__once__", Value = new Snippets("0") }; item.Body.Add(chunk3); ConditionalChunk chunk4 = new ConditionalChunk { Type = ConditionalType.If, Condition = new Snippets("__just__once__ < 1") }; this._sendAttributeOnce = chunk4; SendLiteralChunk chunk5 = new SendLiteralChunk { Text = " " + attributeNode.Name + "=\"" }; this._sendAttributeOnce.Body.Add(chunk5); AssignVariableChunk chunk6 = new AssignVariableChunk { Name = "__just__once__", Value = "1" }; this._sendAttributeIncrement = chunk6; this.Chunks.Add(item); using (new Frame(this, item.Body)) { foreach (Node node4 in source) { base.Accept(node4); } } this._sendAttributeOnce = null; this._sendAttributeIncrement = null; ConditionalChunk chunk2 = new ConditionalChunk { Type = ConditionalType.If, Condition = new Snippets("__just__once__ > 0") }; item.Body.Add(chunk2); SendLiteralChunk chunk8 = new SendLiteralChunk { Text = "\"" }; chunk2.Body.Add(chunk8); } }
protected abstract void Visit(ScopeChunk chunk);
protected override void Visit(ScopeChunk chunk) { PushScope(); CodeIndent(chunk).AppendLine("{"); CodeDefault(); Indent += 4; Accept(chunk.Body); Indent -= 4; AppendIndent().AppendLine("}"); PopScope(); }
protected override void Visit(ScopeChunk chunk) { _source.AppendLine("{"); base.Visit(chunk); _source.AppendLine("}"); }
protected override void Visit(ScopeChunk chunk) { this._source.AppendLine("{"); base.Visit(chunk); this._source.AppendLine("}"); }
private void VisitRender(SpecialNode node, SpecialNodeInspector inspector) { var partial = inspector.TakeAttribute("partial"); if (partial != null) { var scope = new ScopeChunk { Position = Locate(inspector.OriginalNode) }; Chunks.Add(scope); using (new Frame(this, scope.Body)) { foreach (var attr in inspector.Attributes) { Chunks.Add(new LocalVariableChunk { Name = attr.Name, Value = AsCode(attr), Position = Locate(attr) }); } var renderPartial = new RenderPartialChunk { Name = partial.Value, Position = Locate(inspector.OriginalNode) }; Chunks.Add(renderPartial); using (new Frame(this, renderPartial.Body, renderPartial.Sections)) { Accept(inspector.Body); } } } else { var sectionAttr = inspector.TakeAttribute("section"); string sectionName = null; if (sectionAttr != null) sectionName = sectionAttr.Value; var scope = new ScopeChunk { Position = Locate(inspector.OriginalNode) }; Chunks.Add(scope); using (new Frame(this, scope.Body)) { foreach (var attr in inspector.Attributes) { Chunks.Add(new LocalVariableChunk { Name = attr.Name, Value = AsCode(attr), Position = Locate(attr) }); } var render = new RenderSectionChunk { Name = sectionName }; Chunks.Add(render); using (new Frame(this, render.Default)) { Accept(inspector.Body); } } } }
protected override void Visit(ScopeChunk chunk) { _variables.PushScope(); Accept(chunk.Body); _variables.PopScope(); }
private void VisitUse(SpecialNode specialNode, SpecialNodeInspector inspector) { var file = inspector.TakeAttribute("file"); if (file != null) { var scope = new ScopeChunk { Position = Locate(inspector.OriginalNode) }; Chunks.Add(scope); using (new Frame(this, scope.Body)) { foreach (var attr in inspector.Attributes) { Chunks.Add(new LocalVariableChunk { Name = attr.Name, Value = AsCode(attr), Position = Locate(attr) }); } var useFileChunk = new RenderPartialChunk { Name = file.Value, Position = Locate(inspector.OriginalNode) }; Chunks.Add(useFileChunk); using (new Frame(this, useFileChunk.Body, useFileChunk.Sections)) { Accept(inspector.Body); } } } else { var contentAttr = inspector.TakeAttribute("content"); var namespaceAttr = inspector.TakeAttribute("namespace"); var assemblyAttr = inspector.TakeAttribute("assembly"); var importAttr = inspector.TakeAttribute("import"); var masterAttr = inspector.TakeAttribute("master"); var pageBaseTypeAttr = inspector.TakeAttribute("pageBaseType"); if (contentAttr != null) { var useContentChunk = new UseContentChunk { Name = contentAttr.Value, Position = Locate(inspector.OriginalNode) }; Chunks.Add(useContentChunk); using (new Frame(this, useContentChunk.Default)) { Accept(specialNode.Body); } } else if (namespaceAttr != null || assemblyAttr != null) { if (namespaceAttr != null) { var useNamespaceChunk = new UseNamespaceChunk { Namespace = AsCode(namespaceAttr) }; AddUnordered(useNamespaceChunk); } if (assemblyAttr != null) { var useAssemblyChunk = new UseAssemblyChunk { Assembly = assemblyAttr.Value }; AddUnordered(useAssemblyChunk); } } else if (importAttr != null) { var useImportChunk = new UseImportChunk { Name = importAttr.Value }; AddUnordered(useImportChunk); } else if (masterAttr != null) { var useMasterChunk = new UseMasterChunk { Name = masterAttr.Value }; AddUnordered(useMasterChunk); } else if (pageBaseTypeAttr != null) { var usePageBaseTypeChunk = new PageBaseTypeChunk { BaseClass = AsCode(pageBaseTypeAttr) }; AddUnordered(usePageBaseTypeChunk); } else { throw new CompilerException("Special node use had no understandable attributes"); } } }
protected override void Visit(AttributeNode attributeNode) { var accumulatedNodes = new List <Node>(); var processedNodes = new List <Node>(); foreach (var node in attributeNode.Nodes) { if (node is ConditionNode) { // condition nodes take the prior unconditional nodes as content var conditionNode = (ConditionNode)node; MovePriorNodesUnderCondition(conditionNode, accumulatedNodes); // prior nodes and condition are set for output processedNodes.AddRange(accumulatedNodes); processedNodes.Add(conditionNode); accumulatedNodes.Clear(); } else { // other types add to the unconditional list accumulatedNodes.Add(node); } } processedNodes.AddRange(accumulatedNodes); var allNodesAreConditional = processedNodes.All(node => node is ConditionNode); if (allNodesAreConditional == false || processedNodes.Any() == false) { // This attribute may not disapper - send it literally AddLiteral(" " + attributeNode.Name + "=\""); foreach (var node in processedNodes) { Accept(node); } AddLiteral("\""); } else { var scope = new ScopeChunk(); scope.Body.Add(new LocalVariableChunk { Name = "__just__once__", Value = new Snippets("0") }); _sendAttributeOnce = new ConditionalChunk { Type = ConditionalType.If, Condition = new Snippets("__just__once__ < 1") }; _sendAttributeOnce.Body.Add(new SendLiteralChunk { Text = " " + attributeNode.Name + "=\"" }); _sendAttributeIncrement = new AssignVariableChunk { Name = "__just__once__", Value = "1" }; Chunks.Add(scope); using (new Frame(this, scope.Body)) { foreach (var node in processedNodes) { Accept(node); } } _sendAttributeOnce = null; _sendAttributeIncrement = null; var ifWasSent = new ConditionalChunk { Type = ConditionalType.If, Condition = new Snippets("__just__once__ > 0") }; scope.Body.Add(ifWasSent); ifWasSent.Body.Add(new SendLiteralChunk { Text = "\"" }); } }
private void VisitRender(SpecialNode node, SpecialNodeInspector inspector) { var partial = inspector.TakeAttribute("partial"); if (partial != null) { var scope = new ScopeChunk { Position = Locate(inspector.OriginalNode) }; Chunks.Add(scope); using (new Frame(this, scope.Body)) { foreach (var attr in inspector.Attributes) { Chunks.Add(new LocalVariableChunk { Name = attr.Name, Value = AsCode(attr), Position = Locate(attr) }); } var renderPartial = new RenderPartialChunk { Name = partial.Value, Position = Locate(inspector.OriginalNode) }; Chunks.Add(renderPartial); using (new Frame(this, renderPartial.Body, renderPartial.Sections)) { Accept(inspector.Body); } } } else { var sectionAttr = inspector.TakeAttribute("section"); string sectionName = null; if (sectionAttr != null) { sectionName = sectionAttr.Value; } var scope = new ScopeChunk { Position = Locate(inspector.OriginalNode) }; Chunks.Add(scope); using (new Frame(this, scope.Body)) { foreach (var attr in inspector.Attributes) { Chunks.Add(new LocalVariableChunk { Name = attr.Name, Value = AsCode(attr), Position = Locate(attr) }); } var render = new RenderSectionChunk { Name = sectionName }; Chunks.Add(render); using (new Frame(this, render.Default)) { Accept(inspector.Body); } } } }