ParseTree.cs revision 324c4644fee44b9898524c09511bd33c3f12e2df
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
33namespace Antlr.Runtime.Tree
34{
35    using System.Collections.Generic;
36
37    using StringBuilder = System.Text.StringBuilder;
38
39    /** <summary>
40     *  A record of the rules used to match a token sequence.  The tokens
41     *  end up as the leaves of this tree and rule nodes are the interior nodes.
42     *  This really adds no functionality, it is just an alias for CommonTree
43     *  that is more meaningful (specific) and holds a String to display for a node.
44     *  </summary>
45     */
46    [System.Serializable]
47    public class ParseTree : BaseTree
48    {
49        public object payload;
50        public List<IToken> hiddenTokens;
51
52        public ParseTree( object label )
53        {
54            this.payload = label;
55        }
56
57        #region Properties
58        public override string Text
59        {
60            get
61            {
62                return ToString();
63            }
64            set
65            {
66            }
67        }
68        public override int TokenStartIndex
69        {
70            get
71            {
72                return 0;
73            }
74            set
75            {
76            }
77        }
78        public override int TokenStopIndex
79        {
80            get
81            {
82                return 0;
83            }
84            set
85            {
86            }
87        }
88        public override int Type
89        {
90            get
91            {
92                return 0;
93            }
94            set
95            {
96            }
97        }
98        #endregion
99
100        public override ITree DupNode()
101        {
102            return null;
103        }
104
105        public override string ToString()
106        {
107            if ( payload is IToken )
108            {
109                IToken t = (IToken)payload;
110                if ( t.Type == TokenTypes.EndOfFile )
111                {
112                    return "<EOF>";
113                }
114                return t.Text;
115            }
116            return payload.ToString();
117        }
118
119        /** <summary>
120         *  Emit a token and all hidden nodes before.  EOF node holds all
121         *  hidden tokens after last real token.
122         *  </summary>
123         */
124        public virtual string ToStringWithHiddenTokens()
125        {
126            StringBuilder buf = new StringBuilder();
127            if ( hiddenTokens != null )
128            {
129                for ( int i = 0; i < hiddenTokens.Count; i++ )
130                {
131                    IToken hidden = (IToken)hiddenTokens[i];
132                    buf.Append( hidden.Text );
133                }
134            }
135            string nodeText = this.ToString();
136            if ( !nodeText.Equals( "<EOF>" ) )
137                buf.Append( nodeText );
138            return buf.ToString();
139        }
140
141        /** <summary>
142         *  Print out the leaves of this tree, which means printing original
143         *  input back out.
144         *  </summary>
145         */
146        public virtual string ToInputString()
147        {
148            StringBuilder buf = new StringBuilder();
149            ToStringLeaves( buf );
150            return buf.ToString();
151        }
152
153        protected virtual void ToStringLeaves( StringBuilder buf )
154        {
155            if ( payload is IToken )
156            { // leaf node token?
157                buf.Append( this.ToStringWithHiddenTokens() );
158                return;
159            }
160            for ( int i = 0; Children != null && i < Children.Count; i++ )
161            {
162                ParseTree t = (ParseTree)Children[i];
163                t.ToStringLeaves( buf );
164            }
165        }
166    }
167}
168