public Node CreateSampleTree() { // 0 // /|\ // 1 7 8 // /|\ | // 2 3 4 9 // / \ // 5 6 var root = new Node("0"); root.Add(new Node("1")); root.Add(new Node("7")); root.Add(new Node("8")); root.Children[0].Add(new Node("2")); root.Children[0].Add(new Node("3")); root.Children[0].Add(new Node("4")); root.Children[2].Add(new Node("9")); root.Children[0].Children[2].Add(new Node("5")); root.Children[0].Children[2].Add(new Node("6")); return root; }
private void comment(Node node) { node.Comment = true; foreach(var child in node.Children) comment(child); }
public Create(Node parent, string template, Dictionary<string,object> variables, string group, string delimiter, int? position) { this.Parent = parent; if (group != null) this.Group = new TypeCobol.Codegen.Skeletons.Templates.RazorEngine().Replace(group, variables, delimiter); var solver = TypeCobol.Codegen.Skeletons.Templates.RazorEngine.Create(template, variables, delimiter); this.Child = new GeneratedNode((TypeCobol.Codegen.Skeletons.Templates.RazorEngine)solver); this.position = position; }
public Replace(Node node, string template, Dictionary<string,object> variables, string group, string delimiter) { this.Old = node; if (group != null) this.Group = new TypeCobol.Codegen.Skeletons.Templates.RazorEngine().Replace(group, variables, delimiter); var solver = TypeCobol.Codegen.Skeletons.Templates.RazorEngine.Create(template, variables, delimiter); this.New = new GeneratedNode((TypeCobol.Codegen.Skeletons.Templates.RazorEngine)solver); }
public Comment(Node node) { this.Node = node; }
private Node GetLocation(Node node, string location, out int? index) { index = null; if (location.EndsWith(".begin")) { location = location.Substring(0, location.Length - ".begin".Length); index = 0; } else if (location.EndsWith(".end")) { location = location.Substring(0, location.Length - ".end".Length); } if (location == null || location.ToLower().Equals("node")) return node; var root = node.Root; var result = root.Get(location); if (result != null) return result; result = Create(root, location); if (result != null) return result; throw new System.ArgumentException("Undefined URI: "+location); }
private Dictionary<string, object> GetProperties(Node node, IEnumerable<string> properties) { var result = new Dictionary<string,object>(); var errors = new System.Text.StringBuilder(); foreach(var pname in properties) { var property = node[pname]; if (property != null) result[pname] = property; else errors.Append(pname).Append(", "); } if (errors.Length > 0) { errors.Length -= 2; errors.Insert(0, "Undefined properties for "+node.GetType().Name+": "); errors.Append(". (line:\"").Append(node.CodeElement.InputStream).Append("\")"); throw new System.ArgumentException(errors.ToString()); } return result; }
private ICollection<Action> GetActions(Node node) { var actions = new List<Action>(); var skeleton = GetActiveSkeleton(node); if (skeleton != null) { var properties = GetProperties(node, skeleton.Properties); foreach(var pattern in skeleton) { var action = GetAction(node, properties, pattern); if (action != null) actions.Add(action); } } return actions; }
private Skeleton GetActiveSkeleton(Node node) { foreach(var skeleton in Skeletons) { bool active = false; foreach(var condition in skeleton.Conditions) { active = active || condition.Verify(node); // OR } if (active) return skeleton;//TODO: what if more than 1 skel activates? } return null; }
private Node Create(Node node, string location) { var factory = new Codegen.Nodes.Factory(); var parts = location.Split(new char[] { '.' }); var path = new System.Text.StringBuilder(); Node result = null; foreach(var part in parts) { path.Append(part); var current = node.Get(path.ToString()); if (current == null) { string nextsibling; current = factory.Create(part, out nextsibling); if (current == null) return null; int index = 0; if (nextsibling != null) { var sibling = result.Get(nextsibling); index = sibling.Parent.IndexOf(sibling); } result.Add(current, index); } result = current; path.Append('.'); } return result; }
private Action GetAction(Node source, Dictionary<string,object> properties, Pattern pattern) { int? index; var destination = GetLocation(source, pattern.Location, out index); if ("create".Equals(pattern.Action)) { return new Create(destination, pattern.Template, properties, pattern.Group, pattern.Delimiter, index); } if ("replace".Equals(pattern.Action)) { return new Replace(destination, pattern.Template, properties, pattern.Group, pattern.Delimiter); } if ("comment".Equals(pattern.Action)) { return new Comment(destination); } if ("expand".Equals(pattern.Action)) { return new Expand(source, destination, pattern.Location); } if ("erase".Equals(pattern.Action)) { return new Erase(destination, pattern.Template); } System.Console.WriteLine("Unknown action: \""+pattern.Action+"\""); return null; }
public void Visit(Node node) { var actions = GetActions(node); Actions.AddRange(actions); foreach(var child in new List<Node>(node.Children)) child.Accept(this); }
public Expand(Node source, Node destination, string destinationURI) { this.Source = source; this.Destination = destination; this.DestinationURI = destinationURI; }
public Erase(Node node, string word) { this.Node = node; this.Words = new List<string> { word }; }
public void Visit(Node node) { bool doVisitChildren = Process(node); if (doVisitChildren) foreach(var child in node.Children) child.Accept(this); }
private bool Process(Node node) { var generated = node as Generated; foreach(var line in node.Lines) { if (generated != null) // if we write generated code, we INSERT one line of code between Input lines; // thus, we must decrease offset as it'll be re-increased by Write(line) and // we don't want to f**k up next iteration offset--; else // before we copy an original line of code, we must still write non-source // lines (eg. comments or empty lines) so they are preserved in Output WriteInputLinesUpTo(line); Write(line, node.Comment); } return generated == null || !generated.IsLeaf; }