/// <summary> /// For all subtrees that match the pattern, execute the visit action. /// </summary> /// <remarks> /// The implementation uses the root node of the pattern in combination /// with visit(t, ttype, visitor) so nil-rooted patterns are not allowed. /// Patterns with wildcard roots are also not allowed. /// </remarks> public void Visit(object t, string pattern, ContextVisitor visitor) { // Create a TreePattern from the pattern TreePatternLexer tokenizer = new TreePatternLexer(pattern); TreePatternParser parser = new TreePatternParser(tokenizer, this, new TreePatternTreeAdaptor()); TreePattern tpattern = (TreePattern)parser.Pattern(); // don't allow invalid patterns if ((tpattern == null) || tpattern.IsNil || (tpattern.GetType() == typeof(WildcardTreePattern))) { return; } //IDictionary labels = new Hashtable(); // reused for each _parse int rootTokenType = tpattern.Type; Visit(t, rootTokenType, new TreeWizard.InvokeVisitorOnPatternMatchContextVisitor(this, tpattern, visitor)); }
/// <summary>Return a List of subtrees matching pattern</summary> public IList Find(object t, string pattern) { IList subtrees = new ArrayList(); // Create a TreePattern from the pattern TreePatternLexer tokenizer = new TreePatternLexer(pattern); TreePatternParser parser = new TreePatternParser(tokenizer, this, new TreePatternTreeAdaptor()); TreePattern tpattern = (TreePattern)parser.Pattern(); // don't allow invalid patterns if ((tpattern == null) || tpattern.IsNil || (tpattern.GetType() == typeof(WildcardTreePattern))) { return(null); } int rootTokenType = tpattern.Type; Visit(t, rootTokenType, new TreeWizard.PatternMatchingContextVisitor(this, tpattern, subtrees)); return(subtrees); }
/** <summary> * Do the work for parse. Check to see if the t2 pattern fits the * structure and token types in t1. Check text if the pattern has * text arguments on nodes. Fill labels map with pointers to nodes * in tree matched against nodes in pattern with labels. * </summary> */ protected virtual bool ParseCore(object t1, TreePattern tpattern, IDictionary <string, object> labels) { // make sure both are non-null if (t1 == null || tpattern == null) { return(false); } // check roots (wildcard matches anything) if (tpattern.GetType() != typeof(WildcardTreePattern)) { if (adaptor.GetType(t1) != tpattern.Type) { return(false); } // if pattern has text, check node text if (tpattern.hasTextArg && !adaptor.GetText(t1).Equals(tpattern.Text)) { return(false); } } if (tpattern.label != null && labels != null) { // map label in pattern to node in t1 labels[tpattern.label] = t1; } // check children int n1 = adaptor.GetChildCount(t1); int n2 = tpattern.ChildCount; if (n1 != n2) { return(false); } for (int i = 0; i < n1; i++) { object child1 = adaptor.GetChild(t1, i); TreePattern child2 = (TreePattern)tpattern.GetChild(i); if (!ParseCore(child1, child2, labels)) { return(false); } } return(true); }
/// <summary> /// Do the work for Parse(). Check to see if the t2 pattern fits the /// structure and token types in t1. Check text if the pattern has /// text arguments on nodes. Fill labels map with pointers to nodes /// in tree matched against nodes in pattern with labels. /// </summary> protected bool _Parse(object t1, TreePattern t2, IDictionary labels) { // make sure both are non-null if (t1 == null || t2 == null) { return(false); } // check roots (wildcard matches anything) if (t2.GetType() != typeof(WildcardTreePattern)) { if (adaptor.GetNodeType(t1) != t2.Type) { return(false); } if (t2.hasTextArg && !adaptor.GetNodeText(t1).Equals(t2.Text)) { return(false); } } if (t2.label != null && labels != null) { // map label in pattern to node in t1 labels[t2.label] = t1; } // check children int n1 = adaptor.GetChildCount(t1); int n2 = t2.ChildCount; if (n1 != n2) { return(false); } for (int i = 0; i < n1; i++) { object child1 = adaptor.GetChild(t1, i); TreePattern child2 = (TreePattern)t2.GetChild(i); if (!_Parse(child1, child2, labels)) { return(false); } } return(true); }
/** <summary> * For all subtrees that match the pattern, execute the visit action. * The implementation uses the root node of the pattern in combination * with visit(t, ttype, visitor) so nil-rooted patterns are not allowed. * Patterns with wildcard roots are also not allowed. * </summary> */ public void Visit(object t, string pattern, IContextVisitor visitor) { // Create a TreePattern from the pattern TreePatternLexer tokenizer = new TreePatternLexer(pattern); TreePatternParser parser = new TreePatternParser(tokenizer, this, new TreePatternTreeAdaptor()); TreePattern tpattern = (TreePattern)parser.Pattern(); // don't allow invalid patterns if (tpattern == null || tpattern.IsNil || tpattern.GetType() == typeof(WildcardTreePattern)) { return; } IDictionary <string, object> labels = new Dictionary <string, object>(); // reused for each _parse int rootTokenType = tpattern.Type; Visit(t, rootTokenType, new VisitTreeWizardContextVisitor(this, visitor, labels, tpattern)); }
/** <summary> * Do the work for parse. Check to see if the t2 pattern fits the * structure and token types in t1. Check text if the pattern has * text arguments on nodes. Fill labels map with pointers to nodes * in tree matched against nodes in pattern with labels. * </summary> */ protected virtual bool _Parse( object t1, TreePattern tpattern, IDictionary<string, object> labels ) { // make sure both are non-null if ( t1 == null || tpattern == null ) { return false; } // check roots (wildcard matches anything) if ( tpattern.GetType() != typeof( WildcardTreePattern ) ) { if ( adaptor.GetType( t1 ) != tpattern.Type ) { return false; } // if pattern has text, check node text if ( tpattern.hasTextArg && !adaptor.GetText( t1 ).Equals( tpattern.Text ) ) { return false; } } if ( tpattern.label != null && labels != null ) { // map label in pattern to node in t1 labels[tpattern.label] = t1; } // check children int n1 = adaptor.GetChildCount( t1 ); int n2 = tpattern.ChildCount; if ( n1 != n2 ) { return false; } for ( int i = 0; i < n1; i++ ) { object child1 = adaptor.GetChild( t1, i ); TreePattern child2 = (TreePattern)tpattern.GetChild( i ); if ( !_Parse( child1, child2, labels ) ) { return false; } } return true; }
protected virtual bool ParseCore(object t1, TreePattern tpattern, IDictionary<string, object> labels) { if ((t1 == null) || (tpattern == null)) { return false; } if (tpattern.GetType() != typeof(WildcardTreePattern)) { if (this.adaptor.GetType(t1) != tpattern.Type) { return false; } if (tpattern.hasTextArg && !this.adaptor.GetText(t1).Equals(tpattern.Text)) { return false; } } if ((tpattern.label != null) && (labels != null)) { labels[tpattern.label] = t1; } int childCount = this.adaptor.GetChildCount(t1); int num2 = tpattern.ChildCount; if (childCount != num2) { return false; } for (int i = 0; i < childCount; i++) { object child = this.adaptor.GetChild(t1, i); TreePattern pattern = (TreePattern) tpattern.GetChild(i); if (!this.ParseCore(child, pattern, labels)) { return false; } } return true; }