protected static bool EqualsCore(object t1, object t2, ITreeAdaptor adaptor) { if ((t1 == null) || (t2 == null)) { return false; } if (adaptor.GetType(t1) != adaptor.GetType(t2)) { return false; } if (!adaptor.GetText(t1).Equals(adaptor.GetText(t2))) { return false; } int childCount = adaptor.GetChildCount(t1); int num2 = adaptor.GetChildCount(t2); if (childCount != num2) { return false; } for (int i = 0; i < childCount; i++) { object child = adaptor.GetChild(t1, i); object obj3 = adaptor.GetChild(t2, i); if (!EqualsCore(child, obj3, adaptor)) { return false; } } return true; }
public TreePatternParser( TreePatternLexer tokenizer, TreeWizard wizard, ITreeAdaptor adaptor ) { this.tokenizer = tokenizer; this.wizard = wizard; this.adaptor = adaptor; ttype = tokenizer.NextToken(); // kickstart }
/// <summary> /// The worker for <see cref="InContext(TreeParser, string)"/>. It's <see langword="static"/> and full of /// parameters for testing purposes. /// </summary> private static bool InContext( ITreeAdaptor adaptor, string[] tokenNames, object t, string context) { if (Regex.IsMatch(context, DotDot)) { // don't allow "..", must be "..." throw new ArgumentException("invalid syntax: .."); } if (Regex.IsMatch(context, DoubleEtc)) { // don't allow double "..." throw new ArgumentException("invalid syntax: ... ..."); } context = context.Replace("...", " ... "); // ensure spaces around ... context = context.Trim(); string[] nodes = context.Split(new[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); int ni = nodes.Length - 1; t = adaptor.GetParent(t); while (ni >= 0 && t != null) { if (nodes[ni].Equals("...")) { // walk upwards until we see nodes[ni-1] then continue walking if (ni == 0) { // ... at start is no-op return true; } string goal = nodes[ni - 1]; object ancestor = GetAncestor(adaptor, tokenNames, t, goal); if (ancestor == null) return false; t = ancestor; ni--; } string name = tokenNames[adaptor.GetType(t)]; if (!name.Equals(nodes[ni])) { //System.Console.Error.WriteLine("not matched: " + nodes[ni] + " at " + t); return false; } // advance to parent and to previous element in context node list ni--; t = adaptor.GetParent(t); } if (t == null && ni >= 0) return false; // at root but more nodes to match return true; }
public TreeRewriter( ITreeNodeStream input, RecognizerSharedState state ) : base( input, state ) { originalAdaptor = input.TreeAdaptor; originalTokenStream = input.TokenStream; topdown_func = () => Topdown(); bottomup_func = () => Bottomup(); }
public TreeViewModel(ITreeAdaptor adaptor, object tree) { if (adaptor == null) throw new ArgumentNullException("adaptor"); if (tree == null) throw new ArgumentNullException("tree"); _adaptor = adaptor; _tree = tree; }
// Private interface private static string Concatenate_Depth2_Subtree_Name( ITreeAdaptor tree_adapter, object node ) { StringBuilder builder = new StringBuilder(); builder.Append( tree_adapter.GetText( node ) ); for ( int i = 0; i < tree_adapter.GetChildCount( node ); i++ ) { object child_node = tree_adapter.GetChild( node, i ); builder.Append( tree_adapter.GetText( child_node ) ); } return builder.ToString(); }
public TreeVisualizerViewModel(ITreeAdaptor adaptor, object tree, ITokenStream tokenStream, string sourceText) { if (adaptor == null) throw new ArgumentNullException("adaptor"); if (tree == null) throw new ArgumentNullException("tree"); _adaptor = adaptor; _tree = tree; _tokenStream = tokenStream; _sourceText = sourceText; object root = adaptor.Nil(); adaptor.AddChild(root, tree); _treeViewModel = new TreeViewModel(_adaptor, root); }
public TreeRewriter(ITreeNodeStream input, RecognizerSharedState state) : base(input, state) { Func<IAstRuleReturnScope> func = null; Func<IAstRuleReturnScope> func2 = null; this.originalAdaptor = input.TreeAdaptor; this.originalTokenStream = input.TokenStream; if (func == null) { func = () => this.Topdown(); } this.topdown_func = func; if (func2 == null) { func2 = () => this.Bottomup(); } this.bottomup_func = func2; }
protected virtual IEnumerable<string> DefineNodes(object tree, ITreeAdaptor adaptor) { if (tree != null) { int childCount = adaptor.GetChildCount(tree); if (childCount != 0) { yield return this.GetNodeText(adaptor, tree); for (int i = 0; i < childCount; i++) { object child = adaptor.GetChild(tree, i); yield return this.GetNodeText(adaptor, child); foreach (string iteratorVariable3 in this.DefineNodes(child, adaptor)) { yield return iteratorVariable3; } } } } }
protected virtual IEnumerable<string> DefineEdges(object tree, ITreeAdaptor adaptor) { if (tree != null) { int childCount = adaptor.GetChildCount(tree); if (childCount != 0) { string iteratorVariable1 = "n" + this.GetNodeNumber(tree); string text = adaptor.GetText(tree); for (int i = 0; i < childCount; i++) { object child = adaptor.GetChild(tree, i); string iteratorVariable5 = adaptor.GetText(child); string iteratorVariable6 = "n" + this.GetNodeNumber(child); yield return string.Format(" {0} -> {1} // \"{2}\" -> \"{3}\"", new object[] { iteratorVariable1, iteratorVariable6, this.FixString(text), this.FixString(iteratorVariable5) }); foreach (string iteratorVariable7 in this.DefineEdges(child, adaptor)) { yield return iteratorVariable7; } } } } }
public TreeWizard( ITreeAdaptor adaptor, string[] tokenNames ) { this.adaptor = adaptor; this.tokenNameToTypeMap = ComputeTokenTypes( tokenNames ); }
/// <summary>Create a stream with one element</summary> public RewriteRuleNodeStream( ITreeAdaptor adaptor, string elementDescription, object oneElement ) : base(adaptor, elementDescription, oneElement) { }
public TreeWizard( ITreeAdaptor adaptor ) { this.adaptor = adaptor; }
public CommonTreeNodeStream(ITreeAdaptor adaptor, object tree, int initialBufferSize) { this.root = tree; this.adaptor = adaptor; nodes = new ArrayList(initialBufferSize); down = adaptor.Create(Token.DOWN, "DOWN"); up = adaptor.Create(Token.UP, "UP"); eof = adaptor.Create(Token.EOF, "EOF"); }
partial void CreateTreeAdaptor(ref ITreeAdaptor adaptor) { adaptor = new ParseAdaptor(this.parseTreeContainer); }
// Implement this function in your helper file to use a custom tree adaptor partial void CreateTreeAdaptor(ref ITreeAdaptor adaptor);
/** <summary>Create a stream with one element</summary> */ public RewriteRuleElementStream( ITreeAdaptor adaptor, string elementDescription, object oneElement ) : this( adaptor, elementDescription ) { Add( oneElement ); }
/** Generate DOT (graphviz) for a whole tree not just a node. * For example, 3+4*5 should generate: * * digraph { * node [shape=plaintext, fixedsize=true, fontsize=11, fontname="Courier", * width=.4, height=.2]; * edge [arrowsize=.7] * "+"->3 * "+"->"*" * "*"->4 * "*"->5 * } * * Takes a Tree interface object. */ public virtual string ToDot(object tree, ITreeAdaptor adaptor) { StringBuilder builder = new StringBuilder(); foreach (string line in HeaderLines) builder.AppendLine(line); nodeNumber = 0; var nodes = DefineNodes(tree, adaptor); nodeNumber = 0; var edges = DefineEdges(tree, adaptor); foreach (var s in nodes) builder.AppendLine(s); builder.AppendLine(); foreach (var s in edges) builder.AppendLine(s); builder.AppendLine(); builder.AppendLine(Footer); return builder.ToString(); }
public TreeVisitor(ITreeAdaptor adaptor) { this.adaptor = adaptor; }
public BufferedTreeNodeStream(ITreeAdaptor adaptor, object tree) : this(adaptor, tree, DEFAULT_INITIAL_BUFFER_SIZE) { }
/** <summary> * Compare t1 and t2; return true if token types/text, structure match exactly. * The trees are examined in their entirety so that (A B) does not match * (A B C) nor (A (B C)). * </summary> * * <remarks> * TODO: allow them to pass in a comparator * TODO: have a version that is nonstatic so it can use instance adaptor * * I cannot rely on the tree node's equals() implementation as I make * no constraints at all on the node types nor interface etc... * </remarks> */ public static bool Equals(object t1, object t2, ITreeAdaptor adaptor) { return(EqualsCore(t1, t2, adaptor)); }
protected virtual void ExtractInformationFromTreeNodeStream(IIntStream input) { ITokenStreamInformation streamInformation = input as ITokenStreamInformation; if (streamInformation != null) { IToken lastToken = streamInformation.LastToken; IToken lastRealToken = streamInformation.LastRealToken; if (lastRealToken != null) { this._token = lastRealToken; this._line = lastRealToken.Line; this._charPositionInLine = lastRealToken.CharPositionInLine; this._approximateLineInfo = lastRealToken.Equals(lastToken); } } else { ITreeNodeStream nodes = (ITreeNodeStream)input; this._node = nodes.LT(1); ITreeAdaptor adaptor = nodes.TreeAdaptor; IToken payload = adaptor.GetToken(_node); if (payload != null) { this._token = payload; if (payload.Line <= 0) { // imaginary node; no line/pos info; scan backwards int i = -1; object priorNode = nodes.LT(i); while (priorNode != null) { IToken priorPayload = adaptor.GetToken(priorNode); if (priorPayload != null && priorPayload.Line > 0) { // we found the most recent real line / pos info this._line = priorPayload.Line; this._charPositionInLine = priorPayload.CharPositionInLine; this._approximateLineInfo = true; break; } --i; priorNode = nodes.LT(i); } } else // node created from real token { this._line = payload.Line; this._charPositionInLine = payload.CharPositionInLine; } } else if (this._node is Tree.ITree) { this._line = ((Tree.ITree) this._node).Line; this._charPositionInLine = ((Tree.ITree) this._node).CharPositionInLine; if (this._node is CommonTree) { this._token = ((CommonTree)this._node).Token; } } else { int type = adaptor.GetType(this._node); string text = adaptor.GetText(this._node); this._token = new CommonToken(type, text); } } }
partial void CreateTreeAdaptor(ref ITreeAdaptor adaptor) { adaptor = new grammar_Adaptor(this); }
partial void CreateTreeAdaptor(ref ITreeAdaptor adaptor) { adaptor = new OurTreeAdaptor(); }
protected static bool _Equals( object t1, object t2, ITreeAdaptor adaptor ) { // make sure both are non-null if ( t1 == null || t2 == null ) { return false; } // check roots if ( adaptor.GetType( t1 ) != adaptor.GetType( t2 ) ) { return false; } if ( !adaptor.GetText( t1 ).Equals( adaptor.GetText( t2 ) ) ) { return false; } // check children int n1 = adaptor.GetChildCount( t1 ); int n2 = adaptor.GetChildCount( t2 ); if ( n1 != n2 ) { return false; } for ( int i = 0; i < n1; i++ ) { object child1 = adaptor.GetChild( t1, i ); object child2 = adaptor.GetChild( t2, i ); if ( !_Equals( child1, child2, adaptor ) ) { return false; } } return true; }
/** <summary>Create a stream, but feed off an existing list</summary> */ public RewriteRuleElementStream( ITreeAdaptor adaptor, string elementDescription, IList elements ) : this( adaptor, elementDescription ) { this.singleElement = null; this.elements = elements; }
protected virtual string GetNodeText(ITreeAdaptor adaptor, object t) { string text = adaptor.GetText(t); return(string.Format(" {0} [label=\"{1}\"];", (object)("n" + (object)this.GetNodeNumber(t)), (object)this.FixString(text))); }
public TreeListViewModel(ITreeAdaptor treeAdaptor) { _treeAdaptor = treeAdaptor; TreeNode.CreateRoot(this); }
public TreeFilter(ITreeNodeStream input, RecognizerSharedState state) : base(input, state) { originalAdaptor = input.TreeAdaptor; originalTokenStream = input.TokenStream; }
public RewriteRuleNodeStream(ITreeAdaptor adaptor, string elementDescription) : base(adaptor, elementDescription) { }
protected virtual void ExtractInformationFromTreeNodeStream(ITreeNodeStream input) { this._node = input.LT(1); object positionNode = null; IPositionTrackingStream positionTrackingStream = input as IPositionTrackingStream; if (positionTrackingStream != null) { positionNode = positionTrackingStream.GetKnownPositionElement(false); if (positionNode == null) { positionNode = positionTrackingStream.GetKnownPositionElement(true); this._approximateLineInfo = positionNode != null; } } ITokenStreamInformation streamInformation = input as ITokenStreamInformation; if (streamInformation != null) { IToken lastToken = streamInformation.LastToken; IToken lastRealToken = streamInformation.LastRealToken; if (lastRealToken != null) { this._token = lastRealToken; this._line = lastRealToken.Line; this._charPositionInLine = lastRealToken.CharPositionInLine; this._approximateLineInfo = lastRealToken.Equals(lastToken); } } else { ITreeAdaptor adaptor = input.TreeAdaptor; IToken payload = adaptor.GetToken(positionNode ?? _node); if (payload != null) { this._token = payload; if (payload.Line <= 0) { // imaginary node; no line/pos info; scan backwards int i = -1; object priorNode = input.LT(i); while (priorNode != null) { IToken priorPayload = adaptor.GetToken(priorNode); if (priorPayload != null && priorPayload.Line > 0) { // we found the most recent real line / pos info this._line = priorPayload.Line; this._charPositionInLine = priorPayload.CharPositionInLine; this._approximateLineInfo = true; break; } --i; try { priorNode = input.LT(i); } catch (NotSupportedException) { priorNode = null; } } } else { // node created from real token this._line = payload.Line; this._charPositionInLine = payload.CharPositionInLine; } } else if (this._node is Tree.ITree) { this._line = ((Tree.ITree) this._node).Line; this._charPositionInLine = ((Tree.ITree) this._node).CharPositionInLine; if (this._node is CommonTree) { this._token = ((CommonTree)this._node).Token; } } else { int type = adaptor.GetType(this._node); string text = adaptor.GetText(this._node); this._token = new CommonToken(type, text); } } }
/** <summary>Create a stream with one element</summary> */ public RewriteRuleSubtreeStream(ITreeAdaptor adaptor, string elementDescription, object oneElement) : base(adaptor, elementDescription, oneElement) { }
public UnBufferedTreeNodeStream(ITreeAdaptor adaptor, object tree) { this.root = tree; this.adaptor = adaptor; Reset(); down = adaptor.Create(Token.DOWN, "DOWN"); up = adaptor.Create(Token.UP, "UP"); eof = adaptor.Create(Token.EOF, "EOF"); }
/** <summary>Create a stream, but feed off an existing list</summary> */ public RewriteRuleSubtreeStream(ITreeAdaptor adaptor, string elementDescription, IList elements) : base(adaptor, elementDescription, elements) { }
public RewriteRuleElementStream( ITreeAdaptor adaptor, string elementDescription ) { this.elementDescription = elementDescription; this.adaptor = adaptor; }
public ReadOnlyTreeVisitor(ITreeAdaptor adaptor) { this.adaptor = adaptor; }
public CommonTreeNodeStream(ITreeAdaptor adaptor, object tree) : this(adaptor, tree, DEFAULT_INITIAL_BUFFER_SIZE) { }
/////////////////////////////////////////// // // Construction // /////////////////////////////////////////// public NadirTreePatternMatcher(ITreeNodeStream input, RecognizerSharedState state) : base(input, state) { originalAdaptor = (ITreeAdaptor)input.TreeAdaptor; originalTokenStream = input.TokenStream; }
public CommonTreeNodeStream(ITreeAdaptor adaptor, object tree) { this._root = tree; this._adaptor = adaptor; this._it = new TreeIterator(adaptor, this._root); }
/// <summary> /// The worker for <see cref="InContext(TreeParser, string)"/>. It's <see langword="static"/> and full of /// parameters for testing purposes. /// </summary> private static bool InContext( ITreeAdaptor adaptor, string[] tokenNames, object t, string context) { if (Regex.IsMatch(context, DotDot)) { // don't allow "..", must be "..." throw new ArgumentException("invalid syntax: .."); } if (Regex.IsMatch(context, DoubleEtc)) { // don't allow double "..." throw new ArgumentException("invalid syntax: ... ..."); } context = context.Replace("...", " ... "); // ensure spaces around ... context = context.Trim(); string[] nodes = context.Split(new[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); int ni = nodes.Length - 1; t = adaptor.GetParent(t); while (ni >= 0 && t != null) { if (nodes[ni].Equals("...")) { // walk upwards until we see nodes[ni-1] then continue walking if (ni == 0) { // ... at start is no-op return(true); } string goal = nodes[ni - 1]; object ancestor = GetAncestor(adaptor, tokenNames, t, goal); if (ancestor == null) { return(false); } t = ancestor; ni--; } string name = tokenNames[adaptor.GetType(t)]; if (!name.Equals(nodes[ni])) { //System.Console.Error.WriteLine("not matched: " + nodes[ni] + " at " + t); return(false); } // advance to parent and to previous element in context node list ni--; t = adaptor.GetParent(t); } if (t == null && ni >= 0) { return(false); // at root but more nodes to match } return(true); }
public RewriteRuleSubtreeStream(ITreeAdaptor adaptor, string elementDescription, IList elements) : base(adaptor, elementDescription, elements) { }
/** <summary>Create a stream, but feed off an existing list</summary> */ public RewriteRuleElementStream(ITreeAdaptor adaptor, string elementDescription, IList elements) : this(adaptor, elementDescription) { this.singleElement = null; this.elements = elements; }
public ModifiableTreeVisitor(ITreeAdaptor adaptor) { this.adaptor = adaptor; }
public RewriteRuleElementStream(ITreeAdaptor adaptor, string elementDescription) { this.elementDescription = elementDescription; this.adaptor = adaptor; }
/** <summary>Create a stream with one element</summary> */ public RewriteRuleElementStream(ITreeAdaptor adaptor, string elementDescription, object oneElement) : this(adaptor, elementDescription) { Add(oneElement); }
/// <summary>Create a stream, but feed off an existing list</summary> public RewriteRuleNodeStream( ITreeAdaptor adaptor, string elementDescription, IList<object> elements ) : base(adaptor, elementDescription, elements) { }
public TreeWizard(ITreeAdaptor adaptor) { this.adaptor = adaptor; }
public TreeWizard( ITreeAdaptor adaptor, IDictionary<string, int> tokenNameToTypeMap ) { this.adaptor = adaptor; this.tokenNameToTypeMap = tokenNameToTypeMap; }
public TreeWizard(ITreeAdaptor adaptor, IDictionary <string, int> tokenNameToTypeMap) { this.adaptor = adaptor; this.tokenNameToTypeMap = tokenNameToTypeMap; }
/** <summary> * Compare t1 and t2; return true if token types/text, structure match exactly. * The trees are examined in their entirety so that (A B) does not match * (A B C) nor (A (B C)). * </summary> * * <remarks> * TODO: allow them to pass in a comparator * TODO: have a version that is nonstatic so it can use instance adaptor * * I cannot rely on the tree node's equals() implementation as I make * no constraints at all on the node types nor interface etc... * </remarks> */ public static bool Equals( object t1, object t2, ITreeAdaptor adaptor ) { return _Equals( t1, t2, adaptor ); }
public TreeWizard(ITreeAdaptor adaptor, string[] tokenNames) { this.adaptor = adaptor; this.tokenNameToTypeMap = ComputeTokenTypes(tokenNames); }
protected virtual void ExtractInformationFromTreeNodeStream(ITreeNodeStream input) { this._node = input.LT(1); ITokenStreamInformation streamInformation = input as ITokenStreamInformation; if (streamInformation != null) { IToken lastToken = streamInformation.LastToken; IToken lastRealToken = streamInformation.LastRealToken; if (lastRealToken == null) { return; } this._token = lastRealToken; this._line = lastRealToken.Line; this._charPositionInLine = lastRealToken.CharPositionInLine; this._approximateLineInfo = lastRealToken.Equals((object)lastToken); } else { ITreeAdaptor treeAdaptor = input.TreeAdaptor; IToken token1 = treeAdaptor.GetToken(this._node); if (token1 != null) { this._token = token1; if (token1.Line <= 0) { int k = -1; object t = input.LT(k); while (t != null) { IToken token2 = treeAdaptor.GetToken(t); if (token2 != null && token2.Line > 0) { this._line = token2.Line; this._charPositionInLine = token2.CharPositionInLine; this._approximateLineInfo = true; break; } --k; try { t = input.LT(k); } catch (ArgumentException ex) { t = (object)null; } } } else { this._line = token1.Line; this._charPositionInLine = token1.CharPositionInLine; } } else if (this._node is ITree) { this._line = ((ITree)this._node).Line; this._charPositionInLine = ((ITree)this._node).CharPositionInLine; if (!(this._node is CommonTree)) { return; } this._token = ((CommonTree)this._node).Token; } else { this._token = (IToken) new CommonToken(treeAdaptor.GetType(this._node), treeAdaptor.GetText(this._node)); } } }