• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * [The "BSD licence"]
3  * Copyright (c) 2005-2008 Terence Parr
4  * All rights reserved.
5  *
6  * Conversion to C#:
7  * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 namespace Antlr.Runtime.Tree {
34     using System.Collections.Generic;
35 
36     using StringBuilder = System.Text.StringBuilder;
37 
38     /** <summary>
39      *  A record of the rules used to match a token sequence.  The tokens
40      *  end up as the leaves of this tree and rule nodes are the interior nodes.
41      *  This really adds no functionality, it is just an alias for CommonTree
42      *  that is more meaningful (specific) and holds a String to display for a node.
43      *  </summary>
44      */
45     [System.Serializable]
46     public class ParseTree : BaseTree {
47         public object payload;
48         public List<IToken> hiddenTokens;
49 
ParseTree(object label)50         public ParseTree(object label) {
51             this.payload = label;
52         }
53 
54         #region Properties
55         public override string Text {
56             get {
57                 return ToString();
58             }
59             set {
60             }
61         }
62         public override int TokenStartIndex {
63             get {
64                 return 0;
65             }
66             set {
67             }
68         }
69         public override int TokenStopIndex {
70             get {
71                 return 0;
72             }
73             set {
74             }
75         }
76         public override int Type {
77             get {
78                 return 0;
79             }
80             set {
81             }
82         }
83         #endregion
84 
DupNode()85         public override ITree DupNode() {
86             return null;
87         }
88 
ToString()89         public override string ToString() {
90             if (payload is IToken) {
91                 IToken t = (IToken)payload;
92                 if (t.Type == TokenTypes.EndOfFile) {
93                     return "<EOF>";
94                 }
95                 return t.Text;
96             }
97             return payload.ToString();
98         }
99 
100         /** <summary>
101          *  Emit a token and all hidden nodes before.  EOF node holds all
102          *  hidden tokens after last real token.
103          *  </summary>
104          */
ToStringWithHiddenTokens()105         public virtual string ToStringWithHiddenTokens() {
106             StringBuilder buf = new StringBuilder();
107             if (hiddenTokens != null) {
108                 for (int i = 0; i < hiddenTokens.Count; i++) {
109                     IToken hidden = (IToken)hiddenTokens[i];
110                     buf.Append(hidden.Text);
111                 }
112             }
113             string nodeText = this.ToString();
114             if (!nodeText.Equals("<EOF>"))
115                 buf.Append(nodeText);
116             return buf.ToString();
117         }
118 
119         /** <summary>
120          *  Print out the leaves of this tree, which means printing original
121          *  input back out.
122          *  </summary>
123          */
ToInputString()124         public virtual string ToInputString() {
125             StringBuilder buf = new StringBuilder();
126             ToStringLeaves(buf);
127             return buf.ToString();
128         }
129 
ToStringLeaves(StringBuilder buf)130         protected virtual void ToStringLeaves(StringBuilder buf) {
131             if (payload is IToken) { // leaf node token?
132                 buf.Append(this.ToStringWithHiddenTokens());
133                 return;
134             }
135             for (int i = 0; Children != null && i < Children.Count; i++) {
136                 ParseTree t = (ParseTree)Children[i];
137                 t.ToStringLeaves(buf);
138             }
139         }
140     }
141 }
142