public Dictionary <string, T> SelectOnTree <T>(StructureExpr tree, Func <StructureExpr, T> transform) { Dictionary <string, T> dict = new Dictionary <string, T>(); // propagation function RecurseInto(tree, string.Empty); void RecurseInto(StructureExpr branch, string loc) { // get location identification function if (branch.Mode == FilterExoConfig.StructurizerMode.root) { loc = "r"; } else { loc += "." + branch.Parent.Children.IndexOf(branch); } var res = transform(branch); dict.Add(loc, res); // recurse into children foreach (var leaf in branch.Children) { RecurseInto(leaf, loc); } } return(dict); }
public StructureExpr PackageAtomicChildren() { var adoptableChildren = new List <StructureExpr>(); var count = this.Children.Count; for (int t = count - 1; t >= 0; t--) { if (this.Children[t].Mode == StructurizerMode.atom) { adoptableChildren.Add(this.Children[t]); this.Children.RemoveAt(t); } else { break; } } if (adoptableChildren.Count == 0) { return(this); } var scope = StructureExpr.CreateScope(StructurizerScopeType.impl); adoptableChildren.ForEach(x => x.Parent = scope); scope.Children.AddRange(adoptableChildren); scope.Parent = this; this.Children.Add(scope); return(this); }
public string CreateTreeString(StructureExpr tree) { var result = new List <string>(); TreatProps(tree); void TreatProps(StructureExpr level) { var meta = BuildMetaString(level); bool atomFound = false; var lvlstr = ""; int depth = level.CalculateDepth(); for (int i = 0; i < depth; i++) { lvlstr += "\t"; } string props = " "; if (level.PropertyExpression.Count > 0) { props += string.Join(" ", level.PropertyExpression.Select(x => x.PrimitiveValue?.value ?? "NO PRIM VALUE")); } result.Add(lvlstr + meta + " " + level?.PrimitiveValue?.value + props); foreach (var item in level.Children) { if (item.Parent != level) { result[result.Count - 1] += "WRONG PARENT!!!"; } if (item.Mode == FilterExoConfig.StructurizerMode.atom) { if (!atomFound) { var metaString2 = lvlstr + "\t" + BuildMetaString(item); result.Add(metaString2); } atomFound = true; result[result.Count - 1] += " " + item.Value; } else { TreatProps(item); } } } string BuildMetaString(StructureExpr level) { return($"{level.Mode.ToString()} {level.ScopeType}"); } return(string.Join(System.Environment.NewLine, result)); }
public static StructureExpr CreateScope(StructurizerScopeType scopeType, ExoToken token = null) { var child = new StructureExpr() { ScopeType = scopeType, Mode = StructurizerMode.scop, PrimitiveValue = token, }; return(child); }
public StructureExpr AddChild(StructureExpr child) { child.Parent = this; this.Children.Add(child); return(this); }
public StructureExpr AddAndScopeOnChild(StructureExpr child) { child.Parent = this; this.Children.Add(child); return(child); }
public StructureExpr Execute(List <List <ExoToken> > tokens) { int tokenLine = 0; int tokenCol = 0; StructureExpr cursor = new StructureExpr(); cursor.Mode = FilterExoConfig.StructurizerMode.root; cursor.ScopeType = FilterExoConfig.StructurizerScopeType.none; for (tokenLine = 0; tokenLine < tokens.Count; tokenLine++) { var currentLine = tokens[tokenLine]; // single line level for (tokenCol = 0; tokenCol < currentLine.Count; tokenCol++) { DecideOnExpressionTreatment(currentLine[tokenCol]); } TreatLineEnd(); } // Single Token treatment main routine void DecideOnExpressionTreatment(ExoToken token) { // Handle special characters if (token.IsOperator) { if (SpecialCharacterTreatment(token)) { return; } } else if (token.type == TokenizerMode.comment) { cursor.AddChild(new StructureExpr(token, StructurizerMode.comm)); return; } // If we're in an explicit scope, we have to create a new scope before writing. else if (cursor.Mode == StructurizerMode.root || cursor.ScopeType == StructurizerScopeType.expl) { var child = StructureExpr.CreateScope(StructurizerScopeType.impl); cursor = cursor.AddAndScopeOnChild(child); } // currently never happens! keeping it around for safety purposes else if (cursor.Children.Count > 0 && !cursor.Children.All(x => x.Mode == StructurizerMode.atom)) { cursor = cursor.PackageAtomicChildren(); cursor = cursor.ScopeOnLastChild(); } // add token as atomic piece to the current cursor. cursor.AddChild(new StructureExpr(token)); } bool SpecialCharacterTreatment(ExoToken token) { if (token.value == ";") { TreatLineEnd(); return(true); } if (token.value == "{") { // finds the last child that contextualizes the explicit scope cursor = cursor.ScopeOnLastImplicit(); // Detach children from parent var children = cursor.Children.ToList(); cursor.Children = new List <StructureExpr>(); // Create new scope var scope = StructureExpr.CreateScope(StructurizerScopeType.expl, token); scope.Parent = cursor; cursor.Children.Add(scope); // Add children as descriptors scope.PropertyExpression.AddRange(children); cursor = scope; return(true); } if (token.value == "}") { cursor = cursor.GetExplParent(); cursor = cursor.GetParent(); return(true); } return(false); } void TreatLineEnd() { cursor = cursor.GetExplParent(); } // TODO: go to root. return(cursor.GoToRoot()); }