Exemplo n.º 1
0
 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;
 }
Exemplo n.º 2
0
 private void comment(Node node)
 {
     node.Comment = true;
     foreach(var child in node.Children) comment(child);
 }
Exemplo n.º 3
0
 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;
 }
Exemplo n.º 4
0
 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);
 }
Exemplo n.º 5
0
 public Comment(Node node)
 {
     this.Node = node;
 }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
 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;
 }
Exemplo n.º 8
0
 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;
 }
Exemplo n.º 9
0
 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;
 }
Exemplo n.º 10
0
 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;
 }
Exemplo n.º 11
0
 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;
 }
Exemplo n.º 12
0
 public void Visit(Node node)
 {
     var actions = GetActions(node);
     Actions.AddRange(actions);
     foreach(var child in new List<Node>(node.Children)) child.Accept(this);
 }
Exemplo n.º 13
0
 public Expand(Node source, Node destination, string destinationURI)
 {
     this.Source = source;
     this.Destination = destination;
     this.DestinationURI = destinationURI;
 }
Exemplo n.º 14
0
 public Erase(Node node, string word)
 {
     this.Node = node;
     this.Words = new List<string> { word };
 }
Exemplo n.º 15
0
 public void Visit(Node node)
 {
     bool doVisitChildren = Process(node);
     if (doVisitChildren) foreach(var child in node.Children) child.Accept(this);
 }
Exemplo n.º 16
0
 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;
 }