protected void VisitFor(SpecialNode specialNode, SpecialNodeInspector inspector) { if (!FirstChildBeginsWithNewline(specialNode)) { var eachAttr = inspector.TakeAttribute("each"); var eachInspector = new ForEachInspector(AsCode(eachAttr)); var code = eachInspector.Recognized ? eachInspector.VariableName + "IsFirst" : "Once(\"" + Guid.NewGuid() + "\")"; var newNode = new SpecialNode(new ElementNode("if", new[] { new AttributeNode("condition", code) }, false)) { Body = new [] { new TextNode(specialNode.Element.PreceedingWhitespace), }, }; var newBody = new List <Node>(); newBody.Add(newNode); if (specialNode.Body != null) { newBody.AddRange(specialNode.Body); } specialNode.Body = newBody; } specialNode.Element.PreceedingWhitespace = string.Empty; }
public void LocatingTheVariable() { var inspector = new ForEachInspector("my var thing in collection.thing foo"); Assert.AreEqual("my var", inspector.VariableType); Assert.AreEqual("thing", inspector.VariableName); Assert.AreEqual("collection.thing foo", inspector.CollectionCode); }
protected override void Visit(ForEachChunk chunk) { ForEachInspector inspector = new ForEachInspector(chunk.Code); if (inspector.Recognized) { DetectCodeExpressionVisitor visitor = new DetectCodeExpressionVisitor(base.OuterPartial); DetectCodeExpressionVisitor.Entry entry = visitor.Add(inspector.VariableName + "Index"); DetectCodeExpressionVisitor.Entry entry2 = visitor.Add(inspector.VariableName + "Count"); DetectCodeExpressionVisitor.Entry entry3 = visitor.Add(inspector.VariableName + "IsFirst"); DetectCodeExpressionVisitor.Entry entry4 = visitor.Add(inspector.VariableName + "IsLast"); visitor.Accept(chunk.Body); if (entry4.Detected) { entry.Detected = true; entry2.Detected = true; } string str = "__iter__" + inspector.VariableName; if (entry2.Detected) { this._source.Append("var ").Append(inspector.VariableName).Append("Count=0;for(var ").Append(str).Append(" in ").Append(inspector.CollectionCode).Append("){ if(typeof(").Append(inspector.CollectionCode).Append("[").Append(str).Append("])!='function') {").Append("++").Append(inspector.VariableName).Append("Count;}}"); } if (entry.Detected) { this._source.Append("var ").Append(inspector.VariableName).Append("Index=0;"); } if (entry3.Detected) { this._source.Append("var ").Append(inspector.VariableName).Append("IsFirst=true;"); } this._source.Append("for (var ").Append(str).Append(" in ").Append(inspector.CollectionCode).Append(") {"); this._source.Append("var ").Append(inspector.VariableName).Append("=").Append(inspector.CollectionCode).Append("[__iter__").Append(inspector.VariableName).Append("];"); this._source.Append("if(typeof(").Append(inspector.VariableName).Append(")!='function') {"); if (entry4.Detected) { this._source.Append("var ").Append(inspector.VariableName).Append("IsLast=(").Append(inspector.VariableName).Append("Index==").Append(inspector.VariableName).Append("Count-1);"); } this._source.AppendLine(); base.Accept(chunk.Body); if (entry3.Detected) { this._source.Append(inspector.VariableName).Append("IsFirst=false;"); } if (entry.Detected) { this._source.Append("++").Append(inspector.VariableName).Append("Index;"); } this._source.AppendLine("}}"); } else { this._source.Append("for (").Append((string)chunk.Code).AppendLine(") {"); base.Accept(chunk.Body); this._source.Append("}"); } }
protected void VisitFor(SpecialNode specialNode, SpecialNodeInspector inspector) { if (!this.FirstChildBeginsWithNewline(specialNode)) { AttributeNode attr = inspector.TakeAttribute("each"); ForEachInspector inspector2 = new ForEachInspector(this.AsCode(attr)); string str = inspector2.Recognized ? (inspector2.VariableName + "IsFirst") : ("Once(\"" + Guid.NewGuid() + "\")"); SpecialNode node2 = new SpecialNode(new ElementNode("if", new AttributeNode[] { new AttributeNode("condition", str) }, false)) { Body = new TextNode[] { new TextNode(specialNode.Element.PreceedingWhitespace) } }; List <Node> list = new List <Node> { node2 }; if (specialNode.Body != null) { list.AddRange(specialNode.Body); } specialNode.Body = list; } specialNode.Element.PreceedingWhitespace = string.Empty; }
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 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("}"); } }
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; } string iteratorName = "__iter__" + inspector.VariableName; if (autoCount.Detected) { // var itemCount=0;for(var __iter__item in coll){if(typeof(__iter__item)!='function'){++itemCount;}} _source .Append("var ") .Append(inspector.VariableName) .Append("Count=0;for(var ") .Append(iteratorName) .Append(" in ") .Append(inspector.CollectionCode) .Append("){ if(typeof(") .Append(inspector.CollectionCode) .Append("[") .Append(iteratorName) .Append("])!='function') {") .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 ") .Append(iteratorName) .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(typeof(item)!='function') { _source.Append("if(typeof(") .Append(inspector.VariableName) .Append(")!='function') {"); 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("}"); } }