public void IterationInPartialFile() { var viewFolder = new InMemoryViewFolder { {"home\\index.spark", "<Guts items='new[]{1,2,3}'><section:each>${xIndex}</section:each></Guts>"}, {"home\\_Guts.spark", "<for each='var x in items'><render:each/></for>"} }; var loader = new ViewLoader { SyntaxProvider = new DefaultSyntaxProvider(ParserSettings.DefaultBehavior), ViewFolder = viewFolder }; var chunks = loader.Load("home\\index.spark"); var detectCode = new DetectCodeExpressionVisitor(null); var index = detectCode.Add("xIndex"); var count = detectCode.Add("xCount"); detectCode.Accept(chunks); Assert.IsTrue(index.Detected); Assert.IsFalse(count.Detected); }
public void FindLoopParameters() { var context = new VisitorContext { SyntaxProvider = new DefaultSyntaxProvider(new SparkSettings()) }; var nodes = ParseNodes("<for each='var x in new [] {1,2,3}'>${xIndex}${xIsLast}</for>", new SpecialNodeVisitor(context)); var visitor = new ChunkBuilderVisitor(context); visitor.Accept(nodes); var expressionVisitor = new DetectCodeExpressionVisitor(null); var index = expressionVisitor.Add("xIndex"); var count = expressionVisitor.Add("xCount"); var isFirst = expressionVisitor.Add("xIsFirst"); var isLast = expressionVisitor.Add("xIsLast"); expressionVisitor.Accept(visitor.Chunks); Assert.IsTrue(index.Detected); Assert.IsFalse(count.Detected); Assert.IsFalse(isFirst.Detected); Assert.IsTrue(isLast.Detected); }
protected override void Visit(ForEachChunk chunk) { var terms = chunk.Code.ToString().Split(' ', '\r', '\n', '\t').ToList(); var inIndex = terms.IndexOf("in"); var variableName = (inIndex < 2 ? null : terms[inIndex - 1]); if (variableName == null) { CodeIndent(chunk) .Write("foreach(") .WriteCode(chunk.Code) .WriteLine(")"); CodeDefault(); AppendOpenBrace(); Accept(chunk.Body); AppendCloseBrace(); } else { var detect = new DetectCodeExpressionVisitor(OuterPartial); var autoIndex = detect.Add(variableName + "Index"); var autoCount = detect.Add(variableName + "Count"); var autoIsFirst = detect.Add(variableName + "IsFirst"); var autoIsLast = detect.Add(variableName + "IsLast"); detect.Accept(chunk.Body); if (autoIsLast.Detected) { autoIndex.Detected = true; autoCount.Detected = true; } AppendOpenBrace(); if (autoIndex.Detected) { DeclareVariable(variableName + "Index"); _source.WriteLine("int {0}Index = 0;", variableName); } if (autoIsFirst.Detected) { DeclareVariable(variableName + "IsFirst"); _source.WriteLine("bool {0}IsFirst = true;", variableName); } if (autoCount.Detected) { DeclareVariable(variableName + "Count"); var collectionCode = string.Join(" ", terms.ToArray(), inIndex + 1, terms.Count - inIndex - 1); _source.WriteLine("int {0}Count = global::Spark.Compiler.CollectionUtility.Count({1});", variableName, collectionCode); } CodeIndent(chunk) .Write("foreach(") .WriteCode(chunk.Code) .WriteLine(")"); CodeDefault(); AppendOpenBrace(); DeclareVariable(variableName); CodeHidden(); if (autoIsLast.Detected) { DeclareVariable(variableName + "IsLast"); _source.WriteLine("bool {0}IsLast = ({0}Index == {0}Count - 1);", variableName); } CodeDefault(); Accept(chunk.Body); CodeHidden(); if (autoIndex.Detected) _source.WriteLine("++{0}Index;", variableName); if (autoIsFirst.Detected) _source.WriteLine("{0}IsFirst = false;", variableName); CodeDefault(); AppendCloseBrace(); AppendCloseBrace(); } }
protected override void Visit(ForEachChunk chunk) { var terms = chunk.Code.ToString().Split(' ', '\r', '\n', '\t').ToList(); var inIndex = terms.FindIndex(IsIn); var inOrAsIndex = terms.FindIndex(IsInOrAs); var variableName = (inOrAsIndex < 1 ? null : terms[inOrAsIndex - 1]); if (variableName == null) { CodeIndent(chunk) .Write("For Each ") .WriteLine(chunk.Code) .AddIndent(); CodeDefault(); PushScope(); Accept(chunk.Body); _source .RemoveIndent() .WriteLine("Next"); PopScope(); } else { var detect = new DetectCodeExpressionVisitor(OuterPartial); var autoIndex = detect.Add(variableName + "Index"); var autoCount = detect.Add(variableName + "Count"); var autoIsFirst = detect.Add(variableName + "IsFirst"); var autoIsLast = detect.Add(variableName + "IsLast"); detect.Accept(chunk.Body); if (autoIsLast.Detected) { autoIndex.Detected = true; autoCount.Detected = true; } AppendOpenScope(); if (autoIndex.Detected) { DeclareVariable(variableName + "Index"); _source.WriteLine("Dim {0}Index As Integer = 0", variableName); } if (autoIsFirst.Detected) { DeclareVariable(variableName + "IsFirst"); _source.WriteLine("Dim {0}IsFirst As Boolean = True", variableName); } if (autoCount.Detected) { DeclareVariable(variableName + "Count"); var collectionCode = string.Join(" ", terms.ToArray(), inIndex + 1, terms.Count - inIndex - 1); _source.WriteLine("Dim {0}Count As Integer = Global.Spark.Compiler.CollectionUtility.Count({1})", variableName, collectionCode); } CodeIndent(chunk) .Write("For Each ") .WriteLine(chunk.Code) .AddIndent(); CodeDefault(); PushScope(); DeclareVariable(variableName); CodeHidden(); if (autoIsLast.Detected) { DeclareVariable(variableName + "IsLast"); _source.WriteLine("Dim {0}IsLast As Boolean = ({0}Index = {0}Count - 1)", variableName); } CodeDefault(); Accept(chunk.Body); CodeHidden(); if (autoIndex.Detected) _source.WriteLine("{0}Index = {0}Index + 1", variableName); if (autoIsFirst.Detected) _source.WriteLine("{0}IsFirst = False", variableName); CodeDefault(); PopScope(); _source.RemoveIndent().WriteLine("Next"); AppendCloseScope(); } }
protected override void Visit(ForEachChunk chunk) { var forEach = new ForEachInspector(chunk.Code); if (!forEach.Recognized) { _source.Write("for ").Write(chunk.Code).WriteLine(":"); _source.Indent++; _variables.PushScope(); Accept(chunk.Body); _source.WriteLine("pass"); _variables.PopScope(); _source.Indent--; return; } _variables.PushScope(); var detect = new DetectCodeExpressionVisitor(OuterPartial); var autoIndex = detect.Add(forEach.VariableName + "Index"); var autoCount = detect.Add(forEach.VariableName + "Count"); var autoIsFirst = detect.Add(forEach.VariableName + "IsFirst"); var autoIsLast = detect.Add(forEach.VariableName + "IsLast"); detect.Accept(chunk.Body); if (autoIsLast.Detected) { autoIndex.Detected = true; autoCount.Detected = true; } if (autoIndex.Detected) { _variables.Declare(forEach.VariableName + "Index"); _source.Write(forEach.VariableName).WriteLine("Index=0"); } if (autoIsFirst.Detected) { _variables.Declare(forEach.VariableName + "IsFirst"); _source.Write(forEach.VariableName).WriteLine("IsFirst=True"); } if (autoCount.Detected) { _variables.Declare(forEach.VariableName + "Count"); _source .Write(forEach.VariableName).Write("Count=len([0 for ") .Write(chunk.Code).WriteLine("])"); } _variables.Declare(forEach.VariableName); _source.Write("for ").Write(chunk.Code).WriteLine(":"); _source.Indent++; if (autoIsLast.Detected) { _variables.Declare(forEach.VariableName + "IsLast"); _source .Write(forEach.VariableName).Write("IsLast=(") .Write(forEach.VariableName).Write("Index==") .Write(forEach.VariableName).WriteLine("Count - 1)"); } Accept(chunk.Body); if (autoIndex.Detected) { _source .Write(forEach.VariableName).Write("Index=") .Write(forEach.VariableName).WriteLine("Index+1"); } if (autoIsFirst.Detected) { _source.Write(forEach.VariableName).WriteLine("IsFirst=False"); } _source.WriteLine("pass"); _source.Indent--; _variables.PopScope(); }
protected override void Visit(ForEachChunk chunk) { var terms = chunk.Code.ToString().Split(' ', '\r', '\n', '\t').ToList(); var inIndex = terms.IndexOf("in"); var variableName = (inIndex < 2 ? null : terms[inIndex - 1]); if (variableName == null) { CodeIndent(chunk) .Append("foreach(") .Append(chunk.Code) .AppendLine(")"); CodeDefault(); PushScope(); AppendIndent().AppendLine("{"); Indent += 4; Accept(chunk.Body); Indent -= 4; AppendIndent().AppendLine(string.Format("}} //foreach {0}", chunk.Code.ToString().Replace("\r", "").Replace("\n", " "))); PopScope(); } else { var detect = new DetectCodeExpressionVisitor(OuterPartial); var autoIndex = detect.Add(variableName + "Index"); var autoCount = detect.Add(variableName + "Count"); var autoIsFirst = detect.Add(variableName + "IsFirst"); var autoIsLast = detect.Add(variableName + "IsLast"); detect.Accept(chunk.Body); if (autoIsLast.Detected) { autoIndex.Detected = true; autoCount.Detected = true; } PushScope(); AppendIndent().AppendLine("{"); if (autoIndex.Detected) { DeclareVariable(variableName + "Index"); _source.Append(' ', Indent + 4).AppendFormat("int {0}Index = 0;\r\n", variableName); } if (autoIsFirst.Detected) { DeclareVariable(variableName + "IsFirst"); _source.Append(' ', Indent + 4).AppendFormat("bool {0}IsFirst = true;\r\n", variableName); } if (autoCount.Detected) { DeclareVariable(variableName + "Count"); string collectionCode = string.Join(" ", terms.ToArray(), inIndex + 1, terms.Count - inIndex - 1); _source.Append(' ', Indent + 4).AppendFormat("int {0}Count = global::Spark.Compiler.CollectionUtility.Count({1});\r\n", variableName, collectionCode); } Indent += 4; CodeIndent(chunk) .Append("foreach(") .Append(chunk.Code) .AppendLine(")"); CodeDefault(); PushScope(); DeclareVariable(variableName); _source.Append(' ', Indent).AppendLine("{"); CodeHidden(); if (autoIsLast.Detected) { DeclareVariable(variableName + "IsLast"); _source.Append(' ', Indent + 4).AppendFormat("bool {0}IsLast = ({0}Index == {0}Count - 1);\r\n", variableName); } CodeDefault(); Indent += 8; Accept(chunk.Body); Indent -= 8; CodeHidden(); if (autoIndex.Detected) _source.Append(' ', Indent + 4).AppendFormat("++{0}Index;\r\n", variableName); if (autoIsFirst.Detected) _source.Append(' ', Indent + 4).AppendFormat("{0}IsFirst = false;\r\n", variableName); CodeDefault(); _source.Append(' ').AppendLine("}"); PopScope(); AppendIndent().AppendFormat("}} //foreach {0}\r\n", chunk.Code.ToString().Replace("\r", "").Replace("\n", " ")); PopScope(); } }
public void ParametersInCallerBody() { var viewFolder = new InMemoryViewFolder { {"home\\index.spark", "<for each='var x in new[]{1,2,3}'><Guts>${xIndex}</Guts></for>"}, {"home\\_Guts.spark", "<p><render/></p>"} }; var loader = new ViewLoader { SyntaxProvider = new DefaultSyntaxProvider(ParserSettings.DefaultBehavior), ViewFolder = viewFolder }; var chunks = loader.Load("home\\index.spark"); var detectCode = new DetectCodeExpressionVisitor(null); var index = detectCode.Add("xIndex"); var count = detectCode.Add("xCount"); detectCode.Accept(chunks); Assert.IsTrue(index.Detected); Assert.IsFalse(count.Detected); }
protected override void Visit(ForEachChunk chunk) { var inspector = new ForEachInspector(chunk.Code); if (inspector.Recognized) { var detect = new DetectCodeExpressionVisitor(OuterPartial); var autoIndex = detect.Add(inspector.VariableName + "Index"); var autoCount = detect.Add(inspector.VariableName + "Count"); var autoIsFirst = detect.Add(inspector.VariableName + "IsFirst"); var autoIsLast = detect.Add(inspector.VariableName + "IsLast"); detect.Accept(chunk.Body); if (autoIsLast.Detected) { autoIndex.Detected = true; autoCount.Detected = true; } if (autoCount.Detected) { // var itemCount=0;for(var __iter__item in coll){++itemCount;} _source .Append("var ") .Append(inspector.VariableName) .Append("Count=0;for(var __iter__") .Append(inspector.VariableName) .Append(" in ") .Append(inspector.CollectionCode) .Append("){++") .Append(inspector.VariableName) .Append("Count;}"); } if (autoIndex.Detected) { // var itemIndex=0; _source.Append("var ").Append(inspector.VariableName).Append("Index=0;"); } if (autoIsFirst.Detected) { // var itemIsFirst=true; _source.Append("var ").Append(inspector.VariableName).Append("IsFirst=true;"); } // for(var __iter__item in coll) { _source .Append("for (var __iter__") .Append(inspector.VariableName) .Append(" in ") .Append(inspector.CollectionCode) .Append(") {"); // var item=coll[__iter__item]; _source .Append("var ") .Append(inspector.VariableName) .Append("=") .Append(inspector.CollectionCode) .Append("[__iter__") .Append(inspector.VariableName) .Append("];"); if (autoIsLast.Detected) { // var itemIsLast=(itemIndex==itemCount-1); _source .Append("var ") .Append(inspector.VariableName) .Append("IsLast=(") .Append(inspector.VariableName) .Append("Index==") .Append(inspector.VariableName) .Append("Count-1);"); } _source.AppendLine(); Accept(chunk.Body); if (autoIsFirst.Detected) { // itemIsFirst=false; _source.Append(inspector.VariableName).Append("IsFirst=false;"); } if (autoIndex.Detected) { // ++itemIndex; _source.Append("++").Append(inspector.VariableName).Append("Index;"); } _source.AppendLine("}"); } else { _source.Append("for (").Append(chunk.Code).AppendLine(") {"); Accept(chunk.Body); _source.Append("}"); } }