CommonErrorNode.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    /** <summary>A node representing erroneous token range in token stream</summary> */
36    [System.Serializable]
37    public class CommonErrorNode : CommonTree {
38        public IIntStream input;
39        public IToken start;
40        public IToken stop;
41        public RecognitionException trappedException;
42
43        public CommonErrorNode(ITokenStream input, IToken start, IToken stop,
44                               RecognitionException e) {
45            //System.out.println("start: "+start+", stop: "+stop);
46            if (stop == null ||
47                 (stop.TokenIndex < start.TokenIndex &&
48                  stop.Type != TokenTypes.EndOfFile)) {
49                // sometimes resync does not consume a token (when LT(1) is
50                // in follow set.  So, stop will be 1 to left to start. adjust.
51                // Also handle case where start is the first token and no token
52                // is consumed during recovery; LT(-1) will return null.
53                stop = start;
54            }
55            this.input = input;
56            this.start = start;
57            this.stop = stop;
58            this.trappedException = e;
59        }
60
61        #region Properties
62        public override bool IsNil {
63            get {
64                return false;
65            }
66        }
67        public override string Text {
68            get {
69                string badText = null;
70                if (start is IToken) {
71                    int i = ((IToken)start).TokenIndex;
72                    int j = ((IToken)stop).TokenIndex;
73                    if (((IToken)stop).Type == TokenTypes.EndOfFile) {
74                        j = ((ITokenStream)input).Count;
75                    }
76                    badText = ((ITokenStream)input).ToString(i, j);
77                } else if (start is ITree) {
78                    badText = ((ITreeNodeStream)input).ToString(start, stop);
79                } else {
80                    // people should subclass if they alter the tree type so this
81                    // next one is for sure correct.
82                    badText = "<unknown>";
83                }
84                return badText;
85            }
86            set {
87            }
88        }
89        public override int Type {
90            get {
91                return TokenTypes.Invalid;
92            }
93            set {
94            }
95        }
96        #endregion
97
98        public override string ToString() {
99            if (trappedException is MissingTokenException) {
100                return "<missing type: " +
101                       ((MissingTokenException)trappedException).MissingType +
102                       ">";
103            } else if (trappedException is UnwantedTokenException) {
104                return "<extraneous: " +
105                       ((UnwantedTokenException)trappedException).UnexpectedToken +
106                       ", resync=" + Text + ">";
107            } else if (trappedException is MismatchedTokenException) {
108                return "<mismatched token: " + trappedException.Token + ", resync=" + Text + ">";
109            } else if (trappedException is NoViableAltException) {
110                return "<unexpected: " + trappedException.Token +
111                       ", resync=" + Text + ">";
112            }
113            return "<error: " + Text + ">";
114        }
115    }
116}
117