ParseTree.cs revision 324c4644fee44b9898524c09511bd33c3f12e2df
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * [The "BSD licence"]
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (c) 2005-2008 Terence Parr
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * All rights reserved.
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) *
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * Conversion to C#:
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc.
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * All rights reserved.
958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) *
1058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * modification, are permitted provided that the following conditions
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * are met:
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 1. Redistributions of source code must retain the above copyright
1403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) *    notice, this list of conditions and the following disclaimer.
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * 2. Redistributions in binary form must reproduce the above copyright
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    notice, this list of conditions and the following disclaimer in the
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    documentation and/or other materials provided with the distribution.
1803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) * 3. The name of the author may not be used to endorse or promote products
193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) *    derived from this software without specific prior written permission.
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace Antlr.Runtime.Tree {
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    using System.Collections.Generic;
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    using StringBuilder = System.Text.StringBuilder;
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /** <summary>
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     *  A record of the rules used to match a token sequence.  The tokens
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     *  end up as the leaves of this tree and rule nodes are the interior nodes.
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     *  This really adds no functionality, it is just an alias for CommonTree
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     *  that is more meaningful (specific) and holds a String to display for a node.
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     *  </summary>
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     */
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    [System.Serializable]
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    public class ParseTree : BaseTree {
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        public object payload;
4858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        public List<IToken> hiddenTokens;
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        public ParseTree(object label) {
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            this.payload = label;
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        #region Properties
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        public override string Text {
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            get {
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                return ToString();
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            }
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            set {
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            }
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        public override int TokenStartIndex {
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            get {
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                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
85        public override ITree DupNode() {
86            return null;
87        }
88
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         */
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         */
124        public virtual string ToInputString() {
125            StringBuilder buf = new StringBuilder();
126            ToStringLeaves(buf);
127            return buf.ToString();
128        }
129
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